9 #include <pitchToFrequency.h>
10 #include <Oscil.h> // oscillator template
11 #include <tables/sin2048_int8.h> // sine table for oscillator
12 #include <mozzi_midi.h>
15 // use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
16 Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
18 ADSR <CONTROL_RATE, AUDIO_RATE> envelope;
20 const char* pitch_name(byte pitch) {
21 static const char* names[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
22 return names[pitch % 12];
25 int pitch_octave(byte pitch) {
26 return (pitch / 12) - 1;
29 void noteOn(byte channel, byte pitch, byte velocity) {
30 aSin.setFreq(pitchFrequency[pitch]);
33 Serial.print("Note On: ");
34 Serial.print(pitch_name(pitch));
35 Serial.print(pitch_octave(pitch));
36 Serial.print(", channel=");
37 Serial.print(channel);
38 Serial.print(", velocity=");
39 Serial.println(velocity);
42 void noteOff(byte channel, byte pitch, byte velocity) {
46 Serial.print("Note Off: ");
47 Serial.print(pitch_name(pitch));
48 Serial.print(pitch_octave(pitch));
49 Serial.print(", channel=");
50 Serial.print(channel);
51 Serial.print(", velocity=");
52 Serial.println(velocity);
55 void controlChange(byte channel, byte control, byte value) {
56 Serial.print("Control change: control=");
57 Serial.print(control);
58 Serial.print(", value=");
60 Serial.print(", channel=");
61 Serial.println(channel);
66 envelope.setADLevels(255,48);
67 envelope.setTimes(150,200,700,200); // 700 is so the note will sustain 0.7 seconds unless a noteOff comes
69 aSin.setFreq(440); // default frequency
70 startMozzi(CONTROL_RATE);
77 AudioOutput_t updateAudio(){
78 return MonoOutput::from16Bit(envelope.next() * aSin.next()); // return an int signal centred around 0
83 midiEventPacket_t rx = MidiUSB.read();
86 break; //No pending events
90 rx.byte1 & 0xF, //channel
98 rx.byte1 & 0xF, //channel
106 rx.byte1 & 0xF, //channel
113 Serial.print("Unhandled MIDI message: ");
114 Serial.print(rx.header, HEX);
116 Serial.print(rx.byte1, HEX);
118 Serial.print(rx.byte2, HEX);
120 Serial.println(rx.byte3, HEX);