From 5f994fcf7f81c9cec2791e93b1be3e14bb2c5608 Mon Sep 17 00:00:00 2001 From: Jakob Bak Date: Sun, 10 Feb 2013 08:39:44 +0100 Subject: [PATCH] Added examples --- software/lib/MMM/Music.h | 56 ++++++--------- .../examples/Music/_1_minimal/_1_minimal.ino | 25 +++++++ .../Music/_2_waveform/_2_waveform.ino | 56 +++++++++++++++ .../Music/_3_frequency/_3_frequency.ino | 42 +++++++++++ .../MMM/examples/Music/_4_gain/_4_gain.ino | 47 +++++++++++++ .../Music/_5_semi_detune/_5_semi_detune.ino | 31 ++++++++ .../MMM/examples/Music/_6_midi/_6_midi.ino | 30 ++++++++ .../_6_noteOn_noteOff/_6_noteOn_noteOff.ino | 70 +++++++++++++++++++ 8 files changed, 323 insertions(+), 34 deletions(-) create mode 100644 software/lib/MMM/examples/Music/_1_minimal/_1_minimal.ino create mode 100644 software/lib/MMM/examples/Music/_2_waveform/_2_waveform.ino create mode 100644 software/lib/MMM/examples/Music/_3_frequency/_3_frequency.ino create mode 100644 software/lib/MMM/examples/Music/_4_gain/_4_gain.ino create mode 100644 software/lib/MMM/examples/Music/_5_semi_detune/_5_semi_detune.ino create mode 100644 software/lib/MMM/examples/Music/_6_midi/_6_midi.ino create mode 100644 software/lib/MMM/examples/Music/_6_noteOn_noteOff/_6_noteOn_noteOff.ino diff --git a/software/lib/MMM/Music.h b/software/lib/MMM/Music.h index 59ce681..ddadd1c 100644 --- a/software/lib/MMM/Music.h +++ b/software/lib/MMM/Music.h @@ -174,22 +174,17 @@ public: float getGain3(); // 0.0 - 1.0 // NOTE FUNCTIONS - void noteOn(uint8_t note, uint8_t vel); // 0 - 255 - void noteOn(uint8_t note); // 0 - 255 - void noteOff(uint8_t note); // 0 - 255 + void noteOn(uint8_t note, uint8_t vel); // 0 - 127 + void noteOn(uint8_t note); // 0 - 127 + void noteOff(uint8_t note); // 0 - 127 void noteOff(); - uint16_t getNoteFrequency(uint8_t note); // 0 - 127 CHECK THIS OUT + uint16_t getNoteFrequency(uint8_t note); // 0 - 127 // ENVELOPE FUNCTIONS void enableEnvelope(); void disableEnvelope(); void setEnvStage(uint8_t stage); // 0 - 4 - void setAttack16bit(uint16_t att); // 0 - 65535 - void setDecay16bit(uint16_t dec); // 0 - 65535 - void setSustain16bit(uint16_t sus); // 0 - 65535 - void setRelease16bit(uint16_t rel); // 0 - 65535 - void setAttack(uint8_t att); // 0 - 127 void setDecay(uint8_t dec); // 0 - 127 void setSustain(uint8_t sus); // 0 - 127 @@ -898,6 +893,13 @@ void MMusic::noteOff() +uint16_t MMusic::getNoteFrequency(uint8_t note) +{ + int f; + memcpy_P(&f, &hertzTable[note], 2); + return f; +} + ///////////////////////////////////// // @@ -923,30 +925,6 @@ void MMusic::setEnvStage(uint8_t stage) } -void MMusic::setAttack16bit(uint16_t att) -{ - attack = att; -} - - -void MMusic::setDecay16bit(uint16_t dec) -{ - decay = dec; -} - - -void MMusic::setSustain16bit(uint16_t sus) -{ - sustain = sus; -} - - -void MMusic::setRelease16bit(uint16_t rel) -{ - release = rel; -} - - void MMusic::setAttack(uint8_t att) { if(att>127) att = 127; @@ -1000,7 +978,7 @@ bool midiRead = false; void MMidi::init() { - Serial.begin(115200); + Serial.begin(9600); midiBufferIndex = 0; midiChannel = MIDI_CHANNEL - 1; @@ -1111,6 +1089,7 @@ void MMidi::aftertouch(uint8_t channel, uint8_t note, uint8_t pressure) { void MMidi::controller(uint8_t channel, uint8_t number, uint8_t value) { + //Serial.print(value); switch(number) { case DETUNE: @@ -1119,6 +1098,15 @@ void MMidi::controller(uint8_t channel, uint8_t number, uint8_t value) { case PORTAMENTO: //Music.setPortamento(value); // function to be defined, also argument break; + case FREQUENCY1: + Music.setFrequency1(Music.getNoteFrequency(value)); + break; + case FREQUENCY2: + Music.setFrequency2(Music.getNoteFrequency(value)); + break; + case FREQUENCY3: + Music.setFrequency3(Music.getNoteFrequency(value)); + break; case DETUNE1: Music.setDetune1(value/5120.0); break; diff --git a/software/lib/MMM/examples/Music/_1_minimal/_1_minimal.ino b/software/lib/MMM/examples/Music/_1_minimal/_1_minimal.ino new file mode 100644 index 0000000..bbe5b73 --- /dev/null +++ b/software/lib/MMM/examples/Music/_1_minimal/_1_minimal.ino @@ -0,0 +1,25 @@ +// You can set the number of oscillators (1 to 3) and the bit depth of the +// oscillators (8 or 12 bit). These settings must be defined before the +// inclusion of the MMM library files. They default to 1 osciallator +// and 8bit respectively. + +#define NUM_OSCILLATORS 1 +#define BIT_DEPTH 12 + +// The Music object is automatically instantiated when the header file is +// included. Make calls to the Music objects wit "Music.function(args)". +// You still need to call Music.init() in the setup() function below. +#include + + +void setup() { + + // We initialise the sound engine by calling Music.init() which outputs a tone + Music.init(); + +} + +void loop() { + +} + diff --git a/software/lib/MMM/examples/Music/_2_waveform/_2_waveform.ino b/software/lib/MMM/examples/Music/_2_waveform/_2_waveform.ino new file mode 100644 index 0000000..764cd06 --- /dev/null +++ b/software/lib/MMM/examples/Music/_2_waveform/_2_waveform.ino @@ -0,0 +1,56 @@ +// You can set the number of oscillators (1 to 3) and the bit depth of the +// oscillators (8 or 12 bit). These settings must be defined before the +// inclusion of the MMM library files. They default to 1 osciallator +// and 8bit respectively. + +#define NUM_OSCILLATORS 1 +#define BIT_DEPTH 8 + +// The Music object is automatically instantiated when the header file is +// included. Make calls to the Music objects with "Music.function(args)". +// You still need to call Music.init() in the setup() function below. +#include + +int delayTime = 1000; +int cnt = 0; +long timeNow; +long lastTime = 0; + +byte waveFormArray[] = { SINE, + SQUARE, + PULSE, + TRIANGLE, + SAW, + FUZZ, + DIGI1, + DIGI2, + DIGI3, + DIGI4, + NOISE, + DIGI6, + TAN1, + TAN2, + TAN3, + TAN4 + }; + +void setup() { + + // We initialise the sound engine by calling Music.init() which outputs a tone + Music.init(); + Music.setFrequency(220); + Music.setWaveform(SINE); + +} + +void loop() { + + timeNow = millis(); + if((timeNow-lastTime) > delayTime) { + cnt = cnt + 1; + if(cnt>15) cnt = 0; + Music.setWaveform(waveFormArray[cnt]); + lastTime = timeNow; + } +} + diff --git a/software/lib/MMM/examples/Music/_3_frequency/_3_frequency.ino b/software/lib/MMM/examples/Music/_3_frequency/_3_frequency.ino new file mode 100644 index 0000000..a1c5393 --- /dev/null +++ b/software/lib/MMM/examples/Music/_3_frequency/_3_frequency.ino @@ -0,0 +1,42 @@ +// You can set the number of oscillators (1 to 3) and the bit depth of the +// oscillators (8 or 12 bit). These settings must be defined before the +// inclusion of the MMM library files. They default to 1 osciallator +// and 8bit respectively. + +#define NUM_OSCILLATORS 3 +#define BIT_DEPTH 8 + +// The Music object is automatically instantiated when the header file is +// included. Make calls to the Music objects with "Music.function(args)". +// You still need to call Music.init() in the setup() function below. +#include + +int delayTime = 400; +int cnt = 0; +float baseFrequency = 110; +long timeNow; +long lastTime = 0; + +void setup() { + + // We initialise the sound engine by calling Music.init() which outputs a tone + Music.init(); + Music.setFrequency(220); + Music.setDetune(0.001); // ranges from 0.00 - 0.02 are usually good + Music.setWaveform(SAW); + +} + +void loop() { + + timeNow = millis(); + if((timeNow-lastTime) > delayTime) { + cnt = cnt + 1; + if(cnt>3) cnt = 0; + Music.setFrequency1(baseFrequency*1*cnt); + Music.setFrequency2(baseFrequency*1.3333*cnt); + Music.setFrequency3(baseFrequency*1.5*cnt); + lastTime = timeNow; + } +} + diff --git a/software/lib/MMM/examples/Music/_4_gain/_4_gain.ino b/software/lib/MMM/examples/Music/_4_gain/_4_gain.ino new file mode 100644 index 0000000..d93eb4c --- /dev/null +++ b/software/lib/MMM/examples/Music/_4_gain/_4_gain.ino @@ -0,0 +1,47 @@ +// You can set the number of oscillators (1 to 3) and the bit depth of the +// oscillators (8 or 12 bit). These settings must be defined before the +// inclusion of the MMM library files. They default to 1 osciallator +// and 8bit respectively. + +#define NUM_OSCILLATORS 3 +#define BIT_DEPTH 8 + +// The Music object is automatically instantiated when the header file is +// included. Make calls to the Music objects with "Music.function(args)". +// You still need to call Music.init() in the setup() function below. +#include + +int delayTime = 100; +int cnt = 0; +float baseFrequency = 110; +long timeNow; +long lastTime = 0; + +void setup() { + + // We initialise the sound engine by calling Music.init() which outputs a tone + Music.init(); + Music.setFrequency(220); + Music.setDetune(0.005); // ranges from 0.00 - 0.02 are usually good + Music.setWaveform(PULSE); + Music.setFrequency1(baseFrequency*1); + Music.setFrequency2(baseFrequency*1.3333); + Music.setFrequency3(baseFrequency*1.5); + + +} + +void loop() { + + timeNow = millis(); + if((timeNow-lastTime) > delayTime) { + cnt = cnt + 1; + if(cnt>16) cnt = 0; + float counter = float(cnt); + Music.setGain1(1.0/cnt); + Music.setGain2(1.0/cnt); + Music.setGain3(1.0/cnt); + lastTime = timeNow; + } +} + diff --git a/software/lib/MMM/examples/Music/_5_semi_detune/_5_semi_detune.ino b/software/lib/MMM/examples/Music/_5_semi_detune/_5_semi_detune.ino new file mode 100644 index 0000000..1c5c1f0 --- /dev/null +++ b/software/lib/MMM/examples/Music/_5_semi_detune/_5_semi_detune.ino @@ -0,0 +1,31 @@ +// You can set the number of oscillators (1 to 3) and the bit depth of the +// oscillators (8 or 12 bit). These settings must be defined before the +// inclusion of the MMM library files. They default to 1 osciallator +// and 8bit respectively. + +#define NUM_OSCILLATORS 3 +#define BIT_DEPTH 8 + +// The Music object is automatically instantiated when the header file is +// included. Make calls to the Music objects with "Music.function(args)". +// You still need to call Music.init() in the setup() function below. +#include + +void setup() { + + // We initialise the sound engine by calling Music.init() which outputs a tone + Music.init(); + Music.setFrequency(110); + Music.setDetune(0.005); // ranges from 0.00 - 0.02 are usually good + Music.setWaveform(DIGI2); + Music.setSemitone1(0); + Music.setSemitone2(7); + Music.setSemitone3(-12); + + +} + +void loop() { + +} + diff --git a/software/lib/MMM/examples/Music/_6_midi/_6_midi.ino b/software/lib/MMM/examples/Music/_6_midi/_6_midi.ino new file mode 100644 index 0000000..c738708 --- /dev/null +++ b/software/lib/MMM/examples/Music/_6_midi/_6_midi.ino @@ -0,0 +1,30 @@ +// You can set the number of oscillators (1 to 3) and the bit depth of the +// oscillators (8 or 12 bit). These settings must be defined before the +// inclusion of the MMM library files. They default to 1 osciallator +// and 8bit respectively. + +#define NUM_OSCILLATORS 3 +#define BIT_DEPTH 8 +#define MIDI + +// The Music object is automatically instantiated when the header file is +// included. Make calls to the Music objects with "Music.function(args)". +// You still need to call Music.init() in the setup() function below. +#include + +void setup() { + + // We initialise the sound engine by calling Music.init() which outputs a tone + Music.init(); + Midi.init(); + Music.enableEnvelope(); + +} + +void loop() { + + // In order to send MIDI to the sketch, use the Music_Controls.pde Processing sketch + Midi.checkMidi(); + +} + diff --git a/software/lib/MMM/examples/Music/_6_noteOn_noteOff/_6_noteOn_noteOff.ino b/software/lib/MMM/examples/Music/_6_noteOn_noteOff/_6_noteOn_noteOff.ino new file mode 100644 index 0000000..8d67a7f --- /dev/null +++ b/software/lib/MMM/examples/Music/_6_noteOn_noteOff/_6_noteOn_noteOff.ino @@ -0,0 +1,70 @@ +// You can set the number of oscillators (1 to 3) and the bit depth of the +// oscillators (8 or 12 bit). These settings must be defined before the +// inclusion of the MMM library files. They default to 1 osciallator +// and 8bit respectively. + +#define NUM_OSCILLATORS 3 +#define BIT_DEPTH 8 + +// The Music object is automatically instantiated when the header file is +// included. Make calls to the Music objects with "Music.function(args)". +// You still need to call Music.init() in the setup() function below. +#include + +boolean noteIsOn = false; +int n = 0; +int dir = 1; +int rootNote = 36; +int note[] = {0,2,3,5,7,9,10,12,14}; + +long time = 0; +long lastTime = 0; +long timeDelay = 80; + +void setup() { + + // We initialise the sound engine by calling Music.init() which outputs a tone + Music.init(); + + // Choosing the square wave oscillator. + Music.setWaveform(DIGI3); + + // Detuning the three oscillators slightly to create movement in the sound. + Music.setDetune(0.008); + + // enabling the envelope lets us define an gain envelope for the synth + // without having to specify it in our loop() or physics code. + Music.enableEnvelope(); + Music.setAttack(8); + Music.setDecay(90); + Music.setSustain(48); + Music.setRelease(64); + +} + +void loop() { + + // This short routine loops note over and over again + time = millis(); + if(time - lastTime > timeDelay) { + if(!noteIsOn) { + Music.noteOn(rootNote+note[n]); + noteIsOn = true; + n = n + dir; + if(n > 7) + { + dir = -1; + } + else if(n < 1) + { + dir = 1; + } + } else { + Music.noteOff(); + noteIsOn = false; + } + lastTime = time; + } + +} +