Okay, below is my first stab. I tried queueing the MIDI data both in
the main thread and in the PA callback (code below shows the PA
callback version which to my ears sounded like a tighter sync). I am
not sure if PortMidi allows this kind of queuing. One question I have
(really a PortAudio question) is the difference between streamtime and
DacTime -- I use both here (Dac Time is my start reference, but
Streamtime is what I use for the timing function for PortMidi) and it
happens to give me good timing, but maybe that's not correct.
The problem I have run into is that sometimes (about 1 out of every 3
or four times I run it) the first note-off message is never sent. I
can verify this both with my ears and by watching the MIDI receive LED
on my device. Unless I have encountered a bug (which seems unlikely)
This suggests that something is wrong with my code and therefore my
understanding of how this all works. I noted this behavior whether I
queued the data from the main thread or the callback, so I think maybe
something is wrong with how I am queuing the data or the latency or
something else.
I am working on OS X 10.5, with a firewire MIDI and Audio interface
(presonus firepod). Suggestions very much appreciated!
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
{
// set to true once the callback is called once.
int once;
PortMidiStream *midiStream;
// 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;
/*
* load midi data
*/
PmTimestamp start = (PmTimestamp) ( timeInfo-
>outputBufferDacTime * 1000 + .5 );
//printf( "%d\n", start );
for( int i=0; i<NUM_SECONDS; ++i ) {
Pm_WriteShort( data->midiStream, start + i*1000,
Pm_Message( 0x90, 0x3C, 0x60 ) );
//printf( "Start %d\n", i*1000 );
Pm_WriteShort( data->midiStream, start + i*1000 + (int)
(1000*TONE_LENGTH+.5), Pm_Message( 0x80, 0x3C, 0x00 ) );
//printf( "stop %d\n", i*1000 + (int) (1000*TONE_LENGTH+.
5) );
}
}
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;
for( i=0; i<framesPerBuffer; i++ )
{
if( data->countdown == SAMPLE_RATE )
data->tonetime = (int) (SAMPLE_RATE * TONE_LENGTH + .5 ) ;
if( data->tonetime > 0 ) {
int t = data->tonetime - (int) (SAMPLE_RATE * TONE_LENGTH
+ .5 ) ;
*out++ = sin( 2. * M_PI * PITCH * t / SAMPLE_RATE ) ;
*out++ = sin( 2. * M_PI * PITCH * t / SAMPLE_RATE ) ;
--data->tonetime;
} else {
*out++ = 0;
*out++ = 0;
}
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 )
{
//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;
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;
}
-----------------------------
Bjorn Roche
http://www.xonami.com
Audio Collaboration
_______________________________________________
media_api mailing list
[email protected]
http://lists.create.ucsb.edu/mailman/listinfo/media_api