Slight improvement of the tone generator.

This commit is contained in:
teixeluis 2021-06-11 00:13:56 +01:00
parent edcd780402
commit c2dc7d1cf5
6 changed files with 99 additions and 38 deletions

View File

@ -76,8 +76,7 @@ void RSSI::paint(Painter& painter) {
);
if (pitch_rssi_enabled) {
//baseband::set_pitch_rssi((avg_ - raw_min) * 2000 / raw_delta, true);
baseband::set_pitch_rssi((min_ - raw_min) * 2000 / raw_delta, true);
baseband::set_pitch_rssi((avg_ - raw_min) * 2000 / raw_delta, true);
}
}

View File

@ -34,6 +34,8 @@ SondeProcessor::SondeProcessor() {
decim_1.configure(taps_11k0_decim_1.taps, 131072);
audio_output.configure(false);
tone_gen.configure(0, 1, ToneGen::tone_type::square);
}
void SondeProcessor::execute(const buffer_c8_t& buffer) {
@ -53,11 +55,21 @@ void SondeProcessor::execute(const buffer_c8_t& buffer) {
}
}
if(pitch_rssi_enabled && beep_playing) {
generate_beep();
}
else {
generate_silence();
if(pitch_rssi_enabled) {
if(beep_play) {
// if we let the buffer underrun, for some reason
// once it starts looping it ignores zero (silence)
// samples, so we need to keep feeding the buffer
// and not be able to take advantage of the circular
// buffer loop:
//beep_play = false;
generate_beep();
}
if(silence_play) {
//silence_play = false;
generate_silence();
}
}
}
@ -66,7 +78,7 @@ void SondeProcessor::on_message(const Message* const msg) {
case Message::ID::RequestSignal:
if ((*reinterpret_cast<const RequestSignalMessage*>(msg)).signal == RequestSignalMessage::Signal::BeepRequest) {
play_beep();
chThdSleepMilliseconds(100);
chThdSleepMilliseconds(150);
stop_beep();
}
break;
@ -81,31 +93,20 @@ void SondeProcessor::on_message(const Message* const msg) {
}
void SondeProcessor::play_beep() {
beep_playing = true;
beep_play = true;
silence_play = false;
}
void SondeProcessor::stop_beep() {
beep_playing = false;
beep_play = false;
silence_play = true;
}
void SondeProcessor::generate_beep() {
// if(curr_sample == sizeof(audio_buffer.p)) {
// audio_output.write(audio_buffer);
// curr_sample = 0;
// //tone_phase = 0;
// }
// else if(beep_playing) {
// audio_buffer.p[curr_sample++] = (sine_table_i16[(tone_phase & 0xFF000000U) >> 24]);
// tone_phase += tone_delta;
// }
// else {
// audio_buffer.p[curr_sample++] = 0;
// tone_phase = 0;
// }
// here we let the samples be created using the ToneGen class:
for(uint8_t i = 0; i < sizeof(audio_buffer.p); i++) {
audio_buffer.p[i] = (sine_table_i16_1024[(tone_phase & 0xFFC00000U) >> 22]);
tone_phase += tone_delta;
audio_buffer.p[i] = (int16_t) ((tone_gen.process(0) >> 16) & 0x0000FFFF);
}
audio_output.write(audio_buffer);
@ -114,7 +115,6 @@ void SondeProcessor::generate_beep() {
void SondeProcessor::generate_silence() {
for(uint8_t i = 0; i < sizeof(audio_buffer.p); i++) {
audio_buffer.p[i] = 0;
tone_phase = 0;
}
audio_output.write(audio_buffer);
@ -122,7 +122,8 @@ void SondeProcessor::generate_silence() {
void SondeProcessor::pitch_rssi_config(const PitchRSSIConfigureMessage& message) {
pitch_rssi_enabled = message.enabled;
tone_delta = (message.rssi + 1000) * ((1ULL << 32) / 24000);
uint32_t tone_delta = (message.rssi + 1000) * ((1ULL << 32) / 24000);
tone_gen.configure(tone_delta, 1.0, ToneGen::tone_type::square);
}
int main() {

View File

@ -90,8 +90,6 @@
#include "audio_output.hpp"
#include "tone_gen.hpp"
#include "tonesets.hpp"
#include "sine_table_int16.hpp"
#include "buffer.hpp"
@ -108,7 +106,6 @@ public:
private:
static constexpr size_t baseband_fs = 2457600;
static constexpr size_t beep_iterations = 60;
std::array<int16_t, 32> audio { };
@ -119,12 +116,11 @@ private:
AudioOutput audio_output { };
bool beep_playing { false };
bool beep_play { false };
bool silence_play { false };
bool pitch_rssi_enabled { false };
uint32_t tone_delta { 0 };
uint32_t tone_phase { 0 };
uint8_t curr_sample { 0 };
ToneGen tone_gen { };
BasebandThread baseband_thread { baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive };
RSSIThread rssi_thread { NORMALPRIO + 10 };
@ -178,7 +174,16 @@ private:
void play_beep();
void stop_beep();
/**
* Used for filling the audio buffer with the waveform
* generated by the ToneGen class:
*
*/
void generate_beep();
/**
* Used for filling the audio buffer with silence:
*/
void generate_silence();
void pitch_rssi_config(const PitchRSSIConfigureMessage& message);

View File

@ -23,18 +23,55 @@
#include "tone_gen.hpp"
#include "sine_table_int8.hpp"
int32_t ToneGen::tone_from_sine_table() {
int32_t tone_sample = sine_table_i8[(tone_phase_ & 0xFF000000U) >> 24] << 24;
tone_phase_ += delta_;
return tone_sample;
}
int32_t ToneGen::tone_square() {
int32_t tone_sample = 0;
if(tone_phase_ < (UINT32_MAX / 2)) {
tone_sample = INT32_MAX;
}
else {
tone_sample = INT32_MIN;
}
tone_phase_ += delta_;
return tone_sample;
}
void ToneGen::configure(const uint32_t delta, const float tone_mix_weight) {
delta_ = delta;
tone_mix_weight_ = tone_mix_weight;
input_mix_weight_ = 1.0 - tone_mix_weight;
current_tone_type_ = sine;
}
void ToneGen::configure(const uint32_t delta, const float tone_mix_weight, const tone_type tone_type) {
delta_ = delta;
tone_mix_weight_ = tone_mix_weight;
input_mix_weight_ = 1.0 - tone_mix_weight;
current_tone_type_ = tone_type;
}
int32_t ToneGen::process(const int32_t sample_in) {
if (!delta_)
return sample_in;
int32_t tone_sample = sine_table_i8[(tone_phase_ & 0xFF000000U) >> 24];
tone_phase_ += delta_;
int32_t tone_sample = 0;
if(current_tone_type_ == sine) {
tone_sample = tone_from_sine_table();
}
else if(current_tone_type_ == square) {
tone_sample = tone_square();
}
return (sample_in * input_mix_weight_) + (tone_sample * tone_mix_weight_);
}

View File

@ -25,22 +25,41 @@
#include <cstdint>
#include <cstddef>
#include <bitset>
static const std::bitset<2048> wave_bits (0xFFFFFF);
class ToneGen {
public:
enum tone_type { sine, square };
/*ToneGen(const size_t sample_rate
) : sample_rate_ { sample_rate }
{};*/
void configure(const uint32_t delta, const float tone_mix_weight);
void configure(const uint32_t delta, const float tone_mix_weight, const tone_type tone_type);
int32_t process(const int32_t sample_in);
private:
tone_type current_tone_type_ { sine };
//size_t sample_rate_;
float input_mix_weight_ { 1 };
float tone_mix_weight_ { 0 };
uint32_t delta_ { 0 };
uint32_t tone_phase_ { 0 };
/**
* Generator function for sine waves:
*/
int32_t tone_from_sine_table();
/**
* Generator function for square waves:
*/
int32_t tone_square();
};
#endif
#endif /* __TONE_GEN_H__ */

View File

@ -25,7 +25,7 @@
#include <cmath>
static const int16_t sine_table_i16[256] = { 0, 804, 1607, 2410, 3211, 4011, 4807,
static const int16_t sine_table_i16_256[256] = { 0, 804, 1607, 2410, 3211, 4011, 4807,
5601, 6392, 7179, 7961, 8739, 9511, 10278, 11039, 11792, 12539, 13278, 14009, 14732,
15446, 16151, 16845, 17530, 18204, 18867, 19519, 20159, 20787, 21402, 22005, 22594,
23170, 23731, 24279, 24811, 25329, 25832, 26319, 26790, 27245, 27683, 28105, 28510,