int main(void) { PortAudioStream *stream; PaError err; mute=false; bool quit; char ch; printf("A sample on how to use RakVoice. You need a microphone for this sample.\n"); printf("RakVoice relies on Speex for voice encoding and decoding.\n"); printf("See DependentExtensions/RakVoice/speex-1.1.12 for speex projects.\n"); printf("For windows, I had to define HAVE_CONFIG_H, include win32/config.h,\n"); printf("and include the files under libspeex, except those that start with test.\n"); printf("PortAudio is also included and is used to read and write audio data. You\n"); printf("can substitute whatever you want if you do not want to use portaudio.\n"); printf("Difficulty: Advanced\n\n"); // Since voice is peer to peer, we give the option to use the nat punchthrough client if desired. NatPunchthroughClient natPunchthroughClient; char port[256]; rakPeer = RakNetworkFactory::GetRakPeerInterface(); printf("Enter local port (enter for default): "); gets(port); if (port[0]==0) strcpy(port, "60000"); SocketDescriptor socketDescriptor(atoi(port),0); rakPeer->Startup(4, 30, &socketDescriptor, 1); rakPeer->SetMaximumIncomingConnections(4); rakPeer->AttachPlugin(&rakVoice); rakPeer->AttachPlugin(&natPunchthroughClient); rakVoice.Init(SAMPLE_RATE, FRAMES_PER_BUFFER*sizeof(SAMPLE)); err = Pa_Initialize(); if( err != paNoError ) goto error; err = Pa_OpenStream( &stream, Pa_GetDefaultInputDeviceID(), 1, // Num channels, whatever the f**k that means PA_SAMPLE_TYPE, NULL, Pa_GetDefaultOutputDeviceID(), 1, // Num channels PA_SAMPLE_TYPE, NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, /* frames per buffer */ 0, /* number of buffers, if zero then use default minimum */ 0, /* paDitherOff, // flags */ PACallback, 0 ); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("Support NAT punchthrough? (y/n)? "); bool useNatPunchthrough; useNatPunchthrough=(getche()=='y'); printf("\n"); char facilitatorIP[256]; SystemAddress facilitator; if (useNatPunchthrough) { printf("My GUID is %s\n", rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS).ToString()); printf("Enter IP of facilitator (enter for default): "); gets(facilitatorIP); if (facilitatorIP[0]==0) strcpy(facilitatorIP, "216.224.123.180"); facilitator.SetBinaryAddress(facilitatorIP); facilitator.port=NAT_PUNCHTHROUGH_FACILITATOR_PORT; rakPeer->Connect(facilitatorIP, NAT_PUNCHTHROUGH_FACILITATOR_PORT, 0, 0); printf("Connecting to facilitator...\n"); } else { printf("Not supporting NAT punchthrough.\n"); } Packet *p; quit=false; if (useNatPunchthrough==false) printf("(Q)uit. (C)onnect. (D)isconnect. (M)ute. ' ' for stats.\n"); while (!quit) { if (kbhit()) { ch=getch(); if (ch=='y') { quit=true; } else if (ch=='c') { if (useNatPunchthrough) { RakNetGUID destination; printf("Enter GUID of destination: "); char guidStr[256]; while (1) { gets(guidStr); if (!destination.FromString(guidStr)) printf("Invalid GUID format. Try again.\nEnter GUID of destination: "); else break; } printf("Starting NAT punch. Please wait...\n"); natPunchthroughClient.OpenNAT(destination,facilitator); } else { char ip[256]; printf("Enter IP of remote system: "); gets(ip); if (ip[0]==0) strcpy(ip, "127.0.0.1"); printf("Enter port of remote system: "); gets(port); if (port[0]==0) strcpy(port, "60000"); rakPeer->Connect(ip, atoi(port), 0,0); } } else if (ch=='m') { mute=!mute; if (mute) printf("Now muted.\n"); else printf("No longer muted.\n"); } else if (ch=='d') { rakPeer->Shutdown(100,0); } else if (ch==' ') { char message[2048]; RakNetStatistics *rss=rakPeer->GetStatistics(rakPeer->GetSystemAddressFromIndex(0)); StatisticsToString(rss, message, 2); printf("%s", message); } else if (ch=='q') quit=true; ch=0; } p=rakPeer->Receive(); while (p) { if (p->data[0]==ID_CONNECTION_REQUEST_ACCEPTED) { if (p->systemAddress==facilitator) { printf("Connection to facilitator completed\n"); printf("(Q)uit. (C)onnect. (D)isconnect. (M)ute. ' ' for stats.\n"); } else { printf("ID_CONNECTION_REQUEST_ACCEPTED from %s\n", p->systemAddress.ToString()); rakVoice.RequestVoiceChannel(p->systemAddress); } } else if (p->data[0]==ID_CONNECTION_ATTEMPT_FAILED) { if (p->systemAddress==facilitator) { printf("Connection to facilitator failed. Using direct connections\n"); useNatPunchthrough=false; printf("(Q)uit. (C)onnect. (D)isconnect. (M)ute. ' ' for stats.\n"); } else { printf("ID_CONNECTION_ATTEMPT_FAILED\n"); } } else if (p->data[0]==ID_RAKVOICE_OPEN_CHANNEL_REQUEST || p->data[0]==ID_RAKVOICE_OPEN_CHANNEL_REPLY) { printf("Got new channel from %s\n", p->systemAddress.ToString()); } else if (p->data[0]==ID_NAT_TARGET_NOT_CONNECTED) { RakNetGUID g; RakNet::BitStream b(p->data, p->length, false); b.IgnoreBits(8); // Ignore the ID_... b.Read(g); printf("ID_NAT_TARGET_NOT_CONNECTED for %s\n", g.ToString()); } else if (p->data[0]==ID_NAT_TARGET_UNRESPONSIVE) { RakNetGUID g; RakNet::BitStream b(p->data, p->length, false); b.IgnoreBits(8); // Ignore the ID_... b.Read(g); printf("ID_NAT_TARGET_UNRESPONSIVE for %s\n", g.ToString()); } else if (p->data[0]==ID_NAT_CONNECTION_TO_TARGET_LOST) { RakNetGUID g; RakNet::BitStream b(p->data, p->length, false); b.IgnoreBits(8); // Ignore the ID_... b.Read(g); printf("ID_NAT_CONNECTION_TO_TARGET_LOST for %s\n", g.ToString()); } else if (p->data[0]==ID_NAT_ALREADY_IN_PROGRESS) { RakNetGUID g; RakNet::BitStream b(p->data, p->length, false); b.IgnoreBits(8); // Ignore the ID_... b.Read(g); printf("ID_NAT_ALREADY_IN_PROGRESS for %s\n", g.ToString()); } else if (p->data[0]==ID_NAT_PUNCHTHROUGH_FAILED) { printf("ID_NAT_PUNCHTHROUGH_FAILED for %s\n", p->guid.ToString()); } else if (p->data[0]==ID_NAT_PUNCHTHROUGH_SUCCEEDED) { printf("ID_NAT_PUNCHTHROUGH_SUCCEEDED for %s. Connecting...\n", p->guid.ToString()); rakPeer->Connect(p->systemAddress.ToString(false),p->systemAddress.port,0,0); } else if (p->data[0]==ID_ALREADY_CONNECTED) { printf("ID_ALREADY_CONNECTED\n"); } else { printf("Unknown packet ID %i\n", p->data[0]); } rakPeer->DeallocatePacket(p); p=rakPeer->Receive(); } Pa_Sleep( 30 ); } err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; Pa_Terminate(); rakPeer->Shutdown(300); RakNetworkFactory::DestroyRakPeerInterface(rakPeer); return 0; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return -1; }
int main(int argc, char ** argv){ size_t i, j; size_t numThreads = 2; // FFT stuff int fftSize = 1024 * 4; int fftWinInc = fftSize / 4; // Portaudio stuff PaError paer; PaStream * stream; struct fftBuf ffts[numThreads]; struct aBuf buf = {44100, fftSize, fftWinInc, 1.0, fftSize, 0, NULL, numThreads, ffts}; buf.samples = fmalloc(buf.length * sizeof *buf.samples); for(j = 0; j < buf.length; j++) buf.samples[j] = 0.0; for(i = 0; i < numThreads; i++){ ffts[i].fftIn = fmalloc(ffts[i].length * sizeof *ffts[i].samples); for(j = 0; j < ffts[i].length; j++) ffts[i].fftIn[j] = 0.0; ffts[i].fftOut = fftw_malloc(ffts[i].length * sizeof *ffts[i].fftOut); for(j = 0; j < ffts[i].length; j++){ ffts[i].fftOut[j][0] = 0.0; ffts[i].fftOut[j][1] = 0.0; } ffts[i].panama = fftw_plan_dft_r2c_1d(buf.length, buf.fftIn, buf.fftOut, FFTW_ESTIMATE); ffts[i].mutex = PTHREAD_MUTEX_INITIALIZER; ffts[i].cond = PTHREAD_COND_INITIALIZER; ffts[i].info = &buf; } if(argc > 1 && ((buf.amplifier = strtod(argv[1], NULL)) < 1.0)){ buf.amplifier = 1.0; } printf("---- ----\nInit done\n---- ----\n"); printf("FFT-size: %i\nWindow-length: %f\nWindow-inc: %i\n", fftSize, (double)fftSize/(double)buf.samplerate, fftWinInc); pthread_create(&ffThread1, NULL, fftThread, &buf); //pthread_create(&ffThread2, NULL, fftThread, &buf); paer = Pa_Initialize(); if(paer != paNoError){ fprintf(stderr, "! Pa_Initialize failed: %s\n", Pa_GetErrorText(paer)); return EXIT_FAILURE; } paer = Pa_OpenDefaultStream(&stream, 1, 0, paFloat32, buf.samplerate, fftWinInc, recordCallback, &buf); if(paer != paNoError){ fprintf(stderr, "! Pa_OpenDefaultStream failed: %s\n", Pa_GetErrorText(paer)); return EXIT_FAILURE; } paer = Pa_StartStream(stream); if(paer != paNoError){ fprintf(stderr, "! Pa_StartStream failed: %s\n", Pa_GetErrorText(paer)); return EXIT_FAILURE; } while(Pa_IsStreamActive(stream)) Pa_Sleep(100); Pa_CloseStream(stream); Pa_Terminate(); free(buf.samples); free(buf.fftIn); fftw_free(buf.fftOut); fftw_destroy_plan(buf.panama); return 0; }
int main(void) { PaStreamParameters outputParameters; PaAsioStreamInfo asioOutputInfo; PaStream *stream; PaError err; paTestData data; int outputChannelSelectors[1]; int i; printf("PortAudio Test: output MONO sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); /* initialise sinusoidal wavetable */ for( i=0; i<TABLE_SIZE; i++ ) { data.sine[i] = (float) (AMPLITUDE * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. )); } data.phase = 0; err = Pa_Initialize(); if( err != paNoError ) goto error; outputParameters.device = OUTPUT_DEVICE; outputParameters.channelCount = 1; /* MONO output */ outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; asioOutputInfo.size = sizeof(PaAsioStreamInfo); asioOutputInfo.hostApiType = paASIO; asioOutputInfo.version = 1; asioOutputInfo.flags = paAsioUseChannelSelectors; outputChannelSelectors[0] = 1; /* select the second (right) ASIO device channel */ asioOutputInfo.channelSelectors = outputChannelSelectors; outputParameters.hostApiSpecificStreamInfo = &asioOutputInfo; err = 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( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("Play for %d seconds.\n", NUM_SECONDS ); fflush(stdout); Pa_Sleep( NUM_SECONDS * 1000 ); err = Pa_StopStream( stream ); if( err != paNoError ) goto error; err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; Pa_Terminate(); printf("Test finished.\n"); return err; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }
int main(void) { PortAudioStream *stream; PaError err; int i; paTestData data = {0}; printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER ); err = Pa_Initialize(); if( err != paNoError ) goto error; err = Pa_OpenStream( &stream, paNoDevice,/* default input device */ 0, /* no input */ paFloat32, /* 32 bit floating point input */ NULL, Pa_GetDefaultOutputDeviceID(), /* default output device */ 1, /* mono output */ paFloat32, /* 32 bit floating point output */ NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, /* frames per buffer */ 0, /* number of buffers, if zero then use default minimum */ paClipOff, /* we won't output out of range samples so don't bother clipping them */ patestCallback, &data ); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; /* Gradually increase sleep time. */ for( i=0; i<10000; i+= 1000 ) { printf("Sleep for %d milliseconds in audio callback.\n", i ); data.sleepFor = i; fflush(stdout); Pa_Sleep( ((i<1000) ? 1000 : i) ); } printf("Suffer for 10 seconds.\n"); fflush(stdout); Pa_Sleep( 10000 ); err = Pa_StopStream( stream ); if( err != paNoError ) goto error; err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; Pa_Terminate(); printf("Test finished.\n"); return err; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }
int main(int argc, const char * argv[]){ (void) argc; (void) argv; PaError err; static paTestData data; static PaStream * stream; /* initialise sinusoidal wavetable */ int i; for (i=0; i<TABLE_SIZE; i++){ data.sine[i] = sin(((double)i/(double)TABLE_SIZE) * M_PI * 2.)*32767; } data.left_phase = data.right_phase = 0; err = Pa_Initialize(); if (err != paNoError){ printf("Error initializing portaudio: \"%s\"\n", Pa_GetErrorText(err)); return paNoError; } PaStreamParameters outputParameters; outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ outputParameters.channelCount = NUM_CHANNELS; outputParameters.sampleFormat = PA_SAMPLE_TYPE; outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; /* -- setup stream -- */ err = Pa_OpenStream( &stream, NULL, /* &inputParameters */ &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, /* we won't output out of range samples so don't bother clipping them */ patestCallback, /* use callback */ &data ); /* callback userData */ memset(ring_buffer_storage, 0, sizeof(ring_buffer_storage)); btstack_ring_buffer_init(&ring_buffer, ring_buffer_storage, sizeof(ring_buffer_storage)); wav_writer_open(wav_filename, NUM_CHANNELS, SAMPLE_RATE); if (err != paNoError){ printf("Error opening default stream: \"%s\"\n", Pa_GetErrorText(err)); return paNoError; } err = Pa_StartStream(stream); if (err != paNoError){ printf("Error starting the stream: \"%s\"\n", Pa_GetErrorText(err)); return paNoError; } /* Sleep for several seconds. */ while (1){ fill_ring_buffer(&data); Pa_Sleep(1); } err = Pa_StopStream(stream); if (err != paNoError){ printf("Error stopping the stream: \"%s\"\n", Pa_GetErrorText(err)); return paNoError; } err = Pa_CloseStream(stream); if (err != paNoError){ printf("Error closing the stream: \"%s\"\n", Pa_GetErrorText(err)); return paNoError; } err = Pa_Terminate(); if (err != paNoError){ printf("Error terminating portaudio: \"%s\"\n", Pa_GetErrorText(err)); return paNoError; } return 0; }
Audio::Audio(Oscilloscope* scope, int16_t initialJitterBufferSamples) : _stream(NULL), _ringBuffer(true), _scope(scope), _averagedLatency(0.0), _measuredJitter(0), _jitterBufferSamples(initialJitterBufferSamples), _wasStarved(0), _numStarves(0), _lastInputLoudness(0), _lastVelocity(0), _lastAcceleration(0), _totalPacketsReceived(0), _firstPacketReceivedTime(), _echoSamplesLeft(NULL), _packetsReceivedThisPlayback(0), _isSendingEchoPing(false), _pingAnalysisPending(false), _pingFramesToRecord(0), _samplesLeftForFlange(0), _lastYawMeasuredMaximum(0), _flangeIntensity(0.0f), _flangeRate(0.0f), _flangeWeight(0.0f) { outputPortAudioError(Pa_Initialize()); // NOTE: Portaudio documentation is unclear as to whether it is safe to specify the // number of frames per buffer explicitly versus setting this value to zero. // Possible source of latency that we need to investigate further. // unsigned long FRAMES_PER_BUFFER = BUFFER_LENGTH_SAMPLES_PER_CHANNEL; // Manually initialize the portaudio stream to ask for minimum latency PaStreamParameters inputParameters, outputParameters; inputParameters.device = Pa_GetDefaultInputDevice(); outputParameters.device = Pa_GetDefaultOutputDevice(); if (inputParameters.device == -1 || outputParameters.device == -1) { printLog("Audio: Missing device.\n"); outputPortAudioError(Pa_Terminate()); return; } inputParameters.channelCount = 2; // Stereo input inputParameters.sampleFormat = (paInt16 | paNonInterleaved); inputParameters.suggestedLatency = Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency; inputParameters.hostApiSpecificStreamInfo = NULL; outputParameters.channelCount = 2; // Stereo output outputParameters.sampleFormat = (paInt16 | paNonInterleaved); outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; outputPortAudioError(Pa_OpenStream(&_stream, &inputParameters, &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paNoFlag, audioCallback, (void*) this)); if (! _stream) { return; } _echoSamplesLeft = new int16_t[AEC_BUFFERED_SAMPLES + AEC_TMP_BUFFER_SIZE]; memset(_echoSamplesLeft, 0, AEC_BUFFERED_SAMPLES * sizeof(int16_t)); // start the stream now that sources are good to go outputPortAudioError(Pa_StartStream(_stream)); // Uncomment these lines to see the system-reported latency //printLog("Default low input, output latency (secs): %0.4f, %0.4f\n", // Pa_GetDeviceInfo(Pa_GetDefaultInputDevice())->defaultLowInputLatency, // Pa_GetDeviceInfo(Pa_GetDefaultOutputDevice())->defaultLowOutputLatency); const PaStreamInfo* streamInfo = Pa_GetStreamInfo(_stream); printLog("Started audio with reported latency msecs In/Out: %.0f, %.0f\n", streamInfo->inputLatency * 1000.f, streamInfo->outputLatency * 1000.f); gettimeofday(&_lastReceiveTime, NULL); }
int main(void) { PaStreamParameters outputParameters; PaStream *stream; PaError err; TestData data; int i, j; printf( "PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER ); /* initialise sinusoidal wavetable */ for( i=0; i<TABLE_SIZE; i++ ) { data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); } err = Pa_Initialize(); if( err != paNoError ) goto error; outputParameters.device = Pa_GetDefaultOutputDevice(); if (outputParameters.device == paNoDevice) { fprintf(stderr,"Error: No default 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; err = Pa_OpenStream( &stream, NULL, /* no input */ &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, /* output will be in-range, so no need to clip */ TestCallback, &data ); if( err != paNoError ) goto error; sprintf( data.message, "Loop: XX" ); err = Pa_SetStreamFinishedCallback( stream, &StreamFinished ); if( err != paNoError ) goto error; printf("Repeating test %d times.\n", NUM_LOOPS ); for( i=0; i < NUM_LOOPS; ++i ) { data.phase = 0; data.generatedFramesCount = 0; data.callbackReturnedPaComplete = 0; data.callbackInvokedAfterReturningPaComplete = 0; sprintf( data.message, "Loop: %d", i ); err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("Play for %d seconds.\n", NUM_SECONDS ); /* wait for the callback to complete generating NUM_SECONDS of tone */ do { Pa_Sleep( 500 ); } while( !data.callbackReturnedPaComplete ); printf( "Callback returned paComplete.\n" ); printf( "Waiting for buffers to finish playing...\n" ); /* wait for stream to become inactive, or for a timeout of approximately NUM_SECONDS */ j = 0; while( (err = Pa_IsStreamActive( stream )) == 1 && j < NUM_SECONDS * 2 ) { printf(".\n" ); Pa_Sleep( 500 ); ++j; } if( err < 0 ) { goto error; } else if( err == 1 ) { printf( "TEST FAILED: Timed out waiting for buffers to finish playing.\n" ); } else { printf("Buffers finished.\n" ); } if( data.callbackInvokedAfterReturningPaComplete ) { printf( "TEST FAILED: Callback was invoked after returning paComplete.\n" ); } err = Pa_StopStream( stream ); if( err != paNoError ) goto error; printf( "sleeping for 1 second...\n" ); Pa_Sleep( 1000 ); } err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; Pa_Terminate(); printf("Test finished.\n"); return err; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }
int main(void) { PortAudioStream *stream; PaError err; paTestData data; int i; PaDeviceID inputDevice; const PaDeviceInfo *pdi; printf("PortAudio Test: input signal from each channel. %d buffers\n", NUM_BUFFERS ); data.liveChannel = 0; err = Pa_Initialize(); if( err != paNoError ) goto error; #ifdef INPUT_DEVICE_NAME printf("Try to use device: %s\n", INPUT_DEVICE_NAME ); inputDevice = PaFindDeviceByName(INPUT_DEVICE_NAME); if( inputDevice == paNoDevice ) { printf("Could not find %s. Using default instead.\n", INPUT_DEVICE_NAME ); inputDevice = Pa_GetDefaultInputDeviceID(); } #else printf("Using default input device.\n"); inputDevice = Pa_GetDefaultInputDeviceID(); #endif pdi = Pa_GetDeviceInfo( inputDevice ); if( pdi == NULL ) { printf("Could not get device info!\n"); goto error; } data.numChannels = pdi->maxInputChannels; printf("Input Device name is %s\n", pdi->name ); printf("Input Device has %d channels.\n", pdi->maxInputChannels); err = Pa_OpenStream( &stream, inputDevice, pdi->maxInputChannels, paFloat32, /* 32 bit floating point input */ NULL, OUTPUT_DEVICE, 2, paFloat32, /* 32 bit floating point output */ NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, /* frames per buffer */ NUM_BUFFERS, /* number of buffers, if zero then use default minimum */ paClipOff, /* we won't output out of range samples so don't bother clipping them */ patestCallback, &data ); if( err != paNoError ) goto error; data.liveChannel = 0; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; for( i=0; i<data.numChannels; i++ ) { data.liveChannel = i; printf("Channel %d being sent to output. Hit ENTER for next channel.", i ); fflush(stdout); getchar(); } err = Pa_StopStream( stream ); if( err != paNoError ) goto error; err = Pa_CloseStream( stream ); Pa_Terminate(); printf("Test finished.\n"); return err; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }
int main(void) { PaStreamParameters outputParameters; PaStream *stream; PaError err; paTestData data; int i; printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); /* initialise sinusoidal wavetable */ for( i=0; i<TABLE_SIZE; i++ ) { data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); } data.left_phase = data.right_phase = 0; err = Pa_Initialize(); if( err != paNoError ) goto error; outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ outputParameters.channelCount = 2; /* stereo output */ outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; err = 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( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("Play for %d seconds.\n", NUM_SECONDS ); Pa_Sleep( NUM_SECONDS * 1000 ); err = Pa_StopStream( stream ); if( err != paNoError ) goto error; err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; Pa_Terminate(); printf("Test finished.\n"); return err; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }
AudioIO::~AudioIO() { Pa_Terminate(); }
int quit(int rc) { Pa_Terminate(); exit(rc); };
int main(void) { char pad[256]; PortAudioStream *stream; PaError err; const PaDeviceInfo *pdi; paTestData data = {0}; printf("PortAudio Test: output sine wave on each channel.\n" ); err = Pa_Initialize(); if( err != paNoError ) goto error; pdi = Pa_GetDeviceInfo( OUTPUT_DEVICE ); data.numChannels = pdi->maxOutputChannels; if( data.numChannels > MAX_CHANNELS ) data.numChannels = MAX_CHANNELS; printf("Number of Channels = %d\n", data.numChannels ); data.amplitude = 1.0; err = Pa_OpenStream( &stream, paNoDevice, /* default input device */ 0, /* no input */ paFloat32, /* 32 bit floating point input */ NULL, OUTPUT_DEVICE, data.numChannels, paFloat32, /* 32 bit floating point output */ NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, /* frames per buffer */ 0, /* number of buffers, if zero then use default minimum */ paClipOff, /* we won't output out of range samples so don't bother clipping them */ patestCallback, &data ); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; do { printf("Current amplitude = %f\n", data.amplitude ); printf("Enter new amplitude or 'q' to quit.\n"); fflush(stdout); gets( pad ); if( pad[0] != 'q' ) { // I tried to use atof but it seems to be broken on Mac OS X 10.1 float amp; sscanf( pad, "%f", & ); data.amplitude = amp; } } while( pad[0] != 'q' ); err = Pa_StopStream( stream ); if( err != paNoError ) goto error; Pa_CloseStream( stream ); Pa_Terminate(); printf("Test finished.\n"); return err; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }
int main(void) { PaStreamParameters inputParameters, outputParameters; PaStream* stream; PaStream* stream2; PaError err = paNoError; paTestData data; paTestData data2; int i; int totalFrames; int numSamples; int numBytes; SAMPLE max, val; double average; printf("patest_record.c\n"); fflush(stdout); data.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */ data.frameIndex = 0; data2.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */ data2.frameIndex = 0; numSamples = totalFrames * NUM_CHANNELS; numBytes = numSamples * sizeof(SAMPLE); data.recordedSamples = (SAMPLE *) malloc( numBytes ); /* From now on, recordedSamples is initialised. */ data2.recordedSamples = (SAMPLE *) malloc( numBytes ); /* From now on, recordedSamples is initialised. */ if( data.recordedSamples == NULL ) { printf("Could not allocate record array.\n"); goto done; } for( i=0; i<numSamples; i++ ) data.recordedSamples[i] = 0; for( i=0; i<numSamples; i++ ) data2.recordedSamples[i] = 0; err = Pa_Initialize(); if( err != paNoError ) goto done; inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */ if (inputParameters.device == paNoDevice) { fprintf(stderr,"Error: No default input device.\n"); goto done; } inputParameters.channelCount = 2; /* stereo input */ inputParameters.sampleFormat = PA_SAMPLE_TYPE; inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency; inputParameters.hostApiSpecificStreamInfo = NULL; /* Record some audio. -------------------------------------------- */ err = Pa_OpenStream( &stream, &inputParameters, NULL, /* &outputParameters, */ SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, /* we won't output out of range samples so don't bother clipping them */ recordCallback, &data ); if( err != paNoError ) goto done; err = Pa_StartStream( stream ); if( err != paNoError ) goto done; printf("\n=== Now recording!! Please speak into the microphone. ===\n"); fflush(stdout); while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) { Pa_Sleep(1000); printf("index = %d\n", data.frameIndex ); fflush(stdout); } if( err < 0 ) goto done; err = Pa_CloseStream( stream ); if( err != paNoError ) goto done; /* Measure maximum peak amplitude. */ max = 0; average = 0.0; for( i=0; i<numSamples; i++ ) { val = data.recordedSamples[i]; if( val < 0 ) val = -val; /* ABS */ if( val > max ) { max = val; } average += val; } average = average / (double)numSamples; printf("sample max amplitude = "PRINTF_S_FORMAT"\n", max ); printf("sample average = %lf\n", average ); /* Segunda gravacao:*/ err = Pa_OpenStream( &stream, &inputParameters, NULL, /* &outputParameters, */ SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, /* we won't output out of range samples so don't bother clipping them */ recordCallback, &data2 ); if( err != paNoError ) goto done; err = Pa_StartStream( stream ); if( err != paNoError ) goto done; printf("\n=== Now recording!! Please speak into the microphone. ===\n"); fflush(stdout); while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) { Pa_Sleep(1000); printf("index = %d\n", data2.frameIndex ); fflush(stdout); } if( err < 0 ) goto done; err = Pa_CloseStream( stream ); if( err != paNoError ) goto done; /* Measure maximum peak amplitude. */ max = 0; average = 0.0; for( i=0; i<numSamples; i++ ) { val = data2.recordedSamples[i]; if( val < 0 ) val = -val; /* ABS */ if( val > max ) { max = val; } average += val; } average = average / (double)numSamples; printf("sample max amplitude = "PRINTF_S_FORMAT"\n", max ); printf("sample average = %lf\n", average ); /* Write recorded data to a file. */ #if WRITE_TO_FILE { FILE *fid; fid = fopen("recorded.raw", "wb"); if( fid == NULL ) { printf("Could not open file."); } else { fwrite( data.recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), totalFrames, fid ); fclose( fid ); printf("Wrote data to 'recorded.raw'\n"); } } #endif /* Playback recorded data. -------------------------------------------- */ data.frameIndex = 0; data2.frameIndex = 0; outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ if (outputParameters.device == paNoDevice) { fprintf(stderr,"Error: No default output device.\n"); goto done; } outputParameters.channelCount = 2; /* stereo output */ outputParameters.sampleFormat = PA_SAMPLE_TYPE; outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; printf("\n=== Now playing back. ===\n"); fflush(stdout); err = 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 */ playCallback, &data ); if( err != paNoError ) goto done; err = Pa_OpenStream( &stream2, NULL, /* no input */ &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, /* we won't output out of range samples so don't bother clipping them */ playCallback, &data2 ); if( err != paNoError ) goto done; if( stream ) { err = Pa_StartStream( stream ); if( err != paNoError ) goto done; err = Pa_StartStream( stream2 ); if( err != paNoError ) goto done; printf("Waiting for playback to finish.\n"); fflush(stdout); while((err = Pa_IsStreamActive(stream2)) == 1) Pa_Sleep(100); if( err < 0 ) goto done; err = Pa_CloseStream( stream ); if( err != paNoError ) goto done; err = Pa_CloseStream( stream2 ); if( err != paNoError ) goto done; printf("Done.\n"); fflush(stdout); } done: Pa_Terminate(); if( data.recordedSamples ) /* Sure it is NULL or valid. */ free( data.recordedSamples ); if( err != paNoError ) { fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); err = 1; /* Always return 0 or 1, but no other return codes. */ } return err; }
int main(void) { PaError err; WireConfig_t CONFIG; WireConfig_t *config = &CONFIG; int configIndex = 0;; err = Pa_Initialize(); if( err != paNoError ) goto error; printf("Please connect audio signal to input and listen for it on output!\n"); printf("input format = %lu\n", INPUT_FORMAT ); printf("output format = %lu\n", OUTPUT_FORMAT ); printf("input device ID = %d\n", INPUT_DEVICE ); printf("output device ID = %d\n", OUTPUT_DEVICE ); if( INPUT_FORMAT == OUTPUT_FORMAT ) { gInOutScaler = 1.0; } else if( (INPUT_FORMAT == paInt16) && (OUTPUT_FORMAT == paFloat32) ) { gInOutScaler = 1.0/32768.0; } else if( (INPUT_FORMAT == paFloat32) && (OUTPUT_FORMAT == paInt16) ) { gInOutScaler = 32768.0; } for( config->isInputInterleaved = 0; config->isInputInterleaved < 2; config->isInputInterleaved++ ) { for( config->isOutputInterleaved = 0; config->isOutputInterleaved < 2; config->isOutputInterleaved++ ) { for( config->numInputChannels = 1; config->numInputChannels < 3; config->numInputChannels++ ) { for( config->numOutputChannels = 1; config->numOutputChannels < 3; config->numOutputChannels++ ) { /* If framesPerCallback = 0, assertion fails in file pa_common/pa_process.c, line 1413: EX. */ for( config->framesPerCallback = 64; config->framesPerCallback < 129; config->framesPerCallback += 64 ) { printf("-----------------------------------------------\n" ); printf("Configuration #%d\n", configIndex++ ); err = TestConfiguration( config ); /* Give user a chance to bail out. */ if( err == 1 ) { err = paNoError; goto done; } else if( err != paNoError ) goto error; } } } } } done: Pa_Terminate(); printf("Full duplex sound test complete.\n"); fflush(stdout); printf("Hit ENTER to quit.\n"); fflush(stdout); getchar(); return 0; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); printf("Hit ENTER to quit.\n"); fflush(stdout); getchar(); return -1; }
int main(void) { PortAudioStream *stream; PaError err; const PaDeviceInfo *inputInfo; const PaDeviceInfo *outputInfo; int numChannels; err = Pa_Initialize(); if( err != paNoError ) goto error; printf("PortAudio Test: input device ID = %d\n", INPUT_DEVICE ); printf("PortAudio Test: output device ID = %d\n", OUTPUT_DEVICE ); /* Use as many channels aspossible. */ inputInfo = Pa_GetDeviceInfo( INPUT_DEVICE ); outputInfo = Pa_GetDeviceInfo( OUTPUT_DEVICE ); /* Use smaller count. */ numChannels = (inputInfo->maxInputChannels < outputInfo->maxOutputChannels) ? inputInfo->maxInputChannels : outputInfo->maxOutputChannels; printf("maxInputChannels channels = %d\n", inputInfo->maxInputChannels ); printf("maxOutputChannels channels = %d\n", outputInfo->maxOutputChannels ); if( numChannels > 0 ) { printf("Using %d channels.\n", numChannels ); err = Pa_OpenStream( &stream, INPUT_DEVICE, numChannels, PA_SAMPLE_TYPE, NULL, OUTPUT_DEVICE, numChannels, PA_SAMPLE_TYPE, NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, /* frames per buffer */ 0, /* number of buffers, if zero then use default minimum */ paClipOff, /* we won't output out of range samples so don't bother clipping them */ wireCallback, (void *) numChannels ); /* pass numChannels to callback */ if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("Full duplex sound test in progress.\n"); printf("Hit ENTER to exit test.\n"); fflush(stdout); getchar(); printf("Closing stream.\n"); err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; } else { printf("Sorry, not enough channels.\n"); } Pa_Terminate(); printf("Full duplex sound test complete.\n"); fflush(stdout); return 0; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return -1; }
int main(void) { PortAudioStream *stream; PaError err; paTestData data; int i; int done = 0; printf("PortAudio Test: enter letter then hit ENTER. numBuffers = %d\n", NUM_BUFFERS ); /* initialise sinusoidal wavetable */ for( i=0; i<TABLE_SIZE; i++ ) { data.sine[i] = 0.90f * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); } data.sine[TABLE_SIZE] = data.sine[0]; // set guard point data.left_phase = data.right_phase = 0.0; data.phase_increment = CalcPhaseIncrement(MIN_FREQ); err = Pa_Initialize(); if( err != paNoError ) goto error; printf("PortAudio Test: output device = %d\n", OUTPUT_DEVICE ); err = Pa_OpenStream( &stream, paNoDevice, 0, /* no input */ paFloat32, /* 32 bit floating point input */ NULL, OUTPUT_DEVICE, 2, /* stereo output */ paFloat32, /* 32 bit floating point output */ NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, NUM_BUFFERS, /* number of buffers, if zero then use default minimum */ paClipOff|paDitherOff, /* we won't output out of range samples so don't bother clipping them */ patestCallback, &data ); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("Play ASCII keyboard. Hit 'q' to stop. (Use RETURN key on Mac)\n"); fflush(stdout); while ( !done ) { float freq; int index; char c; do { c = getchar(); } while( c < ' '); /* Strip white space and control chars. */ if( c == 'q' ) done = 1; index = c % 26; freq = MIN_FREQ + (index * 40.0); data.phase_increment = CalcPhaseIncrement(freq); } printf("Call Pa_StopStream()\n"); err = Pa_StopStream( stream ); if( err != paNoError ) goto error; Pa_Terminate(); printf("Test finished.\n"); return err; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }
int main(void) { PaStream *stream; PaStreamParameters outputParameters; PaError err; paTestData data; int i; int done = 0; printf("PortAudio Test: enter letter then hit ENTER.\n" ); /* initialise sinusoidal wavetable */ for( i=0; i<TABLE_SIZE; i++ ) { data.sine[i] = 0.90f * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); } data.sine[TABLE_SIZE] = data.sine[0]; /* set guard point. */ data.left_phase = data.right_phase = 0.0; data.phase_increment = CalcPhaseIncrement(MIN_FREQ); err = Pa_Initialize(); if( err != paNoError ) goto error; printf("PortAudio Test: output device = %d\n", OUTPUT_DEVICE ); outputParameters.device = OUTPUT_DEVICE; outputParameters.channelCount = 2; /* stereo output */ outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; printf("Requested output latency = %.4f seconds.\n", outputParameters.suggestedLatency ); printf("%d frames per buffer.\n.", FRAMES_PER_BUFFER ); err = Pa_OpenStream( &stream, NULL, /* no input */ &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff|paDitherOff, /* we won't output out of range samples so don't bother clipping them */ patestCallback, &data ); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("Play ASCII keyboard. Hit 'q' to stop. (Use RETURN key on Mac)\n"); fflush(stdout); while ( !done ) { float freq; int index; char c; do { c = getchar(); } while( c < ' '); /* Strip white space and control chars. */ if( c == 'q' ) done = 1; index = c % 26; freq = MIN_FREQ + (index * 40.0); data.phase_increment = CalcPhaseIncrement(freq); } printf("Call Pa_StopStream()\n"); err = Pa_StopStream( stream ); if( err != paNoError ) goto error; Pa_Terminate(); printf("Test finished.\n"); return err; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }
~InitSingleton(){ if(mCleanUp){ Pa_Terminate(); } }
int main(void) { PaStream* stream; PaStreamParameters outputParameters; PaError err; int i; paTestData data = {0}; printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER ); err = Pa_Initialize(); if( err != paNoError ) goto error; outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */ if (outputParameters.device == paNoDevice) { fprintf(stderr,"Error: No default output device.\n"); goto error; } outputParameters.channelCount = 1; /* Mono output. */ outputParameters.sampleFormat = paFloat32; /* 32 bit floating point. */ outputParameters.hostApiSpecificStreamInfo = NULL; outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device) ->defaultLowOutputLatency; err = Pa_OpenStream(&stream, NULL, /* No input. */ &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, /* No out of range samples. */ patestCallback, &data); if (err != paNoError) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; /* Gradually increase sleep time. */ /* Was: for( i=0; i<10000; i+= 1000 ) */ for(i=0; i <= 1000; i += 100) { printf("Sleep for %d milliseconds in audio callback.\n", i ); data.sleepFor = i; Pa_Sleep( ((i<1000) ? 1000 : i) ); } printf("Suffer for 10 seconds.\n"); Pa_Sleep( 10000 ); err = Pa_StopStream( stream ); if( err != paNoError ) goto error; err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; Pa_Terminate(); printf("Test finished.\n"); return err; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }
int main(void) { PaStreamParameters outputParameters; PaStream *stream; PaError err; paTestData data; PaTime startTime; printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); data.left_phase = data.right_phase = 0; err = Pa_Initialize(); if( err != paNoError ) goto error; outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ outputParameters.channelCount = 2; /* stereo output */ outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */ outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; err = 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( err != paNoError ) goto error; /* Watch until sound is halfway finished. */ printf("Play for %d seconds.\n", NUM_SECONDS/2 ); fflush(stdout); data.outTime = -1.0; /* mark time for callback as undefined */ err = Pa_StartStream( stream ); if( err != paNoError ) goto error; startTime = Pa_GetStreamTime( stream ); do { ReportStreamTime( stream, &data ); Pa_Sleep(100); } while( (Pa_GetStreamTime( stream ) - startTime) < (NUM_SECONDS/2) ); /* Stop sound for 2 seconds. */ err = Pa_StopStream( stream ); if( err != paNoError ) goto error; printf("Pause for 2 seconds.\n"); fflush(stdout); Pa_Sleep( 2000 ); data.outTime = -1.0; /* mark time for callback as undefined */ err = Pa_StartStream( stream ); if( err != paNoError ) goto error; startTime = Pa_GetStreamTime( stream ); printf("Play until sound is finished.\n"); fflush(stdout); do { ReportStreamTime( stream, &data ); Pa_Sleep(100); } while( (Pa_GetStreamTime( stream ) - startTime) < (NUM_SECONDS/2) ); err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; Pa_Terminate(); printf("Test finished.\n"); return err; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }
int main(void) { PaStreamParameters outputParameters; PaStream *stream; PaError err; paTestData data; #ifdef __APPLE__ PaMacCoreStreamInfo macInfo; #endif int i; /* initialise sinusoidal wavetable */ for( i=0; i<TABLE_SIZE; i++ ) { data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); } data.left_phase = data.right_phase = 0; err = Pa_Initialize(); if( err != paNoError ) goto error; for( i=0; i<2; ++i ) { const float sr = i ? SAMPLE_RATE2 : SAMPLE_RATE1; printf("PortAudio Test: output sine wave. SR = %g, BufSize = %d\n", sr, FRAMES_PER_BUFFER); outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ if (outputParameters.device == paNoDevice) { fprintf(stderr,"Error: No default 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; /** setup host specific info */ #ifdef __APPLE__ PaMacCore_SetupStreamInfo( &macInfo, paMacCorePro ); outputParameters.hostApiSpecificStreamInfo = &macInfo; #else printf( "Hardware SR changing not being tested on this platform.\n" ); outputParameters.hostApiSpecificStreamInfo = NULL; #endif err = Pa_OpenStream( &stream, NULL, /* no input */ &outputParameters, sr, FRAMES_PER_BUFFER, paClipOff, /* we won't output out of range samples so don't bother clipping them */ patestCallback, &data ); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("Play for %d seconds.\n", NUM_SECONDS ); Pa_Sleep( NUM_SECONDS * 1000 ); err = Pa_StopStream( stream ); if( err != paNoError ) goto error; err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; } Pa_Terminate(); printf("Test finished.\n"); return err; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }