diff --git a/common/wave.cpp b/common/wave.cpp index 516a1fa..dc0fb49 100644 --- a/common/wave.cpp +++ b/common/wave.cpp @@ -1,17 +1,17 @@ #include "wave.h" -#include -#include -#include +#include +#include +#include #include - // Save signal in floating point format (-1 .. +1) as a WAVE file using 16-bit signed integers. -void save_wav(const float *signal, int num_samples, int sample_rate, const char *path) { +void save_wav(const float *signal, int num_samples, int sample_rate, const char *path) +{ char subChunk1ID[4] = {'f', 'm', 't', ' '}; - uint32_t subChunk1Size = 16; // 16 for PCM - uint16_t audioFormat = 1; // PCM = 1 + uint32_t subChunk1Size = 16; // 16 for PCM + uint16_t audioFormat = 1; // PCM = 1 uint16_t numChannels = 1; uint16_t bitsPerSample = 16; uint32_t sampleRate = sample_rate; @@ -26,10 +26,13 @@ void save_wav(const float *signal, int num_samples, int sample_rate, const char char format[4] = {'W', 'A', 'V', 'E'}; int16_t *raw_data = (int16_t *)malloc(num_samples * blockAlign); - for (int i = 0; i < num_samples; i++) { + for (int i = 0; i < num_samples; i++) + { float x = signal[i]; - if (x > 1.0) x = 1.0; - else if (x < -1.0) x = -1.0; + if (x > 1.0) + x = 1.0; + else if (x < -1.0) + x = -1.0; raw_data[i] = int(0.5 + (x * 32767.0)); } @@ -59,24 +62,24 @@ void save_wav(const float *signal, int num_samples, int sample_rate, const char free(raw_data); } - // Load signal in floating point format (-1 .. +1) as a WAVE file using 16-bit signed integers. -int load_wav(float *signal, int &num_samples, int &sample_rate, const char *path) { +int load_wav(float *signal, int *num_samples, int *sample_rate, const char *path) +{ char subChunk1ID[4]; // = {'f', 'm', 't', ' '}; uint32_t subChunk1Size; // = 16; // 16 for PCM uint16_t audioFormat; // = 1; // PCM = 1 uint16_t numChannels; // = 1; uint16_t bitsPerSample; // = 16; uint32_t sampleRate; - uint16_t blockAlign; // = numChannels * bitsPerSample / 8; - uint32_t byteRate; // = sampleRate * blockAlign; + uint16_t blockAlign; // = numChannels * bitsPerSample / 8; + uint32_t byteRate; // = sampleRate * blockAlign; char subChunk2ID[4]; // = {'d', 'a', 't', 'a'}; uint32_t subChunk2Size; // = num_samples * blockAlign; - char chunkID[4]; // = {'R', 'I', 'F', 'F'}; - uint32_t chunkSize; // = 4 + (8 + subChunk1Size) + (8 + subChunk2Size); - char format[4]; // = {'W', 'A', 'V', 'E'}; + char chunkID[4]; // = {'R', 'I', 'F', 'F'}; + uint32_t chunkSize; // = 4 + (8 + subChunk1Size) + (8 + subChunk2Size); + char format[4]; // = {'W', 'A', 'V', 'E'}; FILE *f = fopen(path, "rb"); @@ -87,7 +90,8 @@ int load_wav(float *signal, int &num_samples, int &sample_rate, const char *path fread((void *)subChunk1ID, sizeof(subChunk1ID), 1, f); fread((void *)&subChunk1Size, sizeof(subChunk1Size), 1, f); - if (subChunk1Size != 16) return -1; + if (subChunk1Size != 16) + return -1; fread((void *)&audioFormat, sizeof(audioFormat), 1, f); fread((void *)&numChannels, sizeof(numChannels), 1, f); @@ -96,23 +100,26 @@ int load_wav(float *signal, int &num_samples, int &sample_rate, const char *path fread((void *)&blockAlign, sizeof(blockAlign), 1, f); fread((void *)&bitsPerSample, sizeof(bitsPerSample), 1, f); - if (audioFormat != 1 || numChannels != 1 || bitsPerSample != 16) return -1; + if (audioFormat != 1 || numChannels != 1 || bitsPerSample != 16) + return -1; fread((void *)subChunk2ID, sizeof(subChunk2ID), 1, f); fread((void *)&subChunk2Size, sizeof(subChunk2Size), 1, f); - if (subChunk2Size / blockAlign > num_samples) return -2; - - num_samples = subChunk2Size / blockAlign; - sample_rate = sampleRate; + if (subChunk2Size / blockAlign > *num_samples) + return -2; - int16_t *raw_data = (int16_t *)malloc(num_samples * blockAlign); + *num_samples = subChunk2Size / blockAlign; + *sample_rate = sampleRate; - fread((void *)raw_data, blockAlign, num_samples, f); - for (int i = 0; i < num_samples; i++) { + int16_t *raw_data = (int16_t *)malloc(*num_samples * blockAlign); + + fread((void *)raw_data, blockAlign, *num_samples, f); + for (int i = 0; i < *num_samples; i++) + { signal[i] = raw_data[i] / 32768.0f; } - + free(raw_data); fclose(f); diff --git a/common/wave.h b/common/wave.h index 72bb30d..2dfee74 100644 --- a/common/wave.h +++ b/common/wave.h @@ -1,9 +1,10 @@ -#pragma once - +#ifndef _INCLUDE_WAVE_H_ +#define _INCLUDE_WAVE_H_ // Save signal in floating point format (-1 .. +1) as a WAVE file using 16-bit signed integers. void save_wav(const float *signal, int num_samples, int sample_rate, const char *path); - // Load signal in floating point format (-1 .. +1) as a WAVE file using 16-bit signed integers. -int load_wav(float *signal, int &num_samples, int &sample_rate, const char *path); +int load_wav(float *signal, int *num_samples, int *sample_rate, const char *path); + +#endif // _INCLUDE_WAVE_H_ diff --git a/decode_ft8.cpp b/decode_ft8.cpp index 929eb14..d8e1baa 100644 --- a/decode_ft8.cpp +++ b/decode_ft8.cpp @@ -1,7 +1,8 @@ -#include -#include -#include -#include +#include +#include +#include +#include +#include #include "ft8/unpack.h" #include "ft8/ldpc.h" @@ -192,7 +193,7 @@ int main(int argc, char **argv) int num_samples = 15 * sample_rate; float signal[num_samples]; - int rc = load_wav(signal, num_samples, sample_rate, wav_path); + int rc = load_wav(signal, &num_samples, &sample_rate, wav_path); if (rc < 0) { return -1; @@ -229,12 +230,12 @@ int main(int argc, char **argv) int num_decoded = 0; for (int idx = 0; idx < num_candidates; ++idx) { - Candidate &cand = candidate_list[idx]; - if (cand.score < kMin_score) + const Candidate *cand = &candidate_list[idx]; + if (cand->score < kMin_score) continue; - float freq_hz = (cand.freq_offset + (float)cand.freq_sub / kFreq_osr) * kFSK_dev; - float time_sec = (cand.time_offset + (float)cand.time_sub / kTime_osr) / kFSK_dev; + float freq_hz = (cand->freq_offset + (float)cand->freq_sub / kFreq_osr) * kFSK_dev; + float time_sec = (cand->time_offset + (float)cand->time_sub / kTime_osr) / kFSK_dev; float log174[FT8_N]; extract_likelihood(&power, cand, kGray_map, log174); @@ -313,7 +314,7 @@ int main(int argc, char **argv) // Fake WSJT-X-like output for now int snr = 0; // TODO: compute SNR - printf("000000 %3d [%2d] %4.1f %4d ~ %s\n", cand.score, k, time_sec, (int)(freq_hz + 0.5f), message); + printf("000000 %3d [%2d] %4.1f %4d ~ %s\n", cand->score, k, time_sec, (int)(freq_hz + 0.5f), message); continue; } } diff --git a/ft8/decode.cpp b/ft8/decode.cpp index 1a2692c..7e8ba12 100644 --- a/ft8/decode.cpp +++ b/ft8/decode.cpp @@ -120,11 +120,10 @@ int find_sync(const MagArray *power, const uint8_t *sync_map, int num_candidates // Compute log likelihood log(p(1) / p(0)) of 174 message bits // for later use in soft-decision LDPC decoding -void extract_likelihood(const MagArray *power, const Candidate &cand, const uint8_t *code_map, float *log174) +void extract_likelihood(const MagArray *power, const Candidate *cand, const uint8_t *code_map, float *log174) { int num_alt = power->time_osr * power->freq_osr; - // int offset = (cand.time_offset * num_alt + cand.time_sub * power->freq_osr + cand.freq_sub) * power->num_bins + cand.freq_offset; - int offset = get_index(power, cand.time_offset, cand.time_sub, cand.freq_sub, cand.freq_offset); + int offset = get_index(power, cand->time_offset, cand->time_sub, cand->freq_sub, cand->freq_offset); // Go over FSK tones and skip Costas sync symbols const int n_syms = 1; diff --git a/ft8/decode.h b/ft8/decode.h index e358fb4..e56cb81 100644 --- a/ft8/decode.h +++ b/ft8/decode.h @@ -27,6 +27,6 @@ int find_sync(const MagArray *power, const uint8_t *sync_map, int num_candidates // Compute log likelihood log(p(1) / p(0)) of 174 message bits // for later use in soft-decision LDPC decoding -void extract_likelihood(const MagArray *power, const Candidate &cand, const uint8_t *code_map, float *log174); +void extract_likelihood(const MagArray *power, const Candidate *cand, const uint8_t *code_map, float *log174); #endif // _INCLUDE_DECODE_H_ diff --git a/ft8/text.h b/ft8/text.h index 9421e3c..1c07f1e 100644 --- a/ft8/text.h +++ b/ft8/text.h @@ -29,7 +29,7 @@ void fmtmsg(char *msg_out, const char *msg_in); int dd_to_int(const char *str, int length); // Convert a 2 digit integer to string -void int_to_dd(char *str, int value, int width, bool full_sign = false); +void int_to_dd(char *str, int value, int width, bool full_sign); char charn(int c, int table_idx); int nchar(char c, int table_idx); diff --git a/ft8/unpack.cpp b/ft8/unpack.cpp index 7bfc6c0..d8cb1cf 100644 --- a/ft8/unpack.cpp +++ b/ft8/unpack.cpp @@ -29,7 +29,7 @@ int unpack28(uint32_t n28, uint8_t ip, uint8_t i3, char *result) { // CQ_nnn with 3 digits strcpy(result, "CQ "); - int_to_dd(result + 3, n28 - 3, 3); + int_to_dd(result + 3, n28 - 3, 3, false); return 0; // Success } if (n28 <= 532443L) @@ -63,7 +63,7 @@ int unpack28(uint32_t n28, uint8_t ip, uint8_t i3, char *result) // TODO: implement // strcpy(result, "<...>"); result[0] = '<'; - int_to_dd(result + 1, n28, 7); + int_to_dd(result + 1, n28, 7, false); result[8] = '>'; result[9] = '\0'; return 0; @@ -303,7 +303,7 @@ int unpack_nonstandard(const uint8_t *a77, char *field1, char *field2, char *fie // should replace with hash12(n12, call_3); // strcpy(call_3, "<...>"); call_3[0] = '<'; - int_to_dd(call_3 + 1, n12, 4); + int_to_dd(call_3 + 1, n12, 4, false); call_3[5] = '>'; call_3[6] = '\0'; diff --git a/gen_ft8.cpp b/gen_ft8.cpp index f0fb5d2..f741ebd 100644 --- a/gen_ft8.cpp +++ b/gen_ft8.cpp @@ -1,12 +1,11 @@ -#include -#include -#include -#include +#include +#include +#include +#include +#include #include "common/wave.h" #include "common/debug.h" -//#include "ft8/v1/pack.h" -//#include "ft8/v1/encode.h" #include "ft8/pack.h" #include "ft8/encode.h" #include "ft8/constants.h" diff --git a/test.cpp b/test.cpp index c68b08b..6665259 100644 --- a/test.cpp +++ b/test.cpp @@ -1,12 +1,10 @@ -#include -#include -#include -#include +#include +#include +#include +#include +#include #include "ft8/text.h" -//#include "ft8/v1/pack.h" -//#include "ft8/v1/unpack.h" -//#include "ft8/v1/encode.h" #include "ft8/pack.h" #include "ft8/encode.h" #include "ft8/constants.h"