Björn -

I'll give a good write-up of a practical implementation tomorrow when I'm in 
front of a computer.   I've written a fair number of products with both 
portaudio and portmidi

Bjorn Roche <[email protected]> wrote:

>My updated code is below. I no longer get the dropped note off message  
>(although I'm still unclear as to why I got that in the first place).  
>I started with code from Phil and Ross's sine wave generating code, so  
>hence the credits and license.
>
>Any suggestions for cleanup/correctness (especially if it isn't  
>obvious) please let me know. I am going to tinker a bit with the Dac/ 
>stream time issue tomorrow if I have time.
>
>       bjorn
>
>
>/** @file main.c
>       @brief Play a sine wave and a MIDI note every second for ten seconds.
>    @author Bjorn Roche
>       @author Ross Bencina <[email protected]>
>     @author Phil Burk <[email protected]>
>*/
>/*
>  * This program demonstrates the synchronization of PortAudio and  
>PortMIDI.
>  *
>  * For more information see: http://www.portaudio.com/
>  * and http://portmedia.sourceforge.net/
>  *
>  * Copyright (c) Bjorn Roche, Ross Bencina and Phil Burk 2011
>  *
>  * Permission is hereby granted, free of charge, to any person  
>obtaining
>  * a copy of this software and associated documentation files
>  * (the "Software"), to deal in the Software without restriction,
>  * including without limitation the rights to use, copy, modify, merge,
>  * publish, distribute, sublicense, and/or sell copies of the Software,
>  * and to permit persons to whom the Software is furnished to do so,
>  * subject to the following conditions:
>  *
>  * The above copyright notice and this permission notice shall be
>  * included in all copies or substantial portions of the Software.
>  *
>  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
>  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
>  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  
>NONINFRINGEMENT.
>  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
>  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
>  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
>  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
>  */
>
>/*
>  * The text above constitutes the entire PortAudio license; however,
>  * the PortAudio community also makes the following non-binding  
>requests:
>  *
>  * Any person wishing to distribute modifications to the Software is
>  * requested to send the modifications to the original developer so  
>that
>  * they can be incorporated into the canonical version. It is also
>  * requested that these non-binding requests be included along with the
>  * license above.
>  */
>#include <stdio.h>
>#include <math.h>
>#include <limits.h>
>#include "src/portaudio.h"
>#include "src/portmidi.h"
>
>#define NUM_SECONDS   (10)
>#define SAMPLE_RATE   (44100)
>#define FRAMES_PER_BUFFER  (64)
>//middle C (C4)
>#define PITCH (261.63)
>#define TONE_LENGTH (.5)
>
>#define MIDI_OUTPUT_BUFFER_SIZE (100)
>
>#ifndef M_PI
>#define M_PI  (3.14159265)
>#endif
>
>PmTimestamp timeProc(void *timeData) {
>    PaStream *pas = (PaStream *) timeData ;
>    PaTime t = Pa_GetStreamTime( pas );
>    return (PmTimestamp) (int64_t) ( t * 1000 + .5 ) ;
>}
>
>typedef struct
>{
>     //this is used to mark when we've initialized the start time
>     int once;
>     PmTimestamp start;
>     PortMidiStream *midiStream;
>     //each on-off cycle is one iteration
>     int iterations;
>     // counts down the number of samples until we start the next tone
>     int countdown;
>     // counts down the number of samples in the current tone
>     int tonetime;
>}
>paTestData;
>
>/* This routine will be called by the PortAudio engine when audio is  
>needed.
>** It may called at interrupt level on some machines so don't do  
>anything
>** that could mess up the system like calling malloc() or free().
>*/
>static int patestCallback( const void *inputBuffer, void *outputBuffer,
>                             unsigned long framesPerBuffer,
>                             const PaStreamCallbackTimeInfo* timeInfo,
>                             PaStreamCallbackFlags statusFlags,
>                             void *userData )
>{
>     paTestData *data = (paTestData*)userData;
>     float *out = (float*)outputBuffer;
>     unsigned long i;
>
>     if( !data->once ) {
>        data->once = 1;
>        //initialize start time
>        data->start = (PmTimestamp) ( timeInfo->outputBufferDacTime *  
>1000 + .5 );
>     }
>
>     (void) timeInfo; /* Prevent unused variable warnings. */
>     (void) statusFlags;
>     (void) inputBuffer;
>
>     for( i=0; i<framesPerBuffer; i++ )
>     {
>         if( data->countdown == SAMPLE_RATE ) {
>            if( data->iterations<NUM_SECONDS ) {
>               //Note: Priority inversions may make this unsafe to  
>call in PA Callback on ALSA (unconfirmed)
>               Pm_WriteShort( data->midiStream,
>                       data->start + (data->iterations)*1000,
>                       Pm_Message( 0x90, 0x3C, 0x60 ) );
>            }
>            ++data->iterations;
>            data->tonetime = (int) (SAMPLE_RATE * TONE_LENGTH + .5 ) ;
>         }
>         if( data->tonetime > 0 && data->iterations-1<NUM_SECONDS ) {
>            int t = data->tonetime - (int) (SAMPLE_RATE * TONE_LENGTH  
>+ .5 ) ;
>            //play the tone on the left and right side:
>            *out++ = sin( 2. * M_PI * PITCH * t / SAMPLE_RATE ) ;
>            *out++ = sin( 2. * M_PI * PITCH * t / SAMPLE_RATE ) ;
>         } else {
>            if( data->tonetime == 0 && data- 
> >iterations-1<NUM_SECONDS ) {
>               Pm_WriteShort( data->midiStream,
>                       data->start + (data->iterations-1)*1000+(int) 
>(1000*TONE_LENGTH+.5),
>                       Pm_Message( 0x80, 0x3C, 0x00 ) );
>            }
>            *out++ = 0;
>            *out++ = 0;
>         }
>         --data->tonetime;
>
>         if( data->countdown == 0 )
>            data->countdown = SAMPLE_RATE; //one second
>         else
>            --data->countdown;
>     }
>
>     return paContinue;
>}
>
>/*
>  * This routine is called by portaudio when playback is done.
>  */
>static void StreamFinished( void* userData )
>{
>    //may want to send all-notes off here.
>    //paTestData *data = (paTestData *) userData;
>    printf( "Stream Completed." );
>}
>
>/*******************************************************************/
>int main(void);
>int main(void)
>{
>     PaStreamParameters outputParameters;
>     PaStream *stream;
>     PaError paErr = paNoError;
>     PmError pmErr = pmNoError;
>     paTestData data;
>     PortMidiStream *midiStream;
>     PmDeviceID defaultPmDevice;
>     long latency1, latency2;
>
>     printf("Sync Test: Opening Stream. SR = %d, BufSize = %d\n",  
>SAMPLE_RATE, FRAMES_PER_BUFFER);
>
>     /* initialize countdown data */
>     data.countdown = SAMPLE_RATE ;
>     data.tonetime = (int) (SAMPLE_RATE * TONE_LENGTH + .5 ) ;
>     data.once = 0;
>     data.iterations = 0;
>
>     paErr = Pa_Initialize();
>     if( paErr != paNoError ) goto error;
>
>     pmErr = Pm_Initialize();
>     if( pmErr != pmNoError ) goto error;
>
>     /*
>      * Setup the Audio output stream
>      */
>     outputParameters.device = Pa_GetDefaultOutputDevice(); /* default  
>audio output device */
>     if (outputParameters.device == paNoDevice) {
>       fprintf(stderr,"Error: No default audio output device.\n");
>       goto error;
>     }
>     outputParameters.channelCount = 2;       /* stereo output */
>     outputParameters.sampleFormat = paFloat32; /* 32 bit floating  
>point output */
>     outputParameters.suggestedLatency =  
>Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
>     outputParameters.hostApiSpecificStreamInfo = NULL;
>
>     paErr = Pa_OpenStream(
>               &stream,
>               NULL, /* no input */
>               &outputParameters,
>               SAMPLE_RATE,
>               FRAMES_PER_BUFFER,
>               paClipOff,      /* we won't output out of range samples  
>so don't bother clipping them */
>               patestCallback,
>               &data );
>     if( paErr != paNoError ) goto error;
>
>     paErr = Pa_SetStreamFinishedCallback( stream, &StreamFinished );
>     if( paErr != paNoError ) goto error;
>
>     /*
>      * Setup the midi output stream
>      */
>     defaultPmDevice = Pm_GetDefaultOutputDeviceID();
>     if( defaultPmDevice == pmNoDevice ) {
>        fprintf( stderr, "Error: no portmidi default device found." );
>        goto error;
>     }
>     // calculate latency
>     latency1 = (long) ( Pa_GetStreamInfo(stream)->outputLatency *  
>1000 + .5 ) ;
>     latency2 = (long) ( FRAMES_PER_BUFFER * 1000 * 2. / SAMPLE_RATE  
>+ .5 ) ;
>     if( latency1 == 0 || latency2 > latency1 )
>        latency1 = latency2;
>     if( latency1 == 0 )
>        latency1 = 10;
>     //actually open:
>     pmErr = Pm_OpenOutput( &midiStream,
>                            defaultPmDevice,
>                            NULL,
>                            MIDI_OUTPUT_BUFFER_SIZE,
>                            &timeProc,
>                            stream,
>                            latency1 );
>     if( pmErr != pmNoError ) goto error;
>     data.midiStream = midiStream;
>
>     /*
>      * start the audio stream
>      */
>     paErr = Pa_StartStream( stream );
>     if( paErr != paNoError ) goto error;
>
>     printf("Play for %d seconds.\n", NUM_SECONDS );
>     Pa_Sleep( NUM_SECONDS * 1000 );
>
>     paErr = Pa_StopStream( stream );
>     if( paErr != paNoError ) goto error;
>
>     paErr = Pa_CloseStream( stream );
>     if( paErr != paNoError ) goto error;
>
>     pmErr = Pm_Close( midiStream );
>     if( pmErr != pmNoError ) goto error;
>
>     Pa_Terminate();
>     Pm_Terminate();
>     printf("Test finished.\n");
>
>     return paErr;
>error:
>     Pa_Terminate();
>     if( paErr != paNoError ) {
>        fprintf( stderr, "An error occured while using the portaudio  
>stream\n" );
>        fprintf( stderr, "PA Error number: %d\n", paErr );
>        fprintf( stderr, "Error message: %s\n",  
>Pa_GetErrorText( paErr ) );
>     } else {
>        fprintf( stderr, "An error occured while using PortMIDI\n" );
>        fprintf( stderr, "PM Error number: %d\n", pmErr );
>        fprintf( stderr, "Error message: %s\n",  
>Pa_GetErrorText( pmErr ) );
>     }
>     return paErr;
>}
>_______________________________________________
>media_api mailing list
>[email protected]
>http://lists.create.ucsb.edu/mailman/listinfo/media_api
_______________________________________________
media_api mailing list
[email protected]
http://lists.create.ucsb.edu/mailman/listinfo/media_api

Reply via email to