Files

376 lines
11 KiB
C++
Raw Permalink Normal View History

/*
* Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc.
*
* 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 "ui_record_view.hpp"
#include "portapack.hpp"
using namespace portapack;
#include "io_file.hpp"
#include "io_wave.hpp"
2023-07-22 02:20:56 -05:00
#include "io_convert.hpp"
#include "baseband_api.hpp"
2023-06-20 00:32:37 -07:00
#include "metadata_file.hpp"
2023-08-02 12:59:26 -07:00
#include "oversample.hpp"
#include "rtc_time.hpp"
#include "string_format.hpp"
#include "utility.hpp"
#include <cstdint>
2023-09-23 12:56:37 -07:00
#include <vector>
namespace ui {
/*void RecordView::toggle_pitch_rssi() {
2023-05-19 08:16:05 +12:00
pitch_rssi_enabled = !pitch_rssi_enabled;
// Send to RSSI widget
const PitchRSSIConfigureMessage message {
pitch_rssi_enabled,
0
};
shared_memory.application_queue.push(message);
if( !pitch_rssi_enabled ) {
2024-05-27 21:02:52 +02:00
button_pitch_rssi.set_foreground(Theme::getInstance()->fg_orange->foreground);
2023-05-19 08:16:05 +12:00
} else {
2024-05-27 21:02:52 +02:00
button_pitch_rssi.set_foreground(Theme::getInstance()->fg_green->foreground);
2023-05-19 08:16:05 +12:00
}
}*/
RecordView::RecordView(
2023-05-19 08:16:05 +12:00
const Rect parent_rect,
const std::filesystem::path& filename_stem_pattern,
const std::filesystem::path& folder,
const FileType file_type,
const size_t write_size,
const size_t buffer_count)
: View{parent_rect},
filename_stem_pattern{filename_stem_pattern},
folder{folder},
file_type{file_type},
write_size{write_size},
buffer_count{buffer_count} {
ensure_directory(folder);
add_children({
&rect_background,
//&button_pitch_rssi,
&button_record,
2024-03-30 16:48:45 +01:00
&gps_icon,
2023-05-19 08:16:05 +12:00
&text_record_filename,
&text_record_dropped,
&text_time_available,
});
2023-05-19 08:16:05 +12:00
rect_background.set_parent_rect({{0, 0}, size()});
2023-05-19 08:16:05 +12:00
/*button_pitch_rssi.on_select = [this](ImageButton&) {
this->toggle_pitch_rssi();
};*/
button_record.on_select = [this](ImageButton&) {
this->toggle();
};
signal_token_tick_second = rtc_time::signal_tick_second += [this]() {
this->on_tick_second();
};
2024-03-30 16:48:45 +01:00
gps_icon.hidden(true);
}
RecordView::~RecordView() {
2023-05-19 08:16:05 +12:00
rtc_time::signal_tick_second -= signal_token_tick_second;
}
void RecordView::focus() {
2023-05-19 08:16:05 +12:00
button_record.focus();
}
2023-08-02 12:59:26 -07:00
uint32_t RecordView::set_sampling_rate(uint32_t new_sampling_rate) {
// Determine the oversampling needed (if any) and the actual sampling rate.
auto oversample_rate = get_oversample_rate(new_sampling_rate);
auto actual_sampling_rate = new_sampling_rate * toUType(oversample_rate);
2023-08-29 10:26:38 -07:00
// Change the "REC" icon background to yellow when the selected rate exceeds hardware limits.
// Above this threshold, samples will be dropped resulting incomplete capture files.
if (new_sampling_rate > 1'250'000) {
2024-05-27 21:02:52 +02:00
button_record.set_background(Theme::getInstance()->fg_yellow->foreground);
2023-05-19 08:16:05 +12:00
} else {
2024-05-27 21:02:52 +02:00
button_record.set_background(Theme::getInstance()->fg_yellow->background);
2023-05-19 08:16:05 +12:00
}
2023-08-02 12:59:26 -07:00
if (sampling_rate != new_sampling_rate) {
2023-05-19 08:16:05 +12:00
stop();
sampling_rate = new_sampling_rate;
2023-08-02 12:59:26 -07:00
baseband::set_sample_rate(sampling_rate, oversample_rate);
2023-05-19 08:16:05 +12:00
button_record.hidden(sampling_rate == 0);
text_record_filename.hidden(sampling_rate == 0);
text_record_dropped.hidden(sampling_rate == 0);
text_time_available.hidden(sampling_rate == 0);
rect_background.hidden(sampling_rate != 0);
update_status_display();
}
2023-08-02 12:59:26 -07:00
return actual_sampling_rate;
}
OversampleRate RecordView::get_oversample_rate(uint32_t sample_rate) {
// No oversampling necessary for baseband audio processors.
if (file_type == FileType::WAV)
return OversampleRate::None;
2023-08-29 10:26:38 -07:00
return ::get_oversample_rate(sample_rate);
}
2022-03-28 20:53:09 -07:00
// Setter for datetime and frequency filename
void RecordView::set_filename_date_frequency(bool set) {
2023-05-19 08:16:05 +12:00
filename_date_frequency = set;
2023-12-28 11:25:53 +01:00
if (set)
filename_as_is = false;
}
// Setter for leaving the filename untouched
void RecordView::set_filename_as_is(bool set) {
filename_as_is = set;
if (set)
filename_date_frequency = false;
2022-03-28 20:53:09 -07:00
}
bool RecordView::is_active() const {
2023-05-19 08:16:05 +12:00
return (bool)capture_thread;
}
void RecordView::toggle() {
2023-05-19 08:16:05 +12:00
if (is_active()) {
stop();
} else {
start();
}
}
void RecordView::start() {
2023-05-19 08:16:05 +12:00
stop();
2023-05-19 08:16:05 +12:00
text_record_filename.set("");
text_record_dropped.set("");
2025-10-28 01:10:09 +01:00
trim_path = "";
2023-05-19 08:16:05 +12:00
if (sampling_rate == 0) {
return;
}
2022-03-28 20:53:09 -07:00
std::filesystem::path base_path;
2024-03-30 16:48:45 +01:00
auto tmp_path = filename_stem_pattern; // store it, to be able to modify without causing permanent change
// check for geo data, if present append filename with _GEO
if (latitude != 0 && longitude != 0 && latitude < 200 && longitude < 200) {
tmp_path.append_filename(u"_GEO");
}
2023-05-19 08:16:05 +12:00
if (filename_date_frequency) {
2024-01-21 12:47:28 -06:00
rtc_time::now(datetime);
2022-03-28 20:53:09 -07:00
2023-05-19 08:16:05 +12:00
// ISO 8601
std::string date_time =
to_string_dec_uint(datetime.year(), 4, '0') +
to_string_dec_uint(datetime.month(), 2, '0') +
to_string_dec_uint(datetime.day(), 2, '0') + "T" +
to_string_dec_uint(datetime.hour()) +
to_string_dec_uint(datetime.minute()) +
to_string_dec_uint(datetime.second());
2022-03-28 20:53:09 -07:00
2024-03-30 16:48:45 +01:00
base_path = tmp_path.string() + "_" + date_time + "_" +
2023-06-11 11:47:13 -07:00
trim(to_string_freq(receiver_model.target_frequency())) + "Hz";
2023-05-19 08:16:05 +12:00
base_path = folder / base_path;
2023-12-28 11:25:53 +01:00
} else if (filename_as_is) {
2024-03-30 16:48:45 +01:00
base_path = tmp_path.string();
2023-12-28 11:25:53 +01:00
base_path = folder / base_path;
} else
2024-03-30 16:48:45 +01:00
base_path = next_filename_matching_pattern(folder / tmp_path);
2022-03-28 20:53:09 -07:00
2023-05-19 08:16:05 +12:00
if (base_path.empty()) {
return;
}
2023-05-19 08:16:05 +12:00
std::unique_ptr<stream::Writer> writer;
switch (file_type) {
case FileType::WAV: {
auto p = std::make_unique<WAVFileWriter>();
auto create_error = p->create(
base_path.replace_extension(u".WAV"),
sampling_rate,
2023-06-11 11:47:13 -07:00
to_string_dec_uint(receiver_model.target_frequency()) + "Hz");
2023-05-19 08:16:05 +12:00
if (create_error.is_valid()) {
handle_error(create_error.value());
} else {
writer = std::move(p);
}
} break;
2023-07-22 02:20:56 -05:00
case FileType::RawS8:
2023-05-19 08:16:05 +12:00
case FileType::RawS16: {
const auto metadata_file_error = write_metadata_file(
get_metadata_path(base_path), {receiver_model.target_frequency(), sampling_rate, latitude, longitude, satinuse});
2023-05-19 08:16:05 +12:00
if (metadata_file_error.is_valid()) {
handle_error(metadata_file_error.value());
return;
}
2023-07-22 02:20:56 -05:00
auto p = std::make_unique<FileConvertWriter>();
2023-09-21 08:43:10 -07:00
trim_path = base_path.replace_extension((file_type == FileType::RawS8) ? u".C8" : u".C16");
auto create_error = p->create(trim_path);
2023-05-19 08:16:05 +12:00
if (create_error.is_valid()) {
handle_error(create_error.value());
} else {
writer = std::move(p);
}
} break;
2023-05-19 08:16:05 +12:00
default:
break;
};
2023-05-19 08:16:05 +12:00
if (writer) {
text_record_filename.set(truncate(base_path.filename().string(), 8));
button_record.set_bitmap(&bitmap_stop);
capture_thread = std::make_unique<CaptureThread>(
std::move(writer),
write_size, buffer_count,
[]() {
CaptureThreadDoneMessage message{};
EventDispatcher::send_message(message);
},
[](File::Error error) {
CaptureThreadDoneMessage message{error.code()};
EventDispatcher::send_message(message);
});
}
2023-05-19 08:16:05 +12:00
update_status_display();
}
void RecordView::on_hide() {
2023-05-19 08:16:05 +12:00
stop(); // Stop current recording
View::on_hide();
}
void RecordView::stop() {
2023-05-19 08:16:05 +12:00
if (is_active()) {
capture_thread.reset();
button_record.set_bitmap(&bitmap_record);
2023-09-21 08:43:10 -07:00
trim_capture();
2023-05-19 08:16:05 +12:00
}
2023-05-19 08:16:05 +12:00
update_status_display();
}
void RecordView::on_tick_second() {
2023-05-19 08:16:05 +12:00
update_status_display();
}
void RecordView::update_status_display() {
2023-05-19 08:16:05 +12:00
if (is_active()) {
const auto dropped_percent = std::min(99U, capture_thread->state().dropped_percent());
const auto s = to_string_dec_uint(dropped_percent, 2, ' ') + "%";
text_record_dropped.set(s);
}
2016-05-11 11:22:57 -07:00
/*
if (pitch_rssi_enabled) {
button_pitch_rssi.invert_colors();
}
*/
2023-05-19 08:16:05 +12:00
if (sampling_rate > 0) {
2023-05-19 08:16:05 +12:00
const auto space_info = std::filesystem::space(u"");
// - Audio is 1 int16_t per sample or '2' bytes per sample.
// - C8 captures 2 (I,Q) int8_t per sample or '2' bytes per sample.
// - C16 captures 2 (I,Q) int16_t per sample or '4' bytes per sample.
const auto bytes_per_sample = file_type == FileType::RawS16 ? 4 : 2;
2023-08-02 12:59:26 -07:00
const uint32_t bytes_per_second = sampling_rate * bytes_per_sample;
2023-05-19 08:16:05 +12:00
const uint32_t available_seconds = space_info.free / bytes_per_second;
const uint32_t seconds = available_seconds % 60;
const uint32_t available_minutes = available_seconds / 60;
const uint32_t minutes = available_minutes % 60;
const uint32_t hours = available_minutes / 60;
const std::string available_time =
to_string_dec_uint(hours, 3, ' ') + ":" +
to_string_dec_uint(minutes, 2, '0') + ":" +
to_string_dec_uint(seconds, 2, '0');
text_time_available.set(available_time);
}
}
2023-09-21 08:43:10 -07:00
void RecordView::trim_capture() {
2023-09-23 12:56:37 -07:00
using bucket_t = iq::PowerBuckets::Bucket;
2023-09-21 08:43:10 -07:00
if (file_type != FileType::WAV && auto_trim && !trim_path.empty()) {
2023-09-23 12:56:37 -07:00
// Need to heap alloc the buckets in this case. The large static buffer overflows the stack.
std::vector<bucket_t> buckets(size_t(255), bucket_t{});
;
iq::PowerBuckets power_buckets{
.p = &buckets[0],
.size = buckets.size()};
2023-09-21 08:43:10 -07:00
trim_ui.show_reading();
2023-09-23 12:56:37 -07:00
auto info = iq::profile_capture(trim_path, power_buckets);
if (info) {
// 7% - decent trimming without being too aggressive.
auto trim_range = iq::compute_trim_range(*info, power_buckets, 7);
2023-09-21 08:43:10 -07:00
trim_ui.show_trimming();
iq::trim_capture_with_range(trim_path, trim_range, trim_ui.get_callback(), 1);
2023-09-21 08:43:10 -07:00
}
trim_ui.clear();
}
2025-10-28 01:10:09 +01:00
trim_path = "";
2023-09-21 08:43:10 -07:00
}
void RecordView::on_gps(const GPSPosDataMessage* msg) {
2024-03-30 16:48:45 +01:00
if (msg->lat == 0 || msg->lat > 399) return; // not valid one
if (latitude == 0) gps_icon.hidden(false); // prev was 0, so not shown already
latitude = msg->lat;
longitude = msg->lon;
satinuse = msg->satinuse;
}
void RecordView::handle_capture_thread_done(const File::Error error) {
2023-05-19 08:16:05 +12:00
stop();
if (error.code()) {
handle_error(error);
}
}
void RecordView::handle_error(const File::Error error) {
2023-05-19 08:16:05 +12:00
if (on_error) {
on_error(error.what());
}
2016-05-12 21:59:46 -07:00
}
} /* namespace ui */