You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

149 lines
6.5 KiB

///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Dan Chokola, KD2HSG //
// Based on the packet modulator plugin: //
// Copyright (C) 2020-2023 Jon Beniston, M7RCE <jon@beniston.com> //
// Copyright (C) 2020-2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// //
// 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 //
// the Free Software Foundation as version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_CHIRPMODEMMODSOURCE_H
#define INCLUDE_CHIRPMODEMMODSOURCE_H
#include <deque>
#include <memory>
#include <vector>
#include <QtGlobal>
#include "dsp/channelsamplesource.h"
#include "util/message.h"
#include "util/messagequeue.h"
#include "chirpmodemmodsettings.h"
#include "chirpmodemmodmessages.h"
class BasebandSampleSink;
namespace ChirpModemModGen {
//! What to generate: a request resolved against the settings, for one channel rate and offset
struct Job {
QString m_source;
QString m_destination;
QByteArray m_body;
ChirpModemModSettings m_settings; //!< As when requested
int m_channelSampleRate = 0;
int m_channelFrequencyOffset = 0;
quint64 m_epoch = 0; //!< Results from before a reset are dropped
bool m_regenerated = false; //!< Made again after a rate change: keeps the last error
};
//! A generated transmission: the frames, separated by the gap, then the gap and the CW ID
struct Transmission {
Job m_job;
std::vector<float> m_iq; //!< Interleaved I/Q, full scale 1.0
size_t m_position = 0; //!< Next complex sample to play
int m_frames = 0;
QString m_error; //!< Not empty if generation failed
};
//! Thread safe: uses only its own libchirpmodem transmitter handle
void generate(Transmission& transmission);
//! A finished generation, posted from the generator thread to the baseband
class MsgGenerated : public Message {
MESSAGE_CLASS_DECLARATION
public:
static MsgGenerated *create(std::shared_ptr<Transmission> transmission) { return new MsgGenerated(transmission); }
std::shared_ptr<Transmission> getTransmission() const { return m_transmission; }
private:
explicit MsgGenerated(std::shared_ptr<Transmission> transmission) : m_transmission(transmission) {}
std::shared_ptr<Transmission> m_transmission;
};
} // namespace ChirpModemModGen
// Plays chirpmodem transmissions at the channel sample rate.
//
// Threading: every method runs on the baseband thread with the baseband mutex
// held, except getLevels() and getMagSq(), which read single values without
// waiting, as in other modulators. Transmissions are generated on the
// baseband's generator thread (ChirpModemModGen::generate), never here, so the
// sample FIFO is always refilled in time.
//
// A transmission is played only at the channel rate and offset it was made
// for. If those change, the one on air is cut off and the waiting ones are
// handed back to be generated again.
class ChirpModemModSource : public ChannelSampleSource
{
public:
ChirpModemModSource();
virtual ~ChirpModemModSource();
virtual void pull(SampleVector::iterator begin, unsigned int nbSamples);
virtual void pullOne(Sample& sample);
virtual void prefetch(unsigned int nbSamples) { (void) nbSamples; }
void applySettings(const QStringList& settingsKeys, const ChirpModemModSettings& settings, bool force = false);
//! Returns the jobs of waiting transmissions, which must be generated again
std::vector<ChirpModemModGen::Job> applyChannelSettings(int channelSampleRate, int channelFrequencyOffset);
//! A request resolved against the current settings, rate and offset, to be generated
ChirpModemModGen::Job makeJob(const ChirpModemModMessages::TxRequest& request);
//! A finished generation. Returns false if it is stale and must be generated again.
bool addGenerated(std::shared_ptr<ChirpModemModGen::Transmission> transmission);
void generationStarted() { m_pending++; sendStatus(QStringLiteral("generating")); }
void reset(); //!< Drop everything queued or on air, zero the counters
void setSpectrumSink(BasebandSampleSink *sampleSink) { m_spectrumSink = sampleSink; }
void setMessageQueueToChannel(MessageQueue *queue) { m_messageQueueToChannel = queue; }
int getChannelSampleRate() const { return m_channelSampleRate; }
double getMagSq() const { return m_rmsLevel * m_rmsLevel; } //!< Mean over the last 10 ms
void getLevels(qreal& rmsLevel, qreal& peakLevel, int& numSamples) const
{
rmsLevel = m_rmsLevel;
peakLevel = m_peakLevel;
numSamples = m_levelNbSamples;
}
private:
ChirpModemModSettings m_settings;
MessageQueue *m_messageQueueToChannel = nullptr;
BasebandSampleSink *m_spectrumSink = nullptr;
int m_channelSampleRate = 0;
int m_channelFrequencyOffset = 0;
quint64 m_epoch = 0;
std::deque<std::shared_ptr<ChirpModemModGen::Transmission>> m_queue;
std::shared_ptr<ChirpModemModGen::Transmission> m_current; //!< On air, or null
int m_pending = 0; //!< Being generated
qint64 m_framesSent = 0;
qint64 m_transmissions = 0;
qint64 m_samplesOnAir = 0;
double m_lastDuration = 0;
QString m_lastError;
double m_levelSum = 0;
double m_levelPeak = 0;
int m_levelCount = 0;
int m_levelNbSamples = 2500; //!< 10 ms at the channel rate
qreal m_rmsLevel = 0;
qreal m_peakLevel = 0;
void sendStatus(const QString& event);
};
#endif // INCLUDE_CHIRPMODEMMODSOURCE_H