-
Notifications
You must be signed in to change notification settings - Fork 1
/
toneplayer.js
173 lines (139 loc) · 4.16 KB
/
toneplayer.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
class TonePlayer {
constructor(numRows, numCols) {
this.numRows = numRows;
this.numCols = numCols;
const bpm = 220;
Tone.Transport.bpm.value = bpm;
// this.synth = new Tone.PluckSynth().toMaster();
this.synth = new Tone.Synth().toMaster();
this.polySynth = new Tone.PolySynth(numRows, Tone.Synth).toMaster();
//set the transport to repeat
// Tone.Transport.loopEnd = '1m'
// Tone.Transport.loop = true
// Tone.Transport.start('+0.1');
}
playNote(note = "C4", duration = "8n") {
// Tone.Transport.schedule(this.noteFnFactory('C1'), 0);
//play a middle 'C' for the duration of an 8th note
this.synth.triggerAttackRelease(note, duration);
}
playPoly(arrOfNotes, duration = "8n") {
this.polySynth.triggerAttackRelease(arrOfNotes);
}
noteFnFactory(noteStr, duration = '8n') {
return function (time) {
this.synth.triggerAttackRelease(noteStr, duration, time);
}
}
convertNumToDuration(num, maxNum = this.numRows) {
const maxDuration = 2; // max time to hold a note
const durationPerNum = maxDuration / maxNum;
return Math.round(durationPerNum * num * 100) / 100;
}
convertNumToNote(num, notesArr = ['A', 'B', 'C']) {
let numNotes = 3;
let dividend = num / 3; // This is the octave?
dividend = Math.round(dividend);
dividend += 2; // Start at the second octave
let mod = num % 3; // This is the note
let note = notesArr[mod] + '' + dividend; // string
return note;
}
clearPoly() {
this.polySynth.releaseAll();
}
oscillatorTest(freqVal = "C4", duration = "+8n") {
var osc = new Tone.OmniOscillator();
osc.frequency.value = freqVal;
// osc.phase = 200;
osc.start().stop(duration);
var env = new Tone.AmplitudeEnvelope();
osc.connect(env);
env.toMaster();
osc.start();
env.triggerAttackRelease();
}
}
// doStuff();
function doStuff() {
// helloTone();
oscillatorTest();
// setInterval(helloTone, 125);
// checkCs(220);
}
function helloTone() {
//create a synth and connect it to the master output (your speakers)
var synth = new Tone.Synth().toMaster();
//play a middle 'C' for the duration of an 8th note
synth.triggerAttackRelease("C4", "8n");
// synth.triggerAttackRelease("C4", "8n");
}
function checkCs(bpm) {
var synth = new Tone.PluckSynth().toMaster()
//this function is called right before the scheduled time
// function triggerSynth(time) {
// //the time is the sample-accurate time of the event
// synth.triggerAttackRelease('C2', '8n', time)
// }
function noteFnFactory(noteStr, duration = '8n') {
return function (time) {
synth.triggerAttackRelease(noteStr, duration, time);
}
}
// Set bpm
Tone.Transport.bpm.value = bpm;
//schedule a few notes
Tone.Transport.schedule(noteFnFactory('C1'), 0)
Tone.Transport.schedule(noteFnFactory('C2'), '0:2')
// Tone.Transport.schedule(triggerSynth, '0:2:2.5')
Tone.Transport.schedule(noteFnFactory('C3'), '0:4')
//set the transport to repeat
Tone.Transport.loopEnd = '1m'
Tone.Transport.loop = true
Tone.Transport.start('+0.1');
// //start/stop the transport
// document.querySelector('.playToggle').addEventListener('change', function (e) {
// if (e.target.checked) {
// Tone.Transport.start('+0.1')
// } else {
// Tone.Transport.stop()
// }
// })
}
function oscillatorTest() {
var osc = new Tone.OmniOscillator();
osc.frequency.value = "C4";
osc.phase = 200;
osc.start().stop("+8n");
var env = new Tone.AmplitudeEnvelope();
osc.connect(env);
env.toMaster();
osc.start();
env.triggerAttack(); // Keeps going
}
function playNotes(noteStr) {
}
function keyboard() {
var synth = new Tone.Synth({
"oscillator": {
"type": "amtriangle",
"harmonicity": 0.5,
"modulationType": "sine"
},
"envelope": {
"attackCurve": 'exponential',
"attack": 0.05,
"decay": 0.2,
"sustain": 0.2,
"release": 1.5,
},
"portamento": 0.05
}).toMaster();
var keyboard = Interface.Keyboard();
keyboard.keyDown = function (note) {
synth.triggerAttack(note);
};
keyboard.keyUp = function () {
synth.triggerRelease();
};
}