[Stk] Simple question regarding real-time MIDI input

Ge Wang gewang at CS.Princeton.EDU
Mon Mar 22 21:23:36 PST 2004


Hi Ian!

Here is a monophonic, aesthetically-challenged version of the sample...

reminders:
    - need to add RtMidi.o to the makefile
    - to reduce latency, may want to lower RT_BUFFER_SIZE (256, 128, ...)
    - please let me know if you see any bugs
    - on OS X, Gary has fixed a documented RtMidi bug that occasionally 
drops
      messages clumped by coremidi.  however, I don't think this is in 
the
      release yet.  If you run into this problem, someone can probably
      provide a workaround.
things not in the sample (that could be fun!):
    - polyphony
    - handlers for more types of messages
    - envelope generator!
    - STK models!!

hope this helps!

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 - could 
be improved)
         if( ++count > 100 )
         {
             if( midi->nextMessage() ) // poll
             {
                 if( midi->getType() == 0x90 )  // note on
                 {
                     if( (int)( vel = midi->getByteThree() ) ) // 
velocity
                     {
                         // set frequency
                         input->setFrequency( 
Midi2Pitch[(int)(midi->getByteTwo()+.5)] );
                         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;
}


On Monday, March 22, 2004, at 04:30 PM, Ian Gibson wrote:

> I've seen and run the demonstration program which allows real-time 
> MIDI input from a keyboard. However, I am trying to adapt the simple 
> sample player program (below) given in the documentation using 
> real-time MIDI input and am having difficulties. Could some kind sole 
> give me some pointers or (even better) the solution?
>
> Many thanks in advance!
>
>
>
>
> #include "WaveLoop.h"
> #include "RtWvOut.h"
>
> int main()
> {
>  // Set the global sample rate before creating class instances.
>  Stk::setSampleRate( 44100.0 );
>
>  WaveLoop *input = 0;
>  RtWvOut *output = 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);
>  }
>  catch (StkError &) {
>    goto cleanup;
>  }
>
>  input->setFrequency(440.0);
>
>  // Play the oscillator for 40000 samples
>  int i;
>  for ( i=0; i<40000; i++ ) {
>    try {
>      output->tick(input->tick());
>    }
>    catch (StkError &) {
>      goto cleanup;
>    }
>  }
>
> cleanup:
>  delete input;
>  delete output;
>
>  return 0;
> }
>
> _________________________________________________________________
> It's fast, it's easy and it's free. Get MSN Messenger today! 
> http://www.msn.co.uk/messenger
>
> _______________________________________________
> Stk mailing list
> Stk at ccrma.stanford.edu
> http://ccrma-mail.stanford.edu/mailman/listinfo/stk
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 7143 bytes
Desc: not available
URL: <https://cm-mail.stanford.edu/pipermail/stk/attachments/20040323/23c8059a/attachment.bin>


More information about the Stk mailing list