I’ve been trying to make a MIDI-style wrapper for the music APIs. This is what I have so far:
namespace MIDImusic {
let pressed_notes: string[] = [""]
function note_name(note: string) {
let notes = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"];
let octave
let keyNumber
octave = parseInt(note.charAt(1))
keyNumber = notes.indexOf(note.slice(0, -1))
if (keyNumber < 3) {
keyNumber = keyNumber + 12 + ((octave - 1) * 12) + 1
} else {
keyNumber = keyNumber + ((octave - 1) * 12) + 1
}
return 440 * Math.pow(2, (keyNumber- 49) / 12)
}
export function update(pause_time: number) {
for (let i = 0; i < Math.min(pressed_notes.length - 1, 5); i++) {
timer.background(function() {
music.playTone(note_name(pressed_notes[i]), pause_time)
})
}
}
export function on_note(note_name: string) {
pressed_notes.push(note_name)
}
export function off_note(note_name: string) {
let index = pressed_notes.indexOf(note_name)
if (index > -1) {
pressed_notes.removeAt(index)
}
}
}
And here is some sample code:
game.onUpdateInterval(20, function() {
MIDImusic.update(20)
})
MIDImusic.on_note("C4")
pause(500)
MIDImusic.on_note("E4")
pause(500)
MIDImusic.on_note("G4")
pause(500)
MIDImusic.on_note("C5")
pause(500)
MIDImusic.off_note("C5")
pause(500)
MIDImusic.off_note("G4")
pause(500)
MIDImusic.off_note("E4")
pause(500)
MIDImusic.off_note("C4")
pause(500)
It works…ish. If you listen to it, it pops a lot. I’m guessing this is because there is a 1ms delay after scheduling the tone or just how tasks are handled in Arcade - is there a better way to do this??? (Also you need the timers extension)