Updates to Music.h, deleted Midi.h and Midi.cpp
We can now use these commands in the arduino sketch: #define NUM_OSCILLATORS x // where x is 1, 2 or 3 #define BIT_DEPTH x // where x is 8 or 12 Deleted Midi library files as they where causing multiple definition errors with the outdated Music.h file.
This commit is contained in:
parent
f5246baf9c
commit
c983a92107
@ -1,248 +0,0 @@
|
||||
/*
|
||||
Midi.cpp - Music library
|
||||
Copyright (c) 2012 Copenhagen Institute of Interaction Design.
|
||||
All right reserved.
|
||||
|
||||
This library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser Public License
|
||||
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
+ author: Jakob Bak
|
||||
+ contact: j.bak@ciid.dk
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/pgmspace.h>
|
||||
#include <hardwareSerial.h>
|
||||
//#include <MidiTables.h>
|
||||
#include <Music.h>
|
||||
#include <Midi.h>
|
||||
|
||||
prog_uint16_t hertzTable[] PROGMEM = {8,8,9,9,10,10,11,12,12,13,14,15,16,17,18,19,20,21,23,24,25,27,29,30,32,34,36,38,41,43,46,48,51,54,58,61,65,69,73,77,82,87,92,97,103,109,116,123,130,138,146,155,164,174,184,195,207,219,233,246,261,277,293,311,329,349,369,391,415,440,466,493,523,554,587,622,659,698,739,783,830,880,932,987,1046,1108,1174,1244,1318,1396,1479,1567,1661,1760,1864,1975,2093,2217,2349,2489,2637,2793,2959,3135,3322,3520,3729,3951,4186,4434,4698,4978,5274,5587,5919,6271,6644,7040,7458,7902,8372,8869,9397,9956,10548,11175,11839,12543};
|
||||
|
||||
MMidi Midi;
|
||||
|
||||
bool midiRead = false;
|
||||
|
||||
|
||||
void MMidi::init()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
midiBufferIndex = 0;
|
||||
midiChannel = 0;
|
||||
|
||||
}
|
||||
|
||||
void MMidi::checkMidi()
|
||||
{
|
||||
while(Serial.available() > 0) {
|
||||
|
||||
data = Serial.read();
|
||||
|
||||
if(data & 0x80 && (data & 0x0F) == midiChannel) { // bitmask with 10000000 to see if byte is over 127 (data&0x80)
|
||||
midiBufferIndex = 0; // and check if the midi channel corresponds to the midiChannel
|
||||
midiRead = true; // the device is set to listen to.
|
||||
} else if(data & 0x80) { // Else if the byte is over 127 (but not on the device's
|
||||
midiRead = false; // midiChannel, don't read this or any following bytes.
|
||||
}
|
||||
|
||||
if(midiRead) {
|
||||
midiBuffer[midiBufferIndex] = data;
|
||||
midiBufferIndex++;
|
||||
if (midiBufferIndex > 2) {
|
||||
midiHandler();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MMidi::midiHandler() {
|
||||
|
||||
uint8_t midiChannel = (midiBuffer[0] & 0x0F);
|
||||
|
||||
|
||||
switch(midiBuffer[0] & 0xF0) { // bit mask with &0xF0 ?
|
||||
case 0x80:
|
||||
noteOff (midiBuffer[0] & 0x0F, // midi channel 0-16
|
||||
midiBuffer[1] & 0x7F, // note value 0-127
|
||||
midiBuffer[2] & 0x7F); // note velocity 0-127
|
||||
break;
|
||||
|
||||
case 0x90:
|
||||
noteOn (midiBuffer[0] & 0x0F, // midi channel 0-16
|
||||
midiBuffer[1] & 0x7F, // note value 0-127
|
||||
midiBuffer[2] & 0x7F); // note velocity 0-127
|
||||
break;
|
||||
|
||||
case 0xA0:
|
||||
aftertouch (midiBuffer[0] & 0x0F, // midi channel 0-16
|
||||
midiBuffer[1] & 0x7F, // note value 0-127
|
||||
midiBuffer[2] & 0x7F);// note velocity 0-127
|
||||
break;
|
||||
|
||||
case 0xB0:
|
||||
controller (midiBuffer[0] & 0x0F, // midi channel 0-16
|
||||
midiBuffer[1] & 0x7F, // controller number 0-127
|
||||
midiBuffer[2] & 0x7F);// controller value 0-127
|
||||
break;
|
||||
|
||||
case 0xC0:
|
||||
programChange (midiBuffer[0] & 0x0F, // midi channel 0-16
|
||||
midiBuffer[1] & 0x7F); // program number 0-127
|
||||
break;
|
||||
|
||||
case 0xD0:
|
||||
channelPressure (midiBuffer[0] & 0x0F, // midi channel 0-16
|
||||
midiBuffer[1] & 0x7F); // pressure amount 0-127
|
||||
break;
|
||||
|
||||
case 0xE0:
|
||||
pitchWheel (midiBuffer[0] & 0x0F, // midi channel 0-16
|
||||
midiBuffer[1] & 0x7F, // higher bits 0-6
|
||||
midiBuffer[2] & 0x7F);// lower bits 7-13
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MMidi::noteOff(uint8_t channel, uint8_t note, uint8_t vel) {
|
||||
|
||||
if(notePlayed == note) {
|
||||
Music.setEnvStage(4);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MMidi::noteOn(uint8_t channel, uint8_t note, uint8_t vel) {
|
||||
|
||||
Music.setEnvStage(1);
|
||||
Music.setVelSustain(vel);
|
||||
Music.setVelPeak(vel);
|
||||
notePlayed = note;
|
||||
memcpy_P(&frequency, &hertzTable[notePlayed],2);
|
||||
Music.setFrequency1(frequency);
|
||||
Music.setFrequency2(frequency);
|
||||
Music.setFrequency3(frequency);
|
||||
}
|
||||
|
||||
|
||||
void MMidi::aftertouch(uint8_t channel, uint8_t note, uint8_t pressure) {
|
||||
// Write code here for Aftertouch
|
||||
}
|
||||
|
||||
|
||||
void MMidi::controller(uint8_t channel, uint8_t number, uint8_t value) {
|
||||
|
||||
switch(number) {
|
||||
case DETUNE:
|
||||
Music.setDetune(value/5120.0);
|
||||
break;
|
||||
case PORTAMENTO:
|
||||
//Music.setPortamento(value); // function to be defined, also argument
|
||||
break;
|
||||
case DETUNE1:
|
||||
Music.setDetune1(value/5120.0);
|
||||
break;
|
||||
case DETUNE2:
|
||||
Music.setDetune2(value/5120.0);
|
||||
break;
|
||||
case DETUNE3:
|
||||
Music.setDetune3(value/5120.0);
|
||||
break;
|
||||
case SEMITONE1:
|
||||
if(15 < value && value < 113) {
|
||||
int8_t val = (((value-16)/2)-24);
|
||||
Music.setSemitone1(val);
|
||||
} else if (value < 16) {
|
||||
Music.setSemitone1(-24);
|
||||
} else {
|
||||
Music.setSemitone1(24);
|
||||
}
|
||||
break;
|
||||
case SEMITONE2:
|
||||
if(15 < value && value < 113) {
|
||||
int8_t val = (((value-16)/2)-24);
|
||||
Music.setSemitone2(val);
|
||||
} else if (value < 16) {
|
||||
Music.setSemitone2(-24);
|
||||
} else {
|
||||
Music.setSemitone2(24);
|
||||
}
|
||||
break;
|
||||
case SEMITONE3:
|
||||
if(15 < value && value < 113) {
|
||||
int8_t val = (((value-16)/2)-24);
|
||||
Music.setSemitone3(val);
|
||||
} else if (value < 16) {
|
||||
Music.setSemitone3(-24);
|
||||
} else {
|
||||
Music.setSemitone3(24);
|
||||
}
|
||||
break;
|
||||
case GAIN1:
|
||||
Music.setGain1(uint16_t(value * 512));
|
||||
break;
|
||||
case GAIN2:
|
||||
Music.setGain2(uint16_t(value * 512));
|
||||
break;
|
||||
case GAIN3:
|
||||
Music.setGain3(uint16_t(value * 512));
|
||||
break;
|
||||
case WAVEFORM:
|
||||
Music.setWaveform(value / 8);
|
||||
break;
|
||||
case WAVEFORM1:
|
||||
Music.setWaveform1(value / 8);
|
||||
break;
|
||||
case WAVEFORM2:
|
||||
Music.setWaveform2(value / 8);
|
||||
break;
|
||||
case WAVEFORM3:
|
||||
Music.setWaveform3(value / 8);
|
||||
break;
|
||||
case ENV_ATTACK:
|
||||
Music.setAttack(value);
|
||||
break;
|
||||
case ENV_DECAY:
|
||||
Music.setDecay(value);
|
||||
break;
|
||||
case ENV_SUSTAIN:
|
||||
Music.setSustain(value);
|
||||
break;
|
||||
case ENV_RELEASE:
|
||||
Music.setRelease(value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MMidi::programChange(uint8_t channel, uint8_t number) {
|
||||
// Write code here for Program Change
|
||||
}
|
||||
|
||||
|
||||
void MMidi::channelPressure(uint8_t channel, uint8_t pressure) {
|
||||
// Write code here for Channel Pressure
|
||||
}
|
||||
|
||||
|
||||
void MMidi::pitchWheel(uint8_t channel, uint8_t highBits, uint8_t lowBits) {
|
||||
// Write code here for Pitch Wheel
|
||||
}
|
||||
@ -1,101 +0,0 @@
|
||||
/*
|
||||
Midi.h - Music library
|
||||
Copyright (c) 2012 Copenhagen Institute of Interaction Design.
|
||||
All right reserved.
|
||||
|
||||
This library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser Public License
|
||||
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
+ author: Jakob Bak
|
||||
+ contact: j.bak@ciid.dk
|
||||
*/
|
||||
|
||||
// MIDI specific constants
|
||||
|
||||
#ifndef MIDI_CHANNEL
|
||||
#define MIDI_CHANNEL 1
|
||||
#endif
|
||||
|
||||
// SYSEX constants
|
||||
#define SYSEX_LIMIT 16
|
||||
#define CFO_MANUFACTURER_ID 44
|
||||
#define CFO_DEVICE_GROUP_ID 3
|
||||
#define SET_CHANNEL 0
|
||||
|
||||
|
||||
//synth parameters as MIDI controller numbers
|
||||
#define DETUNE 4
|
||||
#define WAVEFORM 5
|
||||
#define PORTAMENTO 6
|
||||
|
||||
#define FREQUENCY1 10
|
||||
#define SEMITONE1 11
|
||||
#define DETUNE1 12
|
||||
#define GAIN1 13
|
||||
#define WAVEFORM1 14
|
||||
|
||||
#define FREQUENCY2 20
|
||||
#define SEMITONE2 21
|
||||
#define DETUNE2 22
|
||||
#define GAIN2 23
|
||||
#define WAVEFORM2 24
|
||||
|
||||
#define FREQUENCY3 30
|
||||
#define SEMITONE3 31
|
||||
#define DETUNE3 32
|
||||
#define GAIN3 33
|
||||
#define WAVEFORM3 34
|
||||
|
||||
#define ENV_ATTACK 114
|
||||
#define ENV_DECAY 115
|
||||
#define ENV_SUSTAIN 116
|
||||
#define ENV_RELEASE 117
|
||||
|
||||
#define ENV2_ATTACK 124
|
||||
#define ENV2_DECAY 125
|
||||
#define ENV2_SUSTAIN 126
|
||||
#define ENV2_RELEASE 127
|
||||
|
||||
|
||||
// Synth parameters used in MIDI code
|
||||
#define ENV_MAX_GAIN (65536 * 4 - 1)
|
||||
|
||||
|
||||
class MMidi {
|
||||
public:
|
||||
void init();
|
||||
void checkMidi();
|
||||
|
||||
void midiHandler();
|
||||
void noteOff(uint8_t channel, uint8_t note, uint8_t vel);
|
||||
void noteOn(uint8_t channel, uint8_t note, uint8_t vel);
|
||||
void aftertouch(uint8_t channel, uint8_t note, uint8_t pressure);
|
||||
void controller(uint8_t channel, uint8_t number, uint8_t value);
|
||||
void programChange(uint8_t channel, uint8_t number);
|
||||
void channelPressure(uint8_t channel, uint8_t pressure);
|
||||
void pitchWheel(uint8_t channel, uint8_t highBits, uint8_t lowBits);
|
||||
|
||||
private:
|
||||
|
||||
// MIDI
|
||||
uint8_t data;
|
||||
uint8_t midiBuffer[SYSEX_LIMIT];
|
||||
uint8_t midiChannel;
|
||||
|
||||
int midiBufferIndex;
|
||||
uint16_t frequency;
|
||||
uint8_t notePlayed;
|
||||
};
|
||||
|
||||
extern MMidi Midi;
|
||||
@ -32,16 +32,6 @@
|
||||
|
||||
#include "Wavetable.h"
|
||||
|
||||
|
||||
////////////////////////////////////
|
||||
//
|
||||
// SET NUMBER OF OSCILLATORS HERE.
|
||||
// SHOULD BE 1, 2 or 3
|
||||
//
|
||||
////////////////////////////////////
|
||||
//#define NUM_OSCILLATORS 3 //edited BV 29Jan13.
|
||||
|
||||
|
||||
// current sample rate is 15625 as defined in the init() section
|
||||
#define SAMPLE_RATE 15625
|
||||
|
||||
@ -52,11 +42,18 @@
|
||||
#error NUM_OSCILLATORS shall be 1, 2 or 3
|
||||
#endif
|
||||
|
||||
#ifndef BIT_DEPTH
|
||||
#define BIT_DEPTH 8
|
||||
#elif (BIT_DEPTH == 8)||(BIT_DEPTH == 12)
|
||||
#else
|
||||
#error BIT_DEPTH shall be 8 or 12
|
||||
#endif
|
||||
|
||||
|
||||
// Maximum possible value for amplification envelope
|
||||
#define MAX_ENV_GAIN 65535
|
||||
|
||||
|
||||
|
||||
// Table of MIDI note values to frequency in Hertz
|
||||
prog_uint16_t hertsTable[] PROGMEM = {8,8,9,9,10,10,11,12,12,13,14,15,16,17,18,19,20,21,23,24,25,27,29,30,32,34,36,38,41,43,46,48,51,54,58,61,65,69,73,77,82,87,92,97,103,109,116,123,130,138,146,155,164,174,184,195,207,219,233,246,261,277,293,311,329,349,369,391,415,440,466,493,523,554,587,622,659,698,739,783,830,880,932,987,1046,1108,1174,1244,1318,1396,1479,1567,1661,1760,1864,1975,2093,2217,2349,2489,2637,2793,2959,3135,3322,3520,3729,3951,4186,4434,4698,4978,5274,5587,5919,6271,6644,7040,7458,7902,8372,8869,9397,9956,10548,11175,11839,12543};
|
||||
|
||||
@ -102,28 +99,14 @@ public:
|
||||
void setWaveform3(uint16_t waveForm); //
|
||||
|
||||
// GAIN FUNCTIONS
|
||||
//void setGainFloat(float value); // 0.0 - 1.0
|
||||
//void setGain16bit(uint16_t value); // 0 - 65535
|
||||
//void setGain(uint16_t value); // 0 - 65535
|
||||
void setGain(float value); // 0.0 - 1.0 USE THIS
|
||||
//void setGain1(uint16_t value); // 0 - 65535
|
||||
//void setGain2(uint16_t value); // 0 - 65535
|
||||
//void setGain3(uint16_t value); // 0 - 65535
|
||||
//float getGainFloat(); // 0.0 - 1.0 USE THIS
|
||||
void setGain1(float value); // 0.0 - 1.0 USE THIS
|
||||
void setGain2(float value); // 0.0 - 1.0 USE THIS
|
||||
void setGain3(float value); // 0.0 - 1.0 USE THIS
|
||||
//float getGain1Float(); // 0.0 - 1.0 USE THIS
|
||||
//float getGain2Float(); // 0.0 - 1.0 USE THIS
|
||||
//float getGain3Float(); // 0.0 - 1.0 USE THIS
|
||||
float getGain(); // 0.0 - 1.0 USE THIS
|
||||
float getGain1(); // 0.0 - 1.0 USE THIS
|
||||
float getGain2(); // 0.0 - 1.0 USE THIS
|
||||
float getGain3(); // 0.0 - 1.0 USE THIS
|
||||
//uint16_t getGain();
|
||||
//uint16_t getGain1();
|
||||
//uint16_t getGain2();
|
||||
//uint16_t getGain3();
|
||||
|
||||
// NOTE FUNCTIONS
|
||||
void noteOn(uint8_t note, uint8_t vel); // 0 - 255
|
||||
@ -223,6 +206,8 @@ extern MMusic Music;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////
|
||||
//
|
||||
// 8 BIT WAVETABLE - AUDIO INTERRUPT SERVICE ROUTINE
|
||||
@ -447,8 +432,13 @@ void MMusic::synthInterrupt12bitSine()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
MMusic Music;
|
||||
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
//
|
||||
// AUDIO INTERRUPT. USE EITHER 8bit or 12bitSine VERSION
|
||||
@ -459,9 +449,16 @@ ISR(TIMER2_COMPA_vect) { // timer 2 is audio interrupt timer
|
||||
|
||||
OCR2A = 127; // don't change this
|
||||
|
||||
#if BIT_DEPTH == 8
|
||||
|
||||
Music.synthInterrupt8bit();
|
||||
|
||||
// Music.synthInterrupt12bitSine();
|
||||
#endif
|
||||
#if BIT_DEPTH == 12
|
||||
|
||||
Music.synthInterrupt12bitSine();
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@ -696,24 +693,6 @@ void MMusic::setWaveform3(uint16_t waveForm)
|
||||
//
|
||||
/////////////////////////////////////
|
||||
|
||||
/*
|
||||
void MMusic::setGainFloat(float value)
|
||||
{
|
||||
gain = uint16_t(value * 65535);
|
||||
gain1 = gain;
|
||||
gain2 = gain;
|
||||
gain3 = gain;
|
||||
}
|
||||
|
||||
|
||||
void MMusic::setGain16bit(uint16_t value)
|
||||
{
|
||||
gain = value;
|
||||
gain1 = value;
|
||||
gain2 = value;
|
||||
gain3 = value;
|
||||
}
|
||||
*/
|
||||
|
||||
void MMusic::setGain(float value)
|
||||
{
|
||||
@ -723,15 +702,6 @@ void MMusic::setGain(float value)
|
||||
gain3 = gain;
|
||||
}
|
||||
|
||||
/*
|
||||
void MMusic::setGain(uint16_t value)
|
||||
{
|
||||
gain = value;
|
||||
gain1 = value;
|
||||
gain2 = value;
|
||||
gain3 = value;
|
||||
}
|
||||
*/
|
||||
|
||||
void MMusic::setGain1(float value)
|
||||
{
|
||||
@ -750,24 +720,6 @@ void MMusic::setGain3(float value)
|
||||
gain3 = uint16_t(value * 65535);
|
||||
}
|
||||
|
||||
/*
|
||||
void MMusic::setGain1(uint16_t value)
|
||||
{
|
||||
gain1 = value;
|
||||
}
|
||||
|
||||
|
||||
void MMusic::setGain2(uint16_t value)
|
||||
{
|
||||
gain2 = value;
|
||||
}
|
||||
|
||||
|
||||
void MMusic::setGain3(uint16_t value)
|
||||
{
|
||||
gain3 = value;
|
||||
}
|
||||
*/
|
||||
|
||||
float MMusic::getGain()
|
||||
{
|
||||
@ -792,56 +744,8 @@ float MMusic::getGain3()
|
||||
return float(gain3)/65535.0;
|
||||
}
|
||||
|
||||
/*
|
||||
float MMusic::getGainFloat()
|
||||
{
|
||||
return float(gain)/65535.0;
|
||||
}
|
||||
|
||||
|
||||
float MMusic::getGain1Float()
|
||||
{
|
||||
return float(gain1)/65535.0;
|
||||
}
|
||||
|
||||
|
||||
float MMusic::getGain2Float()
|
||||
{
|
||||
return float(gain2)/65535.0;
|
||||
}
|
||||
|
||||
|
||||
float MMusic::getGain3Float()
|
||||
{
|
||||
return float(gain3)/65535.0;
|
||||
}
|
||||
|
||||
|
||||
uint16_t MMusic::getGain()
|
||||
{
|
||||
return gain;
|
||||
}
|
||||
|
||||
|
||||
uint16_t MMusic::getGain1()
|
||||
{
|
||||
return gain1;
|
||||
}
|
||||
|
||||
|
||||
uint16_t MMusic::getGain2()
|
||||
{
|
||||
return gain2;
|
||||
}
|
||||
|
||||
|
||||
uint16_t MMusic::getGain3()
|
||||
{
|
||||
return gain3;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/////////////////////////////////////
|
||||
//
|
||||
@ -944,7 +848,6 @@ void MMusic::setAttack(uint8_t att)
|
||||
{
|
||||
if(att>127) att = 127;
|
||||
memcpy_P(&attack, &envTimeTable[127 - att],2);
|
||||
//attack = envTimeTable[127 - att];
|
||||
}
|
||||
|
||||
|
||||
@ -952,7 +855,6 @@ void MMusic::setDecay(uint8_t dec)
|
||||
{
|
||||
if(dec>127) dec = 127;
|
||||
memcpy_P(&decay, &envTimeTable[127 - dec],2);
|
||||
//decay = envTimeTable[127 - dec];
|
||||
}
|
||||
|
||||
|
||||
@ -966,7 +868,6 @@ void MMusic::setRelease(uint8_t rel)
|
||||
{
|
||||
if(rel>127) rel = 127;
|
||||
memcpy_P(&release, &envTimeTable[127 - rel],2);
|
||||
//release = envTimeTable[127 - rel];
|
||||
}
|
||||
|
||||
|
||||
@ -981,11 +882,5 @@ void MMusic::setVelPeak(uint8_t vel)
|
||||
velPeak = vel * (MAX_ENV_GAIN / 128);
|
||||
}
|
||||
|
||||
#endif // close guard Music_h
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // close guard
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user