/
ui_record_view.cpp
304 lines (256 loc) · 9.46 KB
/
ui_record_view.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* 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"
#include "io_convert.hpp"
#include "baseband_api.hpp"
#include "metadata_file.hpp"
#include "rtc_time.hpp"
#include "string_format.hpp"
#include "utility.hpp"
#include <cstdint>
namespace ui {
/*void RecordView::toggle_pitch_rssi() {
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 ) {
button_pitch_rssi.set_foreground(Color::orange());
} else {
button_pitch_rssi.set_foreground(Color::green());
}
}*/
RecordView::RecordView(
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,
&text_record_filename,
&text_record_dropped,
&text_time_available,
});
rect_background.set_parent_rect({{0, 0}, size()});
/*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();
};
}
RecordView::~RecordView() {
rtc_time::signal_tick_second -= signal_token_tick_second;
}
void RecordView::focus() {
button_record.focus();
}
void RecordView::set_sampling_rate(const size_t new_sampling_rate) {
/* We are changing "REC" icon background to yellow in BW rec Options >600kHz
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 */
if (new_sampling_rate > 4800000) { // > BW >600kHz (fs=8*BW), (750kHz ...2750kHz)
button_record.set_background(ui::Color::yellow());
} else {
button_record.set_background(ui::Color::black());
}
if (new_sampling_rate != sampling_rate) {
stop();
sampling_rate = new_sampling_rate;
baseband::set_sample_rate(sampling_rate);
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();
}
}
// Setter for datetime and frequency filename
void RecordView::set_filename_date_frequency(bool set) {
filename_date_frequency = set;
}
bool RecordView::is_active() const {
return (bool)capture_thread;
}
void RecordView::toggle() {
if (is_active()) {
stop();
} else {
start();
}
}
void RecordView::start() {
stop();
text_record_filename.set("");
text_record_dropped.set("");
if (sampling_rate == 0) {
return;
}
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());
base_path = filename_stem_pattern.string() + "_" + date_time + "_" +
trim(to_string_freq(receiver_model.target_frequency())) + "Hz";
base_path = folder / base_path;
} else {
base_path = next_filename_matching_pattern(folder / filename_stem_pattern);
}
if (base_path.empty()) {
return;
}
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,
to_string_dec_uint(receiver_model.target_frequency()) + "Hz");
if (create_error.is_valid()) {
handle_error(create_error.value());
} else {
writer = std::move(p);
}
} break;
case FileType::RawS8:
case FileType::RawS16: {
const auto metadata_file_error =
write_metadata_file(get_metadata_path(base_path),
{receiver_model.target_frequency(), sampling_rate / 8});
// Not sure why sample_rate is div. 8, but stored value matches rate settings.
if (metadata_file_error.is_valid()) {
handle_error(metadata_file_error.value());
return;
}
auto p = std::make_unique<FileConvertWriter>();
auto create_error = p->create(base_path.replace_extension((file_type == FileType::RawS8) ? u".C8" : u".C16"));
if (create_error.is_valid()) {
handle_error(create_error.value());
} else {
writer = std::move(p);
}
} break;
default:
break;
};
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);
});
}
update_status_display();
}
void RecordView::on_hide() {
stop(); // Stop current recording
View::on_hide();
}
void RecordView::stop() {
if (is_active()) {
capture_thread.reset();
button_record.set_bitmap(&bitmap_record);
}
update_status_display();
}
void RecordView::on_tick_second() {
update_status_display();
}
void RecordView::update_status_display() {
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);
}
/*if (pitch_rssi_enabled) {
button_pitch_rssi.invert_colors();
}*/
if (sampling_rate) {
const auto space_info = std::filesystem::space(u"");
const uint32_t bytes_per_second =
// - 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.
// Dividing to get actual sample rate because of decimation in proc_capture.
file_type == FileType::WAV ? (sampling_rate * 2) : (sampling_rate * ((file_type == FileType::RawS8) ? 2 : 4) / 8);
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);
}
}
void RecordView::handle_capture_thread_done(const File::Error error) {
stop();
if (error.code()) {
handle_error(error);
}
}
void RecordView::handle_error(const File::Error error) {
if (on_error) {
on_error(error.what());
}
}
} /* namespace ui */