WIP waveform drawing

This commit is contained in:
Stepland 2022-11-25 00:06:17 +01:00
parent 7ad8571310
commit 11d6f26c73
4 changed files with 68 additions and 3 deletions

View File

@ -21,6 +21,8 @@ public:
sf::Time getDuration() const;
TimeSpan getLoopPoints() const;
void setLoopPoints(TimeSpan timePoints);
sf::Uint64 timeToSamples(sf::Time position) const;
sf::Time samplesToTime(sf::Uint64 samples) const;
protected:
[[nodiscard]] bool onGetData(Chunk& data) override;
@ -29,8 +31,6 @@ protected:
private:
void initialize();
sf::Uint64 timeToSamples(sf::Time position) const;
sf::Time samplesToTime(sf::Uint64 samples) const;
sf::InputSoundFile m_file; //!< The streamed music file
std::vector<sf::Int16> m_samples; //!< Temporary buffer of samples

View File

@ -2,5 +2,6 @@ sources += files([
'blank_screen.cpp',
'density_graph.cpp',
'lane_order.cpp',
'linear_view.cpp'
'linear_view.cpp',
'waveform_view.cpp',
])

View File

@ -0,0 +1,43 @@
#include "waveform_view.hpp"
#include <SFML/System/Vector2.hpp>
#include <imgui.h>
#include <imgui_extras.hpp>
#include <imgui_internal.h>
WaveformView::WaveformView(const std::shared_ptr<OpenMusic>& audio_):
audio(audio_)
{
}
void WaveformView::draw(const sf::Time current_time) {
if (ImGui::Begin("Waveform view")) {
if (not data_is_ready) {
feis::CenteredText("Loading ...");
return ImGui::End();
}
const auto window = ImGui::GetCurrentWindow();
const auto work_rect = window->WorkRect;
auto draw_list = window->DrawList;
const float cursor_y = 50.f;
const float waveform_center_x = work_rect.GetCenter().x;
if (max_of_every_64_samples.size() <= 1) {
feis::CenteredText("Not enough samples ... Why ?");
return ImGui::End();
}
const std::int64_t sample_at_cursor = audio->timeToSamples(current_time);
const auto bucket_at_cursor = sample_at_cursor / 64;
const auto first_bucket = bucket_at_cursor - static_cast<std::int64_t>(cursor_y);
const auto end_bucket = first_bucket + static_cast<std::int64_t>(work_rect.GetHeight());
auto it = max_of_every_64_samples.begin();
auto next_it = std::next(it);
for (; next_it != max_of_every_64_samples.end(); ++next_it, ++it) {
}
}
ImGui::End();
}

View File

@ -0,0 +1,21 @@
#pragma once
#include <memory>
#include <thread>
#include <imgui/imgui.h>
#include <SFML/System/Time.hpp>
#include "../custom_sfml_audio/open_music.hpp"
class WaveformView {
public:
explicit WaveformView(const std::shared_ptr<OpenMusic>& audio);
void draw(const sf::Time current_time);
private:
std::shared_ptr<OpenMusic> audio;
std::atomic<bool> data_is_ready;
std::vector<float> max_of_every_64_samples;
void prepare_data();
};