From f301e3d5f971182beca99145e6a2173c2f7e0502 Mon Sep 17 00:00:00 2001 From: "J.A. de Jong - Redu-Sone B.V., ASCEE V.O.F" Date: Sat, 7 Nov 2020 21:36:05 +0100 Subject: [PATCH] Bugfix in RtAudio. Almost working properly --- lasp/device/lasp_cppdaq.h | 44 ++++++++++++++++----------------- lasp/device/lasp_cpprtaudio.cpp | 20 ++++++++++----- lasp/device/lasp_cppuldaq.cpp | 10 +++++--- lasp/device/lasp_daq.pyx | 25 +++++++++---------- 4 files changed, 54 insertions(+), 45 deletions(-) diff --git a/lasp/device/lasp_cppdaq.h b/lasp/device/lasp_cppdaq.h index 82682b7..29eaa44 100644 --- a/lasp/device/lasp_cppdaq.h +++ b/lasp/device/lasp_cppdaq.h @@ -30,17 +30,17 @@ typedef vector usvec; typedef std::lock_guard mutexlock; class DataType { -public: - string name; - unsigned sw; - bool is_floating; + public: + string name; + unsigned sw; + bool is_floating; - DataType(const char *name, unsigned sw, bool is_floating) + DataType(const char *name, unsigned sw, bool is_floating) : name(name), sw(sw), is_floating(is_floating) {} - DataType() : name("invalid data type"), sw(0), is_floating(false) {} - bool operator==(const DataType &o) { - return (name == o.name && sw == o.sw && is_floating == o.is_floating); - } + DataType() : name("invalid data type"), sw(0), is_floating(false) {} + bool operator==(const DataType &o) { + return (name == o.name && sw == o.sw && is_floating == o.is_floating); + } }; const DataType dtype_invalid; @@ -51,25 +51,25 @@ const DataType dtype_int16("16-bits integers", 2, false); const DataType dtype_int32("32-bits integers", 4, false); const std::vector dataTypes = { - dtype_int8, dtype_int16, dtype_int32, dtype_fl32, dtype_fl64, + dtype_int8, dtype_int16, dtype_int32, dtype_fl32, dtype_fl64, }; class DaqApi { -public: - string apiname = "Invalid API"; - int apicode = -1; - unsigned api_specific_subcode = 0; + public: + string apiname = "Invalid API"; + int apicode = -1; + unsigned api_specific_subcode = 0; - DaqApi(string apiname, unsigned apicode, unsigned api_specific_subcode = 0) + DaqApi(string apiname, unsigned apicode, unsigned api_specific_subcode = 0) : apiname(apiname), apicode(apicode), - api_specific_subcode(api_specific_subcode) {} - DaqApi() {} - bool operator==(const DaqApi &other) const { - return (apiname == other.apiname && apicode == other.apicode && - api_specific_subcode == other.api_specific_subcode); - } + api_specific_subcode(api_specific_subcode) {} + DaqApi() {} + bool operator==(const DaqApi &other) const { + return (apiname == other.apiname && apicode == other.apicode && + api_specific_subcode == other.api_specific_subcode); + } - static vector getAvailableApis(); + static vector getAvailableApis(); }; #ifdef HAS_ULDAQ_API diff --git a/lasp/device/lasp_cpprtaudio.cpp b/lasp/device/lasp_cpprtaudio.cpp index 6168659..d5969ff 100644 --- a/lasp/device/lasp_cpprtaudio.cpp +++ b/lasp/device/lasp_cpprtaudio.cpp @@ -160,7 +160,7 @@ class AudioDaq: public Daq { } try { - rtaudio = new RtAudio((RtAudio::Api) devinfo.api_specific_devindex); + rtaudio = new RtAudio((RtAudio::Api) devinfo.api.api_specific_subcode); if(!rtaudio) { throw runtime_error("RtAudio allocation failed"); } @@ -177,6 +177,8 @@ class AudioDaq: public Daq { ); } catch(...) { if(rtaudio) delete rtaudio; + if(instreamparams) delete instreamparams; + if(outstreamparams) delete outstreamparams; throw; } if(monitorOutput) { @@ -224,16 +226,22 @@ class AudioDaq: public Daq { assert(rtaudio); rtaudio->stopStream(); } - if(inqueue) delete inqueue; - if(outqueue) delete outqueue; - if(outDelayqueue) delete outDelayqueue; - + if(inqueue) { + inqueue = nullptr; + } + if(outqueue) { + outqueue = nullptr; + } + if(outDelayqueue) { + delete outDelayqueue; + outDelayqueue = nullptr; + } } bool isRunning() const {return (rtaudio && rtaudio->isStreamRunning());} ~AudioDaq() { assert(rtaudio); - if(rtaudio->isStreamRunning()) { + if(isRunning()) { stop(); } if(rtaudio->isStreamOpen()) { diff --git a/lasp/device/lasp_cppuldaq.cpp b/lasp/device/lasp_cppuldaq.cpp index 343f1cd..d15f6f4 100644 --- a/lasp/device/lasp_cppuldaq.cpp +++ b/lasp/device/lasp_cppuldaq.cpp @@ -192,12 +192,14 @@ public: outqueue = NULL; inqueue = NULL; - if (inbuffer) + if (inbuffer) { delete inbuffer; - if (outbuffer) + inbuffer = nullptr; + } + if (outbuffer) { delete outbuffer; - outbuffer = NULL; - inbuffer = NULL; + outbuffer = nullptr; + } } friend void threadfcn(DT9837A *); diff --git a/lasp/device/lasp_daq.pyx b/lasp/device/lasp_daq.pyx index 99f0418..d41efa6 100644 --- a/lasp/device/lasp_daq.pyx +++ b/lasp/device/lasp_daq.pyx @@ -285,23 +285,22 @@ cdef class Daq: if sd.inQueue: # If waiting in the input queue, hereby we let it run. sd.inQueue.enqueue(NULL) - # printf('Joining thread...\n') - # HERE WE SHOULD RELEASE THE GIL, as exiting the thread function - # will require the GIL, which is locked by this thread! - sd.thread.join() - # printf('Thread joined!\n') - del sd.thread - sd.thread = NULL + + sd.thread.join() + del sd.thread + sd.thread = NULL + + if sd.inQueue: + while not sd.inQueue.empty(): + free(sd.inQueue.dequeue()) + del sd.inQueue if sd.outQueue: while not sd.outQueue.empty(): free(sd.outQueue.dequeue()) - del sd.outQueue - if sd.inQueue: - while not sd.inQueue.empty(): - free(sd.inQueue.dequeue()) - del sd.inQueue - fprintf(stderr, "End cleanup stream queues...\n") + del sd.outQueue + sd.outQueue = NULL + fprintf(stderr, "End cleanup stream queues...\n") if sd.pyCallback: Py_DECREF( sd.pyCallback)