portapack-mayhem/firmware/application/ui_soundboard.cpp

326 lines
7.6 KiB
C++
Raw Normal View History

2016-05-09 14:42:20 -04:00
/*
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2016 Furrtek
2016-05-09 14:42:20 -04: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.
*/
// To prepare samples: for f in ./*.wav; do sox "$f" -r 48000 -c 1 -b8 --norm "conv/$f"; done
2016-05-09 14:42:20 -04:00
#include "ui_soundboard.hpp"
2016-09-23 06:20:36 -04:00
#include "lfsr_random.hpp"
2016-05-09 14:42:20 -04:00
#include "portapack.hpp"
#include "string_format.hpp"
#include "portapack_shared_memory.hpp"
#include <cstring>
#include <vector>
2016-05-09 14:42:20 -04:00
using namespace portapack;
namespace ui {
void SoundBoardView::do_random() {
uint16_t id;
2016-09-23 06:20:36 -04:00
chThdSleepMilliseconds(300); // 300ms
2016-09-23 06:20:36 -04:00
id = lfsr_iterate(lfsr_v) % max_sound;
play_sound(id);
buttons[id % 21].focus();
page = id / 21;
refresh_buttons(id);
}
void SoundBoardView::prepare_audio() {
2016-12-07 14:49:42 -05:00
if (sample_counter >= sample_duration) {
if (tx_mode == NORMAL) {
if (!check_loop.value()) {
pbar.set_value(0);
transmitter_model.disable();
return;
} else {
2016-12-07 14:49:42 -05:00
reader->rewind();
sample_counter = 0;
}
} else {
pbar.set_value(0);
transmitter_model.disable();
do_random();
}
}
2016-12-07 14:49:42 -05:00
pbar.set_value(sample_counter);
2016-12-07 14:49:42 -05:00
size_t bytes_read = reader->read(audio_buffer, 1024);
// Unsigned to signed, pretty stupid :/
for (size_t n = 0; n < bytes_read; n++)
audio_buffer[n] -= 0x80;
2016-12-07 14:49:42 -05:00
for (size_t n = bytes_read; n < 1024; n++)
audio_buffer[n] = 0;
2016-12-07 14:49:42 -05:00
sample_counter += 1024;
baseband::set_fifo_data(audio_buffer);
}
2016-05-09 14:42:20 -04:00
void SoundBoardView::focus() {
buttons[0].focus();
if (!max_sound) {
nav_.display_modal("No files", "No files in /wav/ directory", ABORT, nullptr);
}
2016-05-09 14:42:20 -04:00
}
void SoundBoardView::on_tuning_frequency_changed(rf::Frequency f) {
transmitter_model.set_tuning_frequency(f);
}
void SoundBoardView::play_sound(uint16_t id) {
2016-09-23 17:08:54 -04:00
uint32_t ctcss_option;
bool ctcss_enabled;
uint32_t divider;
if (sounds[id].size == 0) return;
if (!reader->open(sounds[id].path)) return;
sample_duration = sounds[id].sample_duration;
pbar.set_max(sample_duration);
pbar.set_value(0);
2016-12-07 14:49:42 -05:00
sample_counter = 0;
prepare_audio();
transmitter_model.set_sampling_rate(1536000U);
transmitter_model.set_rf_amp(true);
transmitter_model.set_lna(40);
transmitter_model.set_vga(40);
transmitter_model.set_baseband_bandwidth(1750000);
transmitter_model.enable();
2016-09-23 17:08:54 -04:00
ctcss_option = options_ctcss.selected_index();
if (ctcss_option)
ctcss_enabled = true;
else
ctcss_enabled = false;
divider = (1536000 / sounds[id].sample_rate) - 1;
baseband::set_audiotx_data(
divider,
number_bw.value(),
ctcss_enabled,
(67109.0 * (float)_ctcss_freq)/1536000.0 // TODO: Might not be precise enough
);
}
void SoundBoardView::show_infos(uint16_t id) {
uint32_t duration = sounds[id].ms_duration;
text_duration.set(to_string_dec_uint(duration / 1000) + "." + to_string_dec_uint((duration / 100) % 10) + "s");
}
void SoundBoardView::refresh_buttons(uint16_t id) {
size_t n = 0, n_sound;
text_page.set(to_string_dec_uint(page + 1) + "/" + to_string_dec_uint(max_page));
for (auto& button : buttons) {
n_sound = (page * 21) + n;
button.id = n_sound;
if (n_sound < max_sound) {
button.set_text(sounds[n_sound].path.stem().string());
button.set_style(styles[sounds[n_sound].path.stem().string()[0] & 3]);
} else {
button.set_text("- - -");
button.set_style(styles[0]);
}
n++;
}
show_infos(id);
}
void SoundBoardView::change_page(Button& button, const KeyEvent key) {
// Stupid way to find out if the button is on the sides
if (button.screen_pos().x() < 32) {
if ((key == KeyEvent::Left) && (page > 0)) {
page--;
refresh_buttons(button.id);
}
}
if (button.screen_pos().x() > 120) {
if ((key == KeyEvent::Right) && (page < max_page - 1)) {
page++;
refresh_buttons(button.id);
}
}
}
2016-09-23 17:08:54 -04:00
void SoundBoardView::on_ctcss_changed(uint32_t v) {
_ctcss_freq = v;
}
2016-05-09 14:42:20 -04:00
SoundBoardView::SoundBoardView(
NavigationView& nav
) : nav_ (nav)
2016-05-09 14:42:20 -04:00
{
using name_t = std::string;
using value_t = int32_t;
using option_t = std::pair<name_t, value_t>;
using options_t = std::vector<option_t>;
options_t ctcss_options;
std::vector<std::filesystem::path> file_list;
uint8_t c;
2016-12-07 14:49:42 -05:00
reader = std::make_unique<WAVFileReader>();
2016-05-09 14:42:20 -04:00
file_list = scan_root_files(reinterpret_cast<const TCHAR*>("/wav"), ".WAV");
2016-12-07 14:49:42 -05:00
c = 0;
for (auto& path : file_list) {
if (reader->open(path)) {
2016-12-07 14:49:42 -05:00
if (reader->channels() == 1) {
sounds[c].size = reader->data_size();
sounds[c].sample_duration = reader->data_size() / (reader->bits_per_sample() / 8);
sounds[c].sample_rate = reader->sample_rate();
if (reader->bits_per_sample() > 8)
sounds[c].sixteenbit = true;
2016-12-07 14:49:42 -05:00
else
sounds[c].sixteenbit = false;
2016-12-07 14:49:42 -05:00
sounds[c].ms_duration = reader->ms_duration();
sounds[c].path = path;
c++;
2016-12-07 14:49:42 -05:00
if (c == 105) break; // Limit to 105 files (5 pages)
}
}
2016-05-09 14:42:20 -04:00
}
2016-12-07 14:49:42 -05:00
baseband::run_image(portapack::spi_flash::image_tag_audio_tx);
max_sound = c;
2016-12-07 14:49:42 -05:00
max_page = max_sound / 21; // 3 * 7 = 21 buttons per page
add_children({
2016-05-09 14:42:20 -04:00
&field_frequency,
&number_bw,
&text_kHz,
2016-09-23 17:08:54 -04:00
&options_ctcss,
&text_page,
&text_duration,
&pbar,
&check_loop,
&button_random,
2016-05-09 14:42:20 -04:00
&button_exit
});
ctcss_options.emplace_back(std::make_pair("None", 0));
for (c = 0; c < CTCSS_TONES_NB; c++)
ctcss_options.emplace_back(std::make_pair(ctcss_tones[c].PL_code, c));
options_ctcss.set_options(ctcss_options);
2016-09-23 17:08:54 -04:00
options_ctcss.on_change = [this](size_t, OptionsField::value_t v) {
this->on_ctcss_changed(v);
};
options_ctcss.set_selected_index(0);
2016-05-09 14:42:20 -04:00
const auto button_fn = [this](Button& button) {
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) {
this->change_page(button, key);
return false;
2016-05-09 14:42:20 -04:00
};
size_t n = 0;
2016-05-09 14:42:20 -04:00
for(auto& button : buttons) {
add_child(&button);
button.on_select = button_fn;
button.on_highlight = button_focus;
button.on_dir = button_dir;
2016-05-09 14:42:20 -04:00
button.set_parent_rect({
static_cast<Coord>((n % 3) * 78 + 3),
static_cast<Coord>((n / 3) * 28 + 26),
78, 28
2016-05-09 14:42:20 -04:00
});
n++;
}
refresh_buttons(0);
check_loop.set_value(false);
number_bw.set_value(15);
2016-05-09 14:42:20 -04:00
field_frequency.set_value(transmitter_model.tuning_frequency());
field_frequency.set_step(10000);
2016-05-09 14:42:20 -04: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?
auto new_view = nav.push<FrequencyKeypadView>(transmitter_model.tuning_frequency());
2016-05-09 14:42:20 -04:00
new_view->on_changed = [this](rf::Frequency f) {
this->on_tuning_frequency_changed(f);
this->field_frequency.set_value(f);
};
};
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 14:42:20 -04:00
button_exit.on_select = [&nav](Button&){
nav.pop();
};
}
SoundBoardView::~SoundBoardView() {
transmitter_model.disable();
baseband::shutdown();
2016-05-09 14:42:20 -04:00
}
}