Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Example of a sound being triggered by MIDI input.
- Demonstrates playing notes with Mozzi in response to MIDI input,
- using Arduino MIDI library v4.2
- (https://github.com/FortySevenEffects/arduino_midi_library/releases/tag/4.2)
- Circuit:
- MIDI input circuit as per http://arduino.cc/en/Tutorial/Midi
- Note: midi input on rx pin, not tx as in the illustration on the above page.
- Midi has to be disconnected from rx for sketch to upload.
- Audio output on digital pin 9 on a Uno or similar.
- Mozzi documentation/API
- https://sensorium.github.io/Mozzi/doc/html/index.html
- Mozzi help/discussion/announcements:
- https://groups.google.com/forum/#!forum/mozzi-users
- Tim Barrass 2013-14, CC by-nc-sa.
- */
- #include <MIDIUSB.h>
- #include <MozziGuts.h>
- #include <Oscil.h> // oscillator template
- #include <tables/sin2048_int8.h> // sine table for oscillator
- #include <mozzi_midi.h>
- #include <ADSR.h>
- #include <Phasor.h>
- // use #define for CONTROL_RATE, not a constant
- #define CONTROL_RATE 128 // Hz, powers of 2 are most reliable
- // audio sinewave oscillator
- //Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
- //Oscil <2048, CONTROL_RATE> kVib(SIN2048_DATA);
- Phasor <AUDIO_RATE> aPhasor1;
- Phasor <AUDIO_RATE> aPhasor2;
- // envelope generator
- ADSR <CONTROL_RATE, AUDIO_RATE> envelope;
- #define LED 13 // shows if MIDI is being recieved
- void controlChange(byte channel, byte vv, byte velocity) {
- }
- void noteOn(byte channel, byte pitch, byte velocity) {
- //aSin.setFreq(mtof(float(pitch)));
- aPhasor1.setFreq(mtof(float(pitch)));
- //aPhasor2.setFreq(mtof(float(pitch+.2)));
- envelope.noteOn();
- digitalWrite(LED,HIGH);
- }
- void noteOff(byte channel, byte pitch, byte velocity) {
- envelope.noteOff();
- digitalWrite(LED,LOW);
- }
- void setup() {
- pinMode(10,OUTPUT);
- digitalWrite(10,LOW);
- pinMode(LED, OUTPUT);
- // Connect the HandleNoteOn function to the library, so it is called upon reception of a NoteOn.
- //MIDI.setHandleNoteOn(HandleNoteOn); // Put only the name of the function
- //MIDI.setHandleNoteOff(HandleNoteOff); // Put only the name of the function
- // Initiate MIDI communications, listen to all channels (not needed with Teensy usbMIDI)
- //MIDI.begin(MIDI_CHANNEL_OMNI);
- envelope.setADLevels(255,70);
- envelope.setTimes(10,50,50,200); // 1000 is so the note will sustain 1 seconds unless a noteOff comes
- //aSin.setFreq(440); // default frequency
- float freq = 55.f;
- aPhasor1.setFreq(freq);
- //aPhasor2.setFreq(freq+0.2f);
- //kVib.setFreq(6.5f);
- startMozzi(CONTROL_RATE);
- }
- void updateControl(){
- midiEventPacket_t rx = MidiUSB.read();
- switch (rx.header) {
- case 0:
- break; //No pending events
- case 0x9:
- noteOn(rx.byte1 & 0xF,rx.byte2,rx.byte3);
- break;
- case 0x8:
- noteOff(rx.byte1 & 0xF,rx.byte2,rx.byte3);
- break;
- case 0xB:
- controlChange(rx.byte1 & 0xF,rx.byte2,rx.byte3);
- break;
- default:
- break;
- }
- envelope.update();
- }
- int updateAudio(){
- char asig1 = (char)(aPhasor1.next()>>24);
- //char asig2 = (char)(aPhasor2.next()>>24);
- return ((int)(envelope.next() * asig1)/2);
- //return (int) (envelope.next() * aSin.next())>>8;
- }
- void loop() {
- audioHook(); // required here
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement