diff --git a/openrtx/include/protocols/M17/M17Demodulator.h b/openrtx/include/protocols/M17/M17Demodulator.h index cb74f6b7..5e9cfcc8 100644 --- a/openrtx/include/protocols/M17/M17Demodulator.h +++ b/openrtx/include/protocols/M17/M17Demodulator.h @@ -27,17 +27,15 @@ #error This header is C++ only! #endif -#include #include #include -#include -#include +#include +#include #include -#include -#include +#include +#include #include #include -#include namespace M17 { @@ -146,18 +144,18 @@ private: /* * Buffers */ - streamId basebandId; ///< Id of the baseband input stream. - int16_t *baseband_buffer; ///< Buffer for baseband audio handling. - dataBlock_t baseband; ///< Half buffer, free to be processed. - uint16_t frame_index; ///< Index for filling the raw frame. - frame_t *demodFrame; ///< Frame being demodulated. - frame_t *readyFrame; ///< Fully demodulated frame to be returned. - bool syncDetected; ///< A syncword was detected. - bool locked; ///< A syncword was correctly demodulated. - bool newFrame; ///< A new frame has been fully decoded. - int16_t basebandBridge[M17_BRIDGE_SIZE] = { 0 }; ///< Bridge buffer - int16_t phase; ///< Phase of the signal w.r.t. sampling - bool invPhase; ///< Invert signal phase + std::unique_ptr< int16_t[] > baseband_buffer; ///< Buffer for baseband audio handling. + streamId basebandId; ///< Id of the baseband input stream. + dataBlock_t baseband; ///< Data block with samples to be processed. + uint16_t frame_index; ///< Index for filling the raw frame. + std::unique_ptr demodFrame; ///< Frame being demodulated. + std::unique_ptr readyFrame; ///< Fully demodulated frame to be returned. + bool syncDetected; ///< A syncword was detected. + bool locked; ///< A syncword was correctly demodulated. + bool newFrame; ///< A new frame has been fully decoded. + int16_t basebandBridge[M17_BRIDGE_SIZE] = { 0 }; ///< Bridge buffer + int16_t phase; ///< Phase of the signal w.r.t. sampling + bool invPhase; ///< Invert signal phase /* * State variables diff --git a/openrtx/src/protocols/M17/M17Demodulator.cpp b/openrtx/src/protocols/M17/M17Demodulator.cpp index 6a6f5301..5c28a950 100644 --- a/openrtx/src/protocols/M17/M17Demodulator.cpp +++ b/openrtx/src/protocols/M17/M17Demodulator.cpp @@ -155,8 +155,7 @@ M17Demodulator::M17Demodulator() M17Demodulator::~M17Demodulator() { - // TODO - // terminate(); + terminate(); } void M17Demodulator::init() @@ -167,10 +166,10 @@ void M17Demodulator::init() * placement new. */ - baseband_buffer = new int16_t[2 * M17_SAMPLE_BUF_SIZE]; + baseband_buffer = std::make_unique< int16_t[] >(2 * M17_SAMPLE_BUF_SIZE); + demodFrame = std::make_unique< frame_t >(); + readyFrame = std::make_unique< frame_t >(); baseband = { nullptr, 0 }; - demodFrame = new frame_t; - readyFrame = new frame_t; frame_index = 0; phase = 0; syncDetected = false; @@ -193,9 +192,9 @@ void M17Demodulator::terminate() inputStream_stop(basebandId); // Delete the buffers and deallocate memory. - delete[] baseband_buffer; - delete demodFrame; - delete readyFrame; + baseband_buffer.reset(); + demodFrame.reset(); + readyFrame.reset(); #ifdef ENABLE_DEMOD_LOG logRunning = false; @@ -205,7 +204,7 @@ void M17Demodulator::terminate() void M17Demodulator::startBasebandSampling() { basebandId = inputStream_start(SOURCE_RTX, PRIO_RX, - baseband_buffer, + baseband_buffer.get(), 2 * M17_SAMPLE_BUF_SIZE, BUF_CIRC_DOUBLE, M17_RX_SAMPLE_RATE); @@ -555,7 +554,7 @@ bool M17Demodulator::update() // If the frame buffer is full switch demod and ready frame if (frame_index == M17_FRAME_SYMBOLS) { - std::swap(demodFrame, readyFrame); + demodFrame.swap(readyFrame); frame_index = 0; newFrame = true; } diff --git a/platform/drivers/audio/inputStream_linux.c b/platform/drivers/audio/inputStream_linux.c index d4a55d29..c8f356c3 100644 --- a/platform/drivers/audio/inputStream_linux.c +++ b/platform/drivers/audio/inputStream_linux.c @@ -87,5 +87,7 @@ dataBlock_t inputStream_getData(streamId id) void inputStream_stop(streamId id) { (void) id; + if(baseband_file == NULL) return; fclose(baseband_file); + baseband_file = NULL; }