mirror of
https://github.com/ggerganov/ggwave.git
synced 2026-03-24 12:41:07 +08:00
wip : refactor GGWave
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace RS {
|
||||
class ReedSolomon;
|
||||
@@ -22,10 +23,12 @@ public:
|
||||
struct TxProtocol {
|
||||
const char * name;
|
||||
|
||||
int paramFreqStart;
|
||||
int paramFramesPerTx;
|
||||
int paramBytesPerTx;
|
||||
int paramVolume;
|
||||
int freqStart;
|
||||
int framesPerTx;
|
||||
int bytesPerTx;
|
||||
int volume;
|
||||
|
||||
int nDataBitsPerTx() const { return 8*bytesPerTx; }
|
||||
};
|
||||
|
||||
using AmplitudeData = std::array<float, kMaxSamplesPerFrame>;
|
||||
@@ -39,142 +42,109 @@ public:
|
||||
using CBDequeueAudio = std::function<uint32_t(void * data, uint32_t nMaxBytes)>;
|
||||
|
||||
GGWave(
|
||||
int aSampleRateIn,
|
||||
int aSampleRateOut,
|
||||
int aSamplesPerFrame,
|
||||
int aSampleSizeBytesIn,
|
||||
int aSampleSizeBytesOut);
|
||||
int sampleRateIn,
|
||||
int sampleRateOut,
|
||||
int samplesPerFrame,
|
||||
int sampleSizeBytesIn,
|
||||
int sampleSizeBytesOut);
|
||||
~GGWave();
|
||||
|
||||
bool init(int textLength, const char * stext, const TxProtocol & aProtocol);
|
||||
void send(const CBQueueAudio & cbQueueAudio);
|
||||
void receive(const CBDequeueAudio & CBDequeueAudio);
|
||||
|
||||
const bool & getHasData() const { return hasData; }
|
||||
const bool & hasTxData() const { return m_hasNewTxData; }
|
||||
|
||||
const int & getFramesToRecord() const { return framesToRecord; }
|
||||
const int & getFramesLeftToRecord() const { return framesLeftToRecord; }
|
||||
const int & getFramesToAnalyze() const { return framesToAnalyze; }
|
||||
const int & getFramesLeftToAnalyze() const { return framesLeftToAnalyze; }
|
||||
const int & getSamplesPerFrame() const { return samplesPerFrame; }
|
||||
const int & getSampleSizeBytesIn() const { return sampleSizeBytesIn; }
|
||||
const int & getSampleSizeBytesOut() const { return sampleSizeBytesOut; }
|
||||
const int & getTotalBytesCaptured() const { return totalBytesCaptured; }
|
||||
const int & getFramesToRecord() const { return m_framesToRecord; }
|
||||
const int & getFramesLeftToRecord() const { return m_framesLeftToRecord; }
|
||||
const int & getFramesToAnalyze() const { return m_framesToAnalyze; }
|
||||
const int & getFramesLeftToAnalyze() const { return m_framesLeftToAnalyze; }
|
||||
const int & getSamplesPerFrame() const { return m_samplesPerFrame; }
|
||||
const int & getSampleSizeBytesIn() const { return m_sampleSizeBytesIn; }
|
||||
const int & getSampleSizeBytesOut() const { return m_sampleSizeBytesOut; }
|
||||
|
||||
const float & getSampleRateIn() const { return sampleRateIn; }
|
||||
const float & getAverageRxTime_ms() const { return averageRxTime_ms; }
|
||||
const float & getSampleRateIn() const { return m_sampleRateIn; }
|
||||
|
||||
const TxRxData & getRxData() const { return rxData; }
|
||||
const TxRxData & getRxData() const { return m_rxData; }
|
||||
const TxProtocol & getDefultTxProtocol() const { return txProtocols[1]; }
|
||||
const TxProtocols & getTxProtocols() const { return txProtocols; }
|
||||
|
||||
int takeRxData(TxRxData & dst) {
|
||||
if (lastRxDataLength == 0) return 0;
|
||||
|
||||
auto res = lastRxDataLength;
|
||||
lastRxDataLength = 0;
|
||||
dst = rxData;
|
||||
|
||||
return res;
|
||||
}
|
||||
int takeRxData(TxRxData & dst);
|
||||
|
||||
private:
|
||||
const TxProtocols txProtocols {
|
||||
{ "Normal", 40, 9, 3, 50 },
|
||||
{ "Fast", 40, 6, 3, 50 },
|
||||
{ "Fastest", 40, 3, 3, 50 },
|
||||
{ "Ultrasonic", 320, 9, 3, 50 },
|
||||
{ "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 res = 0;
|
||||
for (const auto & protocol : txProtocols) {
|
||||
res = std::max(res, protocol.paramFramesPerTx);
|
||||
}
|
||||
return res;
|
||||
int maxFramesPerTx() const;
|
||||
int minBytesPerTx() const;
|
||||
|
||||
double bitFreq(const TxProtocol & p, int bit) const {
|
||||
return m_hzPerSample*p.freqStart + m_freqDelta_hz*bit;
|
||||
}
|
||||
|
||||
int minBytesPerTx() const {
|
||||
int res = txProtocols.front().paramFramesPerTx;
|
||||
for (const auto & protocol : txProtocols) {
|
||||
res = std::min(res, protocol.paramBytesPerTx);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
const float m_sampleRateIn;
|
||||
const float m_sampleRateOut;
|
||||
const int m_samplesPerFrame;
|
||||
const float m_isamplesPerFrame;
|
||||
const int m_sampleSizeBytesIn;
|
||||
const int m_sampleSizeBytesOut;
|
||||
|
||||
int nIterations;
|
||||
const float m_hzPerSample;
|
||||
const float m_ihzPerSample;
|
||||
|
||||
const int m_freqDelta_bin;
|
||||
const float m_freqDelta_hz;
|
||||
|
||||
const int m_nBitsInMarker;
|
||||
const int m_nMarkerFrames;
|
||||
const int m_nPostMarkerFrames;
|
||||
const int m_encodedDataOffset;
|
||||
|
||||
// Rx
|
||||
bool receivingData;
|
||||
bool analyzingData;
|
||||
bool hasNewRxData = false;
|
||||
bool m_hasNewRxData;
|
||||
bool m_receivingData;
|
||||
bool m_analyzingData;
|
||||
|
||||
int nCalls = 0;
|
||||
int recvDuration_frames;
|
||||
int totalBytesCaptured;
|
||||
int lastRxDataLength = 0;
|
||||
int m_markerFreqStart;
|
||||
|
||||
float tSum_ms = 0.0f;
|
||||
float averageRxTime_ms = 0.0;
|
||||
int m_recvDuration_frames;
|
||||
int m_lastRxDataLength;
|
||||
|
||||
std::array<float, kMaxSamplesPerFrame> fftIn;
|
||||
std::array<std::complex<float>, kMaxSamplesPerFrame> fftOut;
|
||||
int m_framesLeftToAnalyze;
|
||||
int m_framesLeftToRecord;
|
||||
int m_framesToAnalyze;
|
||||
int m_framesToRecord;
|
||||
|
||||
AmplitudeData sampleAmplitude;
|
||||
SpectrumData sampleSpectrum;
|
||||
std::array<float, kMaxSamplesPerFrame> m_fftIn;
|
||||
std::array<std::complex<float>, kMaxSamplesPerFrame> m_fftOut;
|
||||
|
||||
TxRxData rxData;
|
||||
TxRxData txData;
|
||||
TxRxData txDataEncoded;
|
||||
AmplitudeData m_sampleAmplitude;
|
||||
SpectrumData m_sampleSpectrum;
|
||||
|
||||
int historyId = 0;
|
||||
AmplitudeData sampleAmplitudeAverage;
|
||||
std::array<AmplitudeData, kMaxSpectrumHistory> sampleAmplitudeHistory;
|
||||
TxRxData m_rxData;
|
||||
|
||||
RecordedData recordedAmplitude;
|
||||
int m_historyId = 0;
|
||||
AmplitudeData m_sampleAmplitudeAverage;
|
||||
std::array<AmplitudeData, kMaxSpectrumHistory> m_sampleAmplitudeHistory;
|
||||
|
||||
RecordedData m_recordedAmplitude;
|
||||
|
||||
// Tx
|
||||
bool hasData;
|
||||
bool m_hasNewTxData;
|
||||
int m_nECCBytesPerTx;
|
||||
int m_sendDataLength;
|
||||
|
||||
float freqDelta_hz;
|
||||
float freqStart_hz;
|
||||
float hzPerSample;
|
||||
float ihzPerSample;
|
||||
float isamplesPerFrame;
|
||||
float sampleRateIn;
|
||||
float sampleRateOut;
|
||||
float sendVolume;
|
||||
int m_txDataLength;
|
||||
TxRxData m_txData;
|
||||
TxRxData m_txDataEncoded;
|
||||
|
||||
int frameId;
|
||||
int framesLeftToAnalyze;
|
||||
int framesLeftToRecord;
|
||||
int framesToAnalyze;
|
||||
int framesToRecord;
|
||||
int freqDelta_bin = 1;
|
||||
int nBitsInMarker;
|
||||
int nDataBitsPerTx;
|
||||
int nECCBytesPerTx;
|
||||
int nMarkerFrames;
|
||||
int nPostMarkerFrames;
|
||||
int sampleSizeBytesIn;
|
||||
int sampleSizeBytesOut;
|
||||
int samplesPerFrame;
|
||||
int sendDataLength;
|
||||
TxProtocol m_txProtocol;
|
||||
|
||||
std::string textToSend;
|
||||
|
||||
TxProtocol txProtocol;
|
||||
|
||||
AmplitudeData outputBlock;
|
||||
AmplitudeData16 outputBlock16;
|
||||
|
||||
std::array<bool, kMaxDataBits> dataBits;
|
||||
std::array<double, kMaxDataBits> phaseOffsets;
|
||||
std::array<double, kMaxDataBits> dataFreqs_hz;
|
||||
|
||||
std::array<AmplitudeData, kMaxDataBits> bit1Amplitude;
|
||||
std::array<AmplitudeData, kMaxDataBits> bit0Amplitude;
|
||||
|
||||
RS::ReedSolomon * rsData = nullptr;
|
||||
RS::ReedSolomon * rsLength = nullptr;
|
||||
std::unique_ptr<RS::ReedSolomon> m_rsLength;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user