kgoba-ft8_lib/gen_ft8.cpp

105 wiersze
2.9 KiB
C++
Czysty Zwykły widok Historia

2018-10-18 07:42:43 +00:00
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
2018-11-02 12:03:28 +00:00
#include "common/wave.h"
2018-12-22 14:09:43 +00:00
//#include "ft8/v1/pack.h"
//#include "ft8/v1/encode.h"
#include "ft8/pack.h"
#include "ft8/encode.h"
2018-12-22 14:09:43 +00:00
#include "ft8/constants.h"
2018-10-18 07:42:43 +00:00
2018-11-02 07:55:54 +00:00
// Convert a sequence of symbols (tones) into a sinewave of continuous phase (FSK).
2018-11-02 12:03:28 +00:00
// Symbol 0 gets encoded as a sine of frequency f0, the others are spaced in increasing
2018-11-02 07:55:54 +00:00
// fashion.
void synth_fsk(const uint8_t *symbols, int num_symbols, float f0, float spacing,
float symbol_rate, float signal_rate, float *signal) {
2018-10-18 07:42:43 +00:00
float phase = 0;
float dt = 1/signal_rate;
float dt_sym = 1/symbol_rate;
float t = 0;
int j = 0;
int i = 0;
2018-11-02 07:55:54 +00:00
while (j < num_symbols) {
2018-10-18 07:42:43 +00:00
float f = f0 + symbols[j] * spacing;
phase += 2 * M_PI * f / signal_rate;
signal[i] = sin(phase);
t += dt;
if (t >= dt_sym) {
// Move to the next symbol
t -= dt_sym;
++j;
}
++i;
}
}
2018-10-18 08:16:21 +00:00
void usage() {
printf("Generate a 15-second WAV file encoding a given message.\n");
printf("Usage:\n");
printf("\n");
printf("gen_ft8 MESSAGE WAV_FILE [FREQUENCY]\n");
2018-10-18 08:16:21 +00:00
printf("\n");
printf("(Note that you might have to enclose your message in quote marks if it contains spaces)\n");
}
2018-10-18 07:42:43 +00:00
int main(int argc, char **argv) {
2018-10-18 08:16:21 +00:00
// Expect two command-line arguments
if (argc < 3) {
usage();
return -1;
}
2018-10-18 07:42:43 +00:00
const char *message = argv[1];
const char *wav_path = argv[2];
2019-11-09 14:09:58 +00:00
float frequency = 1000.0;
if (argc > 3) {
2019-11-09 14:09:58 +00:00
frequency = atof(argv[3]);
}
2018-10-18 07:42:43 +00:00
2018-12-22 14:09:43 +00:00
// First, pack the text data into binary message
uint8_t packed[ft8::K_BYTES];
2018-10-29 13:28:46 +00:00
//int rc = packmsg(message, packed);
int rc = ft8::pack77(message, packed);
2018-10-18 07:42:43 +00:00
if (rc < 0) {
printf("Cannot parse message!\n");
printf("RC = %d\n", rc);
return -2;
}
printf("Packed data: ");
2018-10-29 13:28:46 +00:00
for (int j = 0; j < 10; ++j) {
2018-10-18 07:42:43 +00:00
printf("%02x ", packed[j]);
}
printf("\n");
2018-10-18 08:43:51 +00:00
// Second, encode the binary message as a sequence of FSK tones
uint8_t tones[ft8::NN]; // FT8_NN = 79, lack of better name at the moment
2018-10-29 13:28:46 +00:00
//genft8(packed, 0, tones);
ft8::genft8(packed, tones);
2018-10-18 07:42:43 +00:00
printf("FSK tones: ");
for (int j = 0; j < ft8::NN; ++j) {
2018-10-18 07:42:43 +00:00
printf("%d", tones[j]);
}
printf("\n");
2018-10-18 08:43:51 +00:00
// Third, convert the FSK tones into an audio signal
2018-12-24 12:22:26 +00:00
const int sample_rate = 12000;
const float symbol_rate = 6.25f;
const int num_samples = (int)(0.5f + ft8::NN / symbol_rate * sample_rate);
2018-12-24 12:22:26 +00:00
const int num_silence = (15 * sample_rate - num_samples) / 2;
2018-10-18 07:42:43 +00:00
float signal[num_silence + num_samples + num_silence];
for (int i = 0; i < num_silence + num_samples + num_silence; i++) {
signal[i] = 0;
}
synth_fsk(tones, ft8::NN, frequency, symbol_rate, symbol_rate, sample_rate, signal + num_silence);
2018-12-24 12:22:26 +00:00
save_wav(signal, num_silence + num_samples + num_silence, sample_rate, wav_path);
2018-10-18 07:42:43 +00:00
return 0;
2018-10-18 08:43:51 +00:00
}