-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundControl_withMIDI.ino
68 lines (51 loc) · 1.53 KB
/
soundControl_withMIDI.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <MIDI.h>
#include <MozziGuts.h>
#include <utils.h>
#include <Oscil.h>
#include <Line.h>
#include <tables/sin2048_int8.h>
#define CONTROL_RATE 128 // 2^x
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
// for envelope
Line <unsigned int> aGain;
unsigned int release_control_steps = CONTROL_RATE;
unsigned int release_audio_steps = 16384;
int fade_counter;
float vol= 1. ;
unsigned int freq;
void HandleControlChange(byte channel, byte CCnumber, byte value) {
switch(CCnumber){
case 100:
vol = map(value,0, 127, 0., 1.);
break;
}
}
void HandleNoteOn(byte channel, byte pitch, byte velocity) {
freq = mtof(pitch);
aGain.set(velocity<<8); // might need a fade-in to avoid clicks
}
void HandleNoteOff(byte channel, byte pitch, byte velocity) {
aGain.set(0,release_audio_steps);
fade_counter = release_control_steps;
}
void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);
MIDI.setHandleControlChange(HandleControlChange);
MIDI.setHandleNoteOn(HandleNoteOn);
MIDI.setHandleNoteOff(HandleNoteOff);
aSin.setFreq(440u);
startMozzi(CONTROL_RATE);
}
void updateControl(){
// Ideally, call MIDI.read the fastest you can for real-time performance.
// In practice, there is a balance required between real-time
// audio generation and a responsive midi control rate.
MIDI.read();
if (fade_counter-- <=0) aGain.set(0,0,2); // a line along 0
}
int updateAudio(){
return (int) ((aGain.next()>>8) * aSin.next() * vol )>>8;
}
void loop() {
audioHook(); // required here
}