mirror of
https://github.com/cjcliffe/CubicSDR.git
synced 2026-07-17 00:04:33 -04:00
Move code out of frame, proper thread termination
This commit is contained in:
+11
-99
@@ -3,7 +3,7 @@
|
||||
#include <vector>
|
||||
|
||||
AudioThread::AudioThread(AudioThreadInputQueue *inputQueue) :
|
||||
inputQueue(inputQueue), stream(NULL) {
|
||||
inputQueue(inputQueue), stream(NULL), terminated(false) {
|
||||
|
||||
}
|
||||
|
||||
@@ -41,11 +41,6 @@ void AudioThread::threadMain() {
|
||||
|
||||
stream = NULL;
|
||||
|
||||
// err = Pa_OpenStream(&stream, NULL, &outputParameters, AUDIO_FREQUENCY,
|
||||
// paFramesPerBufferUnspecified,
|
||||
// paPrimeOutputBuffersUsingStreamCallback | paClipOff, &audioCallback,
|
||||
// this);
|
||||
|
||||
err = Pa_OpenStream(&stream, NULL, &outputParameters, AUDIO_FREQUENCY,
|
||||
paFramesPerBufferUnspecified, paClipOff, NULL, NULL);
|
||||
|
||||
@@ -56,101 +51,18 @@ void AudioThread::threadMain() {
|
||||
std::cout << "\tPortAudio error: " << Pa_GetErrorText(err) << std::endl;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
while (!terminated) {
|
||||
AudioThreadInput inp;
|
||||
inputQueue->pop(inp);
|
||||
Pa_WriteStream(stream, &inp.data[0], inp.data.size()/2);
|
||||
if (inp.data.size()) {
|
||||
Pa_WriteStream(stream, &inp.data[0], inp.data.size()/2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
#ifdef WIN32
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <cctype>
|
||||
#include <locale>
|
||||
#include <sstream>
|
||||
|
||||
// trim from start
|
||||
static inline std::wstring &wltrim(std::wstring &s) {
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
|
||||
return s;
|
||||
}
|
||||
|
||||
// trim from end
|
||||
static inline std::wstring &wrtrim(std::wstring &s) {
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
||||
return s;
|
||||
}
|
||||
|
||||
// trim from both ends
|
||||
static inline std::wstring &wtrim(std::wstring &s) {
|
||||
return wltrim(wrtrim(s));
|
||||
}
|
||||
#endif
|
||||
|
||||
//wxDEFINE_EVENT(wxEVT_COMMAND_AudioThread_INPUT, wxThreadEvent);
|
||||
|
||||
AudioThread::AudioThread(AudioThreadQueue* pQueue, int id) :
|
||||
wxThread(wxTHREAD_DETACHED), m_pQueue(pQueue), m_ID(id), audio_queue_ptr(0), stream(NULL) {
|
||||
|
||||
}
|
||||
AudioThread::~AudioThread() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
wxThread::ExitCode AudioThread::Entry() {
|
||||
|
||||
|
||||
|
||||
//#ifdef WIN32
|
||||
// wchar_t dev_str[255];
|
||||
// memset(dev_str, 0, sizeof(wchar_t) * 255);
|
||||
// std::wstring env_name(L"PA_RECOMMENDED_OUTPUT_DEVICE");
|
||||
// GetEnvironmentVariable(wtrim(env_name).c_str(), dev_str, 255);
|
||||
// std::wistringstream env_result(dev_str);
|
||||
//
|
||||
// if (!env_result.eof()) {
|
||||
// int env_dev = -1;
|
||||
// env_result >> env_dev;
|
||||
//
|
||||
// if (env_result.eof()) { // read everything, was all a number
|
||||
// if (env_dev >= 0) {
|
||||
// std::cout << "Using preferred PortAudio device PA_RECOMMENDED_OUTPUT_DEVICE=" << env_dev << std::endl;
|
||||
// preferred_device = env_dev;
|
||||
// } else {
|
||||
// std::cout << "Environment variable PA_RECOMMENDED_OUTPUT_DEVICE not set, using PortAudio defaults." << std::endl;
|
||||
// }
|
||||
// } else {
|
||||
// std::cout << "Environment variable PA_RECOMMENDED_OUTPUT_DEVICE didn't evaluate to a number, using PortAudio defaults." << std::endl;
|
||||
// }
|
||||
// }
|
||||
//#endif
|
||||
|
||||
while (!TestDestroy()) {
|
||||
|
||||
if (m_pQueue->stackSize()) {
|
||||
|
||||
while (m_pQueue->stackSize()) {
|
||||
AudioThreadTask task = m_pQueue->pop(); // pop a task from the queue. this will block the worker thread if queue is empty
|
||||
switch (task.m_cmd) {
|
||||
case AudioThreadTask::AUDIO_THREAD_DATA:
|
||||
if (!TestDestroy()) {
|
||||
audio_queue.push(task.data->data);
|
||||
}
|
||||
delete task.data;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this->Yield();
|
||||
this->Sleep(1);
|
||||
}
|
||||
}
|
||||
std::cout << std::endl << "Audio Thread Done." << std::endl << std::endl;
|
||||
|
||||
return (wxThread::ExitCode) 0;
|
||||
}
|
||||
|
||||
*/
|
||||
void AudioThread::terminate() {
|
||||
std::cout << "Terminating audio thread.." << std::endl;
|
||||
terminated = true;
|
||||
AudioThreadInput endCond; // push an empty input to bump the queue
|
||||
inputQueue->push(endCond);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
#include "wx/wxprec.h"
|
||||
|
||||
#ifndef WX_PRECOMP
|
||||
@@ -28,14 +29,15 @@ typedef ThreadQueue<AudioThreadInput> AudioThreadInputQueue;
|
||||
class AudioThread {
|
||||
public:
|
||||
AudioThread(AudioThreadInputQueue *inputQueue);
|
||||
|
||||
~AudioThread();
|
||||
|
||||
void threadMain();
|
||||
void terminate();
|
||||
|
||||
private:
|
||||
AudioThreadInputQueue *inputQueue;
|
||||
PaStreamParameters outputParameters;
|
||||
PaStream *stream;
|
||||
std::atomic<bool> terminated;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user