Moved audio management in M17 opmode handler to audio path system

This commit is contained in:
Silvano Seva 2022-09-21 21:25:19 +02:00
parent edf864337a
commit 21481072e7
7 changed files with 79 additions and 45 deletions

View File

@ -33,6 +33,7 @@
#include <array>
#include <dsp.h>
#include <cmath>
#include <audio_path.h>
#include <interfaces/audio_stream.h>
#include <M17/M17Datatypes.hpp>
#include <M17/M17Constants.hpp>
@ -146,6 +147,7 @@ private:
*/
std::unique_ptr< int16_t[] > baseband_buffer; ///< Buffer for baseband audio handling.
streamId basebandId; ///< Id of the baseband input stream.
pathId basebandPath; ///< Id of the baseband input path.
dataBlock_t baseband; ///< Data block with samples to be processed.
uint16_t frame_index; ///< Index for filling the raw frame.
std::unique_ptr<frame_t > demodFrame; ///< Frame being demodulated.

View File

@ -26,8 +26,9 @@
#endif
#include <interfaces/audio_stream.h>
#include <M17/M17Constants.hpp>
#include <M17/PwmCompensator.hpp>
#include <M17/M17Constants.hpp>
#include <audio_path.h>
#include <cstdint>
#include <memory>
#include <array>
@ -112,6 +113,7 @@ private:
std::unique_ptr< int16_t[] > baseband_buffer; ///< Buffer for baseband audio handling.
stream_sample_t *idleBuffer; ///< Half baseband buffer, free for processing.
streamId outStream; ///< Baseband output stream ID.
pathId outPath; ///< Baseband output path ID.
bool txRunning; ///< Transmission running.
#if defined(PLATFORM_MD3x0) || defined(PLATFORM_MDUV3x0)

View File

