Added examples

This commit is contained in:
Jakob Bak 2013-02-10 08:39:44 +01:00
parent 9c4ffb6e7b
commit 5f994fcf7f
8 changed files with 323 additions and 34 deletions

View File

@ -174,22 +174,17 @@ public:
float getGain3(); // 0.0 - 1.0 float getGain3(); // 0.0 - 1.0
// NOTE FUNCTIONS // NOTE FUNCTIONS
void noteOn(uint8_t note, uint8_t vel); // 0 - 255 void noteOn(uint8_t note, uint8_t vel); // 0 - 127
void noteOn(uint8_t note); // 0 - 255 void noteOn(uint8_t note); // 0 - 127
void noteOff(uint8_t note); // 0 - 255 void noteOff(uint8_t note); // 0 - 127
void noteOff(); void noteOff();
uint16_t getNoteFrequency(uint8_t note); // 0 - 127 CHECK THIS OUT uint16_t getNoteFrequency(uint8_t note); // 0 - 127
// ENVELOPE FUNCTIONS // ENVELOPE FUNCTIONS
void enableEnvelope(); void enableEnvelope();
void disableEnvelope(); void disableEnvelope();
void setEnvStage(uint8_t stage); // 0 - 4 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 setAttack(uint8_t att); // 0 - 127
void setDecay(uint8_t dec); // 0 - 127 void setDecay(uint8_t dec); // 0 - 127
void setSustain(uint8_t sus); // 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) void MMusic::setAttack(uint8_t att)
{ {
if(att>127) att = 127; if(att>127) att = 127;
@ -1000,7 +978,7 @@ bool midiRead = false;
void MMidi::init() void MMidi::init()
{ {
Serial.begin(115200); Serial.begin(9600);
midiBufferIndex = 0; midiBufferIndex = 0;
midiChannel = MIDI_CHANNEL - 1; 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) { void MMidi::controller(uint8_t channel, uint8_t number, uint8_t value) {
//Serial.print(value);
switch(number) { switch(number) {
case DETUNE: case DETUNE:
@ -1119,6 +1098,15 @@ void MMidi::controller(uint8_t channel, uint8_t number, uint8_t value) {
case PORTAMENTO: case PORTAMENTO:
//Music.setPortamento(value); // function to be defined, also argument //Music.setPortamento(value); // function to be defined, also argument
break; 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: case DETUNE1:
Music.setDetune1(value/5120.0); Music.setDetune1(value/5120.0);
break; break;

View File

@ -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 <Music.h>
void setup() {
// We initialise the sound engine by calling Music.init() which outputs a tone
Music.init();
}
void loop() {
}

View File

@ -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 <Music.h>
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;
}
}

View File

@ -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 <Music.h>
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;
}
}

View File

@ -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 <Music.h>
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;
}
}

View File

@ -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 <Music.h>
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() {
}

View File

@ -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 <Music.h>
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();
}

View File

@ -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 <Music.h>
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;
}
}