[Stk] Simple question regarding real-time MIDI input

Ge Wang gewang@CS.Princeton.EDU
Tue, 23 Mar 2004 00:23:36 -0500


--Apple-Mail-4-780485077
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed

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@ccrma.stanford.edu
> http://ccrma-mail.stanford.edu/mailman/listinfo/stk

--Apple-Mail-4-780485077
Content-Transfer-Encoding: 7bit
Content-Type: text/enriched;
	charset=US-ASCII

<fixed><color><param>0000,0000,0000</param>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!</color><color><param>2321,6E6C,2523</param>


---

// (modified) rtsine.cpp STK tutorial program (with midi input)</color>


<color><param>6866,3836,211F</param>#include "WaveLoop.h"</color>

<color><param>6866,3836,211F</param>#include "RtWvOut.h"</color>

<color><param>6866,3836,211F</param>#include "RtMidi.h"</color>

<color><param>6866,3836,211F</param>#include "SKINI.h"</color>


<color><param>7674,0F0D,504E</param>int</color> main()

{

    <color><param>2321,6E6C,2523</param>// Set the global sample rate
before creating class instances.</color>

    Stk::setSampleRate(
<color><param>0000,0000,FFFD</param>44100.0</color> );


    WaveLoop *input = <color><param>0000,0000,FFFD</param>0</color>;

    RtWvOut * output = <color><param>0000,0000,FFFD</param>0</color>;

    RtMidi * midi = <color><param>0000,0000,FFFD</param>0</color>;

    <color><param>7674,0F0D,504E</param>double</color> vel =
<color><param>0000,0000,FFFD</param>0.0</color>;

    <color><param>7674,0F0D,504E</param>int</color> key_count =
<color><param>0000,0000,FFFD</param>0</color>, count =
<color><param>0000,0000,FFFD</param>0</color>;


    <color><param>7674,0F0D,504E</param>try</color> {

        <color><param>2321,6E6C,2523</param>// Define and load the
sine wave file</color>

        input = <color><param>7674,0F0D,504E</param>new</color>
WaveLoop(
<color><param>8987,1311,1513</param>"rawwaves/sinewave.raw"</color>,
<color><param>7674,0F0D,504E</param>TRUE</color> );


        <color><param>2321,6E6C,2523</param>// Define and open the
default realtime output device for one-channel playback</color>

        output = <color><param>7674,0F0D,504E</param>new</color>
RtWvOut(<color><param>0000,0000,FFFD</param>1</color>);

    

        <color><param>2321,6E6C,2523</param>// instantiate midi input 0</color>

        midi = <color><param>7674,0F0D,504E</param>new</color> RtMidi;

    }

    <color><param>7674,0F0D,504E</param>catch</color> (StkError &) {

        <color><param>7674,0F0D,504E</param>goto</color> cleanup;

    }


    printf( <color><param>8987,1311,1513</param>"starting to poll for
midi input...\n"</color> );


    <color><param>2321,6E6C,2523</param>// play the oscillator with
MIDI</color>

    <color><param>7674,0F0D,504E</param>for</color> (;;)

    {

        <color><param>2321,6E6C,2523</param>// poll MIDI every N
samples (regardless of buffer size - could be improved)</color>

        <color><param>7674,0F0D,504E</param>if</color>( ++count >
<color><param>0000,0000,FFFD</param>100</color> )

        {

            <color><param>7674,0F0D,504E</param>if</color>(
midi->nextMessage() ) <color><param>2321,6E6C,2523</param>// poll</color>

            {

                <color><param>7674,0F0D,504E</param>if</color>(
midi->getType() == <color><param>0000,0000,FFFD</param>0x90</color> ) 
<color><param>2321,6E6C,2523</param>// note on</color>

                {

                    <color><param>7674,0F0D,504E</param>if</color>(
(<color><param>7674,0F0D,504E</param>int</color>)( vel =
midi->getByteThree() ) ) <color><param>2321,6E6C,2523</param>//
velocity</color>

                    {

                        <color><param>2321,6E6C,2523</param>// set
frequency</color>

                        input->setFrequency(
Midi2Pitch[(<color><param>7674,0F0D,504E</param>int</color>)(midi->getByteTwo()<color><param>0000,0000,FFFD</param>+.5</color>)]
);

                        key_count++;

                    }

                    <color><param>2321,6E6C,2523</param>// most kb
controllers send 0-velocity note-on messages for "key-up"</color>

                    <color><param>7674,0F0D,504E</param>else</color>
key_count--;

                }

                <color><param>7674,0F0D,504E</param>else</color>
<color><param>7674,0F0D,504E</param>if</color>( midi->getType() ==
<color><param>0000,0000,FFFD</param>0x80</color> )
<color><param>2321,6E6C,2523</param>// note off</color>

                    key_count--;

            }

            

            <color><param>2321,6E6C,2523</param>// reset count</color>

            count = <color><param>0000,0000,FFFD</param>0</color>;

        }


        <color><param>7674,0F0D,504E</param>try</color> {

            output->tick( key_count >
<color><param>0000,0000,FFFD</param>0</color> ? input->tick() *
(vel/<color><param>0000,0000,FFFD</param>128.0</color>) :
<color><param>0000,0000,FFFD</param>0.0</color> );

        }

        <color><param>7674,0F0D,504E</param>catch</color> (StkError &) {

            <color><param>7674,0F0D,504E</param>goto</color> cleanup;

        }

    }


cleanup:

    <color><param>7674,0F0D,504E</param>delete</color> input;

    <color><param>7674,0F0D,504E</param>delete</color> output;

    <color><param>7674,0F0D,504E</param>delete</color> midi;


    <color><param>7674,0F0D,504E</param>return</color>
<color><param>0000,0000,FFFD</param>0</color>;

}

</fixed>


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


<excerpt>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@ccrma.stanford.edu

http://ccrma-mail.stanford.edu/mailman/listinfo/stk

</excerpt>
--Apple-Mail-4-780485077--