portapack-mayhem/firmware/application/ui_audio.cpp

99 lines
2.5 KiB
C++
Raw Normal View History

2015-07-08 11:39:24 -04:00
/*
* Copyright (C) 2015 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_audio.hpp"
2016-01-31 03:34:24 -05:00
#include "event_m0.hpp"
#include "utility.hpp"
2015-07-08 11:39:24 -04:00
#include <algorithm>
namespace ui {
void Audio::on_show() {
2016-01-31 03:34:24 -05:00
EventDispatcher::message_map().register_handler(Message::ID::AudioStatistics,
[this](const Message* const p) {
this->on_statistics_update(static_cast<const AudioStatisticsMessage*>(p)->statistics);
}
);
2015-07-08 11:39:24 -04:00
}
void Audio::on_hide() {
2016-01-31 03:34:24 -05:00
EventDispatcher::message_map().unregister_handler(Message::ID::AudioStatistics);
2015-07-08 11:39:24 -04:00
}
void Audio::paint(Painter& painter) {
const auto r = screen_rect();
2016-01-31 03:34:24 -05:00
constexpr int db_min = -96;
constexpr int db_max = 0;
constexpr int db_delta = db_max - db_min;
const range_t<int> x_rms_range { 0, r.width() - 1 };
const auto x_rms = x_rms_range.clip((rms_db_ - db_min) * r.width() / db_delta);
const range_t<int> x_max_range { x_rms + 1, r.width() };
const auto x_max = x_max_range.clip((max_db_ - db_min) * r.width() / db_delta);
2015-07-08 11:39:24 -04:00
const Rect r0 {
2016-01-31 03:34:24 -05:00
static_cast<ui::Coord>(r.left()), r.top(),
static_cast<ui::Dim>(x_rms), r.height()
2015-07-08 11:39:24 -04:00
};
painter.fill_rectangle(
r0,
Color::green()
);
const Rect r1 {
static_cast<ui::Coord>(r.left() + x_rms), r.top(),
1, r.height()
};
painter.fill_rectangle(
r1,
Color::black()
);
const Rect r2 {
static_cast<ui::Coord>(r.left() + x_rms + 1), r.top(),
static_cast<ui::Dim>(x_max - (x_rms + 1)), r.height()
};
painter.fill_rectangle(
r2,
Color::red()
);
const Rect r3 {
static_cast<ui::Coord>(r.left() + x_max), r.top(),
2016-01-31 03:34:24 -05:00
static_cast<ui::Dim>(r.width() - x_max), r.height()
2015-07-08 11:39:24 -04:00
};
painter.fill_rectangle(
r3,
Color::black()
);
}
void Audio::on_statistics_update(const AudioStatistics& statistics) {
rms_db_ = statistics.rms_db;
max_db_ = statistics.max_db;
set_dirty();
}
} /* namespace ui */