0% found this document useful (0 votes)
11 views

Javascript and Data

Javascript and Data

Uploaded by

orfeas.manolidis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Javascript and Data

Javascript and Data

Uploaded by

orfeas.manolidis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

•jvkr

Advanced interactions
on complex data in Max
using Javascript

johan van kreij | jvkr.nl


•jvkr

The standard Max environment is fine for dealing with one-dimensional


information. When, however, it is required to work with complex data, standard
Max objects may not suffice. Examples of complex data are a continuous
stream of values representing the analysis of microphone input, or
meteorological information that represents the state of the weather at a single
location over the course of a year.
When it comes to dealing with such data, a good option, while staying in Max,
is to incorporate the Javascript language environment. Max provides an object
named ‘js’ which executes Javascript.
•jvkr

This presentation is not intended as an introduction into Javascript coding. If


you have a background in object oriented programming, Javascript will be
easy to pick up. In the case you want to learn it from scratch, these are
resource I found rather valuable:

- Mozilla Javascript Reference


- Eloquent Javascript - Marijn Haverbeke
- Javascript: the Good Parts - Douglas Crockford

Keep in mind that Javascript is continuously under development and that older
resources may not cover recent added techniques. At the same time, the js
object works with a slightly outdated version of Javascript that does not include
latest technologies.
•jvkr

1. introduction
Javascript can be incorporated in Max in the
following way; the first step is to write code,
the second step is to expose that code to
Max with the js object, and finally the
functions in the code are called which then
produce their result.
In this example a single function is defined.
As soon as the js object receives a bang,
the function carrying that name is called and
executed. This results in a line being printed
(or posted) in the Max window.
•jvkr

1. introduction
Multiple functions can be defined in a
single script. Here, two functions have
been defined that respectively double and
triple the incoming value. The outlet()
function can be used to send data out of
js’s outlet. This function take as the first
argument the index of the outlet, after
which one or more chunks of data can be
specified, separated by commas.
Additional inlets or outlets can be created
by changing the default values of 1.
•jvkr

1. introduction
Functions called by other functions
can be defined as well. Since these
functions don’t have any meaning
outside the context in which they are
called, they can be localized in order
to prevent unwanted effects in case
they were called from outside the js
object. Attempting to call the functions
from a Max patch will then result in an
error message in the Max Console.
•jvkr

1. introduction
It is possible to work with multiple inlets in order to
create a logic we know from regular Max objects,
even though the typical strength of the js
environment lies elsewhere. In such cases, the
inlet property needs to be queried for incoming
values. The msg_int function is called when an
integer arrives at any of the inlets.
Also it is possible to provide additional arguments
when creating the js object. The first (at index 0)
is the name of the file that is loaded.
Here, a style is used where globally accessible
arguments are stored in an object.
•jvkr

1. introduction
This is what the code may look like as a
complete replacement for the + object, that
deals with both integers and floats, and
accepts a bang to retrigger the calculation.

Here, the loadbang() function is not strictly


necessary but used in order to minimize the
amount of global code—the code outside
functions. This function is called a soon the
loading of the entire patch in memory is
completed.
•jvkr

1. introduction
This is a situation where the loadbang()
function has code that would not work at a
global level. The patch contains a float
number box which has its varname attribute
set to ‘myfloat’. The script shows a method of
setting the value of the number box from
within the script. This is one of the ways in
which Javascript and Max are tied together.
The loadbang() here is crucial; otherwise
named objects may not be found.
To be continued…

You might also like