final (?) version. Assuming main.c portaudio.h portmidi.h in dir
called src, and libportaudio.a libportmidi_s.a in dir called lib,
compiles on the mac with:
gcc -std=gnu99 -Wall -Werror src/main.c lib/*.a -framework CoreAudio -
framework CoreMidi -framework AudioToolbox -framework AudioUnit -
framework CoreServices -o sync
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 "portaudio.h"
#include "portmidi.h"
#define NUM_SECONDS (10)
#define SAMPLE_RATE (44100)
#define FRAMES_PER_BUFFER (64)
#define PITCH (261.63) // our desired note in Hz (middle C, C4)
#define MIDI_NOTE (0x3C) // our desired note's MIDI note number
(middle C, C4)
#define TONE_LENGTH (.5) // how many seconds will our tone play for?
#define MIDI_OUTPUT_BUFFER_SIZE (100)
#ifndef M_PI
#define M_PI (3.14159265)
#endif
/* This function is used by PortMIDI when it needs to know the current
audio playback time in ms */
PmTimestamp timeProc(void *timeData) {
PaStream *pas = (PaStream *) timeData ;
PaTime t = Pa_GetStreamTime( pas );
// now we convert from seconds to milliseconds.
// we are not concerning ourselves with out-of-range issues at the
moment.
return (PmTimestamp) ( t * 1000 + .5 ) ;
}
/* This is the structure that's passed to the PortAudio Callback. */
typedef struct
{
int once; //set to 1 after we have initialized start
PmTimestamp start; // the time when the first sample will play
PortMidiStream *midiStream; // a pointer to the port midi stream
int iterations; //each on-off cycle is one iteration
int countdown; // counts down the number of samples until we
start the next tone
int tonetime; // counts down the number of samples in the current
tone
}
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;
// avoid unusaed variable warnings
(void) statusFlags;
(void) inputBuffer;
// initialize the start time.
if( !data->once ) {
data->once = 1;
data->start = (PmTimestamp) ( timeInfo->currentTime * 1000 + .
5 );
}
// fill in the audio data. We will also write MIDI data as we
find corresponding spots where
// the MIDI data belongs.
for( i=0; i<framesPerBuffer; i++ )
{
if( data->countdown == SAMPLE_RATE ) {
// we are starting playback of note.
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 ) {
// end the note.
Pm_WriteShort( data->midiStream,
data->start + (data->iterations-1)*1000+(int)
(1000*TONE_LENGTH+.5),
Pm_Message( 0x80, 0x3C, 0x00 ) );
}
// play audio silence.
*out++ = 0;
*out++ = 0;
}
--data->tonetime;
if( data->countdown == 0 )
data->countdown = SAMPLE_RATE; //reset countdown to 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;
// note: sending all notes off should not be necessary
// it is for demonstration only.
printf( "Sending all notes off...\n" );
Pm_WriteShort( data->midiStream,
timeProc( data ), // NOW!
Pm_Message( 0xB0, 0x7B, 0x00 ) );
printf( "Stream Completed.\n" );
}
/**************** main function ******************/
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 callback data */
data.countdown = SAMPLE_RATE ;
data.tonetime = (int) (SAMPLE_RATE * TONE_LENGTH + .5 ) ;
data.once = 0;
data.iterations = 0;
// -- initialize portaudio and portmidi systems -- //
paErr = Pa_Initialize();
if( paErr != paNoError ) goto error;
pmErr = Pm_Initialize();
if( pmErr != pmNoError ) goto error;
/*
* Setup the Audio output stream using the default device,
* with default low latency settings.
*/
outputParameters.device = Pa_GetDefaultOutputDevice();
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;
// open:
pmErr = Pm_OpenOutput( &midiStream,
defaultPmDevice,
NULL,
MIDI_OUTPUT_BUFFER_SIZE,
&timeProc,
stream,
latency1 );
if( pmErr != pmNoError ) goto error;
data.midiStream = midiStream;
/*
* Handle playback start, stop and close.
*/
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;
/*
* Cleanup.
*/
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