@ -25,6 +25,7 @@
#include <M17/M17FrameEncoder.hpp>
#include <M17/M17Demodulator.hpp>
#include <M17/M17Modulator.hpp>
#include <audio_path.h>
#include "OpMode.hpp"
/**
@ -124,6 +125,8 @@ private:
bool startRx; ///< Flag for RX management.
bool startTx; ///< Flag for TX management.
bool locked; ///< Demodulator locked on data stream.
pathId rxAudioPath; ///< Audio path ID for RX
pathId txAudioPath; ///< Audio path ID for TX
M17::M17Modulator modulator; ///< M17 modulator.
M17::M17Demodulator demodulator; ///< M17 demodulator.
M17::M17FrameDecoder decoder; ///< M17 frame decoder

View File

@ -203,6 +203,7 @@ void M17Demodulator::terminate()
void M17Demodulator::startBasebandSampling()
{
basebandPath = audioPath_request(SOURCE_RTX, SINK_MCU, PRIO_RX);
basebandId = inputStream_start(SOURCE_RTX, PRIO_RX,
baseband_buffer.get(),
2 * M17_SAMPLE_BUF_SIZE,
@ -218,6 +219,7 @@ void M17Demodulator::startBasebandSampling()
void M17Demodulator::stopBasebandSampling()
{
inputStream_stop(basebandId);
audioPath_release(basebandPath);
phase = 0;
syncDetected = false;
locked = false;
@ -423,6 +425,7 @@ bool M17Demodulator::update()
uint16_t decoded_syms = 0;
// Read samples from the ADC
if(audioPath_getStatus(basebandPath) != PATH_OPEN) return false;
baseband = inputStream_getData(basebandId);
if(baseband.data != NULL)

View File

@ -87,6 +87,13 @@ void M17Modulator::start()
// Generate baseband signal and then start transmission
symbolsToBaseband();
#ifndef PLATFORM_LINUX
outPath = audioPath_request(SOURCE_MCU, SINK_RTX, PRIO_TX);
if(outPath < 0)
{
txRunning = false;
return;
}
outStream = outputStream_start(SINK_RTX, PRIO_TX, baseband_buffer.get(),
2*M17_FRAME_SAMPLES, BUF_CIRC_DOUBLE,
M17_TX_SAMPLE_RATE);
@ -145,6 +152,7 @@ void M17Modulator::stop()
outputStream_sync(outStream, false);
txRunning = false;
idleBuffer = baseband_buffer.get();
audioPath_release(outPath);
#if defined(PLATFORM_MD3x0) || defined(PLATFORM_MDUV3x0)
pwmComp.reset();
@ -176,6 +184,7 @@ void M17Modulator::symbolsToBaseband()
void M17Modulator::sendBaseband()
{
if(txRunning == false) return;
if(audioPath_getStatus(outPath) != PATH_OPEN) return;
// Transmission is ongoing, syncronise with stream end before proceeding
outputStream_sync(outStream, true);

View File

@ -55,10 +55,9 @@ void OpMode_M17::disable()
startTx = false;
platform_ledOff(GREEN);
platform_ledOff(RED);
codec_stop();
audioPath_release(rxAudioPath);
audioPath_release(txAudioPath);
codec_terminate();
audio_disableAmp();
audio_disableMic();
radio_disableRtx();
modulator.terminate();
demodulator.terminate();
@ -124,8 +123,8 @@ void OpMode_M17::offState(rtxStatus_t *const status)
{
radio_disableRtx();
audio_disableMic();
audio_disableAmp();
audioPath_release(rxAudioPath);
audioPath_release(txAudioPath);
codec_stop();
if(startRx)
@ -147,7 +146,7 @@ void OpMode_M17::rxState(rtxStatus_t *const status)
demodulator.startBasebandSampling();
demodulator.invertPhase(status->invertRxPhase);
audio_enableAmp();
rxAudioPath = audioPath_request(SOURCE_MCU, SINK_SPK, PRIO_RX);
codec_startDecode(SINK_SPK);
radio_enableRx();
@ -168,11 +167,13 @@ void OpMode_M17::rxState(rtxStatus_t *const status)
if(locked && newData)
{
auto& frame = demodulator.getFrame();
auto type = decoder.decodeFrame(frame);
bool lsfOk = decoder.getLsf().valid();
auto& frame = demodulator.getFrame();
auto type = decoder.decodeFrame(frame);
bool lsfOk = decoder.getLsf().valid();
uint8_t pthSts = audioPath_getStatus(rxAudioPath);
if((type == M17FrameType::STREAM) && (lsfOk == true))
if((type == M17FrameType::STREAM) && (lsfOk == true) &&
(pthSts == PATH_OPEN))
{
M17StreamFrame sf = decoder.getStreamFrame();
codec_pushFrame(sf.payload().data(), false);
@ -215,7 +216,7 @@ void OpMode_M17::txState(rtxStatus_t *const status)
encoder.reset();
encoder.encodeLsf(lsf, m17Frame);
audio_enableMic();
txAudioPath = audioPath_request(SOURCE_MIC, SINK_MCU, PRIO_TX);
codec_startEncode(SOURCE_MIC);
radio_enableTx();

View File

@ -24,6 +24,8 @@
#include <interfaces/gpio.h>
#include <hwconfig.h>
#define PATH(x,y) ((x << 4) | y)
static const uint8_t pathCompatibilityMatrix[9][9] =
{
// MIC-SPK MIC-RTX MIC-MCU RTX-SPK RTX-RTX RTX-MCU MCU-SPK MCU-RTX MCU-MCU
@ -71,27 +73,33 @@ void audio_terminate()
void audio_connect(const enum AudioSource source, const enum AudioSink sink)
{
// If source is MIC, turn it on regardless of the sink
#if !defined(PLATFORM_MD9600) && !defined(MDx_ENABLE_SWD)
if(source == SOURCE_MIC) gpio_setPin(MIC_PWR);
#endif
uint32_t path = PATH(source, sink);
switch(path)
{
case PATH(SOURCE_MIC, SINK_SPK):
case PATH(SOURCE_MIC, SINK_RTX):
case PATH(SOURCE_MIC, SINK_MCU):
#if !defined(PLATFORM_MD9600) && !defined(MDx_ENABLE_SWD)
gpio_setPin(MIC_PWR);
#endif
break;
case PATH(SOURCE_RTX, SINK_SPK):
radio_enableAfOutput();
break;
case PATH(SOURCE_MCU, SINK_SPK):
case PATH(SOURCE_MCU, SINK_RTX):
gpio_setMode(BEEP_OUT, ALTERNATE);
break;
default:
break;
}
if(sink == SINK_SPK)
{
switch(source)
{
case SOURCE_RTX:
radio_enableAfOutput();
break;
case SOURCE_MCU:
gpio_setMode(BEEP_OUT, ALTERNATE);
break;
default:
break;
}
// Anti-pop: unmute speaker after 10ms from amp. power on
#ifndef PLATFORM_MD9600
gpio_setPin(AUDIO_AMP_EN);
@ -103,10 +111,7 @@ void audio_connect(const enum AudioSource source, const enum AudioSink sink)
void audio_disconnect(const enum AudioSource source, const enum AudioSink sink)
{
// If source is MIC, turn it off regardless of the sink
#if !defined(PLATFORM_MD9600) && !defined(MDx_ENABLE_SWD)
if(source == SOURCE_MIC) gpio_clearPin(MIC_PWR);
#endif
uint32_t path = PATH(source, sink);
if(sink == SINK_SPK)
{
@ -114,20 +119,29 @@ void audio_disconnect(const enum AudioSource source, const enum AudioSink sink)
#ifndef PLATFORM_MD9600
gpio_clearPin(AUDIO_AMP_EN);
#endif
}
switch(source)
{
case SOURCE_RTX:
radio_disableAfOutput();
break;
switch(path)
{
case PATH(SOURCE_MIC, SINK_SPK):
case PATH(SOURCE_MIC, SINK_RTX):
case PATH(SOURCE_MIC, SINK_MCU):
#if !defined(PLATFORM_MD9600) && !defined(MDx_ENABLE_SWD)
gpio_clearPin(MIC_PWR);
#endif
break;
case SOURCE_MCU:
gpio_setMode(BEEP_OUT, INPUT); // Set output to Hi-Z
break;
case PATH(SOURCE_RTX, SINK_SPK):
radio_disableAfOutput();
break;
default:
break;
}
case PATH(SOURCE_MCU, SINK_SPK):
case PATH(SOURCE_MCU, SINK_RTX):
gpio_setMode(BEEP_OUT, INPUT); // Set output to Hi-Z
break;
default:
break;
}
}