mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-02-06 16:47:59 +08:00
wip : volume no longer protocol parameter
This commit is contained in:
@@ -50,7 +50,7 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
ggWave->init(input.size(), input.data(), ggWave->getTxProtocols()[txProtocol]);
|
||||
ggWave->init(input.size(), input.data(), ggWave->getTxProtocols()[txProtocol], 50);
|
||||
}
|
||||
inputOld = input;
|
||||
}
|
||||
|
||||
@@ -34,8 +34,8 @@ GGWave *g_ggWave = nullptr;
|
||||
// JS interface
|
||||
extern "C" {
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
int setText(int textLength, const char * text, int protocolId) {
|
||||
g_ggWave->init(textLength, text, g_ggWave->getTxProtocols()[protocolId]);
|
||||
int sendData(int textLength, const char * text, int protocolId, int volume) {
|
||||
g_ggWave->init(textLength, text, g_ggWave->getTxProtocols()[protocolId], volume);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -137,7 +137,7 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
|
||||
if (inputCurrent.update) {
|
||||
ggWave->init(inputCurrent.message.data.size(), inputCurrent.message.data.data(), ggWave->getTxProtocols()[2]);
|
||||
ggWave->init(inputCurrent.message.data.size(), inputCurrent.message.data.data(), ggWave->getTxProtocols()[2], 50);
|
||||
inputCurrent.update = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,17 +26,26 @@ public:
|
||||
int freqStart;
|
||||
int framesPerTx;
|
||||
int bytesPerTx;
|
||||
int volume;
|
||||
|
||||
int nDataBitsPerTx() const { return 8*bytesPerTx; }
|
||||
};
|
||||
|
||||
using TxProtocols = std::vector<TxProtocol>;
|
||||
|
||||
const TxProtocols kTxProtocols {
|
||||
{ "Normal", 40, 9, 3, },
|
||||
{ "Fast", 40, 6, 3, },
|
||||
{ "Fastest", 40, 3, 3, },
|
||||
{ "[U] Normal", 320, 9, 3, },
|
||||
{ "[U] Fast", 320, 6, 3, },
|
||||
{ "[U] Fastest", 320, 3, 3, },
|
||||
};
|
||||
|
||||
using AmplitudeData = std::array<float, kMaxSamplesPerFrame>;
|
||||
using AmplitudeData16 = std::array<int16_t, kMaxRecordedFrames*kMaxSamplesPerFrame>;
|
||||
using SpectrumData = std::array<float, kMaxSamplesPerFrame>;
|
||||
using RecordedData = std::array<float, kMaxRecordedFrames*kMaxSamplesPerFrame>;
|
||||
using TxRxData = std::array<std::uint8_t, kMaxDataSize>;
|
||||
using TxProtocols = std::vector<TxProtocol>;
|
||||
|
||||
using CBQueueAudio = std::function<void(const void * data, uint32_t nBytes)>;
|
||||
using CBDequeueAudio = std::function<uint32_t(void * data, uint32_t nMaxBytes)>;
|
||||
@@ -49,7 +58,7 @@ public:
|
||||
int sampleSizeBytesOut);
|
||||
~GGWave();
|
||||
|
||||
bool init(int textLength, const char * stext, const TxProtocol & aProtocol);
|
||||
bool init(int textLength, const char * stext, const TxProtocol & aProtocol, const int volume);
|
||||
void send(const CBQueueAudio & cbQueueAudio);
|
||||
void receive(const CBDequeueAudio & CBDequeueAudio);
|
||||
|
||||
@@ -63,24 +72,15 @@ public:
|
||||
const int & getSampleSizeBytesIn() const { return m_sampleSizeBytesIn; }
|
||||
const int & getSampleSizeBytesOut() const { return m_sampleSizeBytesOut; }
|
||||
|
||||
const float & getSampleRateIn() const { return m_sampleRateIn; }
|
||||
const float & getSampleRateIn() const { return m_sampleRateIn; }
|
||||
|
||||
const TxProtocol & getDefultTxProtocol() const { return kTxProtocols[1]; }
|
||||
const TxProtocols & getTxProtocols() const { return kTxProtocols; }
|
||||
|
||||
const TxRxData & getRxData() const { return m_rxData; }
|
||||
const TxProtocol & getDefultTxProtocol() const { return txProtocols[1]; }
|
||||
const TxProtocols & getTxProtocols() const { return txProtocols; }
|
||||
|
||||
int takeRxData(TxRxData & dst);
|
||||
|
||||
private:
|
||||
const TxProtocols txProtocols {
|
||||
{ "Normal", 40, 9, 3, 50 },
|
||||
{ "Fast", 40, 6, 3, 50 },
|
||||
{ "Fastest", 40, 3, 3, 50 },
|
||||
{ "[U] Normal", 320, 9, 3, 50 },
|
||||
{ "[U] Fast", 320, 6, 3, 50 },
|
||||
{ "[U] Fastest", 320, 3, 3, 50 },
|
||||
};
|
||||
|
||||
int maxFramesPerTx() const;
|
||||
int minBytesPerTx() const;
|
||||
|
||||
@@ -139,6 +139,7 @@ private:
|
||||
bool m_hasNewTxData;
|
||||
int m_nECCBytesPerTx;
|
||||
int m_sendDataLength;
|
||||
float m_sendVolume;
|
||||
|
||||
int m_txDataLength;
|
||||
TxRxData m_txData;
|
||||
|
||||
@@ -130,13 +130,13 @@ GGWave::GGWave(
|
||||
m_encodedDataOffset(3),
|
||||
m_rsLength(new RS::ReedSolomon(1, m_encodedDataOffset - 1))
|
||||
{
|
||||
init(0, "", getDefultTxProtocol());
|
||||
init(0, "", getDefultTxProtocol(), 0);
|
||||
}
|
||||
|
||||
GGWave::~GGWave() {
|
||||
}
|
||||
|
||||
bool GGWave::init(int textLength, const char * stext, const TxProtocol & aProtocol) {
|
||||
bool GGWave::init(int textLength, const char * stext, const TxProtocol & aProtocol, const int volume) {
|
||||
if (textLength > kMaxLength) {
|
||||
printf("Truncating data from %d to 140 bytes\n", textLength);
|
||||
textLength = kMaxLength;
|
||||
@@ -144,6 +144,7 @@ bool GGWave::init(int textLength, const char * stext, const TxProtocol & aProtoc
|
||||
|
||||
m_txProtocol = aProtocol;
|
||||
m_txDataLength = textLength;
|
||||
m_sendVolume = ((double)(volume))/100.0f;
|
||||
|
||||
const uint8_t * text = reinterpret_cast<const uint8_t *>(stext);
|
||||
|
||||
@@ -191,8 +192,6 @@ void GGWave::send(const CBQueueAudio & cbQueueAudio) {
|
||||
|
||||
int frameId = 0;
|
||||
|
||||
float sendVolume = ((double)(m_txProtocol.volume))/100.0f;
|
||||
|
||||
AmplitudeData outputBlock;
|
||||
AmplitudeData16 outputBlock16;
|
||||
|
||||
@@ -264,9 +263,9 @@ void GGWave::send(const CBQueueAudio & cbQueueAudio) {
|
||||
|
||||
for (int i = 0; i < m_nBitsInMarker; ++i) {
|
||||
if (i%2 == 0) {
|
||||
::addAmplitudeSmooth(bit1Amplitude[i], outputBlock, sendVolume, 0, samplesPerFrameOut, frameId, m_nMarkerFrames);
|
||||
::addAmplitudeSmooth(bit1Amplitude[i], outputBlock, m_sendVolume, 0, samplesPerFrameOut, frameId, m_nMarkerFrames);
|
||||
} else {
|
||||
::addAmplitudeSmooth(bit0Amplitude[i], outputBlock, sendVolume, 0, samplesPerFrameOut, frameId, m_nMarkerFrames);
|
||||
::addAmplitudeSmooth(bit0Amplitude[i], outputBlock, m_sendVolume, 0, samplesPerFrameOut, frameId, m_nMarkerFrames);
|
||||
}
|
||||
}
|
||||
} else if (frameId < m_nMarkerFrames + m_nPostMarkerFrames) {
|
||||
@@ -274,9 +273,9 @@ void GGWave::send(const CBQueueAudio & cbQueueAudio) {
|
||||
|
||||
for (int i = 0; i < m_nBitsInMarker; ++i) {
|
||||
if (i%2 == 0) {
|
||||
::addAmplitudeSmooth(bit0Amplitude[i], outputBlock, sendVolume, 0, samplesPerFrameOut, frameId - m_nMarkerFrames, m_nPostMarkerFrames);
|
||||
::addAmplitudeSmooth(bit0Amplitude[i], outputBlock, m_sendVolume, 0, samplesPerFrameOut, frameId - m_nMarkerFrames, m_nPostMarkerFrames);
|
||||
} else {
|
||||
::addAmplitudeSmooth(bit1Amplitude[i], outputBlock, sendVolume, 0, samplesPerFrameOut, frameId - m_nMarkerFrames, m_nPostMarkerFrames);
|
||||
::addAmplitudeSmooth(bit1Amplitude[i], outputBlock, m_sendVolume, 0, samplesPerFrameOut, frameId - m_nMarkerFrames, m_nPostMarkerFrames);
|
||||
}
|
||||
}
|
||||
} else if (frameId <
|
||||
@@ -305,9 +304,9 @@ void GGWave::send(const CBQueueAudio & cbQueueAudio) {
|
||||
|
||||
++nFreq;
|
||||
if (k%2) {
|
||||
::addAmplitudeSmooth(bit0Amplitude[k/2], outputBlock, sendVolume, 0, samplesPerFrameOut, cycleModMain, m_txProtocol.framesPerTx);
|
||||
::addAmplitudeSmooth(bit0Amplitude[k/2], outputBlock, m_sendVolume, 0, samplesPerFrameOut, cycleModMain, m_txProtocol.framesPerTx);
|
||||
} else {
|
||||
::addAmplitudeSmooth(bit1Amplitude[k/2], outputBlock, sendVolume, 0, samplesPerFrameOut, cycleModMain, m_txProtocol.framesPerTx);
|
||||
::addAmplitudeSmooth(bit1Amplitude[k/2], outputBlock, m_sendVolume, 0, samplesPerFrameOut, cycleModMain, m_txProtocol.framesPerTx);
|
||||
}
|
||||
}
|
||||
} else if (frameId <
|
||||
@@ -319,9 +318,9 @@ void GGWave::send(const CBQueueAudio & cbQueueAudio) {
|
||||
int fId = frameId - ((m_nMarkerFrames + m_nPostMarkerFrames) + ((m_sendDataLength + m_nECCBytesPerTx)/m_txProtocol.bytesPerTx + 2)*m_txProtocol.framesPerTx);
|
||||
for (int i = 0; i < m_nBitsInMarker; ++i) {
|
||||
if (i%2 == 0) {
|
||||
addAmplitudeSmooth(bit0Amplitude[i], outputBlock, sendVolume, 0, samplesPerFrameOut, fId, m_nMarkerFrames);
|
||||
addAmplitudeSmooth(bit0Amplitude[i], outputBlock, m_sendVolume, 0, samplesPerFrameOut, fId, m_nMarkerFrames);
|
||||
} else {
|
||||
addAmplitudeSmooth(bit1Amplitude[i], outputBlock, sendVolume, 0, samplesPerFrameOut, fId, m_nMarkerFrames);
|
||||
addAmplitudeSmooth(bit1Amplitude[i], outputBlock, m_sendVolume, 0, samplesPerFrameOut, fId, m_nMarkerFrames);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -408,7 +407,7 @@ void GGWave::receive(const CBDequeueAudio & CBDequeueAudio) {
|
||||
std::unique_ptr<RS::ReedSolomon> rsData;
|
||||
|
||||
bool isValid = false;
|
||||
for (const auto & rxProtocol : txProtocols) {
|
||||
for (const auto & rxProtocol : kTxProtocols) {
|
||||
// skip Rx protocol if start frequency is different from detected one
|
||||
if (rxProtocol.freqStart != m_markerFreqStart) {
|
||||
continue;
|
||||
@@ -529,7 +528,7 @@ void GGWave::receive(const CBDequeueAudio & CBDequeueAudio) {
|
||||
if (m_receivingData == false) {
|
||||
bool isReceiving = false;
|
||||
|
||||
for (const auto & rxProtocol : txProtocols) {
|
||||
for (const auto & rxProtocol : kTxProtocols) {
|
||||
bool isReceivingCur = true;
|
||||
|
||||
for (int i = 0; i < m_nBitsInMarker; ++i) {
|
||||
@@ -568,7 +567,7 @@ void GGWave::receive(const CBDequeueAudio & CBDequeueAudio) {
|
||||
} else {
|
||||
bool isEnded = false;
|
||||
|
||||
for (const auto & rxProtocol : txProtocols) {
|
||||
for (const auto & rxProtocol : kTxProtocols) {
|
||||
bool isEndedCur = true;
|
||||
|
||||
for (int i = 0; i < m_nBitsInMarker; ++i) {
|
||||
@@ -613,15 +612,15 @@ int GGWave::takeRxData(TxRxData & dst) {
|
||||
|
||||
int GGWave::maxFramesPerTx() const {
|
||||
int res = 0;
|
||||
for (const auto & protocol : txProtocols) {
|
||||
for (const auto & protocol : kTxProtocols) {
|
||||
res = std::max(res, protocol.framesPerTx);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int GGWave::minBytesPerTx() const {
|
||||
int res = txProtocols.front().framesPerTx;
|
||||
for (const auto & protocol : txProtocols) {
|
||||
int res = kTxProtocols.front().framesPerTx;
|
||||
for (const auto & protocol : kTxProtocols) {
|
||||
res = std::min(res, protocol.bytesPerTx);
|
||||
}
|
||||
return res;
|
||||
|
||||
Reference in New Issue
Block a user