Created
May 10, 2017 11:30
-
-
Save khuttun/d3980b6a343ae94dd3c793f878931adb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <condition_variable> | |
#include <iostream> | |
#include <mutex> | |
#include <portaudio.h> | |
#include <thread> | |
bool finished = false; | |
std::mutex mtx; | |
std::condition_variable cond; | |
int streamCallback(const void*, void* outbuf, unsigned long nFrames, const PaStreamCallbackTimeInfo*, | |
PaStreamCallbackFlags, void*) | |
{ | |
std::cout << "streamCallback" << std::endl; | |
for (int i = 0; i < nFrames; ++i) | |
static_cast<float*>(outbuf)[i] = 0.0f; | |
static int counter = 0; | |
return counter++ < 3 ? paContinue : paComplete; | |
} | |
void finishedCallback(void*) | |
{ | |
std::cout << "finishedCallback" << std::endl; | |
{ | |
std::lock_guard<std::mutex> lock(mtx); | |
finished = true; | |
} | |
cond.notify_one(); | |
std::cout << "finishedCallback returning" << std::endl; | |
} | |
int main() | |
{ | |
Pa_Initialize(); | |
PaStream* stream = nullptr; | |
Pa_OpenDefaultStream(&stream, 0, 1, paFloat32, 22050, paFramesPerBufferUnspecified, streamCallback, nullptr); | |
Pa_SetStreamFinishedCallback(stream, finishedCallback); | |
std::cout << "Pa_StartStream" << std::endl; | |
Pa_StartStream(stream); | |
std::cout << "Waiting for stream to finish..." << std::endl; | |
{ | |
std::unique_lock<std::mutex> lock(mtx); | |
cond.wait(lock, []{ return finished; }); | |
} | |
//std::this_thread::sleep_for(std::chrono::seconds(1)); | |
std::cout << "Stream finished. Pa_IsStreamActive() = " << Pa_IsStreamActive(stream) << std::endl; | |
Pa_CloseStream(stream); | |
Pa_Terminate(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment