Tag Archives: trigger
Trigger repeated actions metronomically
Image
The metro (metronome) object is the most obvious way to trigger repeated events in Max. Repetition is, of course, a key component of most music (and most time-based art in general), and the metro object encapsulates the whole low-level process of a) cause something to happen, b) schedule the same thing to happen at some future time and repeat the process. The only thing it needs to “know” is what time interval to use for its repetitions. You specify that time interval in milliseconds, by an initial argument and/or a number in the right inlet.
For a bit more about timed repetition, you could read my blog article about it.
You many not find milliseconds to be the most musically useful unit with which to think about music, and in fact Max does provide a more music-friendly way to think about time, which you can also read about in a blog article on the subject.
In this example metro doesn’t really trigger anything very significant, but you should bear in mind that, since there are many objects that can be triggered by the bang message, a metro could trigger almost anything, including a whole other program.
Triggering events with each cycle of a phasor~
Image
How do we detect, with sample-accurate precision, the precise moment when phasor~ begins a new cycle from 0 to 1? We need to detect the sample on which it leaps from 1 back to 0. However, because phasor~ is constantly interpolating between 0 and 1, it might not leap down to exactly 0. So we can’t just use a ==~ object to see when its value is 0. Its exact value is relatively unknowable with any great precision; all we know is that it’s always increasing…except at the one instant when it leaps back down to (approximately) 0 and begins again. However, that instant is so remarkable that there is a way to detect it, with the help of an object called delta~. A delta~ object sends out a signal in which each sample reports the change in its input relative to the previous sample. So, if we send a phasor~ into a delta~, delta~ will always report a positive increase (or 0 if the phasor~ is completely stopped), except for when the phasor~ leaps downward. That’s the one sample on which delta~ will report a negative value. So we can use a <~ 0 object to detect the sample on which that occurs. The <~ 0 object will always output a signal of 0 (false) except for that one sample when it will report a 1 (true).
The sah~ object implements a “sample-and-hold” mechanism. When a particular threshold is surpassed in the signal in its right inlet, it will sample the value of the signal in its left inlet and will hold that as a constant output signal value until the next time the threshold is surpassed in the right inlet. By default its threshold is 0, so every time the <0~ object sends out a 1 preceded by a 0 it will cause sah~ to sample its left input. We use that fact to sample the slowly moving sinusoidal control value coming from cycle~ via mtof~. The cycle~ is moving slowing with a period of 9 seconds, and it’s varying + or – 12 semitones from a central pitch of 72 (C above middle C); that signal value is converted to frequency by the mtof~ (MIDI to frequency) object and that is sampled and held 4 times per second by the sah~ which is triggered by the phasor~. That frequency value from sah~ — which is constant for each 1/4 second until it is changed by sah~ — is used by cycle~ as its frequency value for its notes.
The individual notes are shaped by a trapezoidal amplitude envelope. The envelope is almost rectangular, but it has a very quick ramp up and down during the first and last 2% of each note (5 ms in this case). Because the change in frequency of the cycle~ that we’re listening to is triggered by the beginning of each cycle~ of the phasor~, it’s perfectly synchronized with the trapezoidal amplitude envelope.
If you want to use the beginning of a phasor~ ramp to trigger events in other parts of a Max patch, you can use an edge~ object to detect the 1 values coming from the <~ 0 object. The edge~ will send out a bang when a 0-to-1 or 1-to-0 transition occurs in a signal vector. But it can only do so with as much precision as the Max scheduler provides, which is not sample-accurate. So in this patch we use edge~ to detect the 0-to-1 transitions, then we use that bang to trigger a report of the current frequency value with a snapshot~ object. Note that not only will the edge~ object not send a bang with sample-rate precision, snapshot~ will only report the value at the beginning of the signal vector when (or immediately after) it receives the bang. So these translations between the sample-accurate-but-vector-based timing of MSP and the millisecond (or vector-based) timing of the Max scheduler show the slight difference between the true sample-accurate timing of the phasor~ and the close approximation that we can achieve with edge~. When you stop the audio (by turning off the dac~), you will probably notice a slight difference between the actual frequency being reported by the number~ object and the frequency reported by snapshot~ at the beginning of the vector when it was triggered by edge~. In many cases this difference is negligible, but in some cases it can lead to clicks when sudden changes are made in an audio signal that is not at 0 amplitude.
Trigger Sound Cues with the Mouse or from the Computer Keyboard
Image
Trigger sound cues with the mouse or from the computer keyboard.
This is effectively pretty much identical to Triggering Sound Cues Using a Computer Keyboard, but a) it allows the user to trigger sound cues either with the mouse or with the computer keyboard, b) it shows the user a more refined interface than just a Max patch, c) it demonstrates the use of pasted graphics and ubutton objects to make buttons on a user interface, and d) it demonstrates the use of Max 5’s “presentation” mode.
The loadbang object is again used to start audio and preload the sound cues. (There’s no volume control in this patch.) The three message boxes can again be triggered by the ASCII key codes coming from the computer keyboard (using the key and select objects). This time there are also three invisible buttons (ubutton objects) that the user can click on to trigger the same three messages. How does the user know to click on them if s/he can’t see them? They are placed on top of a picture that has been pasted into the background of the patch with the Paste Picture command from the Edit menu. The picture should be something that invites the user to click on it (usually something buttonlike) and ideally should even give the user a good idea of what will happen if s/he does click on it (that’s left as a surprise in this program). When the patch is locked and running, the ubutton objects become invisible but respond to mouse clicks just like a button object. When clicked upon, they highlight by showing an alternative color, to give the user feedback that the click has been successful. In this program we also connected the select object outlets to the ubuttons so that the appropriate keys in the graphic would appear to flash when we type on the keys.
In Max 5, you can select objects and designate them to be part of the user interface that the user will see, by choosing the Add to Presentation command from the Object menu. When the patch is put in presentation mode (by clicking on the Presentation Mode button that looks like an easel at the bottom of the Patcher window), only the objects that have been added to the presentation will be visible, and the rest of the Max objects will be hidden. Furthermore, the sizing and placement of those objects in the presentation can be set differently for presentation mode than they are in normal Max edit mode or run mode. This is very useful for creating precisely the look you want to present to the user (when it’s in presentation mode) that is laid out differently from the way you want your program to be laid out when you’re editing it. To cause the patch always to open in presentation mode, you can open the Patcher Inspector (from the View menu) and check the option Open in Presentation.
Trigger Sound Cues from the Computer Keyboard
Image
Trigger sound cues from the computer keyboard.
This shows how to prepare multiple sound cues for playback, and how to detect specific keystrokes in order to trigger the sound cues. It also shows how to initialize a program by triggering certain messages as soon as the patch is opened. The sound signal is sent through a multiplier to adjust the gain (volume).
This patch requires three specific sound files in order to work properly. The Max application should find these automatically in the Applications/Max5/Cycling ’74/docs/tutorial-patchers/msp-tut/ folder. If not, you can download them here: they are named bd+hh.aiff, snare.aiff, and cym.aiff.
When the patch is opened, the loadbang object sends out a ‘bang’ message to trigger three message box objects. The first message is ‘startwindow’ to the dac~ object. That turns on audio for this window only, which is a good idea, because one can’t know what other Max windows might be open that might make (potentially loud) sound as soon as audio is turned on; ‘startwindow’ activates audio objects only in the current window and any of its subwindows. The next message is 127, a MIDI-style value that sets the gain~ fader and causes it to scale whatever signal goes through it by a factor of 0.933 (just under full volume, about -0.6 dB). Then it triggers a message box that actually contains three messages, separated by commas. (The commas break up a single message into multiple messages, to be sent out one after the other as fast as possible.) Those messages open three sound files, assigning them to cue numbers 1, 2, and 3 in the sfplay~ object. They’re all monophonic sound files, so there’s only one channel of audio to come out, but that single signal is patched to both inlets of the dac~ object, so it goes to both speakers. When a key is typed on the computer keyboard, the key object reports the ASCII code of that key. The select object looks for the ASCII codes 49, 50, and 51–the codes for the number 1, 2, and 3 keys–and sends a ‘bang’ out the appropriate outlet if it detects one of those numbers. Those bangs trigger the messages ‘1’, ‘2’, or ‘3’ to sfplay~ to play those cues. >