2016-05-09 18:42:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
2016-08-26 06:11:24 +00:00
|
|
|
* Copyright (C) 2016 Furrtek
|
2016-05-09 18:42:20 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-08-26 06:11:24 +00:00
|
|
|
// To prepare samples: for f in ./*.wav; do sox "$f" -r 48000 -c 1 -b8 --norm "conv/$f"; done
|
|
|
|
|
2016-05-09 18:42:20 +00:00
|
|
|
#include "ui_soundboard.hpp"
|
2016-09-23 10:20:36 +00:00
|
|
|
#include "lfsr_random.hpp"
|
2016-05-09 18:42:20 +00:00
|
|
|
#include "string_format.hpp"
|
2017-11-10 00:25:04 +00:00
|
|
|
#include "tonesets.hpp"
|
2016-05-09 18:42:20 +00:00
|
|
|
|
2017-11-09 20:02:34 +00:00
|
|
|
using namespace tonekey;
|
2016-05-09 18:42:20 +00:00
|
|
|
using namespace portapack;
|
|
|
|
|
|
|
|
namespace ui {
|
2016-08-26 06:11:24 +00:00
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
// TODO: Use Sharebrained's PRNG
|
2016-08-26 06:11:24 +00:00
|
|
|
void SoundBoardView::do_random() {
|
2017-02-02 09:29:14 +00:00
|
|
|
uint32_t id;
|
2016-08-26 06:11:24 +00:00
|
|
|
|
2017-02-02 09:29:14 +00:00
|
|
|
chThdSleepMilliseconds(500);
|
2016-08-26 06:11:24 +00:00
|
|
|
|
2017-02-02 09:29:14 +00:00
|
|
|
lfsr_v = lfsr_iterate(lfsr_v);
|
|
|
|
id = lfsr_v % max_sound;
|
2016-08-26 06:11:24 +00:00
|
|
|
|
|
|
|
play_sound(id);
|
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
buttons[id % 15].focus();
|
|
|
|
page = id / 15;
|
2016-08-26 06:11:24 +00:00
|
|
|
|
|
|
|
refresh_buttons(id);
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
bool SoundBoardView::is_active() const {
|
|
|
|
return (bool)replay_thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundBoardView::stop(const bool do_loop) {
|
|
|
|
if( is_active() )
|
|
|
|
replay_thread.reset();
|
2016-08-26 06:11:24 +00:00
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
if (do_loop && check_loop.value()) {
|
|
|
|
play_sound(playing_id);
|
|
|
|
} else {
|
|
|
|
radio::disable();
|
|
|
|
//button_play.set_bitmap(&bitmap_play);
|
2016-08-26 06:11:24 +00:00
|
|
|
}
|
2018-05-15 22:35:30 +00:00
|
|
|
ready_signal = false;
|
|
|
|
}
|
2016-12-06 12:31:38 +00:00
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
void SoundBoardView::handle_replay_thread_done(const uint32_t return_code) {
|
|
|
|
if (return_code == ReplayThread::END_OF_FILE) {
|
|
|
|
stop(true);
|
|
|
|
} else if (return_code == ReplayThread::READ_ERROR) {
|
|
|
|
stop(false);
|
|
|
|
file_error();
|
|
|
|
}
|
2016-08-26 06:11:24 +00:00
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
progressbar.set_value(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundBoardView::set_ready() {
|
|
|
|
ready_signal = true;
|
2016-08-26 06:11:24 +00:00
|
|
|
}
|
2016-05-09 18:42:20 +00:00
|
|
|
|
|
|
|
void SoundBoardView::focus() {
|
|
|
|
buttons[0].focus();
|
2016-12-06 12:31:38 +00:00
|
|
|
|
2017-02-01 23:31:16 +00:00
|
|
|
if (!max_sound)
|
2017-06-22 08:08:37 +00:00
|
|
|
nav_.display_modal("No files", "No files in /WAV/ directory", ABORT, nullptr);
|
2016-05-09 18:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundBoardView::on_tuning_frequency_changed(rf::Frequency f) {
|
|
|
|
transmitter_model.set_tuning_frequency(f);
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
void SoundBoardView::file_error() {
|
|
|
|
nav_.display_modal("Error", "File read error.");
|
|
|
|
}
|
2016-08-26 07:54:17 +00:00
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
void SoundBoardView::play_sound(uint16_t id) {
|
|
|
|
uint32_t sample_rate = 0;
|
|
|
|
auto reader = std::make_unique<WAVFileReader>();
|
|
|
|
uint32_t tone_key_index = options_tone_key.selected_index();
|
2016-08-26 06:11:24 +00:00
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
stop(false);
|
|
|
|
|
2018-05-16 08:45:13 +00:00
|
|
|
if (!reader->open(sounds[id].path)) {
|
2018-05-15 22:35:30 +00:00
|
|
|
file_error();
|
|
|
|
return;
|
|
|
|
}
|
2016-08-26 06:11:24 +00:00
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
playing_id = id;
|
2016-08-26 06:11:24 +00:00
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
progressbar.set_max(reader->sample_count());
|
|
|
|
sample_rate = reader->sample_rate() * 32;
|
2016-09-23 21:08:54 +00:00
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
if( reader ) {
|
|
|
|
//button_play.set_bitmap(&bitmap_stop);
|
|
|
|
baseband::set_sample_rate(sample_rate);
|
|
|
|
|
|
|
|
replay_thread = std::make_unique<ReplayThread>(
|
|
|
|
std::move(reader),
|
|
|
|
read_size, buffer_count,
|
|
|
|
&ready_signal,
|
|
|
|
[](uint32_t return_code) {
|
|
|
|
ReplayThreadDoneMessage message { return_code };
|
|
|
|
EventDispatcher::send_message(message);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-09-23 21:08:54 +00:00
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
baseband::set_audiotx_config(
|
2018-05-16 08:45:13 +00:00
|
|
|
0, // Divider is unused
|
2017-02-02 09:29:14 +00:00
|
|
|
number_bw.value() * 1000,
|
2018-05-16 08:45:13 +00:00
|
|
|
0, // Gain is unused
|
|
|
|
TONES_F2D(tone_key_frequency(tone_key_index), sample_rate)
|
2016-09-23 21:08:54 +00:00
|
|
|
);
|
2018-05-15 22:35:30 +00:00
|
|
|
|
|
|
|
radio::enable({
|
|
|
|
receiver_model.tuning_frequency(),
|
|
|
|
sample_rate,
|
|
|
|
1750000,
|
|
|
|
rf::Direction::Transmit,
|
|
|
|
receiver_model.rf_amp(),
|
|
|
|
static_cast<int8_t>(receiver_model.lna()),
|
|
|
|
static_cast<int8_t>(receiver_model.vga())
|
|
|
|
});
|
2016-08-26 06:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundBoardView::show_infos(uint16_t id) {
|
2017-12-11 04:14:54 +00:00
|
|
|
text_duration.set(to_string_time_ms(sounds[id].ms_duration));
|
2017-03-11 00:59:04 +00:00
|
|
|
|
|
|
|
text_title.set(sounds[id].title);
|
2016-08-26 06:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundBoardView::refresh_buttons(uint16_t id) {
|
|
|
|
size_t n = 0, n_sound;
|
|
|
|
|
2017-03-11 00:59:04 +00:00
|
|
|
text_page.set("Page " + to_string_dec_uint(page + 1) + "/" + to_string_dec_uint(max_page));
|
2016-08-26 06:11:24 +00:00
|
|
|
|
|
|
|
for (auto& button : buttons) {
|
2018-05-15 22:35:30 +00:00
|
|
|
n_sound = (page * 15) + n;
|
2016-08-26 06:11:24 +00:00
|
|
|
|
|
|
|
button.id = n_sound;
|
|
|
|
|
|
|
|
if (n_sound < max_sound) {
|
2017-02-01 23:31:16 +00:00
|
|
|
button.set_text(sounds[n_sound].path.stem().string().substr(0, 8));
|
2017-01-16 03:45:44 +00:00
|
|
|
button.set_style(styles[sounds[n_sound].path.stem().string()[0] & 3]);
|
2016-08-26 06:11:24 +00:00
|
|
|
} else {
|
|
|
|
button.set_text("- - -");
|
|
|
|
button.set_style(styles[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
show_infos(id);
|
|
|
|
}
|
|
|
|
|
2017-12-11 02:40:43 +00:00
|
|
|
bool SoundBoardView::change_page(Button& button, const KeyEvent key) {
|
2016-08-26 06:11:24 +00:00
|
|
|
// Stupid way to find out if the button is on the sides
|
2017-01-16 03:45:44 +00:00
|
|
|
if (button.screen_pos().x() < 32) {
|
2016-08-26 06:11:24 +00:00
|
|
|
if ((key == KeyEvent::Left) && (page > 0)) {
|
|
|
|
page--;
|
|
|
|
refresh_buttons(button.id);
|
2017-12-11 02:40:43 +00:00
|
|
|
return true;
|
2016-08-26 06:11:24 +00:00
|
|
|
}
|
2017-03-12 07:09:22 +00:00
|
|
|
} else if (button.screen_pos().x() > 120) {
|
2016-08-26 06:11:24 +00:00
|
|
|
if ((key == KeyEvent::Right) && (page < max_page - 1)) {
|
|
|
|
page++;
|
|
|
|
refresh_buttons(button.id);
|
2017-12-11 02:40:43 +00:00
|
|
|
return true;
|
2016-08-26 06:11:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-11 02:40:43 +00:00
|
|
|
return false;
|
2016-08-26 06:11:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
void SoundBoardView::on_tx_progress(const uint32_t progress) {
|
|
|
|
progressbar.set_value(progress);
|
|
|
|
}
|
|
|
|
|
2016-05-09 18:42:20 +00:00
|
|
|
SoundBoardView::SoundBoardView(
|
|
|
|
NavigationView& nav
|
2016-12-06 12:31:38 +00:00
|
|
|
) : nav_ (nav)
|
2016-05-09 18:42:20 +00:00
|
|
|
{
|
2018-05-15 22:35:30 +00:00
|
|
|
auto reader = std::make_unique<WAVFileReader>();
|
|
|
|
uint8_t c = 0;
|
2016-05-09 18:42:20 +00:00
|
|
|
|
2018-05-15 22:35:30 +00:00
|
|
|
for(const auto& entry : std::filesystem::directory_iterator(u"WAV", u"*.WAV")) {
|
|
|
|
if( std::filesystem::is_regular_file(entry.status()) ) {
|
|
|
|
if (reader->open(u"WAV/" + entry.path().native())) {
|
|
|
|
if ((reader->channels() == 1) && (reader->bits_per_sample() == 8)) {
|
|
|
|
sounds[c].ms_duration = reader->ms_duration();
|
|
|
|
sounds[c].path = u"WAV/" + entry.path().native();
|
|
|
|
std::string title = reader->title().substr(0, 20);
|
|
|
|
if (title != "")
|
|
|
|
sounds[c].title = title;
|
|
|
|
else
|
|
|
|
sounds[c].title = "-";
|
|
|
|
c++;
|
|
|
|
if (c == 60) break; // Limit to 60 files (4 pages)
|
|
|
|
}
|
2016-09-23 15:34:50 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-09 18:42:20 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 19:49:42 +00:00
|
|
|
baseband::run_image(portapack::spi_flash::image_tag_audio_tx);
|
2018-05-15 22:35:30 +00:00
|
|
|
|
2016-08-26 06:11:24 +00:00
|
|
|
max_sound = c;
|
2018-05-15 22:35:30 +00:00
|
|
|
max_page = (max_sound + 15 - 1) / 15; // 3 * 5 = 15 buttons per page
|
2016-08-26 06:11:24 +00:00
|
|
|
|
2017-01-16 03:45:44 +00:00
|
|
|
add_children({
|
2017-12-11 02:40:43 +00:00
|
|
|
&labels,
|
2016-05-09 18:42:20 +00:00
|
|
|
&field_frequency,
|
|
|
|
&number_bw,
|
2018-05-15 22:35:30 +00:00
|
|
|
&options_tone_key,
|
2017-03-11 00:59:04 +00:00
|
|
|
&text_title,
|
2016-08-26 06:11:24 +00:00
|
|
|
&text_page,
|
|
|
|
&text_duration,
|
2018-05-15 22:35:30 +00:00
|
|
|
&progressbar,
|
2016-08-26 06:11:24 +00:00
|
|
|
&check_loop,
|
|
|
|
&button_random,
|
2016-05-09 18:42:20 +00:00
|
|
|
&button_exit
|
2017-01-16 03:45:44 +00:00
|
|
|
});
|
2018-05-15 22:35:30 +00:00
|
|
|
|
|
|
|
tone_keys_populate(options_tone_key);
|
|
|
|
options_tone_key.set_selected_index(0);
|
|
|
|
|
2016-05-09 18:42:20 +00:00
|
|
|
const auto button_fn = [this](Button& button) {
|
2016-08-26 06:11:24 +00:00
|
|
|
tx_mode = NORMAL;
|
|
|
|
this->play_sound(button.id);
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto button_focus = [this](Button& button) {
|
|
|
|
this->show_infos(button.id);
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto button_dir = [this](Button& button, const KeyEvent key) {
|
2017-12-11 02:40:43 +00:00
|
|
|
return change_page(button, key);
|
2016-05-09 18:42:20 +00:00
|
|
|
};
|
2018-05-15 22:35:30 +00:00
|
|
|
|
2017-03-12 07:09:22 +00:00
|
|
|
// Generate buttons
|
2016-08-26 06:11:24 +00:00
|
|
|
size_t n = 0;
|
2016-05-09 18:42:20 +00:00
|
|
|
for(auto& button : buttons) {
|
|
|
|
add_child(&button);
|
|
|
|
button.on_select = button_fn;
|
2016-08-26 06:11:24 +00:00
|
|
|
button.on_highlight = button_focus;
|
|
|
|
button.on_dir = button_dir;
|
2016-05-09 18:42:20 +00:00
|
|
|
button.set_parent_rect({
|
2016-08-26 06:11:24 +00:00
|
|
|
static_cast<Coord>((n % 3) * 78 + 3),
|
2018-05-15 22:35:30 +00:00
|
|
|
static_cast<Coord>((n / 3) * 38 + 24),
|
|
|
|
78, 38
|
2016-05-09 18:42:20 +00:00
|
|
|
});
|
|
|
|
n++;
|
|
|
|
}
|
2016-08-26 06:11:24 +00:00
|
|
|
refresh_buttons(0);
|
|
|
|
|
|
|
|
check_loop.set_value(false);
|
2017-03-12 07:09:22 +00:00
|
|
|
number_bw.set_value(12);
|
2016-05-09 18:42:20 +00:00
|
|
|
|
|
|
|
field_frequency.set_value(transmitter_model.tuning_frequency());
|
2016-08-26 06:11:24 +00:00
|
|
|
field_frequency.set_step(10000);
|
2016-05-09 18:42:20 +00:00
|
|
|
field_frequency.on_change = [this](rf::Frequency f) {
|
|
|
|
this->on_tuning_frequency_changed(f);
|
|
|
|
};
|
|
|
|
field_frequency.on_edit = [this, &nav]() {
|
|
|
|
// TODO: Provide separate modal method/scheme?
|
2016-08-26 06:11:24 +00:00
|
|
|
auto new_view = nav.push<FrequencyKeypadView>(transmitter_model.tuning_frequency());
|
2016-05-09 18:42:20 +00:00
|
|
|
new_view->on_changed = [this](rf::Frequency f) {
|
|
|
|
this->on_tuning_frequency_changed(f);
|
|
|
|
this->field_frequency.set_value(f);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-08-26 06:11:24 +00:00
|
|
|
button_random.on_select = [this](Button&){
|
|
|
|
if (tx_mode == NORMAL) {
|
|
|
|
tx_mode = RANDOM;
|
|
|
|
button_random.set_text("STOP");
|
|
|
|
do_random();
|
|
|
|
} else {
|
|
|
|
tx_mode = NORMAL;
|
|
|
|
button_random.set_text("Random");
|
|
|
|
}
|
|
|
|
};
|
2016-05-09 18:42:20 +00:00
|
|
|
|
|
|
|
button_exit.on_select = [&nav](Button&){
|
|
|
|
nav.pop();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
SoundBoardView::~SoundBoardView() {
|
|
|
|
transmitter_model.disable();
|
2016-08-26 06:11:24 +00:00
|
|
|
baseband::shutdown();
|
2016-05-09 18:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|