Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Timer.h>
- #include <MIDI.h>
- MIDI_CREATE_DEFAULT_INSTANCE();
- Timer t[10];
- float notes[] = {
- 8.1757989156, 8.6619572180, 9.1770239974, 10.3008611535, 10.3008611535, 10.9133822323, 11.5623257097, 12.2498573744, 12.9782717994, 13.7500000000, 14.5676175474, 15.4338531643, 16.3515978313, 17.3239144361, 18.3540479948, 19.4454364826, 20.6017223071, 21.8267644646, 23.1246514195, 24.4997147489, 25.9565435987, 27.5000000000, 29.1352350949, 30.8677063285, 32.7031956626, 34.6478288721, 36.7080959897, 38.8908729653, 41.2034446141, 43.6535289291, 46.2493028390, 48.9994294977, 51.9130871975, 55.0000000000, 58.2704701898, 61.7354126570, 65.4063913251, 69.2956577442, 73.4161919794, 77.7817459305, 82.4068892282, 87.3070578583, 92.4986056779, 97.9988589954, 103.8261743950, 110.0000000000, 116.5409403795, 123.4708253140, 130.8127826503, 138.5913154884, 146.8323839587, 155.5634918610, 164.8137784564, 174.6141157165, 184.9972113558, 195.9977179909, 207.6523487900, 220.0000000000, 233.0818807590, 246.9416506281, 261.6255653006, 277.1826309769, 293.6647679174, 311.1269837221, 329.6275569129, 349.2282314330, 369.9944227116, 391.9954359817, 415.3046975799, 440.0000000000, 466.1637615181, 493.8833012561, 523.2511306012, 554.3652619537, 587.3295358348, 622.2539674442, 659.2551138257, 698.4564628660, 739.9888454233, 783.9908719635, 830.6093951599, 880.0000000000, 932.3275230362, 987.7666025122, 1046.5022612024, 1108.7305239075, 1174.6590716696, 1244.5079348883, 1318.5102276515, 1396.9129257320, 1479.9776908465, 1567.9817439270, 1661.2187903198, 1760.0000000000, 1864.6550460724, 1975.5332050245, 2093.0045224048, 2217.4610478150, 2349.3181433393, 2489.0158697766, 2637.0204553030, 2793.8258514640, 2959.9553816931, 3135.9634878540, 3322.4375806396, 3520.0000000000, 3729.3100921447, 3951.0664100490, 4186.0090448096, 4434.9220956300, 4698.6362866785, 4978.0317395533, 5274.0409106059, 5587.6517029281, 5919.9107633862, 5919.9107633862, 6644.8751612791, 7040.0000000000, 7458.6201842894, 7902.1328200980, 8372.0180896192, 8869.8441912599, 9397.2725733570, 9956.0634791066, 10548.0818212118, 11175.3034058561, 11839.8215267723, 12543.8539514160
- };
- long pins[] = {
- 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
- };
- void setup() {
- MIDI.setHandleNoteOn(handleNoteOn); // Put only the name of the function
- // Do the same for NoteOffs
- MIDI.setHandleNoteOff(handleNoteOff);
- // Initiate MIDI communications, listen to all channels
- MIDI.begin(MIDI_CHANNEL_OMNI);
- Serial.begin(9600);
- pinMode(13, OUTPUT); //LED
- for(int i = 0; i<11; i++) {
- pinMode(pins[i], OUTPUT);
- }
- }
- void handleNoteOn(byte channel, byte pitch, byte velocity) {
- int chan = map(channel, 1, 16, 1, 10);
- if(velocity > 0) {
- switch (chan) {
- case 1:
- playNote(pins[0], notes[pitch]);
- break;
- case 2:
- playNote(pins[1], notes[pitch]);
- break;
- case 3:
- playNote(pins[2], notes[pitch]);
- break;
- case 4:
- playNote(pins[3], notes[pitch]);
- break;
- case 5:
- playNote(pins[4], notes[pitch]);
- break;
- case 6:
- playNote(pins[5], notes[pitch]);
- break;
- case 7:
- playNote(pins[6], notes[pitch]);
- break;
- case 8:
- playNote(pins[7], notes[pitch]);
- break;
- case 9:
- playNote(pins[8], notes[pitch]);
- break;
- case 10:
- playNote(pins[9], notes[pitch]);
- break;
- }
- }
- }
- void handleNoteOff(byte channel, byte pitch, byte velocity) {
- int chan = map(channel, 1, 16, 1, 10);
- switch (chan) {
- case 1:
- stopNote(pins[0]);
- break;
- case 2:
- stopNote(pins[1]);
- break;
- case 3:
- stopNote(pins[2]);
- break;
- case 4:
- stopNote(pins[3]);
- break;
- case 5:
- stopNote(pins[4]);
- break;
- case 6:
- stopNote(pins[5]);
- break;
- case 7:
- stopNote(pins[6]);
- break;
- case 8:
- stopNote(pins[7]);
- break;
- case 9:
- stopNote(pins[8]);
- break;
- case 10:
- stopNote(pins[9]);
- break;
- }
- }
- void loop() {
- MIDI.read();
- t[1].update();
- t[2].update();
- t[3].update();
- t[4].update();
- t[5].update();
- t[6].update();
- t[7].update();
- t[8].update();
- t[9].update();
- t[10].update();
- }
- long ids[10];
- void playNote(int pin, unsigned long freq) {
- float ms = 1000/freq/2;
- ids[pin] = t[pin].oscillate(pin, ms, HIGH);
- }
- void stopNote(int pin) {
- t[pin].stop(ids[pin]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement