From c9a4bfb199784ee14243660cadb4e30f5447c415 Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Thu, 24 Mar 2022 15:18:31 +0100 Subject: [PATCH] Cleanup and refactoring of M17 modulator class, now using output streams for baseband signal output --- openrtx/include/protocols/M17/M17Modulator.h | 27 +++--- openrtx/src/protocols/M17/M17Modulator.cpp | 99 +++++++++----------- 2 files changed, 59 insertions(+), 67 deletions(-) diff --git a/openrtx/include/protocols/M17/M17Modulator.h b/openrtx/include/protocols/M17/M17Modulator.h index 819224d3..1fa80a7e 100644 --- a/openrtx/include/protocols/M17/M17Modulator.h +++ b/openrtx/include/protocols/M17/M17Modulator.h @@ -1,8 +1,8 @@ /*************************************************************************** - * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * - * Niccolò Izzo IU2KIN * - * Frederik Saraci IU2NRO * - * Silvano Seva IU2KWO * + * Copyright (C) 2021 - 2022 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -25,6 +25,7 @@ #error This header is C++ only! #endif +#include #include #include @@ -107,18 +108,16 @@ private: return symbols; } - static constexpr size_t M17_TX_SAMPLE_RATE = 48000; - static constexpr size_t M17_FRAME_SAMPLES_48K = 1920; - static constexpr size_t M17_FRAME_SYMBOLS = 192; - - using dataBuffer_t = std::array< int16_t, M17_FRAME_SAMPLES_48K >; + static constexpr size_t M17_TX_SAMPLE_RATE = 48000; + static constexpr size_t M17_FRAME_SAMPLES_48K = 1920; + static constexpr size_t M17_FRAME_SYMBOLS = 192; std::array< int16_t, M17_FRAME_SYMBOLS > symbols; - int16_t *baseband_buffer; ///< Buffer for baseband audio handling. - dataBuffer_t *activeBuffer; ///< Half baseband buffer, in transmission. - dataBuffer_t *idleBuffer; ///< Half baseband buffer, free for processing. - bool txRunning; ///< Transmission running. - bool stopTx; ///< Stop transmission request. + stream_sample_t *baseband_buffer; ///< Buffer for baseband audio handling. + stream_sample_t *idleBuffer; ///< Half baseband buffer, free for processing. + streamId outStream; ///< Baseband output stream ID. + bool txRunning; ///< Transmission running. + bool stopTx; ///< Stop transmission request. }; } /* M17 */ diff --git a/openrtx/src/protocols/M17/M17Modulator.cpp b/openrtx/src/protocols/M17/M17Modulator.cpp index 64e0eeeb..e06c68b1 100644 --- a/openrtx/src/protocols/M17/M17Modulator.cpp +++ b/openrtx/src/protocols/M17/M17Modulator.cpp @@ -1,8 +1,8 @@ /*************************************************************************** - * Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * - * Niccolò Izzo IU2KIN * - * Frederik Saraci IU2NRO * - * Silvano Seva IU2KWO * + * Copyright (C) 2021 - 2022 by Federico Amedeo Izzo IU2NUO, * + * Niccolò Izzo IU2KIN * + * Frederik Saraci IU2NRO * + * Silvano Seva IU2KWO * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -21,18 +21,17 @@ #include #include #include +#include #include #include #include -#if defined(PLATFORM_MD3x0) || defined(PLATFORM_MDUV3x0) -#include -#elif defined(PLATFORM_LINUX) +#if defined(PLATFORM_LINUX) #include #endif -namespace M17 -{ +using namespace M17; + M17Modulator::M17Modulator() { @@ -53,17 +52,13 @@ void M17Modulator::init() */ baseband_buffer = new int16_t[2 * M17_FRAME_SAMPLES_48K]; - idleBuffer = new (baseband_buffer) dataBuffer_t; - int16_t *ptr = baseband_buffer + M17_FRAME_SAMPLES_48K; - activeBuffer = new (ptr) dataBuffer_t; + idleBuffer = baseband_buffer; txRunning = false; } void M17Modulator::terminate() { - // Delete the buffers and deallocate memory. - activeBuffer->~dataBuffer_t(); - idleBuffer->~dataBuffer_t(); + // Deallocate memory. delete[] baseband_buffer; } @@ -92,72 +87,70 @@ void M17Modulator::send(const std::array< uint8_t, 2 >& sync, void M17Modulator::generateBaseband() { - idleBuffer->fill(0); + memset(idleBuffer, 0x00, M17_FRAME_SAMPLES_48K * sizeof(stream_sample_t)); + for(size_t i = 0; i < symbols.size(); i++) { - idleBuffer->at(i * 10) = symbols[i]; + idleBuffer[i * 10] = symbols[i]; } - for(size_t i = 0; i < idleBuffer->size(); i++) - { - float elem = static_cast< float >(idleBuffer->at(i)); - idleBuffer->at(i) = static_cast< int16_t >(M17::rrc(elem) * 7168.0); - } -} - -#if defined(PLATFORM_MD3x0) || defined(PLATFORM_MDUV3x0) -void M17Modulator::emitBaseband() -{ - dsp_pwmCompensate(idleBuffer->data(), idleBuffer->size()); - dsp_invertPhase(idleBuffer->data(), idleBuffer->size()); - for(size_t i = 0; i < M17_FRAME_SAMPLES_48K; i++) { - int32_t pos_sample = idleBuffer->at(i) + 32768; - uint16_t shifted_sample = pos_sample >> 8; - idleBuffer->at(i) = shifted_sample; + float elem = static_cast< float >(idleBuffer[i]); + idleBuffer[i] = static_cast< int16_t >(M17::rrc(elem) * 7168.0); } +} + +#ifndef PLATFORM_LINUX +void M17Modulator::emitBaseband() +{ + #if defined(PLATFORM_MD3x0) || defined(PLATFORM_MDUV3x0) + dsp_pwmCompensate(idleBuffer, M17_FRAME_SAMPLES_48K); + dsp_invertPhase(idleBuffer, M17_FRAME_SAMPLES_48K); + #endif if(txRunning == false) { // First run, start transmission - toneGen_playAudioStream(reinterpret_cast< uint16_t *>(baseband_buffer), - 2*M17_FRAME_SAMPLES_48K, M17_TX_SAMPLE_RATE, true); + outStream = outputStream_start(SINK_RTX, + PRIO_TX, + baseband_buffer, + 2*M17_FRAME_SAMPLES_48K, + BUF_CIRC_DOUBLE, + M17_TX_SAMPLE_RATE); txRunning = true; stopTx = false; } else { // Transmission is ongoing, syncronise with stream end before proceeding - toneGen_waitForStreamEnd(); - - // Check if transmission stop is requested - if(stopTx == true) - { - toneGen_stopAudioStream(); - stopTx = false; - txRunning = false; - } + outputStream_sync(outStream, true); } - std::swap(idleBuffer, activeBuffer); + // Check if transmission stop is requested + if(stopTx == true) + { + outputStream_stop(outStream); + stopTx = false; + txRunning = false; + idleBuffer = baseband_buffer; + } + else + { + idleBuffer = outputStream_getIdleBuffer(outStream); + } } -#elif defined(PLATFORM_LINUX) +#else void M17Modulator::emitBaseband() { - std::swap(idleBuffer, activeBuffer); - FILE *outfile = fopen("/tmp/m17_output.raw", "ab"); - for(auto s : *activeBuffer) + for(size_t i = 0; i < M17_FRAME_SAMPLES_48K; i++) { + auto s = idleBuffer[i]; fwrite(&s, sizeof(s), 1, outfile); } fclose(outfile); } -#else -void M17Modulator::emitBaseband() { } #endif - -} /* M17 */