From 9e37cfd67d5fb374307aff83779796499dad5bf9 Mon Sep 17 00:00:00 2001 From: Stepland <10530295-Buggyroom@users.noreply.gitlab.com> Date: Mon, 16 Jan 2023 00:51:48 +0100 Subject: [PATCH] More waveform stuff --- src/main.cpp | 6 ++++++ src/widgets/waveform_view.cpp | 38 ++++++++++++++++++++++++----------- src/widgets/waveform_view.hpp | 16 +++++++++++++-- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d543438..c3512f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -346,12 +346,18 @@ int main() { case sf::Keyboard::Add: if (editor_state) { editor_state->linear_view.zoom_in(); + if (editor_state->waveform_view) { + editor_state->waveform_view->zoom_in(); + } notificationsQueue.push(std::make_shared("Zoom in")); } break; case sf::Keyboard::Subtract: if (editor_state) { editor_state->linear_view.zoom_out(); + if (editor_state->waveform_view) { + editor_state->waveform_view->zoom_out(); + } notificationsQueue.push(std::make_shared("Zoom out")); } break; diff --git a/src/widgets/waveform_view.cpp b/src/widgets/waveform_view.cpp index e7fbb6f..2d8f3ac 100644 --- a/src/widgets/waveform_view.cpp +++ b/src/widgets/waveform_view.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -27,14 +28,14 @@ void WaveformView::draw(const sf::Time current_time) { feis::CenteredText("Loading ..."); return ImGui::End(); } - - const auto channels_it = channels_per_chunk_size.begin(); - if (channels_it == channels_per_chunk_size.end()) { + if (channels_per_chunk_size.empty()) { feis::CenteredText("No data ???"); return ImGui::End(); } - const auto& [chunk_size, channels] = *channels_it; + zoom = std::clamp(zoom, 0, static_cast(channels_per_chunk_size.size()) - 1); + const auto& channels_it = channels_per_chunk_size.at(channels_per_chunk_size.size() - zoom - 1); + const auto& [chunk_size, channels] = channels_it; const auto window = ImGui::GetCurrentWindow(); const auto work_rect = window->WorkRect; auto draw_list = window->DrawList; @@ -72,6 +73,19 @@ void WaveformView::draw(const sf::Time current_time) { ImGui::End(); } +void WaveformView::set_zoom(int new_zoom) { + zoom = std::clamp(new_zoom, 0, 9); +} + +void WaveformView::zoom_in() { + set_zoom(zoom + 1); +} + +void WaveformView::zoom_out() { + set_zoom(zoom - 1); +} + + Channels load_initial_summary( feis::HoldFileStreamMixin& sound_file, const unsigned int window_size @@ -109,16 +123,16 @@ Channels load_initial_summary( } Channels downsample_to_half(const Channels& summary) { - Channels downsample; + Channels downsampled_summary; for (const auto& channel : summary) { - std::vector downsampled{(channel.size() / 2) + 1}; - auto out = downsampled.begin(); + auto& downsampled_channel = downsampled_summary.emplace_back((channel.size() / 2) + 1); + auto out = downsampled_channel.begin(); auto in = channel.begin(); - while (in != channel.end() and out != downsampled.end()) { + while (in != channel.end() and out != downsampled_channel.end()) { const auto next_in = std::next(in); if (next_in != channel.end()) { out->max = std::max(in->max, next_in->max); - out->min = std::max(in->min, next_in->min); + out->min = std::min(in->min, next_in->min); } else { *out = *in; break; @@ -127,14 +141,14 @@ Channels downsample_to_half(const Channels& summary) { std::advance(in, 2); } } - return downsample; + return downsampled_summary; }; void WaveformView::prepare_data() { unsigned int size = 8; - channels_per_chunk_size[size] = load_initial_summary(sound_file, size); + channels_per_chunk_size.emplace_back(size, load_initial_summary(sound_file, size)); while (channels_per_chunk_size.size() < 10) { - channels_per_chunk_size[size * 2] = downsample_to_half(channels_per_chunk_size[size]); + channels_per_chunk_size.emplace_back(size * 2, downsample_to_half(channels_per_chunk_size.rbegin()->second)); size *= 2; } data_is_ready = true; diff --git a/src/widgets/waveform_view.hpp b/src/widgets/waveform_view.hpp index e1c9a8c..2754dbe 100644 --- a/src/widgets/waveform_view.hpp +++ b/src/widgets/waveform_view.hpp @@ -30,12 +30,24 @@ public: void draw(const sf::Time current_time); void draw_settings(); bool display = false; + + void set_zoom(int zoom); + void zoom_in(); + void zoom_out(); private: feis::HoldFileStreamMixin sound_file; std::atomic data_is_ready = false; - std::map channels_per_chunk_size; + std::vector> channels_per_chunk_size; std::optional::iterator> selected_size; std::jthread worker; + int zoom = 0; + void prepare_data(); -}; \ No newline at end of file +}; + +Channels load_initial_summary( + feis::HoldFileStreamMixin& sound_file, + const unsigned int window_size +); +Channels downsample_to_half(const Channels& summary); \ No newline at end of file