35 cfg.sample_rate = info.sample_rate;
36 cfg.channels = info.channels;
37 cfg.bits_per_sample = info.bits_per_sample;
43 using audio_tools::AudioEncoder::begin;
53 if (!
cfg.isValid())
return false;
54 if (
cfg.bits_per_sample != 16) {
55 LOGE(
"CSSEncoder: bits_per_sample must be 16, not %d",
56 (
int)
cfg.bits_per_sample);
59 if (
cfg.channels < 1) {
60 LOGE(
"CSSEncoder: invalid number of channels: %d", (
int)
cfg.channels);
77 size_t write(
const uint8_t* data,
size_t len)
override {
79 for (
size_t j = 0; j < len; j++) {
91 for (
size_t j = 0; j < silence_samples; j++)
samples.write(0.0f);
125 int sf =
cfg.spreading_factor;
139 int nsymbols =
cfg.preamble_symbols + 2 + nh + np;
140 total = llround(nsymbols * (
double)
cfg.symbolTime() *
cfg.sample_rate);
142 for (
int j = 0; j <
cfg.preamble_symbols; j++)
sendChirp(0,
true);
143 for (
int j = 0; j < 2; j++)
sendChirp(0,
false);
144 for (
int j = 0; j < nh; j++)
sendChirp(header_symbols[j],
true);
145 for (
int j = 0; j < np; j++)
sendChirp(payload_symbols[j],
true);
154 return f / 2 + (x - f) * (x - f) / 2;
161 double t_sym =
cfg.symbolTime();
163 uint64_t start = llround(t_start *
cfg.sample_rate);
166 double offset = (double)value /
cfg.chips();
167 double cycles_per_unit =
cfg.bandwidth * t_sym;
168 for (uint64_t j = start; j < stop; j++) {
169 double t = j / (double)
cfg.sample_rate - t_start;
170 double u = t / t_sym;
174 double ph =
phase + 2.0 * M_PI * (
cfg.lowFreq() * t + cycles_per_unit * sweep);
179 samples.write(gain * (
float)sin(ph));
183 phase = fmod(
phase + 2.0 * M_PI *
cfg.center_freq * t_sym, 2.0 * M_PI);
size_t frames
Definition CSSEncoder.h:117
bool isTransmitting() override
true while data is waiting to be sent
Definition CSSEncoder.h:97
bool begin() override
Definition CSSEncoder.h:51
CSSConfig cfg
Definition CSSEncoder.h:112
uint8_t frame[CSSConfig::MAX_FRAME]
Definition CSSEncoder.h:115
size_t ramp
Definition CSSEncoder.h:121
int frame_len
Definition CSSEncoder.h:116
size_t write(const uint8_t *data, size_t len) override
Collects the data: a frame is sent when max_frame bytes are available.
Definition CSSEncoder.h:77
size_t written
Definition CSSEncoder.h:122
bool endTransmission(size_t silence_samples=0) override
Definition CSSEncoder.h:88
void sendChirp(int value, bool up)
Definition CSSEncoder.h:160
void sendFrame()
Definition CSSEncoder.h:124
double phase
Definition CSSEncoder.h:119
void setAudioInfo(audio_tools::AudioInfo info) override
Defines the sample rate and number of channels of the generated audio.
Definition CSSEncoder.h:34
void end() override
Definition CSSEncoder.h:71
bool begin(CSSConfig config)
Starts the processing with the indicated configuration.
Definition CSSEncoder.h:46
static constexpr float RAMP_SEC
Definition CSSEncoder.h:111
CSSConfig & config()
The configuration which was provided in begin().
Definition CSSEncoder.h:106
float symbolRate() override
Symbols per second.
Definition CSSEncoder.h:100
uint64_t symbol_index
Definition CSSEncoder.h:120
void setOutput(Print &out) override
Definition CSSEncoder.h:28
bool is_active
Definition CSSEncoder.h:114
audio_tools::AudioInfo audioInfo() override
Definition CSSEncoder.h:41
SampleOutput samples
Definition CSSEncoder.h:113
size_t frameCount()
Number of sent frames.
Definition CSSEncoder.h:103
CSSConfig defaultConfig()
Provides a default configuration: css7 (SF 7, 1000 Hz).
Definition CSSEncoder.h:26
static double sawIntegral(double x)
Integral of frac(x): the normalized frequency of a chirp is a saw tooth.
Definition CSSEncoder.h:152
CSSEncoder(Print &out)
Definition CSSEncoder.h:23
size_t total
Definition CSSEncoder.h:122
static constexpr int HEADER_BYTES
Definition CSS.h:154
static int toSymbols(const uint8_t *data, int len, int sf, uint16_t *out)
Converts bytes into symbols.
Definition CSS.h:196
static void payload(const uint8_t *data, int len, int ecc, uint8_t *out)
Payload code word: data, CRC-16 and parity.
Definition CSS.h:176
static int payloadBytes(int len, int ecc)
bytes of the payload code word
Definition CSS.h:160
static void header(int len, uint8_t *out)
Header code word: length and parity.
Definition CSS.h:163
Common API of the modulators (FSKEncoder, MFSKEncoder): the written data is converted to 16 bit PCM a...
Definition ModemBase.h:286
void setOutput(Print &out) override
Definition ModemBase.h:288
ToneOutput tones
Definition ModemBase.h:334
static constexpr int MAX_ECC
Definition ReedSolomon.h:24
Writes float samples (-1.0 to 1.0) as 16 bit PCM with the requested number of channels and volume to ...
Definition ModemBase.h:118
TinyModem: software audio modems and packets with error correction.
Definition AX25.h:11
Configuration of the chirp spread spectrum modem (CSSEncoder, CSSDecoder), which works like LoRa in t...
Definition CSS.h:31
static constexpr int MAX_FRAME
max payload of a frame
Definition CSS.h:118