-
Notifications
You must be signed in to change notification settings - Fork 1
/
quantize-to-trigger.js
94 lines (76 loc) · 2.13 KB
/
quantize-to-trigger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Herrmutt Lobby • Minimun Note length js 0.1
* (c) Herrmutt Lobby 2013 • herrmuttlobby.com
* This code is distributed under a
* Creative Commons : Attribution, Share Alike, No-commercial Licence
*
* INPUT : list [ note, velocity ] - buffers input messages
* OUTPUT : list [ note, velocity, duration ] - outputs when trigger message is received
*
* MADE TO BE USED WITHIN the JS object of MAX4LIVE or MAX/MSP or in PureData
* with the jj object of the
* PDJ external (http://www.le-son666.com/software/pdj/)
**/
// queue of notes waiting to be dispatched
var queue = [];
// Fetches "Trigger note" number via script name
function get_trigger_note() {
return parseInt( this.patcher.getnamed( 'trigger_note' ).getvalueof(), 10 );
}
function get_output_at_trigger_vel() {
return this.patcher.getnamed( 'output_at_trigger_vel' ).getvalueof();
}
function list( note, velocity ) {
// Gets trigger note value via "script name"
if( note == get_trigger_note() ) {
// ignoring the note off so far
if( velocity === 0 ) return;
return flush_notes( velocity );
}
// save note
if( velocity > 0 ) {
queue.push( {
note : note,
velocity : velocity,
on_time : ( new Date() ).getTime(),
off_time : null
} );
}
// search for corresponding note and set duration
else {
for( var i = 0; i < queue.length; i++ ) {
if( queue[i].note == note ) {
// TODO: is getTime() same as Date.now() ?
queue[i].off_time = ( new Date() ).getTime();
queue[i].duration = queue[i].off_time - queue[i].on_time;
}
}
}
}
/** output all notes from the queue **/
function flush_notes( velocity ) {
for( var i = 0; i < queue.length; i++ ) {
if( get_output_at_trigger_vel() == 1 ) {
queue[i].velocity = velocity;
}
outlet( 0, [ queue[i].note, queue[i].velocity, queue[i].duration ] );
}
free();
}
/**
Generic methods
**/
function free() {
queue = [];
}
function bang() {
if( DEBUG ) post( "bang output all notes\n" );
flush_notes();
}
/**
Improving javascript timing
@see http://cycling74.com/docs/max5/vignettes/js/jsthreading.html
**/
bang.immediate = 1;
list.immediate = 1;
flush_notes.immediate = 1;