void processNote(MidiMessage& message) { int velocity = message.p2(); int key = message.p1(); int channel = message.p0() & 0x0f; int state = 1; int duration = 0; if (((message.p0() & 0xf0) == 0x80) || velocity == 0) { state = 0; } if (key == A0) { cout << "A0 Triggered" << endl; for (int i=0; i<decaystates.getSize(); i++) { decaystates[i] *= -1.0; } return; } if (state == 1) { notestates[key] = message.time; onvels[key] = velocity; } else { if (notestates[key] == 0) { // do nothing } else { duration = message.time - notestates[key]; if (decaystates[key] == 0.0) { createDecay(channel, key, duration, onvels[key]); } } notestates[key] = 0; } }
void mainloopalgorithms(void) { if (synth.getNoteCount() > 0) { message = synth.extractNote(); oldnote = checkForSquelch(message, memory, mintime, maxtime, t_time); if (!oldnote || sstate == 0) { cout << "New note from performer: " << message << endl; if (message.is_note_on()) { message.p1() += 7; outvel = message.p2() + veladd; if (outvel > 127) { outvel = 127; } synth.send(message.p0(), message.p1(), outvel); message.time = t_time; message.p2() = outvel; memory.insert(message); } else if (message.is_note_off()) { message.p1() += 7; synth.send(message.p0(), message.p1(), message.p2()); message.time = t_time; memory.insert(message); } } else { cout << "Feedback note from piano: " << message << endl; } } }
void sendData(MidiMessage& m, MidiOutPort& output, MidiInput& input) { int count = m.getParameterCount(); if (count >= 0 && count <= 2) { if (count >= 0) output.rawsend(m.p0()); if (count >= 1) output.rawsend(m.p0(), m.p1()); if (count >= 2) output.rawsend(m.p0(), m.p1(), m.p2()); } }
int main(void) { MidiInput midiin; MidiOutput midiout; midiout.setPort(0); midiout.open(); midiin.setPort(0); midiin.open(); MidiMessage message; int sysexloc; unsigned char *sysexdata = NULL; int sysexsize = 0; int i; int running = 1; cout << "sysexio -- display and echo SYSEX messages from MIDI input" << endl; cout << "Press Middle C to quit." << endl; while (running) { if (midiin.getCount() > 0) { message = midiin.extract(); if (message.p0() == 0xf0) { sysexloc = message.p1(); sysexdata = midiin.getSysex(sysexloc); sysexsize = midiin.getSysexSize(sysexloc); // print out the sysex data to the screen: for (i=0; i<sysexsize; i++) { cout << hex << (int)sysexdata[i] << " "; if ((i + 1) % 30 == 0) { cout << endl; } } cout << endl; // Now echo the messages to MIDI output (as a demo // for how to send Sysex outputs) midiout.rawsend(sysexdata, sysexsize); // As a courtesy, mark the midiin sysex buffer free // but this is not necessay (it will be erased when // more space is needed for storing a sysex. midiin.clearSysex(sysexloc); } else if ((message.p0() & 0xf0) == 0x90) { // Exit the program when a middle C note is pressed. if (message.p1() == 60 && message.p2() > 0) { running = 0; } } } } return 0; }
void sillyKeyboard(int key, int chan /* = 0 */) { static int octave = 4; static int newkey = 0; static Voice voice; static MidiMessage message; // check to see if adjusting the octave: if (isdigit(key)) { octave = key - '0'; return; } switch (key) { case 'z': newkey = 12 * octave + 0; break; // C case 's': newkey = 12 * octave + 1; break; // C# case 'x': newkey = 12 * octave + 2; break; // D case 'd': newkey = 12 * octave + 3; break; // D# case 'c': newkey = 12 * octave + 4; break; // E case 'v': newkey = 12 * octave + 5; break; // F case 'g': newkey = 12 * octave + 6; break; // F# case 'b': newkey = 12 * octave + 7; break; // G case 'h': newkey = 12 * octave + 8; break; // G# case 'n': newkey = 12 * octave + 9; break; // A case 'j': newkey = 12 * octave + 10; break; // A# case 'm': newkey = 12 * octave + 11; break; // B case ',': newkey = 12 * octave + 12; break; // C case 'l': newkey = 12 * octave + 12; break; // C# case '.': newkey = 12 * octave + 12; break; // D case '\'': newkey = 12 * octave + 12; break; // D# case '/': newkey = 12 * octave + 12; break; // E default: return; // don't do anything if not a key } newkey = limit(newkey, 0, 127); // put note-off message in synth's input buffer: message.time = t_time; message.p0() = 0x90 | voice.getChan(); message.p1() = voice.getKey(); message.p2() = 0; synth.insert(message); // turn off the last note: voice.off(); // set parameters for next note-on: voice.setChan(chan & 0x0f); // limit channel to range from 0 to 15 voice.setVel(rand() % 127 +1); // random attack in range from 1 to 127 voice.setKey(newkey); // use the newly selected key number // play the MIDI note: voice.play(); // insert the played note into synth's input MIDI buffer: message.command() = 0x90 | voice.getChan(); message.p1() = voice.getKey(); message.p2() = voice.getVel(); synth.insert(message); }
void processMidiCommand(MidiMessage& message) { if (message.p0() != 0x90 || message.p2() == 0) { return; } switch (message.p1()) { case 60: // Middle C = beat keyboardchar(' '); break; case 61: // C# = amplitude control { double amp = performance.getAmp(); amp = amp * message.p2() / 64; if (amp < 0) { amp = 0; } else if (amp > 127) { amp = 127; } performance.setAmp((int)amp); } break; case 71: // B = 1 beat tempo follow keyboardchar('1'); break; case 72: // C = 2 beat tempo follow keyboardchar('2'); break; case 73: // C# = 3 beat tempo follow keyboardchar('3'); break; case 74: // D = 4 beat tempo follow keyboardchar('4'); break; case 79: // G = constant tempo follow keyboardchar('9'); break; case 80: // G# = automatic keyboardchar('0'); break; case 62: // amplitude decrease keyboardchar('['); break; case 63: // amplitude increase keyboardchar(']'); break; case 64: // tempo decrease keyboardchar('-'); break; case 65: // tempo increase keyboardchar('='); break; } }
void mainloopalgorithms(void) { eventBuffer.checkPoll(); // see if any notes to play while (synth.getNoteCount() > 0) { message = synth.extractNote(); if (message.p2() != 0) { lastnotes.insert(message.p1()); lasttimes.insert(message.time); distancee = lastnotes[0] - lastnotes[1]; duration = lasttimes[0] - lasttimes[1]; channel = 0x0f & message.p0(); if (distancee != 0) { playgliss(message.p1(), message.p2(), channel, duration, distancee); } } } }
void processKeyboard(void) { MidiMessage message; int key; int vel; int command; while (synth.getNoteCount() > 0) { message = synth.extractNote(); command = message.p0(); key = message.p1(); vel = message.p2(); if (vel == 0 || (command & 0xf0 == 0x80)) { // note-off command section long duration = t_time - performerNotes[key]; durations.insert(duration); durtimes.insert(t_time); performerNotes[key] = 0; performerPC[key%12]--; if (performerPC[key%12] < 0) { performerPC[key%12] = 0; } } else { // note-on command performerNotes[key] = t_time; performerPC[key%12]++; performerPCHistory[key%12]++; keys.insert(key); keytimes.insert(t_time); volumes.insert(vel); voltimes.insert(t_time); } // end of the note-on command section } // end of the while loop for processing notes from the performer // update the time that the performer last play a note on/off: lastPerformerTime = t_time; updatePerformRegions(); updateDuration(); updateVolume(); }
int checkForSquelch(MidiMessage& message, CircularBuffer<MidiMessage>& memory, int mintime, int maxtime, int curtime) { int i; if (memory.getSize() == 0) { return 0; } for (i=0; i<memory.getSize(); i++) { if ((curtime - (int)memory[i].time) < mintime) { continue; } if ((curtime - (int)memory[i].time) > maxtime) { break; } if ((memory[i].p0() == message.p0()) && (memory[i].p1() == message.p1())) { return 1; } } return 0; }
void initialization(void) { batonTimer.setPeriod(50); // time to get new state of baton offTimer.setPeriod(200); // time to check buffer for forgetting controlDisplayTimer.setPeriod(200); // time to check buffer for forgetting // set the voice channels all to be 0 for the disklavier and so // the channels do not have to be specified when playing the note. for (int i=0; i<MAXVOICES; i++) { voice[i].setChannel(0); } computer.setChannel(0); computerMessage.time = t_time; computerMessage.p0() = 0x90; computerMessage.p1() = 0; computerMessage.p2() = 0; keys.setSize(1000); // store keys for memory of previous notes keytimes.setSize(1000); // note times for keys buffer volumes.setSize(1000); // duration of notes being held by performer voltimes.setSize(1000); // duration of notes being held by performer durations.setSize(1000); // duration of notes being held by performer durtimes.setSize(1000); // duration of notes being held by performer }
void keyboardchar(int key) { if (isdigit(key)) { int mkey = 5 * 12 + key - '0'; int mvel = (int)(drand48() * 40 + 40); //turn off old note from computer's point of view computerMessage.time = t_time; computerMessage.p2() = 0; synth.insert(computerMessage); cout << "played note: " << mkey << " with velocity: " << mvel << endl; computer.play(mkey, mvel); computerMessage.time = t_time; computerMessage.p0() = 0x90; // midi note-on command, channel 1 computerMessage.p1() = mkey; computerMessage.p2() = mvel; synth.insert(computerMessage); cout << "played note: " << mkey << " with velocity: " << mvel << endl; return; } switch (key) { case ' ': // turn output notes on/off activeQ = !activeQ; if (activeQ) { cout << "Program Activated" << endl; } else { cout << "Program Deactivated" << endl; } break; case 'c': // toggle baton control display controlDisplayQ = !controlDisplayQ; if (controlDisplayQ == 0) { cout << endl; } break; case 'd': // display baton's output notes displayOutput = !displayOutput; if (displayOutput) { cout << "Baton output notes display turned ON" << endl; } else { cout << "Baton output notes display turned OFF" << endl; } break; case 'n': // display performerPCHistory pitches { cout << "Piano notes: "; for (int i=0; i<11; i++) { cout << performerPCHistory[i] << ", "; } cout << performerPCHistory[11] << endl; } break; case 'm': // display performerPC pitches { cout << "Notes on: "; for (int i=0; i<11; i++) { cout << performerPC[i] << ", "; } cout << performerPC[11] << endl; } break; case 'r': // display the register information { cout << "Register: " << performerRegion[0] << performerRegion[1] << performerRegion[2] << endl; } break; case 'v': // display the active voice count for baton { cout << endl << "Active voices = " << countActiveVoices(); cout << ": "; for (int z=0; z<MAXVOICES; z++) { if (voiceState[z]) { cout << " " << (int)voice[z].getKey(); } else { cout << " 0"; } } cout << endl; } break; case 'z': // forget the current piano's key history { for (int z=0; z<12; z++) { performerPCHistory[z] = 0; } keys.reset(); keytimes.reset(); } break; case '[': // decrease the note memory time keyDuration -= 1000; if (keyDuration < 1000) { keyDuration = 1000; } cout << "Note memory time set to : " << keyDuration/1000 << " seconds" << endl; break; case ']': // increase the note memory time keyDuration += 1000; if (keyDuration > 60000) { keyDuration = 60000; } cout << "Note memory time set to : " << keyDuration/1000 << " seconds" << endl; break; case '-': // turn off computer keyboard note // turn off old note from computer's point of view computerMessage.time = t_time; computerMessage.p2() = 0; synth.insert(computerMessage); // turn off old note from synthesizer's point of view computer.off(); break; } }
int main(int argc, char** argv) { options.setOptions(argc, argv); checkOptions(options); displayHeader(cout); if (fileQ) { displayHeader(outputfile); } KeyboardInput keyboard; // for typing comments into output file char keych; // character from keyboard MidiMessage message; int lastTime = -1; midi.open(); while (1) { while (midi.getCount() > 0) { message = midi.extract(); if (echoQ) { midi.send(message); } if ((!activeSensingQ) && (message.p0() == 0xfe)) { // don't display incoming active-sensing messages continue; } // filter any specified message types if (suppressOffQ && ((message.p0() & 0xf0) == 0x90) && (message.p2() == 0)) { continue; } else if (filter[(message.p0() >> 4) - 8]) { continue; } else if (cfilter[message.p0() & 0x0f]) { continue; } // adjust message time to delta time if necessary if (!absoluteQ) { if (lastTime == -1) { lastTime = message.time; message.time = 0; } else { int temp = message.time; message.time = message.time - lastTime; lastTime = temp; } } displayMessage(cout, message, style); if (fileQ) { displayMessage(outputfile, message, style); } } if (keyboardQ && keyboard.hit()) { keych = keyboard.getch(); switch (keych) { case 27: // escape key if (fileQ && bufferIndex != 0 && bufferIndex < MAX_KEY_BUFF) { inputBuffer[bufferIndex] = '\0'; outputfile << inputBuffer; } keyboard.deinitialize(); exit(0); break; case 0x08: // backspace key case 0x7f: // delete key if (bufferIndex > 0) { cout << "\b \b" << flush; bufferIndex--; } break; case 0x0a: // enter key only #ifdef VISUAL break; #endif case 13: // line feed cout << endl; if (bufferIndex < MAX_KEY_BUFF) { inputBuffer[bufferIndex] = '\0'; if (fileQ) { outputfile << inputBuffer << '\n'; } examineInputForCommand(inputBuffer); } bufferIndex = 0; break; case 0x0c: // ^L key (redraw input) cout << endl; if (bufferIndex < MAX_KEY_BUFF) { inputBuffer[bufferIndex] = '\0'; cout << inputBuffer << flush; } break; default: // normal key cout << keych << flush; if (bufferIndex < MAX_KEY_BUFF) { inputBuffer[bufferIndex++] = keych; } else { // buffer is WAY to long: kill it bufferIndex = 0; } } } millisleep(1); // sleep for 1 millisec for multi-tasking courtesy }
void mainloopalgorithms(void) { if (comparestate && notetimer.expired()) { if (notetimer.expired() > 2) { notetimer.reset(); } else { notetimer.update(); } notestate = !notestate; if (notestate == 1 || notestate == -1) { synth.play(0, note, 64); data = 0x90; sentout.insert(data); data = note; sentout.insert(data); data = 64; sentout.insert(data); } else { synth.play(0, note, 0); data = 0x90; sentout.insert(data); data = note; sentout.insert(data); data = 0; sentout.insert(data); note += step * direction; if (note > highestnote) { note = lowestnote; } if (note < lowestnote) { note = highestnote; } } } if (midiinput.getCount() > 0) { message = midiinput.extract(); receivedin.insert(message.p0()); receivedin.insert(message.p1()); receivedin.insert(message.p2()); // check that the messages are identical if (receivedin.getCount() < 3) { cout << "Error: not enough received data" << endl; } else { checkin[0] = receivedin.extract(); checkin[1] = receivedin.extract(); checkin[2] = receivedin.extract(); } if (sentout.getCount() < 3) { cout << "Error: not enough sent data" << endl; } else { checkout[0] = sentout.extract(); checkout[1] = sentout.extract(); checkout[2] = sentout.extract(); } if ((checkout[0] != checkin[0]) || (checkout[1] != checkin[1]) || (checkout[2] != checkin[2])) { synth.rawsend(0xaa, 0x7f, 0x00); cout << "Error " << "output was = (" << hex << (int)checkout[0] << ") " << dec << (int)checkout[1] << " " << dec << (int)checkout[2] << "\tbut input is = (" << hex << (int)checkin[0] << ") " << dec << (int)checkin[1] << " " << dec << (int)checkin[2] << " " << endl; // assume that a note message was missed. if (sentout.getCount() < 3) { cout << "Error: not enough sent data during error" << endl; } else { checkout[0] = sentout.extract(); checkout[1] = sentout.extract(); checkout[2] = sentout.extract(); } stop(); cout << "Press space to restart testing, " "or press 'S' to silence synth" << endl; } } }