Csonic to
JS
Compiler
Peter Sovietov
, 2011-12-25
// Lars Kindermann's algorithm (http://www.reglos.de/musinum/) // Author: Peter Sovietov desc:Simple algorithmic music generator slider1:33<1,30000,1>song number slider2:0.5<0.1,4>note duration (beats) slider3:0<0,3,1{Major,Natural minor,Melodic minor,Harmonic minor,Pentatonic,Blues}>scale slider4:48<0,127,1<0,127,1{0: C-1,1: C#/Db-1,2: D-1,3: D#/Eb-1,4: E-1,5: F-1,6: F#/Gb-1,7: G-1,8: G#/Ab-1,9: A-1,10: A#/Bb-1,11: B-1,12: C0,13: C#/Db0,14: D0,15: D#/Eb0,16: E0,17: F0,18: F#/Gb0,19: G0,20: G#/Ab0,21: A0,22: A#/Bb0,23: B0,24: C1,25: C#/Db1,26: D1,27: D#/Eb1,28: E1,29: F1,30: F#/Gb1,31: G1,32: G#/Ab1,33: A1,34: A#/Bb1,35: B1,36: C2,37: C#/Db2,38: D2,39: D#/Eb2,40: E2,41: F2,42: F#/Gb2,43: G2,44: G#/Ab2,45: A2,46: A#/Bb2,47: B2,48: C3,49: C#/Db3,50: D3,51: D#/Eb3,52: E3,53: F3,54: F#/Gb3,55: G3,56: G#/Ab3,57: A3,58: A#/Bb3,59: B3,60: C4,61: C#/Db4,62: D4,63: D#/Eb4,64: E4,65: F4,66: F#/Gb4,67: G4,68: G#/Ab4,69: A4,70: A#/Bb4,71: B4,72: C5,73: C#/Db5,74: D5,75: D#/Eb5,76: E5,77: F5,78: F#/Gb5,79: G5,80: G#/Ab5,81: A5,82: A#/Bb5,83: B5,84: C6,85: C#/Db6,86: D6,87: D#/Eb6,88: E6,89: F6,90: F#/Gb6,91: G6,92: G#/Ab6,93: A6,94: A#/Bb6,95: B6,96: C7,97: C#/Db7,98: D7,99: D#/Eb7,100: E7,101: F7,102: F#/Gb7,103: G7,104: G#/Ab7,105: A7,106: A#/Bb7,107: B7,108: C8,109: C#/Db8,110: D8,111: D#/Eb8,112: E8,113: F8,114: F#/Gb8,115: G8,116: G#/Ab8,117: A8,118: A#/Bb8,119: B8,120: C9,121: C#/Db9,122: D9,123: D#/Eb9,124: E9,125: F9,126: F#/Gb9,127: G9}>low note in_pin:none out_pin:none @init time_counter = 0; musinum_counter = 0; note_number = -1; major = 0; major[] = {0, 2, 4, 5, 7, 9, 11}; natural_minor = major + sizeof(major); natural_minor[] = {0, 2, 3, 5, 7, 8, 10}; melodic_minor = natural_minor + sizeof(natural_minor); melodic_minor[] = {0, 2, 3, 5, 7, 9, 11}; harmonic_minor = melodic_minor + sizeof(melodic_minor); harmonic_minor[] = {0, 2, 3, 5, 7, 8, 11}; pentatonic = harmonic_minor + sizeof(harmonic_minor); pentatonic[] = {0, 2, 4, 7, 9}; blues = pentatonic + sizeof(pentatonic); blues[] = {0, 3, 5, 6, 7, 10}; scales = blues + sizeof(blues); scales[] = {major, natural_minor, melodic_minor, harmonic_minor, pentatonic, blues}; scales_length = scales + sizeof(scales); scales_length[] = {sizeof(major), sizeof(natural_minor), sizeof(melodic_minor), sizeof(harmonic_minor), sizeof(pentatonic), sizeof(blues)}; notes = scales_length + sizeof(scales_length); function set_scale(scale, length, first_note) { for (i = 0; i < 32; i += 1) { notes[i] = min(first_note + (i / length | 0) * 12 + scale[i % length], 127); } } function play_time(beats) { return max(1 / (tempo / 60) * beats * srate, samplesblock); } function count_bits(n) { count_bits_n = n; for (count_bits_c = 0; count_bits_n; count_bits_c += 1) { count_bits_n &= count_bits_n - 1; } return count_bits_c; } function musinum(multiple) { if (multiple != musinum_multiple) { musinum_counter = 0; } musinum_multiple = multiple; musinum_bits = count_bits(musinum_counter); musinum_counter += musinum_multiple; musinum_counter &= 0xffffffff; return notes[musinum_bits]; } @slider set_scale(scales[slider3], scales_length[slider3], slider4); @block time_counter -= samplesblock; if (time_counter < 0) { if (note_number >= 0) { midisend(time_counter + samplesblock, 0x90, note_number); } new_note_number = musinum(slider1); if (new_note_number != note_number) { note_number = new_note_number; midisend(time_counter + samplesblock, 0x90, note_number | 0x7f00); } else { note_number = -1; } time_counter += play_time(slider2); }