1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
/******************************************/
/*
Example program to play an N channel
soundfile.
This program will load WAV, SND, AIF, and
MAT-file formatted files of various data
types. If the audio system does not support
the number of channels or sample rate of
the soundfile, the program will stop.
By Gary P. Scavone, 2000 - 2004.
*/
/******************************************/
#include "FileWvIn.h"
#include "RtAudio.h"
#include <signal.h>
#include <iostream>
#include <cstdlib>
using namespace stk;
// Eewww ... global variables! :-)
bool done = false;
StkFrames frames;
static void finish(int ignore){ done = true; }
void usage(void) {
// Error function in case of incorrect command-line
// argument specifications.
std::cout << "\nuseage: play file sr <rate>\n";
std::cout << " where file = the file to play,\n";
std::cout << " where sr = sample rate,\n";
std::cout << " and rate = an optional playback rate.\n";
std::cout << " (default = 1.0, can be negative)\n\n";
exit( 0 );
}
// This tick() function handles sample computation only. It will be
// called automatically when the system needs a new buffer of audio
// samples.
int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *userData )
{
FileWvIn *input = (FileWvIn *) userData;
StkFloat *samples = (StkFloat *) outputBuffer;
input->tick( frames );
for ( unsigned int i=0; i<frames.size(); i++ ) {
*samples++ = frames[i];
if ( input->channelsOut() == 1 ) *samples++ = frames[i]; // play mono files in stereo
}
if ( input->isFinished() ) {
done = true;
return 1;
}
else
return 0;
}
int main(int argc, char *argv[])
{
// Minimal command-line checking.
if ( argc < 3 || argc > 4 ) usage();
// Set the global sample rate before creating class instances.
Stk::setSampleRate( (StkFloat) atof( argv[2] ) );
// Initialize our WvIn and RtAudio pointers.
RtAudio dac;
FileWvIn input;
// Try to load the soundfile.
try {
input.openFile( argv[1] );
}
catch ( StkError & ) {
exit( 1 );
}
// Set input read rate based on the default STK sample rate.
double rate = 1.0;
rate = input.getFileRate() / Stk::sampleRate();
if ( argc == 4 ) rate *= atof( argv[3] );
input.setRate( rate );
input.ignoreSampleRateChange();
// Find out how many channels we have.
int channels = input.channelsOut();
// Figure out how many bytes in an StkFloat and setup the RtAudio stream.
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = ( channels == 1 ) ? 2 : channels; // Play mono files as stereo.
RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
unsigned int bufferFrames = RT_BUFFER_SIZE;
try {
dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&input );
}
catch ( RtAudioError &error ) {
error.printMessage();
goto cleanup;
}
// Install an interrupt handler function.
(void) signal(SIGINT, finish);
// Resize the StkFrames object appropriately.
frames.resize( bufferFrames, channels );
try {
dac.startStream();
}
catch ( RtAudioError &error ) {
error.printMessage();
goto cleanup;
}
// Block waiting until callback signals done.
while ( !done )
Stk::sleep( 100 );
// By returning a non-zero value in the callback above, the stream
// is automatically stopped. But we should still close it.
try {
dac.closeStream();
}
catch ( RtAudioError &error ) {
error.printMessage();
}
cleanup:
return 0;
}
|