[Stk] Simple question regarding real-time MIDI input

Ge Wang gewang at CS.Princeton.EDU
Mon Mar 22 21:38:13 PST 2004


I apologize for the multiple posts.  i have (already) found
a bug with my code: the velocity was set at the wrong place.
the corrected code is below.

Best,
Ge!

---
// (modified) rtsine.cpp STK tutorial program with midi input

#include "WaveLoop.h"
#include "RtWvOut.h"
#include "RtMidi.h"
#include "SKINI.h"

int main()
{
     // Set the global sample rate before creating class instances.
     Stk::setSampleRate( 44100.0 );

     WaveLoop *input = 0;
     RtWvOut * output = 0;
     RtMidi * midi = 0;
     double vel = 0.0;
     int key_count = 0, count = 0;

     try {
         // Define and load the sine wave file
         input = new WaveLoop( "rawwaves/sinewave.raw", TRUE );

         // Define and open the default realtime output device for 
one-channel playback
         output = new RtWvOut(1);

         // instantiate midi input 0
         midi = new RtMidi;
     }
     catch (StkError &) {
         goto cleanup;
     }

     printf( "starting to poll for midi input...\n" );

     // play the oscillator with MIDI
     for (;;)
     {
         // poll MIDI every N samples (regardless of buffer size - may 
want to improve this)
         if( ++count >= 100 )
         {
             if( midi->nextMessage() ) // poll
             {
                 if( midi->getType() == 0x90 )  // note on
                 {
                     if( (int)( midi->getByteThree() ) ) // velocity
                     {
                         // set frequency
                         input->setFrequency( 
Midi2Pitch[(int)(midi->getByteTwo()+.5)] );
						// set velocity
						vel = midi->getByteThree();
						// increment keys down
                         key_count++;
                     }
                     // most kb controllers send 0-velocity note-on 
messages for "key-up"
                     else key_count--;
                 }
                 else if( midi->getType() == 0x80 ) // note off
                     key_count--;
             }

             // reset count
             count = 0;
         }

         try {
             output->tick( key_count > 0 ? input->tick() * (vel/128.0) : 
0.0 );
         }
         catch (StkError &) {
             goto cleanup;
         }
     }

cleanup:
     delete input;
     delete output;
     delete midi;

     return 0;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 5293 bytes
Desc: not available
URL: <https://cm-mail.stanford.edu/pipermail/stk/attachments/20040323/e576f57d/attachment.bin>


More information about the Stk mailing list