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, *
* 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 <interfaces/audio_stream.h>
#include <cstdint>
#include <array>
@ -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 */

View File

@ -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 <new>
#include <dsp.h>
#include <cstddef>
#include <cstring>
#include <experimental/array>
#include <M17/M17Modulator.h>
#include <M17/M17DSP.h>
#if defined(PLATFORM_MD3x0) || defined(PLATFORM_MDUV3x0)
#include <toneGenerator_MDx.h>
#elif defined(PLATFORM_LINUX)
#if defined(PLATFORM_LINUX)
#include <stdio.h>
#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 */