mirror of https://github.com/g4klx/MMDVM.git
Merge pull request #254 from F4FXL/FM_Ext
Handle Incoming external audio
This commit is contained in:
commit
973a1391d7
74
FM.cpp
74
FM.cpp
|
@ -51,10 +51,11 @@ m_useCOS(true),
|
||||||
m_cosInvert(false),
|
m_cosInvert(false),
|
||||||
m_rfAudioBoost(1U),
|
m_rfAudioBoost(1U),
|
||||||
m_extAudioBoost(1U),
|
m_extAudioBoost(1U),
|
||||||
m_downsampler(128U),//Size might need adjustement
|
m_downsampler(1200U),// 100 ms of audio
|
||||||
m_extEnabled(false),
|
m_extEnabled(false),
|
||||||
m_rxLevel(1),
|
m_rxLevel(1),
|
||||||
m_outputRB(2400U) // 100ms of audio
|
m_outputRFRB(2400U), // 100ms of audio
|
||||||
|
m_inputExtRB(2400U) //100ms of Audio
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,46 +73,48 @@ void CFM::samples(bool cos, const q15_t* samples, uint8_t length)
|
||||||
uint8_t i = 0U;
|
uint8_t i = 0U;
|
||||||
for (; i < length; i++) {
|
for (; i < length; i++) {
|
||||||
// ARMv7-M has hardware integer division
|
// ARMv7-M has hardware integer division
|
||||||
q15_t currentSample = q15_t((q31_t(samples[i]) << 8) / m_rxLevel);
|
q15_t currentRFSample = q15_t((q31_t(samples[i]) << 8) / m_rxLevel);
|
||||||
|
uint8_t ctcssState = m_ctcssRX.process(currentRFSample);
|
||||||
|
|
||||||
uint8_t ctcssState = m_ctcssRX.process(currentSample);
|
q15_t currentExtSample;
|
||||||
|
bool inputExt = m_inputExtRB.get(currentExtSample);//always consume the external input data so it does not overflow
|
||||||
|
|
||||||
if (CTCSS_NOT_READY(ctcssState) && m_modemState != STATE_FM) {
|
if ((!inputExt || CTCSS_NOT_READY(ctcssState)) && m_modemState != STATE_FM) {
|
||||||
//Not enough samples to determine if you have CTCSS, just carry on
|
//Not enough samples to determine if you have CTCSS, just carry on.
|
||||||
continue;
|
continue;
|
||||||
} else if (CTCSS_READY(ctcssState) && m_modemState != STATE_FM) {
|
} else if ((inputExt || CTCSS_READY(ctcssState)) && m_modemState != STATE_FM) {
|
||||||
//we had enough samples for CTCSS and we are in some other mode than FM
|
//we had enough samples for CTCSS and we are in some other mode than FM
|
||||||
bool validCTCSS = CTCSS_VALID(ctcssState);
|
bool validCTCSS = CTCSS_VALID(ctcssState);
|
||||||
// XXX Need to have somewhere to get the ext audio state
|
stateMachine(validCTCSS && cos, inputExt);
|
||||||
stateMachine(validCTCSS && cos, false);
|
|
||||||
if (m_modemState != STATE_FM)
|
if (m_modemState != STATE_FM)
|
||||||
continue;
|
continue;
|
||||||
} else if (CTCSS_READY(ctcssState) && m_modemState == STATE_FM) {
|
} else if ((inputExt || CTCSS_READY(ctcssState)) && m_modemState == STATE_FM) {
|
||||||
//We had enough samples for CTCSS and we are in FM mode, trigger the state machine
|
//We had enough samples for CTCSS and we are in FM mode, trigger the state machine
|
||||||
bool validCTCSS = CTCSS_VALID(ctcssState);
|
bool validCTCSS = CTCSS_VALID(ctcssState);
|
||||||
// XXX Need to have somewhere to get the ext audio state
|
stateMachine(validCTCSS && cos, inputExt);
|
||||||
stateMachine(validCTCSS && cos, false);
|
|
||||||
if (m_modemState != STATE_FM)
|
if (m_modemState != STATE_FM)
|
||||||
break;
|
break;
|
||||||
} else if (CTCSS_NOT_READY(ctcssState) && m_modemState == STATE_FM && i == length - 1) {
|
} else if ((inputExt || CTCSS_NOT_READY(ctcssState)) && m_modemState == STATE_FM && i == length - 1) {
|
||||||
//Not enough samples for CTCSS but we already are in FM, trigger the state machine
|
//Not enough samples for CTCSS but we already are in FM, trigger the state machine
|
||||||
//but do not trigger the state machine on every single sample, save CPU!
|
//but do not trigger the state machine on every single sample, save CPU!
|
||||||
bool validCTCSS = CTCSS_VALID(ctcssState);
|
bool validCTCSS = CTCSS_VALID(ctcssState);
|
||||||
// XXX Need to have somewhere to get the ext audio state
|
stateMachine(validCTCSS && cos, inputExt);
|
||||||
stateMachine(validCTCSS && cos, false);
|
}
|
||||||
|
|
||||||
|
q15_t currentSample = currentRFSample;
|
||||||
|
q15_t currentBoost = m_rfAudioBoost;
|
||||||
|
if(m_state == FS_RELAYING_EXT || m_state == FS_KERCHUNK_EXT){
|
||||||
|
currentSample = currentExtSample;
|
||||||
|
currentBoost = m_extAudioBoost;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only let RF audio through when relaying RF audio
|
// Only let RF audio through when relaying RF audio
|
||||||
if (m_state == FS_RELAYING_RF || m_state == FS_KERCHUNK_RF) {
|
if (m_state == FS_RELAYING_RF || m_state == FS_KERCHUNK_RF || m_state == FS_RELAYING_EXT || m_state == FS_KERCHUNK_EXT) {
|
||||||
if (m_extEnabled)
|
if (m_extEnabled && (m_state == FS_RELAYING_RF || m_state == FS_KERCHUNK_RF))
|
||||||
m_downsampler.addSample(currentSample);
|
m_downsampler.addSample(currentSample);
|
||||||
|
|
||||||
currentSample = m_blanking.process(currentSample);
|
currentSample = m_blanking.process(currentSample);
|
||||||
currentSample *= m_rfAudioBoost;
|
currentSample *= currentBoost;
|
||||||
} else if (m_state == FS_RELAYING_EXT || m_state == FS_KERCHUNK_EXT) {
|
|
||||||
// XXX Where do we receive the ext audio?
|
|
||||||
currentSample = m_blanking.process(currentSample);
|
|
||||||
currentSample *= m_extAudioBoost;
|
|
||||||
} else {
|
} else {
|
||||||
currentSample = 0;
|
currentSample = 0;
|
||||||
}
|
}
|
||||||
|
@ -137,7 +140,7 @@ void CFM::samples(bool cos, const q15_t* samples, uint8_t length)
|
||||||
currentSample += m_ctcssTX.getAudio();
|
currentSample += m_ctcssTX.getAudio();
|
||||||
|
|
||||||
if (m_modemState == STATE_FM)
|
if (m_modemState == STATE_FM)
|
||||||
m_outputRB.put(currentSample);
|
m_outputRFRB.put(currentSample);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,7 +149,7 @@ void CFM::process()
|
||||||
if (m_modemState != STATE_FM)
|
if (m_modemState != STATE_FM)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
uint16_t length = m_outputRB.getData();
|
uint16_t length = m_outputRFRB.getData();
|
||||||
if (length == 0U)
|
if (length == 0U)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -161,7 +164,7 @@ void CFM::process()
|
||||||
|
|
||||||
for (uint16_t i = 0U; i < length; i++) {
|
for (uint16_t i = 0U; i < length; i++) {
|
||||||
q15_t sample;
|
q15_t sample;
|
||||||
m_outputRB.get(sample);
|
m_outputRFRB.get(sample);
|
||||||
io.write(STATE_FM, &sample, 1U);
|
io.write(STATE_FM, &sample, 1U);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,7 +186,7 @@ void CFM::reset()
|
||||||
m_callsign.stop();
|
m_callsign.stop();
|
||||||
m_timeoutTone.stop();
|
m_timeoutTone.stop();
|
||||||
|
|
||||||
m_outputRB.reset();
|
m_outputRFRB.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t CFM::setCallsign(const char* callsign, uint8_t speed, uint16_t frequency, uint8_t time, uint8_t holdoff, uint8_t highLevel, uint8_t lowLevel, bool callsignAtStart, bool callsignAtEnd, bool callsignAtLatch)
|
uint8_t CFM::setCallsign(const char* callsign, uint8_t speed, uint16_t frequency, uint8_t time, uint8_t holdoff, uint8_t highLevel, uint8_t lowLevel, bool callsignAtStart, bool callsignAtEnd, bool callsignAtLatch)
|
||||||
|
@ -656,14 +659,11 @@ void CFM::beginRelaying()
|
||||||
uint8_t CFM::getSpace() const
|
uint8_t CFM::getSpace() const
|
||||||
{
|
{
|
||||||
// The amount of free space for receiving external audio, in bytes.
|
// The amount of free space for receiving external audio, in bytes.
|
||||||
return 0U;
|
return m_inputExtRB.getSpace();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t CFM::writeData(const uint8_t* data, uint8_t length)
|
uint8_t CFM::writeData(const uint8_t* data, uint8_t length)
|
||||||
{
|
{
|
||||||
q15_t samples[170U];
|
|
||||||
uint8_t nSamples = 0U;
|
|
||||||
|
|
||||||
for (uint8_t i = 0U; i < length; i += 3U) {
|
for (uint8_t i = 0U; i < length; i += 3U) {
|
||||||
uint16_t sample1 = 0U;
|
uint16_t sample1 = 0U;
|
||||||
uint16_t sample2 = 0U;
|
uint16_t sample2 = 0U;
|
||||||
|
@ -679,9 +679,15 @@ uint8_t CFM::writeData(const uint8_t* data, uint8_t length)
|
||||||
sample2 = uint16_t(pack & MASK);
|
sample2 = uint16_t(pack & MASK);
|
||||||
sample1 = uint16_t(pack >> 12);
|
sample1 = uint16_t(pack >> 12);
|
||||||
|
|
||||||
// Convert from uint16_t (0 - +4095) to Q15 (-2048 - +2047)
|
// Convert from uint16_t (0 - +4095) to Q15 (-2048 - +2047).
|
||||||
samples[nSamples++] = q15_t(sample1) - 2048;
|
// Incoming data has sample rate 8kHz, just add 2 empty samples after
|
||||||
samples[nSamples++] = q15_t(sample2) - 2048;
|
// every incoming sample to upsample to 24kHz
|
||||||
|
m_inputExtRB.put(q15_t(sample1) - 2048);
|
||||||
|
m_inputExtRB.put(0);
|
||||||
|
m_inputExtRB.put(0);
|
||||||
|
m_inputExtRB.put(q15_t(sample2) - 2048);
|
||||||
|
m_inputExtRB.put(0);
|
||||||
|
m_inputExtRB.put(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Received audio is now in Q15 format in samples, with length nSamples.
|
// Received audio is now in Q15 format in samples, with length nSamples.
|
||||||
|
@ -694,5 +700,5 @@ void CFM::insertSilence(uint16_t ms)
|
||||||
uint32_t nSamples = ms * 24U;
|
uint32_t nSamples = ms * 24U;
|
||||||
|
|
||||||
for (uint32_t i = 0U; i < nSamples; i++)
|
for (uint32_t i = 0U; i < nSamples; i++)
|
||||||
m_outputRB.put(0);
|
m_outputRFRB.put(0);
|
||||||
}
|
}
|
||||||
|
|
3
FM.h
3
FM.h
|
@ -95,7 +95,8 @@ private:
|
||||||
CFMDownsampler m_downsampler;
|
CFMDownsampler m_downsampler;
|
||||||
bool m_extEnabled;
|
bool m_extEnabled;
|
||||||
q15_t m_rxLevel;
|
q15_t m_rxLevel;
|
||||||
CFMRB m_outputRB;
|
CFMRB m_outputRFRB;
|
||||||
|
CFMRB m_inputExtRB;
|
||||||
|
|
||||||
void stateMachine(bool validRFSignal, bool validExtSignal);
|
void stateMachine(bool validRFSignal, bool validExtSignal);
|
||||||
void listeningState(bool validRFSignal, bool validExtSignal);
|
void listeningState(bool validRFSignal, bool validExtSignal);
|
||||||
|
|
Loading…
Reference in New Issue