-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusical_images.ts
343 lines (319 loc) · 12.4 KB
/
musical_images.ts
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//% color="#ff00E0"
//% icon="f001"
namespace MusicalImages {
export class MusicalImage {
private image_queue: Image[][] = [];
private stop_signal: boolean = false;
private playing: boolean = false;
private paused: boolean = false;
private pause_signal: boolean = false;
private handler: (channels: number[], notes: number[], frequencys: number[], durations: number[]) => void = undefined;
constructor() {
;
}
/**
* Set the images to play. Note this will stop the currently playing song.
* The images should be a number divisible by 2 x 88 px long.
* First column of pixels is the notes to play, so the pixel at the top is the highest note on a piano, and the pixel at the bottom is the lowest note on the piano.
* The second column of pixels is a binary representation of the number of milliseconds to play all those notes for, with the top pixel being 1, second pixel worth 2, third worth 4, etc.
* @param images: An array of images to play.
*/
set_image_queue(images: Image[][]) {
this.stop_signal = true;
this.image_queue = images;
}
/**
* Get the images to play. Returns a pointer to the internal array.
* @return: A pointer to an array of images that you can modify.
*/
get_image_queue(): Image[][] {
return this.image_queue
}
/**
* Set the handler when we play.
* @param handler: A function that will accept 4 arrays of numbers.
*/
set_handler(hdlr: (channels: number[], notes: number[], frequencys: number[], durations: number[]) => void) {
if (hdlr != undefined) {
this.handler = hdlr;
}
}
/**
* Play the musical images.
*/
play() {
const debug: boolean = false;
this.stop_signal = false;
this.playing = true;
this.paused = false;
this.pause_signal = false;
if (debug) {
game.consoleOverlay.setVisible(true);
}
for (let chunk = 0; chunk < this.image_queue[0].length; chunk ++) {
for (let col = 0; col < this.image_queue[0][0].width; col += 2) {
let freq_to_play: number[][] = [];
let time_to_play: number[] = [];
let note_to_play: number[][] = [];
let channel_to_play: number[][] = [];
for (let i = 0; i < this.image_queue.length; i ++) {
freq_to_play.push([]);
time_to_play.push(0);
note_to_play.push([]);
channel_to_play.push([]);
}
let freqs_playing: number[] = [];
let times_playing: number[] = [];
let note_playing: number[] = [];
let channel_playing: number[] = [];
for (let i = 0; i < this.image_queue.length; i ++) {
let image: Image = this.image_queue[i][chunk];
for (let row = 0; row < 88; row++) {
if (image.getPixel(col, row) != 0) {
let name: string = this._note_num_to_name(row);
let freq: number = Math.round(this._note_num_to_freq(row));
freq_to_play[i].push(freq);
note_to_play[i].push(row);
channel_to_play[i].push(i);
// if (debug) {
// console.log("[" + i + "]" + "Note:" + col + "," + row + ">" + name + "(" + freq + "hz)");
// }
}
if (image.getPixel(col + 1, row) != 0) {
let value: number = Math.pow(2, row);
time_to_play[i] += value;
// if (debug) {
// console.log("[" + i + "]" + "Time:" + (col + 1) + "," + row + ">+" + value);
// }
}
}
for (let chan_i = 0; chan_i < freq_to_play.length; chan_i ++) {
let freq_channel: number[] = freq_to_play[chan_i];
let note_channel: number[] = note_to_play[i];
let channel_channel: number[] = channel_to_play[i];
// console.log("Channels: ");
// console.log(channel_channel);
for (let note_i = 0; note_i < freq_channel.length; note_i ++) {
let freq: number = freq_channel[note_i];
let note: number = note_channel[note_i];
let channel: number = channel_channel[0];
if (freqs_playing.indexOf(freq) == -1) {
freqs_playing.push(freq);
times_playing.push(time_to_play[i]);
note_playing.push(note);
channel_playing.push(channel);
}
}
}
}
if (this.handler == undefined) {
for (let i = 0; i < freqs_playing.length; i++) {
let freq: number = freqs_playing[i];
let dur: number = times_playing[i];
let note: number = note_playing[i];
let channel: number = channel_playing[i];
if (debug) {
console.log("playTone(" + freq + "," + dur + ")//" + channel + ":" + note);
}
((frequency: number, duration: number) => {
control.runInParallel(() => {
music.playTone(frequency, duration);
});
})(freq, dur);
}
} else {
((c: number[], n: number[], f: number[], d: number[]) => {
control.runInParallel(() => {
this.handler(c, n, f, d);
})
})(channel_playing, note_playing, freqs_playing, times_playing);
}
if (debug) {
// console.log("[" + i + "]" + "Playing for " + time_to_play[i] + "ms");
console.log("-------------------------");
}
let smallest_time: number = undefined;
for (let time of time_to_play) {
if (smallest_time == undefined || time < smallest_time) {
smallest_time = time;
}
}
pause(smallest_time);
if (this.pause_signal) {
this.paused = true;
while (this.pause_signal && !this.stop_signal) {
pause(0);
}
this.paused = false;
}
if (this.stop_signal) {
this.playing = false;
return;
}
}
}
this.playing = false;
}
/**
* Get the note name from the note number (0 is A1, 87 C8)
* @param num: The note number.
* @return: The note name.
*/
// https://stackoverflow.com/a/54546263/10291933
_note_num_to_name(num: number): string {
let notes: string[] = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"];
let octave: number = Math.ceil(num / 12);
let name: string = notes[num % 12];
return name + octave;
}
/**
* Get the note frequency from the note num
* @param note: The note num.
* @return: The note frequency.
*/
// https://pages.mtu.edu/~suits/NoteFreqCalcs.html
_note_num_to_freq(note: number): number {
return 440 * Math.pow(Math.pow(2, 1 / 12), note - 36);
}
/**
* Stop playing. Does nothing if not playing.
*/
stop_playing() {
this.stop_signal = true;
}
/**
* Get whether we are playing or not.
*/
is_playing(): boolean {
return this.playing;
}
/**
* Pauses. Does nothing if not playing.
*/
pause_playing() {
this.pause_signal = true;
}
/**
* Resumes. Does nothing if not playing.
*/
resume_playing() {
this.pause_signal = false;
}
/**
* Get whether we are paused or not.
*/
is_paused(): boolean {
return this.paused;
}
}
let _mi: MusicalImage = undefined;
/**
* Initialize MusicalImage.
*/
//% block="musical image initialize"
//$ weight=100
export function init_musical_image() {
if (_mi == undefined) {
_mi = new MusicalImage();
}
}
/**
* Set the image queue of the MusicalImage.
* Note this will stop any song if any is playing.
* @param images: An array of Image.
*/
//% block="musical image set image queue to $images"
//% images.shadow="lists_create_with"
//% weight = 90
export function set_queue(images: Image[][]) {
if (_mi == undefined) {
init_musical_image();
}
_mi.set_image_queue(images);
}
/**
* Get the image queue of the MusicalImage.
* @return: A pointer to an array of Image.
*/
//% block="musical image get image queue"
//% weight=80
export function get_queue(): Image[][] {
if (_mi == undefined) {
init_musical_image();
}
return _mi.get_image_queue();
}
/**
* Start playing the MusicalImage.
*/
//% block="musical image play"
//% weight=70
export function play() {
if (_mi == undefined) {
init_musical_image();
}
_mi.play();
}
/**
* Stop playing the MusicalImage. Does nothing if not playing.
*/
//% block="musical image stop"
//% weight=60
export function stop() {
if (_mi == undefined) {
init_musical_image();
}
_mi.stop_playing();
}
/**
* Get whether we are playing the MusicalImage or not.
*/
//% block="musical image is playing"
//% weight=50
export function is_playing(): boolean {
if (_mi == undefined) {
init_musical_image();
}
return _mi.is_playing();
}
/**
* Pause the playing the MusicalImage. Does nothing if not playing.
*/
//% block="musical image pause"
//% weight=40
export function pause_playing() {
if (_mi == undefined) {
init_musical_image();
}
_mi.pause_playing();
}
/**
* Resume the playing the MusicalImage. Does nothing if not playing.
*/
//% block="musical image resume"
//% weight=30
export function resume_playing() {
_mi.resume_playing();
}
/**
* Get whether the MusicalImage is paused or not.
*/
//% block="musical image is paused"
//% weight=20
export function is_paused(): boolean {
return _mi.is_paused();
}
/**
* Set the handler when playing notes.
* @param hdlr: A function that accepts 4 arrays of numbers.
*/
//% block="musical image set notes handler channels $channels notes $notes frequencys $frequencys durations $durations"
//% draggableParameters="reporter"
//% weight=10
export function set_handler(h: (channels: number[], notes: number[], frequencys: number[], durations: number[]) => void) {
if (_mi == undefined) {
init_musical_image();
}
_mi.set_handler(h);
}
}