Cleanup and refactoring of M17 modulator class, now using output streams for baseband signal output

This commit is contained in:
Silvano Seva 2022-03-24 15:18:31 +01:00
parent 0df1dc4f7e
commit c9a4bfb199
2 changed files with 59 additions and 67 deletions

View File

@ -1,8 +1,8 @@
/*************************************************************************** /***************************************************************************
* Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * * Copyright (C) 2021 - 2022 by Federico Amedeo Izzo IU2NUO, *
* Niccolò Izzo IU2KIN * * Niccolò Izzo IU2KIN *
* Frederik Saraci IU2NRO * * Frederik Saraci IU2NRO *
* Silvano Seva IU2KWO * * Silvano Seva IU2KWO *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
@ -25,6 +25,7 @@
#error This header is C++ only! #error This header is C++ only!
#endif #endif
#include <interfaces/audio_stream.h>
#include <cstdint> #include <cstdint>
#include <array> #include <array>
@ -107,18 +108,16 @@ private:
return symbols; return symbols;
} }
static constexpr size_t M17_TX_SAMPLE_RATE = 48000; static constexpr size_t M17_TX_SAMPLE_RATE = 48000;
static constexpr size_t M17_FRAME_SAMPLES_48K = 1920; static constexpr size_t M17_FRAME_SAMPLES_48K = 1920;
static constexpr size_t M17_FRAME_SYMBOLS = 192; static constexpr size_t M17_FRAME_SYMBOLS = 192;
using dataBuffer_t = std::array< int16_t, M17_FRAME_SAMPLES_48K >;
std::array< int16_t, M17_FRAME_SYMBOLS > symbols; std::array< int16_t, M17_FRAME_SYMBOLS > symbols;
int16_t *baseband_buffer; ///< Buffer for baseband audio handling. stream_sample_t *baseband_buffer; ///< Buffer for baseband audio handling.
dataBuffer_t *activeBuffer; ///< Half baseband buffer, in transmission. stream_sample_t *idleBuffer; ///< Half baseband buffer, free for processing.
dataBuffer_t *idleBuffer; ///< Half baseband buffer, free for processing. streamId outStream; ///< Baseband output stream ID.
bool txRunning; ///< Transmission running. bool txRunning; ///< Transmission running.
bool stopTx; ///< Stop transmission request. bool stopTx; ///< Stop transmission request.
}; };
} /* M17 */ } /* M17 */

View File

@ -1,8 +1,8 @@
/*************************************************************************** /***************************************************************************
* Copyright (C) 2021 by Federico Amedeo Izzo IU2NUO, * * Copyright (C) 2021 - 2022 by Federico Amedeo Izzo IU2NUO, *
* Niccolò Izzo IU2KIN * * Niccolò Izzo IU2KIN *
* Frederik Saraci IU2NRO * * Frederik Saraci IU2NRO *
* Silvano Seva IU2KWO * * Silvano Seva IU2KWO *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
@ -21,18 +21,17 @@
#include <new> #include <new>
#include <dsp.h> #include <dsp.h>
#include <cstddef> #include <cstddef>
#include <cstring>
#include <experimental/array> #include <experimental/array>
#include <M17/M17Modulator.h> #include <M17/M17Modulator.h>
#include <M17/M17DSP.h> #include <M17/M17DSP.h>
#if defined(PLATFORM_MD3x0) || defined(PLATFORM_MDUV3x0) #if defined(PLATFORM_LINUX)
#include <toneGenerator_MDx.h>
#elif defined(PLATFORM_LINUX)
#include <stdio.h> #include <stdio.h>
#endif #endif
namespace M17 using namespace M17;
{
M17Modulator::M17Modulator() M17Modulator::M17Modulator()
{ {
@ -53,17 +52,13 @@ void M17Modulator::init()
*/ */
baseband_buffer = new int16_t[2 * M17_FRAME_SAMPLES_48K]; baseband_buffer = new int16_t[2 * M17_FRAME_SAMPLES_48K];
idleBuffer = new (baseband_buffer) dataBuffer_t; idleBuffer = baseband_buffer;
int16_t *ptr = baseband_buffer + M17_FRAME_SAMPLES_48K;
activeBuffer = new (ptr) dataBuffer_t;
txRunning = false; txRunning = false;
} }
void M17Modulator::terminate() void M17Modulator::terminate()
{ {
// Delete the buffers and deallocate memory. // Deallocate memory.
activeBuffer->~dataBuffer_t();
idleBuffer->~dataBuffer_t();
delete[] baseband_buffer; delete[] baseband_buffer;
} }
@ -92,72 +87,70 @@ void M17Modulator::send(const std::array< uint8_t, 2 >& sync,
void M17Modulator::generateBaseband() 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++) 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++) for(size_t i = 0; i < M17_FRAME_SAMPLES_48K; i++)
{ {
int32_t pos_sample = idleBuffer->at(i) + 32768; float elem = static_cast< float >(idleBuffer[i]);
uint16_t shifted_sample = pos_sample >> 8; idleBuffer[i] = static_cast< int16_t >(M17::rrc(elem) * 7168.0);
idleBuffer->at(i) = shifted_sample;
} }
}
#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) if(txRunning == false)
{ {
// First run, start transmission // First run, start transmission
toneGen_playAudioStream(reinterpret_cast< uint16_t *>(baseband_buffer), outStream = outputStream_start(SINK_RTX,
2*M17_FRAME_SAMPLES_48K, M17_TX_SAMPLE_RATE, true); PRIO_TX,
baseband_buffer,
2*M17_FRAME_SAMPLES_48K,
BUF_CIRC_DOUBLE,
M17_TX_SAMPLE_RATE);
txRunning = true; txRunning = true;
stopTx = false; stopTx = false;
} }
else else
{ {
// Transmission is ongoing, syncronise with stream end before proceeding // Transmission is ongoing, syncronise with stream end before proceeding
toneGen_waitForStreamEnd(); outputStream_sync(outStream, true);
// Check if transmission stop is requested
if(stopTx == true)
{
toneGen_stopAudioStream();
stopTx = false;
txRunning = false;
}
} }
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() void M17Modulator::emitBaseband()
{ {
std::swap(idleBuffer, activeBuffer);
FILE *outfile = fopen("/tmp/m17_output.raw", "ab"); 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); fwrite(&s, sizeof(s), 1, outfile);
} }
fclose(outfile); fclose(outfile);
} }
#else
void M17Modulator::emitBaseband() { }
#endif #endif
} /* M17 */