2016-04-30 13:56:54 -07:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2016-10-04 09:36:52 -07:00
|
|
|
#include "io_file.hpp"
|
|
|
|
#include "io_wave.hpp"
|
|
|
|
|
2018-02-22 07:04:19 +00:00
|
|
|
#include "baseband_api.hpp"
|
2016-08-21 17:49:06 -07:00
|
|
|
#include "rtc_time.hpp"
|
2016-04-30 13:56:54 -07:00
|
|
|
#include "string_format.hpp"
|
|
|
|
#include "utility.hpp"
|
|
|
|
|
2016-05-02 12:34:42 -07:00
|
|
|
#include <cstdint>
|
|
|
|
|
2016-04-30 13:56:54 -07:00
|
|
|
namespace ui {
|
|
|
|
|
2018-02-22 07:04:19 +00:00
|
|
|
/*void RecordView::toggle_pitch_rssi() {
|
2017-11-28 08:52:04 +01:00
|
|
|
pitch_rssi_enabled = !pitch_rssi_enabled;
|
2017-01-16 03:45:44 +00:00
|
|
|
|
|
|
|
// Send to RSSI widget
|
2017-11-28 08:52:04 +01:00
|
|
|
const PitchRSSIConfigureMessage message {
|
|
|
|
pitch_rssi_enabled,
|
2017-01-16 03:45:44 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
shared_memory.application_queue.push(message);
|
|
|
|
|
2017-11-28 08:52:04 +01:00
|
|
|
if( !pitch_rssi_enabled ) {
|
|
|
|
button_pitch_rssi.set_foreground(Color::orange());
|
2017-01-16 03:45:44 +00:00
|
|
|
} else {
|
2017-11-28 08:52:04 +01:00
|
|
|
button_pitch_rssi.set_foreground(Color::green());
|
2017-01-16 03:45:44 +00:00
|
|
|
}
|
2018-02-22 07:04:19 +00:00
|
|
|
}*/
|
2017-01-16 03:45:44 +00:00
|
|
|
|
2016-04-30 13:56:54 -07:00
|
|
|
RecordView::RecordView(
|
|
|
|
const Rect parent_rect,
|
2023-05-12 11:08:07 -07:00
|
|
|
const std::filesystem::path& filename_stem_pattern,
|
|
|
|
const std::filesystem::path& folder,
|
2016-05-02 11:44:37 -07:00
|
|
|
const FileType file_type,
|
2016-05-10 14:12:37 -07:00
|
|
|
const size_t write_size,
|
|
|
|
const size_t buffer_count
|
2016-04-30 13:56:54 -07:00
|
|
|
) : View { parent_rect },
|
|
|
|
filename_stem_pattern { filename_stem_pattern },
|
2023-05-12 11:08:07 -07:00
|
|
|
folder { folder },
|
2016-05-02 11:44:37 -07:00
|
|
|
file_type { file_type },
|
2016-05-10 14:12:37 -07:00
|
|
|
write_size { write_size },
|
|
|
|
buffer_count { buffer_count }
|
2016-04-30 13:56:54 -07:00
|
|
|
{
|
2023-05-12 11:08:07 -07:00
|
|
|
ensure_directory(folder);
|
2016-09-05 14:53:04 -07:00
|
|
|
add_children({
|
2016-05-25 11:33:23 -07:00
|
|
|
&rect_background,
|
2018-02-22 07:04:19 +00:00
|
|
|
//&button_pitch_rssi,
|
2016-04-30 13:56:54 -07:00
|
|
|
&button_record,
|
|
|
|
&text_record_filename,
|
|
|
|
&text_record_dropped,
|
2016-05-11 11:50:40 -07:00
|
|
|
&text_time_available,
|
2016-09-05 14:53:04 -07:00
|
|
|
});
|
2018-02-22 07:04:19 +00:00
|
|
|
|
2016-05-25 11:33:23 -07:00
|
|
|
rect_background.set_parent_rect({ { 0, 0 }, size() });
|
2016-07-28 05:25:33 +02:00
|
|
|
|
2018-02-22 07:04:19 +00:00
|
|
|
/*button_pitch_rssi.on_select = [this](ImageButton&) {
|
2017-11-28 08:52:04 +01:00
|
|
|
this->toggle_pitch_rssi();
|
2018-02-22 07:04:19 +00:00
|
|
|
};*/
|
2016-05-25 11:33:23 -07:00
|
|
|
|
2016-04-30 13:56:54 -07:00
|
|
|
button_record.on_select = [this](ImageButton&) {
|
|
|
|
this->toggle();
|
|
|
|
};
|
|
|
|
|
2016-08-21 17:49:06 -07:00
|
|
|
signal_token_tick_second = rtc_time::signal_tick_second += [this]() {
|
2016-04-30 13:56:54 -07:00
|
|
|
this->on_tick_second();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
RecordView::~RecordView() {
|
2016-08-21 17:49:06 -07:00
|
|
|
rtc_time::signal_tick_second -= signal_token_tick_second;
|
2016-04-30 13:56:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void RecordView::focus() {
|
|
|
|
button_record.focus();
|
|
|
|
}
|
|
|
|
|
2016-06-21 11:33:23 -07:00
|
|
|
void RecordView::set_sampling_rate(const size_t new_sampling_rate) {
|
2021-11-27 23:36:44 +01:00
|
|
|
|
2022-01-26 18:06:01 +01:00
|
|
|
/* We are changing "REC" icon background to yellow in BW rec Options >600kHz
|
2021-11-27 23:36:44 +01:00
|
|
|
where we are NOT recording full IQ .C16 files (recorded files are decimated ones).
|
|
|
|
Those decimated recorded files,has not the full IQ samples .
|
|
|
|
are ok as recorded spectrum indication, but they should not be used by Replay app.
|
|
|
|
|
|
|
|
We keep original black background in all the correct IQ .C16 files BW's Options */
|
2022-01-26 18:06:01 +01:00
|
|
|
if (new_sampling_rate > 4800000) { // > BW >600kHz (fs=8*BW), (750kHz ...2750kHz)
|
2021-11-27 23:36:44 +01:00
|
|
|
button_record.set_background(ui::Color::yellow());
|
|
|
|
} else {
|
|
|
|
button_record.set_background(ui::Color::black());
|
|
|
|
}
|
|
|
|
|
2016-06-21 11:33:23 -07:00
|
|
|
if( new_sampling_rate != sampling_rate ) {
|
|
|
|
stop();
|
2018-02-22 07:04:19 +00:00
|
|
|
|
2016-06-21 11:33:23 -07:00
|
|
|
sampling_rate = new_sampling_rate;
|
2018-02-22 07:04:19 +00:00
|
|
|
baseband::set_sample_rate(sampling_rate);
|
2016-06-21 11:33:23 -07: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);
|
2016-06-21 11:35:41 -07:00
|
|
|
|
|
|
|
update_status_display();
|
2016-06-21 11:33:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 20:53:09 -07:00
|
|
|
// Setter for datetime and frequency filename
|
|
|
|
void RecordView::set_filename_date_frequency(bool set) {
|
|
|
|
filename_date_frequency = set;
|
|
|
|
}
|
|
|
|
|
2016-04-30 13:56:54 -07:00
|
|
|
bool RecordView::is_active() const {
|
|
|
|
return (bool)capture_thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordView::toggle() {
|
|
|
|
if( is_active() ) {
|
|
|
|
stop();
|
|
|
|
} else {
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordView::start() {
|
|
|
|
stop();
|
|
|
|
|
2016-05-02 11:47:44 -07:00
|
|
|
text_record_filename.set("");
|
|
|
|
text_record_dropped.set("");
|
|
|
|
|
2016-04-30 13:56:54 -07:00
|
|
|
if( sampling_rate == 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-28 20:53:09 -07:00
|
|
|
std::filesystem::path base_path;
|
|
|
|
if(filename_date_frequency) {
|
|
|
|
rtcGetTime(&RTCD1, &datetime);
|
|
|
|
|
|
|
|
//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());
|
|
|
|
|
2023-05-12 11:08:07 -07:00
|
|
|
base_path = filename_stem_pattern.string() + "_" + date_time + "_" +
|
|
|
|
trim(to_string_freq(receiver_model.tuning_frequency())) + "Hz";
|
|
|
|
base_path = folder / base_path;
|
2022-03-28 20:53:09 -07:00
|
|
|
} else {
|
2023-05-12 11:08:07 -07:00
|
|
|
base_path = next_filename_matching_pattern(folder / filename_stem_pattern);
|
2022-03-28 20:53:09 -07:00
|
|
|
}
|
|
|
|
|
2016-09-08 12:57:34 -07:00
|
|
|
if( base_path.empty() ) {
|
2016-04-30 13:56:54 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-04 17:13:21 -07:00
|
|
|
std::unique_ptr<stream::Writer> writer;
|
2016-05-02 11:44:37 -07:00
|
|
|
switch(file_type) {
|
|
|
|
case FileType::WAV:
|
2016-05-16 14:01:44 -07:00
|
|
|
{
|
2016-10-04 09:59:47 -07:00
|
|
|
auto p = std::make_unique<WAVFileWriter>();
|
2017-08-12 14:29:54 +01:00
|
|
|
auto create_error = p->create(
|
|
|
|
base_path.replace_extension(u".WAV"),
|
|
|
|
sampling_rate,
|
|
|
|
to_string_dec_uint(receiver_model.tuning_frequency()) + "Hz"
|
|
|
|
);
|
2016-05-16 14:01:44 -07:00
|
|
|
if( create_error.is_valid() ) {
|
2016-06-21 11:13:58 -07:00
|
|
|
handle_error(create_error.value());
|
2016-05-16 14:01:44 -07:00
|
|
|
} else {
|
|
|
|
writer = std::move(p);
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 11:44:37 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FileType::RawS16:
|
2016-05-16 14:01:44 -07:00
|
|
|
{
|
2016-09-08 12:57:34 -07:00
|
|
|
const auto metadata_file_error = write_metadata_file(base_path.replace_extension(u".TXT"));
|
2016-05-16 14:01:44 -07:00
|
|
|
if( metadata_file_error.is_valid() ) {
|
2016-06-21 11:13:58 -07:00
|
|
|
handle_error(metadata_file_error.value());
|
2016-05-16 14:01:44 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto p = std::make_unique<RawFileWriter>();
|
2016-09-08 12:57:34 -07:00
|
|
|
auto create_error = p->create(base_path.replace_extension(u".C16"));
|
2016-05-16 14:01:44 -07:00
|
|
|
if( create_error.is_valid() ) {
|
2016-06-21 11:13:58 -07:00
|
|
|
handle_error(create_error.value());
|
2016-05-16 14:01:44 -07:00
|
|
|
} else {
|
|
|
|
writer = std::move(p);
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 11:44:37 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
2016-04-30 13:56:54 -07:00
|
|
|
|
2016-05-02 11:44:37 -07:00
|
|
|
if( writer ) {
|
2023-05-12 11:08:07 -07:00
|
|
|
text_record_filename.set(truncate(base_path.filename().string(), 8));
|
2016-05-16 14:01:44 -07:00
|
|
|
button_record.set_bitmap(&bitmap_stop);
|
|
|
|
capture_thread = std::make_unique<CaptureThread>(
|
|
|
|
std::move(writer),
|
2016-06-21 11:04:10 -07:00
|
|
|
write_size, buffer_count,
|
2016-06-21 11:53:07 -07:00
|
|
|
[]() {
|
2016-06-21 12:05:55 -07:00
|
|
|
CaptureThreadDoneMessage message { };
|
2016-06-21 11:53:07 -07:00
|
|
|
EventDispatcher::send_message(message);
|
|
|
|
},
|
2016-06-21 11:04:10 -07:00
|
|
|
[](File::Error error) {
|
2016-06-21 12:05:55 -07:00
|
|
|
CaptureThreadDoneMessage message { error.code() };
|
2016-06-21 11:04:10 -07:00
|
|
|
EventDispatcher::send_message(message);
|
|
|
|
}
|
2016-05-16 14:01:44 -07:00
|
|
|
);
|
2016-05-02 11:44:37 -07:00
|
|
|
}
|
2016-06-21 11:35:41 -07:00
|
|
|
|
|
|
|
update_status_display();
|
2016-04-30 13:56:54 -07:00
|
|
|
}
|
|
|
|
|
2020-05-29 00:55:57 +02:00
|
|
|
void RecordView::on_hide() {
|
|
|
|
stop(); // Stop current recording
|
|
|
|
View::on_hide();
|
|
|
|
}
|
|
|
|
|
2016-04-30 13:56:54 -07:00
|
|
|
void RecordView::stop() {
|
|
|
|
if( is_active() ) {
|
|
|
|
capture_thread.reset();
|
|
|
|
button_record.set_bitmap(&bitmap_record);
|
|
|
|
}
|
2016-06-21 11:35:41 -07:00
|
|
|
|
|
|
|
update_status_display();
|
2016-04-30 13:56:54 -07:00
|
|
|
}
|
|
|
|
|
2016-08-21 18:06:39 -07:00
|
|
|
Optional<File::Error> RecordView::write_metadata_file(const std::filesystem::path& filename) {
|
2016-05-16 14:01:44 -07:00
|
|
|
File file;
|
|
|
|
const auto create_error = file.create(filename);
|
|
|
|
if( create_error.is_valid() ) {
|
|
|
|
return create_error;
|
|
|
|
} else {
|
2018-02-22 07:04:19 +00:00
|
|
|
const auto error_line1 = file.write_line("sample_rate=" + to_string_dec_uint(sampling_rate / 8));
|
2016-07-24 20:39:21 -07:00
|
|
|
if( error_line1.is_valid() ) {
|
|
|
|
return error_line1;
|
2016-05-16 14:01:44 -07:00
|
|
|
}
|
2016-07-24 20:39:21 -07:00
|
|
|
const auto error_line2 = file.write_line("center_frequency=" + to_string_dec_uint(receiver_model.tuning_frequency()));
|
|
|
|
if( error_line2.is_valid() ) {
|
|
|
|
return error_line2;
|
2016-05-16 14:01:44 -07:00
|
|
|
}
|
|
|
|
return { };
|
|
|
|
}
|
2016-04-30 13:56:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void RecordView::on_tick_second() {
|
2016-06-21 11:35:15 -07:00
|
|
|
update_status_display();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordView::update_status_display() {
|
2016-04-30 13:56:54 -07:00
|
|
|
if( is_active() ) {
|
|
|
|
const auto dropped_percent = std::min(99U, capture_thread->state().dropped_percent());
|
2023-05-12 11:08:07 -07:00
|
|
|
const auto s = to_string_dec_uint(dropped_percent, 2, ' ') + "%";
|
2016-04-30 13:56:54 -07:00
|
|
|
text_record_dropped.set(s);
|
2016-05-11 12:07:58 -07:00
|
|
|
}
|
2016-07-28 05:25:33 +02:00
|
|
|
|
2018-02-22 07:04:19 +00:00
|
|
|
/*if (pitch_rssi_enabled) {
|
2017-11-28 08:52:04 +01:00
|
|
|
button_pitch_rssi.invert_colors();
|
2018-02-22 07:04:19 +00:00
|
|
|
}*/
|
2016-05-11 11:22:57 -07:00
|
|
|
|
2016-05-11 12:07:58 -07:00
|
|
|
if( sampling_rate ) {
|
2016-08-21 18:06:39 -07:00
|
|
|
const auto space_info = std::filesystem::space(u"");
|
2018-02-22 07:04:19 +00:00
|
|
|
const uint32_t bytes_per_second = file_type == FileType::WAV ? (sampling_rate * 2) : (sampling_rate / 8 * 4);
|
2016-05-11 11:50:40 -07:00
|
|
|
const uint32_t available_seconds = space_info.free / bytes_per_second;
|
2016-05-11 12:02:39 -07:00
|
|
|
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, ' ') + ":" +
|
2016-05-11 12:34:09 -07:00
|
|
|
to_string_dec_uint(minutes, 2, '0') + ":" +
|
2016-05-11 12:02:39 -07:00
|
|
|
to_string_dec_uint(seconds, 2, '0');
|
|
|
|
text_time_available.set(available_time);
|
2016-04-30 13:56:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 12:05:55 -07:00
|
|
|
void RecordView::handle_capture_thread_done(const File::Error error) {
|
2016-06-21 11:04:10 -07:00
|
|
|
stop();
|
2016-06-21 12:05:55 -07:00
|
|
|
if( error.code() ) {
|
|
|
|
handle_error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecordView::handle_error(const File::Error error) {
|
2016-05-12 21:59:46 -07:00
|
|
|
if( on_error ) {
|
2016-06-21 11:13:58 -07:00
|
|
|
on_error(error.what());
|
2016-05-12 21:59:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 13:56:54 -07:00
|
|
|
} /* namespace ui */
|