PaError TestRecording( paTestData *dataPtr ) { PaError err; int i; int lastIndex = 0; /* Open input stream if not already open. */ if( dataPtr->inputStream == NULL ) { /* Record some audio. */ err = Pa_OpenStream( &dataPtr->inputStream, Pa_GetDefaultInputDeviceID(), dataPtr->samplesPerFrame, /* stereo input */ PA_SAMPLE_TYPE, NULL, paNoDevice, 0, PA_SAMPLE_TYPE, NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, /* frames per buffer */ NUM_REC_BUFS, /* number of buffers, if zero then use default minimum */ paClipOff, /* we won't output out of range samples so don't bother clipping them */ recordCallback, dataPtr ); if( err != paNoError ) goto error; } dataPtr->frameIndex = 0; err = Pa_StartStream( dataPtr->inputStream ); if( err != paNoError ) goto error; printf("Now recording!\n"); fflush(stdout); for( i=0; i<(NUM_SECONDS*1000/SLEEP_DUR_MSEC); i++ ) { int frameIndex, delta; Pa_Sleep(SLEEP_DUR_MSEC); frameIndex = dataPtr->frameIndex; if( Pa_StreamActive( dataPtr->inputStream ) <= 0) { printf("Stream inactive!\n"); break; } if( dataPtr->maxFrameIndex <= frameIndex ) { printf("Buffer recording complete.\n"); break; } delta = frameIndex - lastIndex; lastIndex = frameIndex; printf("index = %d, delta = %d\n", frameIndex, delta ); fflush(stdout); } err = Pa_StopStream( dataPtr->inputStream ); if( err != paNoError ) goto error; printf("Done.\n"); fflush(stdout); error: return err; }
int TestStopMode( paTestData *data ) { PortAudioStream *stream; PaError err; data->done = 0; data->phase = 0.0; data->frameCounter = 0; data->noteCounter = 0; data->repeatCounter = 0; data->phase_increment = data->tune[data->noteCounter]; 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, OUTPUT_DEVICE, 2, /* stereo output */ 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; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; if( data->stopMode == MODE_FINISH ) { while( Pa_StreamActive( stream ) ) { /*printf("outTime = %g, note# = %d, repeat# = %d\n", data->outTime, data->noteCounter, data->repeatCounter ); fflush(stdout); /**/ Pa_Sleep( SLEEP_DUR ); } } else { while( data->repeatCounter < MAX_REPEATS ) { /*printf("outTime = %g, note# = %d, repeat# = %d\n", data->outTime, data->noteCounter, data->repeatCounter ); fflush(stdout); /**/ Pa_Sleep( SLEEP_DUR ); } } if( data->stopMode == MODE_ABORT ) { printf("Call Pa_AbortStream()\n"); err = Pa_AbortStream( stream ); } else { printf("Call Pa_StopStream()\n"); err = Pa_StopStream( stream ); } if( err != paNoError ) goto error; printf("Call Pa_CloseStream()\n"); fflush(stdout); 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; int safeSineCount, stressedSineCount; int safeUnderflowCount, stressedUnderflowCount; paTestData data = {0}; double load; printf("PortAudio Test: output sine waves, count underflows. SR = %d, BufSize = %d. MAX_LOAD = %f\n", SAMPLE_RATE, FRAMES_PER_BUFFER, (float)MAX_LOAD ); 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 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("Establishing load conditions...\n" ); /* Determine number of sines required to get to 50% */ do { Pa_Sleep( 100 ); load = Pa_GetStreamCpuLoad( stream ); printf("sineCount = %d, CPU load = %f\n", data.sineCount, load ); if( load < 0.3 ) { data.sineCount += 10; } else if( load < 0.4 ) { data.sineCount += 2; } else { data.sineCount += 1; } } while( load < 0.5 && data.sineCount < (MAX_SINES-1)); safeSineCount = data.sineCount; /* Calculate target stress value then ramp up to that level*/ stressedSineCount = (int) (2.0 * data.sineCount * MAX_LOAD ); if( stressedSineCount > MAX_SINES ) stressedSineCount = MAX_SINES; for( ; data.sineCount < stressedSineCount; data.sineCount+=4 ) { Pa_Sleep( 100 ); load = Pa_GetStreamCpuLoad( stream ); printf("STRESSING: sineCount = %d, CPU load = %f\n", data.sineCount, load ); } printf("Counting underflows for 5 seconds.\n"); data.countUnderflows = 1; Pa_Sleep( 5000 ); stressedUnderflowCount = data.outputUnderflowCount; data.countUnderflows = 0; data.sineCount = safeSineCount; printf("Resuming safe load...\n"); Pa_Sleep( 1500 ); data.outputUnderflowCount = 0; Pa_Sleep( 1500 ); load = Pa_GetStreamCpuLoad( stream ); printf("sineCount = %d, CPU load = %f\n", data.sineCount, load ); printf("Counting underflows for 5 seconds.\n"); data.countUnderflows = 1; Pa_Sleep( 5000 ); safeUnderflowCount = data.outputUnderflowCount; printf("Stop stream.\n"); err = Pa_StopStream( stream ); if( err != paNoError ) goto error; err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; Pa_Terminate(); if( stressedUnderflowCount == 0 ) printf("Test failed, no output underflows detected under stress.\n"); else if( safeUnderflowCount != 0 ) printf("Test failed, %d unexpected underflows detected under safe load.\n", safeUnderflowCount); else printf("Test passed, %d expected output underflows detected under stress, 0 unexpected underflows detected under safe load.\n", stressedUnderflowCount ); 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; 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(); 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; 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; 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; } 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; }
// thread execution starts here void *AudioThread::Entry() { PaError err; err = Pa_OpenStream( &mPortStream, paNoDevice,/* default input device */ 0, /* no input */ paInt16, /* 32 bit floating point input */ NULL, Pa_GetDefaultOutputDeviceID(), /* default output device */ 2, /* stereo output */ paInt16, /* 32 bit floating point output */ NULL, mRate, 2, 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 */ AudioThread::CallbackWrapper, this ); if( err != paNoError ) { WriteText(wxT("There was a PortAudio Error on start\n")); Pa_CloseStream( mPortStream ); return NULL; } while(true) { if ( TestDestroy() ) { WriteText("Audio Thread Destroyed\n"); break; } if (mStatus == doPlay) { PaError err; err = Pa_StartStream( mPortStream ); if( err != paNoError ) { WriteText(wxT("There was a PortAudio Error on start\n")); break; } else mStatus = Playing; } if (mStatus == doPause) { PaError err; err = Pa_StopStream( mPortStream ); if( err != paNoError ) { WriteText(wxT("There was a PortAudio Error on stop\n")); break; } else mStatus = Paused; } wxThread::Sleep(500); } Pa_AbortStream( mPortStream ); Pa_CloseStream( mPortStream ); return NULL; }
void free_portaudio(PaStream *audio_stream) { Pa_StopStream(audio_stream); Pa_Terminate(); }
int main( int argc, char **argv ) { PaStreamParameters outputParameters; PaStream *stream; PaError err; paTestData data; int go; int outLatency = 0; int minLatency = DEFAULT_BUFFER_SIZE * 2; int framesPerBuffer; double sampleRate = 44100.0; char str[256]; printf("pa_minlat - Determine minimum latency for your computer.\n"); printf(" usage: pa_minlat {userBufferSize}\n"); printf(" for example: pa_minlat 64\n"); printf("Adjust your stereo until you hear a smooth tone in each speaker.\n"); printf("Then try to find the smallest number of frames that still sounds smooth.\n"); printf("Note that the sound will stop momentarily when you change the number of buffers.\n"); /* Get bufferSize from command line. */ framesPerBuffer = ( argc > 1 ) ? atol( argv[1] ) : DEFAULT_BUFFER_SIZE; printf("Frames per buffer = %d\n", framesPerBuffer ); data.left_phase = data.right_phase = 0.0; err = Pa_Initialize(); if( err != paNoError ) goto error; outLatency = sampleRate * 200.0 / 1000.0; /* 200 msec. */ /* Try different numBuffers in a loop. */ go = 1; while( go ) { outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */ outputParameters.channelCount = 2; /* Stereo output */ outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output. */ outputParameters.suggestedLatency = (double)outLatency / sampleRate; /* In seconds. */ outputParameters.hostApiSpecificStreamInfo = NULL; printf("Latency = %d frames = %6.1f msec.\n", outLatency, outputParameters.suggestedLatency * 1000.0 ); err = Pa_OpenStream( &stream, NULL, /* no input */ &outputParameters, sampleRate, framesPerBuffer, paClipOff, /* we won't output out of range samples so don't bother clipping them */ paminlatCallback, &data ); if( err != paNoError ) goto error; if( stream == NULL ) goto error; /* Start audio. */ err = Pa_StartStream( stream ); if( err != paNoError ) goto error; /* Ask user for a new nlatency. */ printf("\nMove windows around to see if the sound glitches.\n"); printf("Latency now %d, enter new number of frames, or 'q' to quit: ", outLatency ); gets( str ); if( str[0] == 'q' ) go = 0; else { outLatency = atol( str ); if( outLatency < minLatency ) { printf( "Latency below minimum of %d! Set to minimum!!!\n", minLatency ); outLatency = minLatency; } } /* Stop sound until ENTER hit. */ err = Pa_StopStream( stream ); if( err != paNoError ) goto error; err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; } printf("A good setting for latency would be somewhat higher than\n"); printf("the minimum latency that worked.\n"); printf("PortAudio: Test finished.\n"); Pa_Terminate(); 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; }
Main() { mScale = 0.1f; mOffset = 1.0f; mDataOffset = 0; isClicked = false; screen = Vec2<unsigned int>(800, 600); std::setlocale(LC_ALL, "en_US.UTF-8"); glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); GLFWwindow* window = glfwCreateWindow(screen.x, screen.y, "test", nullptr, nullptr); if (window == nullptr) { printf("cant create window"); return; } glfwSetWindowSizeCallback(window, windowSizeCallback); glfwSetKeyCallback(window, keyCallback); glfwSetMouseButtonCallback(window, clickCallback); glfwSetCursorPosCallback(window, mouseCallback); glfwMakeContextCurrent(window); glewExperimental = true; glewInit(); int tmp; glGetIntegerv(GL_MAX_ELEMENTS_VERTICES , &tmp); std::cout << "GL_MAX_ELEMENTS_VERTICES: " << tmp << std::endl; int err = Pa_Initialize(); if (err != paNoError) printf("error"); int num = Pa_GetDeviceCount(); const PaDeviceInfo* devInfo; const PaHostApiInfo* apiInfo; for (int i = 0; i < num; ++i) { devInfo = Pa_GetDeviceInfo(i); apiInfo = Pa_GetHostApiInfo(devInfo->hostApi); printf("%i, %s on %s\n", i, devInfo->name, apiInfo->name); } float sampleRate = 44100.0f; double t = glfwGetTime(); Kern k(sampleRate, 12, 4 * 16.352f, sampleRate / 2); BlockMatrix<std::complex<double>> b(k.K, k.mN0, k.mB, 0.01); mAudioData = new double[b.getWidth()]; mAudioLength = b.getWidth(); for (unsigned int i = 0; i < mAudioLength; ++i) { mAudioData[i] = wave(55, sampleRate, i) + wave(110, sampleRate, i) + wave(220, sampleRate, i) + wave(440, sampleRate, i) + wave(880, sampleRate, i) + wave(1760, sampleRate, i) + wave(3520, sampleRate, i) + wave(7040, sampleRate, i); } printf("kernel time:%f\n", glfwGetTime() - t); float drawArray[k.mB * 2]; std::complex<double> out[k.mB]; CQT::transform(mAudioData, out, b, mAudioLength); t = glfwGetTime(); printf("transform time:%f\n", glfwGetTime() - t); //glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); glDebugMessageCallback(debugCallback, nullptr); //glEnable(GL_DEBUG_OUTPUT); printf("%s\n", glGetString(GL_VERSION)); Shader fs("res/shader/fragment.c", true, GL_FRAGMENT_SHADER); Shader vs("res/shader/vertex.c", true, GL_VERTEX_SHADER); Program* p = new Program(); p->attach(fs); p->attach(vs); p->build(); p->use(); Program p2; Shader fs2("res/shader/fragment2.c", true, GL_FRAGMENT_SHADER); Shader vs2("res/shader/vertex2.c", true, GL_VERTEX_SHADER); p2.attach(fs2); p2.attach(vs2); p2.build(); p2.use(); int uniformData = p2.getUniformLocation("data"); unsigned int waterfallSize = 512; tm = new TextureManager(); unsigned int waterfallTexture; unsigned int waterfallId = tm->getFreeTexture(); glGenTextures(1, &waterfallTexture); glActiveTexture(GL_TEXTURE0 + waterfallId); glBindTexture( GL_TEXTURE0, waterfallTexture ); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); unsigned char* textureTmp = new unsigned char[waterfallSize * b.getWidth()]; glTexImage2D( GL_TEXTURE_2D_ARRAY, 0, GL_R8, b.getWidth(), waterfallSize, 0, GL_RED, GL_UNSIGNED_BYTE, textureTmp); delete textureTmp; float max = 0; for (unsigned int i = 0; i < k.mB; ++i) { drawArray[2 * i + 0] = (float)i / k.mB * 2.0f - 1.0f; float tmp = std::abs(out[i]); drawArray[2 * i + 1] = tmp; max = std::max(tmp, max); } font = new Font(512, "res/font/DroidSans.woff", 32, tm); print = new Print(font); //print.set(&font, "res/shader/fontVertex.c", "res/shader/fontFragment.c"); print->setScreenSize(screen); glm::vec2* vert = new glm::vec2[1024]; glm::vec2* debug = new glm::vec2[b.getWidth()]; for (unsigned int i = 0; i < b.getWidth(); ++i) { debug[i].x = (float)i / b.getWidth() * 2.0f - 1.0f; } uint32_t vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); uint32_t vbo[2]; glGenBuffers(1, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glEnableVertexAttribArray(0); glBufferData(GL_ARRAY_BUFFER, k.mB * sizeof(glm::vec2), drawArray, GL_DYNAMIC_DRAW); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glEnable(GL_BLEND); glfwSetWindowUserPointer(window, this); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); double time, timeo; glfwSwapInterval(1); PaStream* stream; PaStreamParameters params; params.device = 21; params.channelCount = 1; params.sampleFormat = paFloat32; params.hostApiSpecificStreamInfo = nullptr; params.suggestedLatency = 0.5; err = Pa_OpenStream(&stream, ¶ms, nullptr, sampleRate, paFramesPerBufferUnspecified, 0, paCallback, this); if (err != paNoError) printf("error %i", err); Pa_StartStream(stream); while(!glfwWindowShouldClose(window)) { timeo = time; time = glfwGetTime(); CQT::transform(mAudioData, out, b, mAudioLength); max = 0.0f; for (unsigned int i = 0; i < k.mB; ++i) { drawArray[2 * i + 0] = (float)i / k.mB * 2.0f - 1.0f; float tmp = std::abs(out[i]); drawArray[2 * i + 1] = tmp; max = std::max(tmp, max); } for (unsigned int i = 0; i < k.mB; ++i) { drawArray[2 * i + 1] = std::log(drawArray[2 * i +1]) * mScale + mOffset; } //printf("%f\n", drawArray[1]); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBufferData(GL_ARRAY_BUFFER, k.mB * sizeof(glm::vec2), drawArray, GL_DYNAMIC_DRAW); p->use(); glDrawArrays(GL_LINE_STRIP, 0, k.mB); for (unsigned int i = 0; i < b.getWidth(); ++i) { debug[i].y = mAudioData[i] / 15.0; } glBufferData(GL_ARRAY_BUFFER, b.getWidth() * sizeof(glm::vec2), debug, GL_DYNAMIC_DRAW); glDrawArrays(GL_LINE_STRIP, 0, b.getWidth()); print->printfAt(-300.0f, 100.0f, 16.0f, 16.0f, u8"Fps:%03.3f", 1/(time-timeo)); glfwSwapBuffers(window); glClear(GL_COLOR_BUFFER_BIT); glfwPollEvents(); } Pa_StopStream(stream); Pa_CloseStream(stream); Pa_Terminate(); std::cout << "Hello World. I'm Peach." << std::endl; }
int main(int argc, char* argv[]) { PaStreamParameters outputParameters; PaWinMmeStreamInfo wmmeStreamInfo; PaStream *stream; PaError err; paTestData data; int deviceIndex; FILE *fp; const char *fileName = "c:\\test_48k.ac3.spdif"; data.buffer = NULL; printf("usage: patest_wmme_ac3 fileName [paDeviceIndex]\n"); printf("**IMPORTANT*** The provided file must include the spdif preamble at the start of every AC-3 frame. Using a normal ac3 file won't work.\n"); printf("PortAudio Test: output a raw spdif ac3 stream. SR = %d, BufSize = %d, Chans = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER, CHANNEL_COUNT); if( argc >= 2 ) fileName = argv[1]; printf( "reading spdif ac3 raw stream file %s\n", fileName ); fp = fopen( fileName, "rb" ); if( !fp ){ fprintf( stderr, "error opening spdif ac3 file.\n" ); return -1; } /* get file size */ fseek( fp, 0, SEEK_END ); data.bufferSampleCount = ftell( fp ) / sizeof(short); fseek( fp, 0, SEEK_SET ); /* allocate buffer, read the whole file into memory */ data.buffer = (short*)malloc( data.bufferSampleCount * sizeof(short) ); if( !data.buffer ){ fprintf( stderr, "error allocating buffer.\n" ); return -1; } fread( data.buffer, sizeof(short), data.bufferSampleCount, fp ); fclose( fp ); data.playbackIndex = 0; err = Pa_Initialize(); if( err != paNoError ) goto error; deviceIndex = Pa_GetHostApiInfo( Pa_HostApiTypeIdToHostApiIndex( paMME ) )->defaultOutputDevice; if( argc >= 3 ){ sscanf( argv[1], "%d", &deviceIndex ); } printf( "using device id %d (%s)\n", deviceIndex, Pa_GetDeviceInfo(deviceIndex)->name ); outputParameters.device = deviceIndex; outputParameters.channelCount = CHANNEL_COUNT; outputParameters.sampleFormat = paInt16; /* IMPORTANT must use paInt16 for WMME AC3 */ outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; wmmeStreamInfo.size = sizeof(PaWinMmeStreamInfo); wmmeStreamInfo.hostApiType = paMME; wmmeStreamInfo.version = 1; wmmeStreamInfo.flags = paWinMmeWaveFormatDolbyAc3Spdif; outputParameters.hostApiSpecificStreamInfo = &wmmeStreamInfo; if( Pa_IsFormatSupported( 0, &outputParameters, SAMPLE_RATE ) == paFormatIsSupported ){ printf( "Pa_IsFormatSupported reports device will support %d channels.\n", CHANNEL_COUNT ); }else{ printf( "Pa_IsFormatSupported reports device will not support %d channels.\n", CHANNEL_COUNT ); } err = Pa_OpenStream( &stream, NULL, /* no input */ &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, 0, 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(); free( data.buffer ); printf("Test finished.\n"); return err; error: Pa_Terminate(); free( data.buffer ); 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; }
static PaCtx * pa_phone_setup(void) { PaCtx *ctx; int err, i, srcerr; PaError err2; err = paNoError; err2 = 0; if ((ctx = calloc(1, sizeof(PaCtx))) == NULL) { WHY("Unable to allocate PA context"); err2 = 1; goto error; } /* Init mutex */ if (pthread_mutex_init(&ctx->mtx, NULL) != 0) { WHYF("Unable to init mutex: %s\n", strerror(errno)); err2 = 1; goto error; } /* Allocate FIFOs */ i = IN_FRAMES * 10 * sizeof(int16_t); printf("Allocating %d byte FIFOs\n", i); if ((ctx->incoming = fifo_alloc(i)) == NULL) { WHY("Unable to allocate incoming FIFO\n"); err2 = 1; goto error; } if ((ctx->incrate = fifo_alloc(i)) == NULL) { WHY("Unable to allocate incoming SRC FIFO\n"); err2 = 1; goto error; } if ((ctx->outgoing = fifo_alloc(i)) == NULL) { WHY("Unable to allocate outgoing FIFO\n"); err2 = 1; goto error; } /* Init sample rate converter */ if ((ctx->src = src_new(SRC_SINC_BEST_QUALITY, 1, &srcerr)) == NULL) { WHYF("Unable to init sample rate converter: %d\n", srcerr); err2 = 1; goto error; } /* Init echo canceller */ if ((ctx->echocan = echo_can_init(ECHO_LEN, ADAPT_MODE)) == NULL) { WHY("Unable to init echo canceller\n"); err2 = 1; goto error; } /* Init codec2 */ if ((ctx->codec2 = codec2_create()) == NULL) { WHY("Unable to init codec2\n"); err2 = 1; goto error; } /* Initialize Port Audio library */ if ((err = Pa_Initialize()) != paNoError) goto error; /* Open an audio I/O stream. */ if ((err = Pa_OpenDefaultStream(&ctx->stream, 1, /* input channels */ 1, /* output channels */ paInt16, SAMPLE_RATE, IN_FRAMES, /* frames per buffer */ patestCallback, &ctx)) != paNoError) goto error; /* Start stream */ if ((err = Pa_StartStream(ctx->stream)) != paNoError) goto error; /* Close down stream, PA, etc */ /* XXX: hangs in pthread_join on Ubuntu 10.04 */ #ifndef linux if ((err = Pa_StopStream(ctx->stream)) != paNoError) goto error; #endif /* Do stuff */ if ((err = Pa_CloseStream(ctx->stream)) != paNoError) goto error; error: Pa_Terminate(); /* Free things */ freectx(ctx); if (err != paNoError) WHYF("Port audio error: %s\n", Pa_GetErrorText(err)); return NULL; }
int main(void) { int i; PaStream* stream; PaStreamParameters outputParameters; PaError err; paTestData data = {0}; double load; 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.sine[TABLE_SIZE] = data.sine[0]; /* set guard point */ 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 = 2; /* Stereo output. */ outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output. */ outputParameters.hostApiSpecificStreamInfo = NULL; outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device) ->defaultHighOutputLatency; err = Pa_OpenStream(&stream, NULL, /* no input */ &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, /* No out of range samples should occur. */ patestCallback, &data); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; /* Play an increasing number of sine waves until we hit MAX_USAGE */ do { data.numSines++; Pa_Sleep(200); load = Pa_GetStreamCpuLoad(stream); printf("numSines = %d, CPU load = %f\n", data.numSines, load ); fflush(stdout); } while((load < MAX_USAGE) && (data.numSines < MAX_SINES)); Pa_Sleep(2000); /* Stay for 2 seconds around 80% CPU. */ 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; int i; printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); data.phase = 0; for (i = 0 ; i < 12 ; i++) { data.amplitudes[i] = 0; data.states[i] = DECAY; } 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 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; sprintf( data.message, "No Message" ); err = Pa_SetStreamFinishedCallback( stream, &StreamFinished ); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; /* set up unbuffered reading */ struct termios tio; tcgetattr(1,&tio); tio.c_lflag &=(~ICANON & ~ECHO); tcsetattr(1,TCSANOW,&tio); int pressed = -1; while(1) { switch (getc(stdin)) { case 'C': pressed = 0; break; case 'd': pressed = 1; break; case 'D': pressed = 2; break; case 'e': pressed = 3; break; case 'E': pressed = 4; break; case 'F': pressed = 5; break; case 'g': pressed = 6; break; case 'G': pressed = 7; break; case 'a': pressed = 8; break; case 'A': pressed = 9; break; case 'b': pressed = 10; break; case 'B': pressed = 11; break; } if (pressed != -1) { data.states[pressed] = ATTACK; } } 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; 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; const PaDeviceInfo *device; PaWasapiDeviceRole role; int i; PaWasapiStreamInfo ws_info = { 0 }; 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; for (i = 0; i < Pa_GetDeviceCount(); ++i) { device = Pa_GetDeviceInfo(i); if (Pa_GetDeviceInfo(i)->maxInputChannels != 0) continue; if (Pa_GetDeviceInfo(i)->hostApi != Pa_HostApiTypeIdToHostApiIndex(paWASAPI)) continue; role = PaWasapi_GetDeviceRole(i); if (role == eRoleSpeakers) break; } if (role != eRoleSpeakers) { fprintf(stderr,"Error: No WASAPI Speakers.\n"); return -1; } outputParameters.device = i;//Pa_GetDefaultOutputDevice(); /* default output device */ ws_info.size = sizeof(ws_info); ws_info.hostApiType = paWASAPI; ws_info.version = 1; ws_info.flags = paWinWasapiExclusive; if (outputParameters.device == paNoDevice) { fprintf(stderr,"Error: No default output device.\n"); goto error; } outputParameters.channelCount = 2; outputParameters.sampleFormat = paInt16; outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; outputParameters.hostApiSpecificStreamInfo = &ws_info; err = Pa_OpenStream( &stream, NULL, /* no input */ &outputParameters, SAMPLE_RATE, paFramesPerBufferUnspecified/*FRAMES_PER_BUFFER*/, paClipOff|paPrimeOutputBuffersUsingStreamCallback, /* we won't output out of range samples so don't bother clipping them */ patestCallback, &data ); if( err != paNoError ) goto error; sprintf( data.message, "No Message" ); err = Pa_SetStreamFinishedCallback( stream, &StreamFinished ); 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 ); //while (1) { Pa_Sleep(2); } 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; paTestData DATA; int i; int totalSamps; 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; DATA.framesToGo = totalSamps = NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */ 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 */ 2, /* stereo output */ 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.outTime = -1.0; // mark time for callback as undefined err = Pa_StartStream( stream ); if( err != paNoError ) goto error; /* Watch until sound is halfway finished. */ printf("Play for %d seconds.\n", NUM_SECONDS/2 ); fflush(stdout); do { ReportStreamTime( stream, &DATA ); Pa_Sleep(100); } while( Pa_StreamTime( stream ) < (totalSamps/2) ); /* Stop sound until ENTER hit. */ 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; printf("Play until sound is finished.\n"); fflush(stdout); do { ReportStreamTime( stream, &DATA ); Pa_Sleep(100); } while( Pa_StreamActive( stream ) ); 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; 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 */ 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, /* we won't output out of range samples so don't bother clipping them */ patestCallback, &data ); if( err != paNoError ) goto error; sprintf( data.message, "No Message" ); err = Pa_SetStreamFinishedCallback( stream, &StreamFinished ); 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; }
bool stop() { mErrNum = paNoError; if(mIsRunning) mErrNum = Pa_StopStream(mStream); if(paNoError == mErrNum) mIsRunning = false; return paNoError == mErrNum; }
int main(void) { PortAudioStream *stream; PaError err; paTestData data; int i; int timeout; printf("Play different tone sine waves that alternate between left and right channel.\n"); printf("The low tone should be on the left channel.\n"); /* 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 = data.toggle = 0; data.countDown = SAMPLE_RATE; 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 */ 2, /* stereo 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; printf("Play for several seconds.\n"); timeout = NUM_SECONDS * 4; while( timeout > 0 ) { Pa_Sleep( 300 ); timeout -= 1; } 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, char* argv[]) { PaStreamParameters outputParameters; PaWinDirectSoundStreamInfo directSoundStreamInfo; PaStream *stream; PaError err; paTestData data; int i; int deviceIndex; printf("PortAudio Test: output a sine blip on each channel. SR = %d, BufSize = %d, Chans = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER, CHANNEL_COUNT); err = Pa_Initialize(); if( err != paNoError ) goto error; deviceIndex = Pa_GetHostApiInfo( Pa_HostApiTypeIdToHostApiIndex( paDirectSound ) )->defaultOutputDevice; if( argc == 2 ){ sscanf( argv[1], "%d", &deviceIndex ); } printf( "using device id %d (%s)\n", deviceIndex, Pa_GetDeviceInfo(deviceIndex)->name ); /* initialise sinusoidal wavetable */ for( i=0; i<TABLE_SIZE; i++ ) { data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); } data.phase = 0; data.currentChannel = 0; data.cycleCount = 0; outputParameters.device = deviceIndex; outputParameters.channelCount = CHANNEL_COUNT; outputParameters.sampleFormat = paFloat32; /* 32 bit floating point processing */ outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; /* it's not strictly necessary to provide a channelMask for surround sound output. But if you want to be sure which channel mask PortAudio will use then you should supply one */ directSoundStreamInfo.size = sizeof(PaWinDirectSoundStreamInfo); directSoundStreamInfo.hostApiType = paDirectSound; directSoundStreamInfo.version = 1; directSoundStreamInfo.flags = paWinDirectSoundUseChannelMask; directSoundStreamInfo.channelMask = PAWIN_SPEAKER_5POINT1; /* request 5.1 output format */ outputParameters.hostApiSpecificStreamInfo = &directSoundStreamInfo; if( Pa_IsFormatSupported( 0, &outputParameters, SAMPLE_RATE ) == paFormatIsSupported ){ printf( "Pa_IsFormatSupported reports device will support %d channels.\n", CHANNEL_COUNT ); }else{ printf( "Pa_IsFormatSupported reports device will not support %d channels.\n", CHANNEL_COUNT ); } 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; }
static void *output_thread(void *ptr) { int err; int output_buffer_size; #ifndef PORTAUDIO_DEV int num_mixers, nbVolumes, volumeIdx; #endif struct timeval now; struct timespec timeout; slimaudio_t *audio = (slimaudio_t *) ptr; audio->output_STMs = false; audio->output_STMu = false; err = Pa_Initialize(); if (err != paNoError) { printf("PortAudio error4: %s Could not open any audio devices.\n", Pa_GetErrorText(err) ); exit(-1); } if ( audio->renice ) if ( slimproto_renice_thread (-5) ) /* Increase priority */ fprintf(stderr, "output_thread: renice failed. Got Root?\n"); #ifndef PORTAUDIO_DEV DEBUGF("output_thread: output_device_id : %i\n", audio->output_device_id ); DEBUGF("output_thread: pa_framesPerBuffer: %lu\n", pa_framesPerBuffer ); DEBUGF("output_thread: pa_numberOfBuffers: %lu\n", pa_numberOfBuffers ); err = Pa_OpenStream( &audio->pa_stream, /* stream */ paNoDevice, /* input device */ 0, /* input channels */ 0, /* input sample format */ NULL, /* input driver info */ audio->output_device_id,/* output device */ 2, /* output channels */ paInt16, /* output sample format */ NULL, /* output driver info */ 44100.0, /* sample rate */ pa_framesPerBuffer, /* frames per buffer */ pa_numberOfBuffers, /* number of buffers */ paNoFlag, /* stream flags */ pa_callback, /* callback */ audio); /* user data */ #else PaStreamParameters outputParameters; const PaDeviceInfo * paDeviceInfo; float newLatency; #ifdef PADEV_WASAPI PaWasapiStreamInfo streamInfo; const PaHostApiInfo *paHostApiInfo; #endif paDeviceInfo = Pa_GetDeviceInfo(audio->output_device_id); /* Device is not stereo or better, abort */ if (paDeviceInfo->maxOutputChannels < 2) { printf("output_thread: PortAudio device does not support 44.1KHz, 16-bit, stereo audio.\n"); printf("output_thread: Use -L for a list of supported audio devices, then use -o followed\n"); printf("output_thread: by the device number listed before the colon. See -h for details.\n"); exit(-2); } outputParameters.device = audio->output_device_id; #ifdef SLIMPROTO_ZONES outputParameters.channelCount = 2 * audio->output_num_zones; #else outputParameters.channelCount = 2; #endif outputParameters.sampleFormat = paInt16; outputParameters.suggestedLatency = paDeviceInfo->defaultHighOutputLatency; if ( audio->modify_latency ) { newLatency = (float) audio->user_latency / 1000.0; if ( ( newLatency > 1.0 ) || ( newLatency <= paDeviceInfo->defaultLowOutputLatency ) ) { fprintf (stderr, "User defined latency %f out of range %f-1.0, using default.\n", newLatency, paDeviceInfo->defaultLowOutputLatency ); newLatency = paDeviceInfo->defaultHighOutputLatency; } outputParameters.suggestedLatency = newLatency ; } #ifdef PADEV_WASAPI /* Use exclusive mode for WASAPI device, default is shared */ paHostApiInfo = Pa_GetHostApiInfo ( paDeviceInfo->hostApi ); if ( paHostApiInfo != NULL ) { if ( paHostApiInfo->type == paWASAPI ) { /* Use exclusive mode for WasApi device, default is shared */ if (wasapi_exclusive) { streamInfo.size = sizeof(PaWasapiStreamInfo); streamInfo.hostApiType = paWASAPI; streamInfo.version = 1; streamInfo.flags = paWinWasapiExclusive; outputParameters.hostApiSpecificStreamInfo = &streamInfo; DEBUGF("WASAPI: Exclusive\n"); } else { outputParameters.hostApiSpecificStreamInfo = NULL; DEBUGF("WASAPI: Shared\n"); } } } #else outputParameters.hostApiSpecificStreamInfo = NULL; #endif DEBUGF("paDeviceInfo->deviceid %d\n", outputParameters.device); DEBUGF("paDeviceInfo->maxOutputChannels %i\n", paDeviceInfo->maxOutputChannels); DEBUGF("outputParameters.suggestedLatency %f\n", outputParameters.suggestedLatency); DEBUGF("paDeviceInfo->defaultHighOutputLatency %f\n", (float) paDeviceInfo->defaultHighOutputLatency); DEBUGF("paDeviceInfo->defaultLowOutputLatency %f\n", (float) paDeviceInfo->defaultLowOutputLatency); DEBUGF("paDeviceInfo->defaultSampleRate %f\n", paDeviceInfo->defaultSampleRate); err = Pa_OpenStream ( &audio->pa_stream, /* stream */ NULL, /* inputParameters */ &outputParameters, /* outputParameters */ 44100.0, /* sample rate */ paFramesPerBufferUnspecified, /* framesPerBuffer */ paPrimeOutputBuffersUsingStreamCallback, /* streamFlags */ pa_callback, /* streamCallback */ audio); /* userData */ #endif #ifdef BSD_THREAD_LOCKING pthread_mutex_lock(&audio->output_mutex); #endif if (err != paNoError) { printf("output_thread: PortAudio error1: %s\n", Pa_GetErrorText(err) ); exit(-1); } #ifndef PORTAUDIO_DEV num_mixers = Px_GetNumMixers(audio->pa_stream); while (--num_mixers >= 0) { DEBUGF("Mixer: %s\n", Px_GetMixerName(audio->pa_stream, num_mixers)); } if (audio->volume_control == VOLUME_DRIVER) { DEBUGF("Opening mixer.\n" ); audio->px_mixer = Px_OpenMixer(audio->pa_stream, 0); } if (audio->px_mixer != NULL) { DEBUGF("Px_mixer = %p\n", audio->px_mixer); DEBUGF("PCM volume supported: %d.\n", Px_SupportsPCMOutputVolume(audio->px_mixer)); nbVolumes = Px_GetNumOutputVolumes(audio->px_mixer); DEBUGF("Nb volumes supported: %d.\n", nbVolumes); for (volumeIdx=0; volumeIdx<nbVolumes; ++volumeIdx) { DEBUGF("Volume %d: %s\n", volumeIdx, Px_GetOutputVolumeName(audio->px_mixer, volumeIdx)); } } #endif while (audio->output_state != QUIT) { switch (audio->output_state) { case STOPPED: audio->decode_num_tracks_started = 0L; audio->stream_samples = 0UL; audio->pa_streamtime_offset = audio->stream_samples; DEBUGF("output_thread STOPPED: %llu\n",audio->pa_streamtime_offset); slimaudio_buffer_set_readopt(audio->output_buffer, BUFFER_BLOCKING); case PAUSED: /* We report ourselves to the server every few seconds ** as a keep-alive. This is required for Squeezebox Server ** v6.5.x although technically, "stat" is not a valid event ** code for the STAT Client->Server message. This was ** lifted by observing how a Squeezebox3 reports itself to ** the server using Squeezebox Server's d_slimproto and ** d_slimproto_v tracing services. Note that Squeezebox3 ** seems to report every 1 second or so, but the server only ** drops the connection after 15-20 seconds of inactivity. */ DEBUGF("output_thread PAUSED: %llu\n",audio->pa_streamtime_offset); if (audio->keepalive_interval <= 0) { pthread_cond_wait(&audio->output_cond, &audio->output_mutex); } else { gettimeofday(&now, NULL); timeout.tv_sec = now.tv_sec + audio->keepalive_interval; timeout.tv_nsec = now.tv_usec * 1000; err = pthread_cond_timedwait(&audio->output_cond, &audio->output_mutex, &timeout); if (err == ETIMEDOUT) { DEBUGF("Sending keepalive. Interval=%ds.\n", audio->keepalive_interval); output_thread_stat(audio, "stat"); } } break; case PLAY: audio->output_predelay_frames = audio->output_predelay_msec * 44.100; DEBUGF("output_thread PLAY: output_predelay_frames: %i\n", audio->output_predelay_frames); output_buffer_size = slimaudio_buffer_available(audio->output_buffer); DEBUGF("output_thread BUFFERING: output_buffer_size: %i output_threshold: %i", output_buffer_size, audio->output_threshold); DEBUGF(" buffering_timeout: %i\n", audio->buffering_timeout); if ( (output_buffer_size < audio->output_threshold) && (audio->buffering_timeout > 0) ) { pthread_mutex_unlock(&audio->output_mutex); pthread_cond_broadcast(&audio->output_cond); Pa_Sleep(100); pthread_mutex_lock(&audio->output_mutex); audio->buffering_timeout--; } else { DEBUGF("output_thread PLAY: start stream: %llu\n", audio->pa_streamtime_offset); audio->buffering_timeout = BUFFERING_TIMEOUT; err = Pa_StartStream(audio->pa_stream); if (err != paNoError) { printf("output_thread: PortAudio error2: %s\n", Pa_GetErrorText(err)); exit(-1); } audio->output_state = PLAYING; pthread_cond_broadcast(&audio->output_cond); } break; case BUFFERING: DEBUGF("output_thread BUFFERING: %llu\n",audio->pa_streamtime_offset); case PLAYING: gettimeofday(&now, NULL); timeout.tv_sec = now.tv_sec + 1; timeout.tv_nsec = now.tv_usec * 1000; err = pthread_cond_timedwait(&audio->output_cond, &audio->output_mutex, &timeout); if (err == ETIMEDOUT) { DEBUGF("output_thread ETIMEDOUT-PLAYING: %llu\n",audio->pa_streamtime_offset); output_thread_stat(audio, "STMt"); } /* Track started */ if (audio->output_STMs) { audio->output_STMs = false; audio->decode_num_tracks_started++; audio->replay_gain = audio->start_replay_gain; slimaudio_output_vol_adjust(audio); audio->pa_streamtime_offset = audio->stream_samples; DEBUGF("output_thread STMs-PLAYING: %llu\n",audio->pa_streamtime_offset); output_thread_stat(audio, "STMs"); } /* Data underrun ** On buffer underrun causes the server to switch to the next track. */ if (audio->output_STMu) { audio->output_STMu = false; audio->output_state = STOP; DEBUGF("output_thread STMu-PLAYING: %llu\n",audio->pa_streamtime_offset); output_thread_stat(audio, "STMu"); pthread_cond_broadcast(&audio->output_cond); } break; case STOP: #ifndef PORTAUDIO_DEV if ( (err = Pa_StreamActive(audio->pa_stream) ) > 0) { err = Pa_StopStream(audio->pa_stream); if (err != paNoError) { printf("output_thread: PortAudio error3: %s\n", Pa_GetErrorText(err) ); exit(-1); } } else { if ( err != paNoError) { printf("output_thread: PortAudio error9: %s\n", Pa_GetErrorText(err) ); exit(-1); } } #else if ( (err = Pa_IsStreamActive(audio->pa_stream)) > 0) { err = Pa_StopStream(audio->pa_stream); if (err != paNoError) { printf("output_thread[STOP]: PortAudio error3: %s\n", Pa_GetErrorText(err) ); exit(-1); } } else if ( err != paNoError) { printf("output_thread[STOP ISACTIVE]: PortAudio error3: %s\n", Pa_GetErrorText(err) ); exit(-1); } #endif audio->output_state = STOPPED; DEBUGF("output_thread STOP: %llu\n",audio->pa_streamtime_offset); pthread_cond_broadcast(&audio->output_cond); break; case PAUSE: #ifndef PORTAUDIO_DEV if ( (err = Pa_StreamActive(audio->pa_stream) ) > 0) { err = Pa_StopStream(audio->pa_stream); if (err != paNoError) { printf("output_thread: PortAudio error10: %s\n", Pa_GetErrorText(err)); exit(-1); } } else { if ( err != paNoError) { printf("output_thread: PortAudio error11: %s\n", Pa_GetErrorText(err) ); exit(-1); } } #else if ( (err = Pa_IsStreamActive(audio->pa_stream)) > 0) { err = Pa_StopStream(audio->pa_stream); if (err != paNoError) { printf("output_thread[PAUSE]: PortAudio error3: %s\n", Pa_GetErrorText(err) ); exit(-1); } } else if ( err != paNoError) { printf("output_thread[PAUSE ISACTIVE]: PortAudio error3: %s\n", Pa_GetErrorText(err) ); exit(-1); } #endif audio->output_state = PAUSED; DEBUGF("output_thread PAUSE: %llu\n",audio->pa_streamtime_offset); pthread_cond_broadcast(&audio->output_cond); break; case QUIT: DEBUGF("output_thread QUIT: %llu\n",audio->pa_streamtime_offset); break; } } pthread_mutex_unlock(&audio->output_mutex); #ifndef PORTAUDIO_DEV if (audio->px_mixer != NULL) { Px_CloseMixer(audio->px_mixer); audio->px_mixer = NULL; } #endif err = Pa_CloseStream(audio->pa_stream); if (err != paNoError) { printf("output_thread[exit]: PortAudio error3: %s\n", Pa_GetErrorText(err) ); exit(-1); } audio->pa_stream = NULL; Pa_Terminate(); DEBUGF("output_thread: PortAudio terminated\n"); return 0; }
int main(void) { PaStreamParameters outputParameters; PaStream *stream; PaError err; int numStress; paTestData data = {0}; double load; printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d. MAX_LOAD = %f\n", SAMPLE_RATE, FRAMES_PER_BUFFER, MAX_LOAD ); 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 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; /* Determine number of sines required to get to 50% */ do { data.numSines++; Pa_Sleep( 100 ); load = Pa_GetStreamCpuLoad( stream ); printf("numSines = %d, CPU load = %f\n", data.numSines, load ); } while( load < 0.5 ); /* Calculate target stress value then ramp up to that level*/ numStress = (int) (2.0 * data.numSines * MAX_LOAD ); for( ; data.numSines < numStress; data.numSines++ ) { Pa_Sleep( 200 ); load = Pa_GetStreamCpuLoad( stream ); printf("STRESSING: numSines = %d, CPU load = %f\n", data.numSines, load ); } printf("Suffer for 5 seconds.\n"); Pa_Sleep( 5000 ); printf("Stop stream.\n"); 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) { 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) { PaStream *stream; PaStreamParameters outputParameters; PaError err; paTestData data; int i; int timeout; printf("Play different tone sine waves that alternate between left and right channel.\n"); printf("The low tone should be on the left channel.\n"); /* 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 = data.toggle = 0; data.countDown = SAMPLE_RATE; 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, /* As above. */ 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 several seconds.\n"); timeout = NUM_SECONDS * 4; while( timeout > 0 ) { Pa_Sleep( 300 ); /*(Irix very much likes sleeps <= 1000 ms.)*/ timeout -= 1; } 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, char* argv[]){ char* host = "127.0.0.1"; if (argc != 2){ std::cerr << "Usage: client <host>" << std::endl; return 1; }else{ host = argv[1]; } PaStreamParameters outputParameters; PaStream *stream; PaError err; sAudioBuffer* audiobuf; audiobuf = new sAudioBuffer(); audiobuf->dec = opus_decoder_create(SAMPLE_RATE, 2, &audiobuf->error); if(audiobuf->error != OPUS_OK){ std::cerr << "opus: could not create decoder" << std::endl; return 2; } printf("SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER); audiobuf->readerIndex = 0; 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; outputParameters.sampleFormat = paFloat32; outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; err = Pa_OpenStream( &stream, NULL, &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, audioCallback, audiobuf); if( err != paNoError ) goto error; err = Pa_SetStreamFinishedCallback( stream, &StreamFinished ); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; try{ serveClient(host, audiobuf); }catch (std::exception& e){ std::cerr << e.what() << std::endl; } err = Pa_StopStream( stream ); if( err != paNoError ) goto error; err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; Pa_Terminate(); delete audiobuf; 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 err; }
int main(void) { PaError err; PortAudioStream *stream1; PortAudioStream *stream2; paTestData data1 = {0}; paTestData data2 = {0}; printf("PortAudio Test: two rates.\n" ); err = Pa_Initialize(); if( err != paNoError ) goto error; /* Start first stream. **********************/ err = Pa_OpenStream( &stream1, paNoDevice, /* default input device */ 0, /* no input */ paFloat32, /* 32 bit floating point input */ NULL, OUTPUT_DEVICE, 2, /* Stereo */ paFloat32, /* 32 bit floating point output */ NULL, SAMPLE_RATE_1, 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, &data1 ); if( err != paNoError ) goto error; err = Pa_StartStream( stream1 ); if( err != paNoError ) goto error; Pa_Sleep( 3 * 1000 ); /* Start second stream. **********************/ err = Pa_OpenStream( &stream2, paNoDevice, /* default input device */ 0, /* no input */ paFloat32, /* 32 bit floating point input */ NULL, OUTPUT_DEVICE, 2, /* Stereo */ paFloat32, /* 32 bit floating point output */ NULL, SAMPLE_RATE_2, 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, &data2 ); if( err != paNoError ) goto error; err = Pa_StartStream( stream2 ); if( err != paNoError ) goto error; Pa_Sleep( 3 * 1000 ); err = Pa_StopStream( stream2 ); if( err != paNoError ) goto error; Pa_Sleep( 3 * 1000 ); err = Pa_StopStream( stream1 ); if( err != paNoError ) goto error; Pa_CloseStream( stream2 ); Pa_CloseStream( stream1 ); 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; 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) { srand(time(NULL)); create_list(MUSIC_DIR); max_list = list.size(); PaStreamParameters outputParameters; PaStream *stream; PaError e; signal(SIGINT, signal_catch); /* pthread_t thread; pthread_attr_t thread_attr; pthread_attr_init(&thread_attr); pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); if(pthread_create(&thread, &thread_attr, thread1, NULL) != 0) { perror("pthrad create error"); } pthread_join(thread, NULL); */ if (REC) rec.prepare(); // OSC lo_server_thread server; server = lo_server_thread_new(OSC_PORT, NULL); lo_server_thread_add_method(server, "/1/fader1", "f", osc, NULL); lo_server_thread_add_method(server, "/1/fader2", "f", osc, NULL); lo_server_thread_add_method(server, "/1/toggle1", "i", osc, NULL); lo_server_thread_add_method(server, "/1/toggle2", "i", osc, NULL); lo_server_thread_add_method(server, "/1/toggle3", "i", osc, NULL); lo_server_thread_add_method(server, "/1/toggle4", "i", osc, NULL); lo_server_thread_start(server); // reset lo_send(address, "/1/fader1", "f", 0.0); lo_send(address, "/1/fader2", "f", 0.0); lo_send(address, "/1/rotary2", "f", 0.0); lo_send(address, "/1/rotary3", "f", 0.0); lo_send(address, "/1/rotary6", "f", 0.0); lo_send(address, "/1/toggle1", "i", 1); lo_send(address, "/1/toggle2", "i", 0); lo_send(address, "/1/toggle3", "i", 1); lo_send(address, "/1/toggle4", "i", 0); e = Pa_Initialize(); err(e); // devices /* int numDevices = Pa_GetDeviceCount(); const PaDeviceInfo *deviceInfo; for (int i = 0; i < numDevices; i++) { deviceInfo = Pa_GetDeviceInfo( i ); const PaHostApiInfo *hostInfo = Pa_GetHostApiInfo( deviceInfo->hostApi ); cout << i << ": " << hostInfo->name << endl; } */ outputParameters.device = Pa_GetDefaultOutputDevice(); if (outputParameters.device == paNoDevice) { cerr << "Error: No default output device." << endl; exit(0); } outputParameters.channelCount = 2; outputParameters.sampleFormat = paFloat32; outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; e = Pa_OpenStream( &stream, NULL, &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, paCallback, NULL ); err(e); e = Pa_StartStream(stream); err(e); // input int c; while ((c = getchar())) { if (c == 'q') { break; } } //pthread_exit(NULL); e = Pa_StopStream(stream); err(e); e = Pa_CloseStream(stream); err(e); Pa_Terminate(); return 0; }
int main(void) { PaStreamParameters inputParameters, outputParameters; PaStream *stream = NULL; PaError err; char *sampleBlock; int i; int numBytes; printf("patest_read_write_wire.c\n"); fflush(stdout); numBytes = FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE ; sampleBlock = (char *) malloc( numBytes ); if( sampleBlock == NULL ) { printf("Could not allocate record array.\n"); exit(1); } CLEAR( sampleBlock ); err = Pa_Initialize(); if( err != paNoError ) goto error; inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */ printf( "Input device # %d.\n", inputParameters.device ); printf( "Input LL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency ); printf( "Input HL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ); inputParameters.channelCount = NUM_CHANNELS; inputParameters.sampleFormat = PA_SAMPLE_TYPE; inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ; inputParameters.hostApiSpecificStreamInfo = NULL; outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ printf( "Output device # %d.\n", outputParameters.device ); printf( "Output LL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency ); printf( "Output HL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency ); outputParameters.channelCount = NUM_CHANNELS; outputParameters.sampleFormat = PA_SAMPLE_TYPE; outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; outputParameters.hostApiSpecificStreamInfo = NULL; /* -- setup -- */ err = Pa_OpenStream( &stream, &inputParameters, &outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, /* we won't output out of range samples so don't bother clipping them */ NULL, /* no callback, use blocking API */ NULL ); /* no callback, so no callback userData */ if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("Wire on. Will run %d seconds.\n", NUM_SECONDS); fflush(stdout); for( i=0; i<(NUM_SECONDS*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i ) { err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER ); if( err && CHECK_UNDERFLOW ) goto xrun; err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER ); if( err && CHECK_OVERFLOW ) goto xrun; } err = Pa_StopStream( stream ); if( err != paNoError ) goto error; CLEAR( sampleBlock ); /* err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("Wire on. Interrupt to stop.\n"); fflush(stdout); while( 1 ) { err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER ); if( err ) goto xrun; err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER ); if( err ) goto xrun; } err = Pa_StopStream( stream ); if( err != paNoError ) goto error; Pa_CloseStream( stream ); */ free( sampleBlock ); Pa_Terminate(); return 0; xrun: if( stream ) { Pa_AbortStream( stream ); Pa_CloseStream( stream ); } free( sampleBlock ); Pa_Terminate(); if( err & paInputOverflow ) fprintf( stderr, "Input Overflow.\n" ); if( err & paOutputUnderflow ) fprintf( stderr, "Output Underflow.\n" ); return -2; error: if( stream ) { Pa_AbortStream( stream ); Pa_CloseStream( stream ); } free( sampleBlock ); 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) { 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; }
static int portaudio_driver_audio_stop (portaudio_driver_t *driver) { PaError err = Pa_StopStream(driver->stream); return (err != paNoError) ? -1 : 0; }