Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "charlesMIDI.h"
- #include <arduino.h>
- // general word constructor
- MidiWord::MidiWord(){
- command = nullByte;
- pitch = nullByte;
- velocity = nullByte;
- }
- // word constructor with parameters
- MidiWord::MidiWord(byte inCommand, byte inPitch, byte inVelocity){
- command = inCommand;
- pitch = inPitch;
- velocity = inVelocity;
- }
- byte MidiWord::setAll(byte inCommand, byte inPitch, byte inVelocity){
- command = inCommand;
- pitch = inPitch;
- velocity = inVelocity;
- }
- // check the serial que for next midi word
- void MidiWord::getWord(){
- byte incomingByte = nullByte;
- command = nullByte;
- pitch = nullByte;
- velocity = nullByte;
- boolean fullWord = false;
- //check to see if the serial is available
- if(!Serial.available()){
- return;
- }
- incomingByte = Serial.read();
- while(!fullWord){
- if(incomingByte > 0x7F && incomingByte < 0xF0){
- command = incomingByte;
- }
- while(!Serial.available()){
- }
- incomingByte = Serial.read();
- if(incomingByte > 0x7F){
- continue;
- }
- else{
- pitch = incomingByte;
- }
- while(!Serial.available()){
- }
- incomingByte = Serial.read();
- if(incomingByte > 0x7F){
- continue;
- }
- else{
- velocity = incomingByte;
- fullWord = true;
- return;
- }
- }
- // noteOn between 0x90 and 0x9F, note off 0x80 and 0x8F
- if(incomingByte >= 0x80 && incomingByte <= 0x9F){
- command = incomingByte;
- while(!Serial.available()){
- //delay(1);
- }
- pitch = Serial.read();
- while(!Serial.available()){
- // delay(1);
- }
- velocity = Serial.read();
- }
- return;
- }
- /*
- // check the serial que for next midi word
- void MidiWord::getWord(){
- byte incomingByte = nullByte;
- command = nullByte;
- pitch = nullByte;
- velocity = nullByte;
- //check to see if the serial is available
- if(Serial.available() > 0){
- incomingByte = Serial.read();
- }
- else{
- return;
- }
- // noteOn between 0x90 and 0x9F, note off 0x80 and 0x8F
- if(incomingByte >= 0x80 && incomingByte <= 0x9F){
- command = incomingByte;
- while(!Serial.available()){
- //delay(1);
- }
- pitch = Serial.read();
- while(!Serial.available()){
- // delay(1);
- }
- velocity = Serial.read();
- }
- return;
- }
- */
- byte MidiWord::getCommand() const{
- return command;
- }
- byte MidiWord::getPitch() const{
- return pitch;
- }
- byte MidiWord::getVelocity() const{
- return velocity;
- }
- boolean MidiWord::isEqual(MidiWord inWord) const{
- if(command == inWord.getCommand() && pitch == inWord.getPitch() && velocity == inWord.getVelocity()){
- //digitalWrite(ledPin, flipState(digitalRead(ledPin)));
- return true;
- }
- else{
- //digitalWrite(ledPin, flipState(digitalRead(ledPin)));
- return false;
- }
- }
- // send the midi note out on specific channel.
- void MidiWord::writeWord(byte sdChannel) const{
- //a way to say "don't send
- if(sdChannel > 0x0f){
- return;
- }
- if(command > 0x00){
- //digitalWrite(ledPin, flipState(digitalRead(ledPin)));
- //Serial.write(command);
- //Serial.write(pitch);
- //Serial.write(velocity);
- Serial.write(command + sdChannel);
- Serial.write(pitch);
- Serial.write(velocity);
- }
- else{
- return;
- }
- }
- MidiChannel::MidiChannel(){
- //channel = 0x00;
- currentPitch = nullByte;
- playState = false;
- startTime = millis();
- }
- /*
- MidiChannel::MidiChannel(byte selectChannel){
- channel = selectChannel;
- currentPitch = nullByte;
- playState = false;
- startTime = millis();
- }
- byte MidiChannel::getMChannel() const{
- return channel;
- }
- */
- byte MidiChannel::getCurrentPitch() const{
- return currentPitch;
- }
- boolean MidiChannel::getPlayState() const{
- return playState;
- }
- unsigned long MidiChannel::getStartTime() const{
- return startTime;
- }
- /*
- void MidiChannel::setMChannel(byte inChannel){
- channel = inChannel;
- }
- */
- void MidiChannel::setCurrentPitch(const byte inCurrentPitch){
- currentPitch = inCurrentPitch;
- }
- void MidiChannel::setPlayState(const boolean inState){
- playState = inState;
- }
- void MidiChannel::setStartTime(){
- startTime = millis();
- }
- MCArray::MCArray(){
- }
- // use an algorithm to find which note should be played next
- // killnote is in case all channels are in use.
- byte MCArray::nextNotePlayPitch(MidiWord& killNote, byte numberOfChannels) const{
- // check if all notes are being played
- for(int i = 0x00; i < numberOfChannels; i++){
- if (midiArray[i].getPlayState() == false){
- return i;
- }
- }
- // check play times
- byte dropChannel = 0x00;
- byte maxPitch = midiArray[0x00].getCurrentPitch();
- killNote.setAll(noteOff, midiArray[0x00].getCurrentPitch(), nullByte);
- for(int i = 0x01; i < numberOfChannels; i++){
- if(maxPitch < midiArray[i].getCurrentPitch()){
- dropChannel = i;
- maxPitch = midiArray[i].getCurrentPitch();
- killNote.setAll(noteOff, midiArray[i].getCurrentPitch(), maxVelocity);
- }
- }
- return dropChannel;
- }
- // use an algorithem to find which note should be played next
- // killnote is in case all channels are in use.
- byte MCArray::nextNotePlayTime(MidiWord& killNote, byte numberOfChannels) const{
- // check if all notes are being played
- for(int i = 0x00; i < numberOfChannels; i++){
- if (midiArray[i].getPlayState() == false){
- return i;
- }
- }
- // check play times
- byte dropChannel = 0x00;
- unsigned long lowTime = midiArray[0x00].getStartTime();
- killNote.setAll(noteOff, midiArray[0x00].getCurrentPitch(), nullByte);
- for(int i = 0x01; i < numberOfChannels; i++){
- if(lowTime > midiArray[i].getStartTime()){
- dropChannel = i;
- lowTime = midiArray[i].getStartTime();
- killNote.setAll(noteOff, midiArray[i].getCurrentPitch(), maxVelocity);
- }
- }
- return dropChannel;
- }
- // check the channels with current status on and match pitch to determine note send off.
- byte MCArray::noteOffMatch(const byte inPitch) const{
- for(int i = 0x00; i < TOTAL_CHANNELS; i++){
- if(midiArray[i].getCurrentPitch() == inPitch){
- return i;
- }
- }
- // incase the note has already been turned off
- return 0xFF;
- }
- void MCArray::updateStatus(const MidiWord& currentMessage, byte messageChannel){
- if(currentMessage.getCommand() == noteOn){
- midiArray[messageChannel].setCurrentPitch(currentMessage.getPitch());
- midiArray[messageChannel].setPlayState(true);
- midiArray[messageChannel].setStartTime();
- }
- if(currentMessage.getCommand() == noteOff){
- midiArray[messageChannel].setCurrentPitch(nullByte);
- midiArray[messageChannel].setPlayState(false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement