Skip to content

Instantly share code, notes, and snippets.

@bollu
Created April 17, 2014 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bollu/10960025 to your computer and use it in GitHub Desktop.
Save bollu/10960025 to your computer and use it in GitHub Desktop.
//required to compute frequencies from notes
1.05946 => float TwelthRoot2;
class Note {
int note_number;
fun Note relativeNote(int deltaSemitone) {
Note note;
note_number + deltaSemitone => note.note_number;
return note;
}
fun float getFrequency() {
return 440 * Math.pow(TwelthRoot2, note_number - 49);
}
fun NotePlayer Player(dur duration) {
NotePlayer player;
//why does this language have no 'this' operator? this is stupid, I'm implementing the copy ctor!
note_number => player.note.note_number;
duration => player.duration;
return player;
}
fun static int octaveSemitones() {
return 12;
}
fun static Note MiddleC() {
Note middleC;
40 => middleC.note_number;
return middleC;
}
}
//virtual base class which represents anything that steps times
class Playable {
//TODO: abstract this into a Player or something - it should *not* have direct access to the sinusoidal oscillator
fun void Play(SinOsc s) {}
}
class NotePlayer extends Playable{
Note note;
dur duration;
fun void Play(SinOsc s) {
note.getFrequency() => s.freq;
s => dac;
duration => now;
}
}
class Rest extends Playable{
dur duration;
fun static Rest New(dur duration) {
Rest rest;
duration => rest.duration;
return rest;
}
fun void Play(SinOsc s) {
s =< dac;
duration => now;
}
}
Note middleC, D, E, F, G, A, B, higherC;
Note.MiddleC() @=> middleC;
middleC.relativeNote(2) @=> D ;
D.relativeNote(2) @=> E;
E.relativeNote(1) @=> F;
F.relativeNote(2) @=> G;
G.relativeNote(2) @=> A;
A.relativeNote(2) @=> B;
B.relativeNote(1) @=> higherC;
[middleC.Player(500::ms),
middleC.Player(500::ms),
G.Player(500::ms),
G.Player(500::ms),
A.Player(500::ms),
A.Player(500::ms),
G.Player(1000::ms),
Rest.New(200::ms),
F.Player(500::ms),
F.Player(500::ms),
E.Player(500::ms),
E.Player(500::ms),
D.Player(500::ms),
D.Player(500::ms),
middleC.Player(1000::ms)] @=> Playable notes[];
0 => int i;
SinOsc s;
while( true ) {
s =< dac;
100::ms => now;
notes[i].Play(s);
i++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment