2017-01-10 14:45:40 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2016 Furrtek
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* 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; either version 2, 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 for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "proc_replay.hpp"
|
2017-12-06 08:20:51 -05:00
|
|
|
#include "sine_table_int8.hpp"
|
2017-12-06 19:58:25 -05:00
|
|
|
#include "portapack_shared_memory.hpp"
|
2017-01-10 14:45:40 -05:00
|
|
|
|
|
|
|
#include "event_m4.hpp"
|
|
|
|
|
|
|
|
#include "utility.hpp"
|
|
|
|
|
|
|
|
ReplayProcessor::ReplayProcessor() {
|
2023-05-18 16:16:05 -04:00
|
|
|
channel_filter_low_f = taps_200k_decim_1.low_frequency_normalized * 1000000;
|
|
|
|
channel_filter_high_f = taps_200k_decim_1.high_frequency_normalized * 1000000;
|
|
|
|
channel_filter_transition = taps_200k_decim_1.transition_normalized * 1000000;
|
|
|
|
|
|
|
|
spectrum_samples = 0;
|
|
|
|
|
|
|
|
channel_spectrum.set_decimation_factor(1);
|
|
|
|
|
|
|
|
configured = false;
|
2023-07-23 02:54:17 -04:00
|
|
|
baseband_thread.start();
|
2017-01-10 14:45:40 -05:00
|
|
|
}
|
|
|
|
|
2023-08-02 15:59:26 -04:00
|
|
|
// Change to 1 to enable buffer assertions in replay.
|
|
|
|
#define BUFFER_SIZE_ASSERT 0
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-08-02 15:59:26 -04:00
|
|
|
void ReplayProcessor::execute(const buffer_c8_t& buffer) {
|
2023-07-22 03:20:56 -04:00
|
|
|
if (!configured || !stream) return;
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-08-02 15:59:26 -04:00
|
|
|
// Because this is actually adding samples, alias
|
|
|
|
// oversample_rate so the math below is more clear.
|
|
|
|
const size_t interpolation_factor = toUType(oversample_rate);
|
|
|
|
|
|
|
|
// Wrap the IQ data array in a buffer with the correct sample_rate.
|
|
|
|
buffer_c16_t iq_buffer{iq.data(), iq.size(), baseband_fs / interpolation_factor};
|
|
|
|
|
|
|
|
// The IQ data in stream is C16 format and needs to be converted to C8 (N * 2).
|
|
|
|
// The data also needs to be interpolated so the effective sample rate is closer
|
|
|
|
// to 4Mhz. Because interpolation repeats a sample multiple times, fewer bytes
|
|
|
|
// are needed from the source stream in order to fill the buffer (count / oversample).
|
|
|
|
// Together the C16->C8 conversion and the interpolation give the number of
|
|
|
|
// bytes that need to be read from the source stream.
|
|
|
|
const size_t samples_to_read = buffer.count / interpolation_factor;
|
|
|
|
const size_t bytes_to_read = samples_to_read * sizeof(buffer_c16_t::Type);
|
|
|
|
|
|
|
|
#if BUFFER_SIZE_ASSERT
|
|
|
|
// Verify the output buffer size is divisible by the interpolation factor.
|
|
|
|
if (samples_to_read * interpolation_factor != buffer.count)
|
|
|
|
chDbgPanic("Output not div.");
|
|
|
|
|
|
|
|
// Is the input smaple buffer big enough?
|
2023-08-29 13:26:38 -04:00
|
|
|
if (samples_to_read > iq_buffer.count)
|
2023-08-02 15:59:26 -04:00
|
|
|
chDbgPanic("IQ buf ovf.");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Read the C16 IQ data from the source stream.
|
|
|
|
size_t current_bytes_read = stream->read(iq_buffer.p, bytes_to_read);
|
|
|
|
|
|
|
|
// Compute the number of samples were actually read from the source.
|
|
|
|
size_t samples_read = current_bytes_read / sizeof(buffer_c16_t::Type);
|
|
|
|
|
|
|
|
// Write converted source samples to the output buffer with interpolation.
|
|
|
|
for (auto i = 0u; i < samples_read; ++i) {
|
|
|
|
int8_t re_out = iq_buffer.p[i].real() >> 8;
|
|
|
|
int8_t im_out = iq_buffer.p[i].imag() >> 8;
|
|
|
|
auto out_value = buffer_c8_t::Type{re_out, im_out};
|
|
|
|
|
|
|
|
// Interpolate sample.
|
|
|
|
for (auto j = 0u; j < interpolation_factor; ++j) {
|
|
|
|
size_t index = i * interpolation_factor + j;
|
|
|
|
buffer.p[index] = out_value;
|
|
|
|
|
|
|
|
#if BUFFER_SIZE_ASSERT
|
|
|
|
// Verify the index is within bounds.
|
|
|
|
if (index >= buffer.count)
|
|
|
|
chDbgPanic("Output bounds");
|
|
|
|
#endif
|
2023-05-18 16:16:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 15:59:26 -04:00
|
|
|
// Update tracking stats.
|
|
|
|
bytes_read += current_bytes_read;
|
|
|
|
spectrum_samples += samples_read * interpolation_factor;
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
if (spectrum_samples >= spectrum_interval_samples) {
|
|
|
|
spectrum_samples -= spectrum_interval_samples;
|
2023-08-02 15:59:26 -04:00
|
|
|
channel_spectrum.feed(
|
|
|
|
iq_buffer, channel_filter_low_f,
|
|
|
|
channel_filter_high_f, channel_filter_transition);
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-08-02 15:59:26 -04:00
|
|
|
// Inform UI about progress.
|
|
|
|
txprogress_message.progress = bytes_read;
|
2023-05-18 16:16:05 -04:00
|
|
|
txprogress_message.done = false;
|
|
|
|
shared_memory.application_queue.push(txprogress_message);
|
|
|
|
}
|
2017-01-10 14:45:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReplayProcessor::on_message(const Message* const message) {
|
2023-05-18 16:16:05 -04:00
|
|
|
switch (message->id) {
|
|
|
|
case Message::ID::UpdateSpectrum:
|
|
|
|
case Message::ID::SpectrumStreamingConfig:
|
|
|
|
channel_spectrum.on_message(message);
|
|
|
|
break;
|
|
|
|
|
2023-08-02 15:59:26 -04:00
|
|
|
case Message::ID::SampleRateConfig:
|
|
|
|
sample_rate_config(*reinterpret_cast<const SampleRateConfigMessage*>(message));
|
2023-05-18 16:16:05 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Message::ID::ReplayConfig:
|
|
|
|
configured = false;
|
|
|
|
bytes_read = 0;
|
|
|
|
replay_config(*reinterpret_cast<const ReplayConfigMessage*>(message));
|
|
|
|
break;
|
|
|
|
|
|
|
|
// App has prefilled the buffers, we're ready to go now
|
|
|
|
case Message::ID::FIFOData:
|
|
|
|
configured = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-01-10 14:45:40 -05:00
|
|
|
}
|
|
|
|
|
2023-08-02 15:59:26 -04:00
|
|
|
void ReplayProcessor::sample_rate_config(const SampleRateConfigMessage& message) {
|
|
|
|
baseband_fs = message.sample_rate * toUType(message.oversample_rate);
|
|
|
|
oversample_rate = message.oversample_rate;
|
2023-05-18 16:16:05 -04:00
|
|
|
baseband_thread.set_sampling_rate(baseband_fs);
|
2023-08-02 15:59:26 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
spectrum_interval_samples = baseband_fs / spectrum_rate_hz;
|
2018-02-22 02:04:19 -05:00
|
|
|
}
|
|
|
|
|
2017-01-10 14:45:40 -05:00
|
|
|
void ReplayProcessor::replay_config(const ReplayConfigMessage& message) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (message.config) {
|
|
|
|
stream = std::make_unique<StreamOutput>(message.config);
|
|
|
|
|
|
|
|
// Tell application that the buffers and FIFO pointers are ready, prefill
|
|
|
|
shared_memory.application_queue.push(sig_message);
|
|
|
|
} else {
|
|
|
|
stream.reset();
|
|
|
|
}
|
2017-01-10 14:45:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2023-05-18 16:16:05 -04:00
|
|
|
EventDispatcher event_dispatcher{std::make_unique<ReplayProcessor>()};
|
|
|
|
event_dispatcher.run();
|
|
|
|
return 0;
|
2017-01-10 14:45:40 -05:00
|
|
|
}
|