// // Programmer: Craig Stuart Sapp // Creation Date: Sat Apr 22 16:04:00 PDT 2000 // Last Modified: Sat Apr 22 16:04:04 PDT 2000 // Filename: ...sig/doc/examples/all/decay/decay.cpp // Syntax: C++; synthImprov 2.0 // // #include "synthImprov.h" /*----------------- beginning of improvization algorithms ---------------*/ EventBuffer eventBuffer; // for future notes MidiMessage message; // for reading keyno and velocity (and time) Array notestates(128); // for keeping track of note on/off Array decaystates(128); // for keeping track of note on/off Array onvels(128); // for keeping track of note on/off int gap = 100; // spacing between notes (millisec) double decayrate = 0.87; // echo decay rate. // function declarations: void sillyKeyboard(int key, int chan = 0); void processNote(MidiMessage& message); void createDecay(int channel, int key, int duration, int velocity); /*--------------------- Event Algorithms --------------------------------*/ ////////////////////////////// // // EchoAlgorithm -- // // Global variables needed by this function: // decaystates // // Local variables needed by this function: // 14 short = gap between notes // static void EchoAlgorithm(FunctionEvent& p, EventBuffer& midiOutput) { static NoteEvent note; // temporary note before placing in buffer // check if pausing if (decaystates[p.getKey()] < 0.0) { p.setOnTime(p.getOnTime() + p.getDur() + p.shortValue(14)); return; } // set the parameters for the output note: note.setOnDur(t_time, p.getOffTime()); // off time holds dur note.setVel(p.getVel()); note.setChan(p.getChan()); note.setKey(p.getKey()); // update the parameters for the function: decaystates[p.getKey()] *= decayrate; p.setVel((int)decaystates[p.getKey()]); // if note is too quiet, end the note if (p.getVel() <= 2) { p.off(midiOutput); decaystates[p.getKey()] = 0.0; } // next time includes a gap so that key can raise on keyboard p.setOnTime(p.getOnTime() + p.getDur() + p.shortValue(14)); note.activate(); note.action(midiOutput); // start right now, avoiding any buffer delay midiOutput.insert(note); // the note off message is being buffered } /*--------------------- maintenance algorithms --------------------------*/ void description(void) { printboxtop(); psl( "DECAY -- by Craig Stuart Sapp -- 22 April 2000"); psl(""); printintermediateline(); printboxbottom(); } void initialization(void) { eventBuffer.setPollPeriod(10); for (int i=0; i 0) { message = synth.extractNote(); processNote(message); } } //////////////////////////// // // processNote -- decide when to trigger a decay algorithm. // 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