新增mixer_track控件

新增音量条控件
This commit is contained in:
Nanako 2024-03-04 01:15:59 +08:00
parent f6d8f50cf7
commit 4e9f716931
7 changed files with 83 additions and 28 deletions

View File

@ -13,3 +13,4 @@ target_link_libraries(${PROJECT_NAME} core)
target_include_directories(${PROJECT_NAME} PUBLIC core)
compile_shader("build_shaders" ${CMAKE_CURRENT_SOURCE_DIR}/shaders ${CMAKE_CURRENT_SOURCE_DIR}/shader_src)
add_compile_definitions(-DIMGUI_DEFINE_MATH_OPERATORS)

View File

@ -41,12 +41,12 @@ public:
void post_init() {
singleton_manager::get()->init();
get_plugin_host_manager().create_instrument_plugin_host(R"(D:\Other\VST\VST64\helm64.dll)");
get_plugin_host_manager().create_instrument_plugin_host(R"(F:\VST\VST64\Serum_x64.dll)");
gui_ = new arona_gui(ImGui::GetIO(), this);
get_plugin_host_manager().create_instrument_plugin_host(R"(D:\Other\VST\VST64\helm64.dll)");
get_plugin_host_manager().create_instrument_plugin_host(R"(D:\Other\VST\VST64\helm64.dll)");
// get_plugin_host_manager().create_instrument_plugin_host(R"(F:\VST\VST64\Serum_x64.dll)");
// get_plugin_host_manager().create_instrument_plugin_host(R"(F:\VST\VST64\Serum_x64.dll)");

View File

@ -0,0 +1,41 @@
#include "arona_widget.h"
#include "imgui_internal.h"
using namespace ImGui;
void draw_volume_bar(float volume, float peak, const ImVec2& size_arg, const char* label) {
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return;
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
ImVec2 pos = window->DC.CursorPos;
ImVec2 size = CalcItemSize(size_arg, CalcItemWidth(), g.FontSize + style.FramePadding.y * 2.0f);
ImRect bb(pos, pos + size);
ItemSize(size, style.FramePadding.y);
if (!ItemAdd(bb, 0))
return;
window->DrawList->PushClipRect(bb.Min, bb.Max, true);
// Render
volume = ImSaturate(volume);
RenderFrame(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding);
bb.Expand(ImVec2(-style.FrameBorderSize, -style.FrameBorderSize));
const float y = bb.Max.y - ImLerp(0.0f, size.y, volume);
const ImVec2 fill_min = ImVec2(bb.Min.x, y);
const ImVec2 fill_br = ImVec2(bb.Max.x, bb.Max.y);
window->DrawList->AddRectFilled(fill_min, fill_br, GetColorU32(ImGuiCol_PlotHistogram));
// Peak
if (peak > 0.0f) {
const float peak_y = bb.Max.y - ImLerp(0.0f, size.y, peak);
if (peak_y < fill_min.y + 1.0f) // Draw line at the top of the fill
window->DrawList->AddLine(ImVec2(bb.Min.x, peak_y), ImVec2(bb.Max.x, peak_y), GetColorU32(ImGuiCol_Text));
}
window->DrawList->PopClipRect();
}

View File

@ -0,0 +1,5 @@
#pragma once
#include "imgui.h"
void draw_volume_bar(float volume, float peak, const ImVec2& size_arg, const char* label);

View File

@ -1,14 +1,14 @@
#include "widget_mixer.h"
#include "arona_widget.h"
#include "audio/device/audio_device_manager.h"
#include "audio/mixer/mixer.h"
#include "audio/mixer/mixer_track.h"
widget_mixer_track::widget_mixer_track(mixer_track* in_mixer_track): mixer_track_(in_mixer_track) {
name_ = mixer_track_->get_name();
in_mixer_track->on_processed.add_raw(this, &widget_mixer_track::on_processed);
l_buffer.SetCapacity(get_audio_device_manager().get_buffer_size() * 3);
r_buffer.SetCapacity(get_audio_device_manager().get_buffer_size() * 3);
peak_.resize(in_mixer_track->buffer.get_num_channels());
peak_decay_.resize(in_mixer_track->buffer.get_num_channels());
}
widget_mixer_track::~widget_mixer_track() {
@ -21,31 +21,38 @@ void widget_mixer_track::on_paint(ImGuiIO& io) {
{
ImGui::Text(name_.c_str());
ImGui::SliderFloat("dB", &volume_, -60.0f, 3.0f);
ImGui::SameLine();
uint32_t sample_count = get_audio_device_manager().get_sample_rate() * io.DeltaTime * 2; // 2 channels
sample_count = std::min(sample_count, l_buffer.Num());
uint32_t sample_count = get_audio_device_manager().get_sample_rate() * io.DeltaTime;
for (uint32_t i = 0; i < mixer_track_->ui_buffers.size(); i++) {
auto& bu_buffer = mixer_track_->ui_buffers[i];
sample_count = std::min(sample_count, bu_buffer.Num());
float temp_max = 0.f;
for (int i = 0; i < sample_count; ++i) {
float sample = bu_buffer.Pop();
temp_max = std::max(temp_max, std::abs(sample));
}
float l_max = 0.f;
float r_max = 0.f;
for (int i = 0; i < sample_count; ++i) {
float l = l_buffer.Pop();
float r = r_buffer.Pop();
l_max = std::max(l_max, std::abs(l));
r_max = std::max(r_max, std::abs(r));
update_peak(i, temp_max, io.DeltaTime);
draw_volume_bar(temp_max, peak_[i], ImVec2(5.0f, 100.0f), "");
ImGui::SameLine();
}
ImGui::ProgressBar(l_max, ImVec2(0.0f, 100.0f));
ImGui::ProgressBar(r_max, ImVec2(0.0f, 100.0f));
}
ImGui::EndChild();
}
void widget_mixer_track::on_processed(mixer_track* track) {
const auto left = track->buffer.get_headers()[0];
const auto right = track->buffer.get_headers()[1];
l_buffer.Push(left, track->buffer.get_num_samples());
r_buffer.Push(right, track->buffer.get_num_samples());
void widget_mixer_track::update_peak(uint32_t channel, float new_peak, float delta_time) {
float& current_peak = peak_[channel];
float& current_decay = peak_decay_[channel];
if (new_peak > current_peak) {
current_peak = new_peak;
current_decay = 1.f;
}
current_decay -= delta_time;
if (current_decay < 0.0f) {
current_peak -= delta_time * 0.5f;
current_peak = std::max(current_peak, 0.0f);
}
}
widget_mixer::widget_mixer() {

View File

@ -15,12 +15,13 @@ public:
void on_paint(ImGuiIO& io) override;
mixer_track* get_mixer_track() const { return mixer_track_; }
private:
void on_processed(mixer_track* track);
circular_audio_buffer<float> l_buffer;
circular_audio_buffer<float> r_buffer;
void update_peak(uint32_t channel, float new_peak, float delta_time);
mixer_track* mixer_track_;
std::string name_;
float volume_ = 1.0f;
std::vector<float> peak_;
std::vector<float> peak_decay_;
};
class widget_mixer : public widget {

@ -1 +1 @@
Subproject commit e8d341df18690f0733c961c584bbcfeff3dce2b0
Subproject commit 0acf109ef22ed905b48114f4a471961708089160