forked from HerrmuttLobby/chord-splitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chordsplitter.js
129 lines (102 loc) · 2.76 KB
/
chordsplitter.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Herrmutt Lobby • Chord Splitter JS 0.1 */
/* (c) Herrmutt Lobby 2012 • herrmuttlobby.com */
/* This code is distributed under a Creative Commons : Attribution, Share Alike, No-commercial Licence */
/* INPUT : list message starting with note then a note number and velocity ( note noteNbr velocity ) or "reset" message to reset the chord */
/* OUTPUT : quick series of ordered note tuples ( [index, noteNbr, velocity, nbrOfNoteInTheChord] ) */
/* 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/) */
/* CONFIG (should be done with message through an inlet */
groupSend = false; // choose between pushing note once by once through the outlet, or all in a big tuple
debug = false; // put in debug mode
/* CODE */
quicksort = function(input){ /* quick sort function */
if(input.length <= 1)
{
return input;
}else
{
var pivot = input.splice(0, 1);
var less = [];
var greater = [];
var output = [];
for (i = 0; i < input.length; i++)
{
x = input[i];
if (x[0] <= pivot[0][0])
{
less.push(x);
}else{
greater.push(x);
}
}
return output.concat(quicksort(less), pivot, quicksort(greater));
}
}
var splitter = new Object;
splitter.groupSend = groupSend;
splitter.debug = debug;
splitter.chord = [];
splitter.newNote = function(note) /* receive a new note info */
{
if(note[1] === 0)
{
this.delNote(note);
}else
{
this.addNote(note);
}
}
splitter.addNote = function(note){ /* add a note to the chord */
this.chord.push(note);
this.chord = quicksort(this.chord);
this.output();
}
splitter.delNote = function(note){
for (i = 0; i < this.chord.length; i++)
{
if(chord[i][0] === note[0])
{
this.chord.splice(i, 1);
}
}
this.output();
}
splitter.reset()
{
this.chord = [];
}
splitter.output = function(chord){
var outChord = [];
for (i = 0; i < this.chord.length; i++)
{
var out = [i+1, this.chord[i][0], this.chord[i][1], chord.length]
if(splitter.groupSend == false){ /* SENDING TUPLE ONE AT A TIME */
outlet(0, out);
}
outChord.push(out);
}
if(splitter.groupSend == true){ /* SENDING THE WHOLE CHORD AS A BIG TUPLE CONTAINING TUPLES */
outlet(0, outChord);
}
}
/* MAIN */
inlets = 1; // number of inlets
outlets = 1; // number of outlets
function note(note, vel)
{
splitter.newNote([note, val]);
}
function list(info, note, vel)
{
splitter.newNote([note, val]);
}
function reset()
{
splitter.reset();
}
/* DEBUG */
if(splitter.debug == true)
{
setInterval(function(){
splitter.newNote([Math.round(Math.random()*127),Math.round(Math.random()*127)]);
}, 3000 );
}