Files
AronaCore/core/audio/mixer/mixer_track.cpp
Nanako e65c44899a 1. 优化内存池
2. 延迟补偿功能完成
3. 优化内存布局
2024-07-12 07:55:12 +08:00

94 lines
3.1 KiB
C++

#include "mixer_track.h"
#include "channel_interface.h"
#include "audio/device/audio_device_manager.h"
#include "audio/plugin_host/plugin_host.h"
#include "thread_message/thread_message_hubs.h"
mixer_track::~mixer_track() {
delete channel_interface_;
channel_interface_ = nullptr;
for (auto e : effects_) {
// e->on_latency_changed.remove_all(this);
delete_effect(this, e);
}
}
void mixer_track::init() {
const uint32_t channel_count = g_audio_device_manager.get_output_channel_count();
uint32_t block_size = g_audio_device_manager.get_buffer_size();
buffer_.resize(channel_count, block_size);
ui_buffers_.resize(channel_count);
for (auto& buffer : ui_buffers_) {
buffer.set_capacity(block_size * 2);
}
channel_interface_ = new mixer_channel_interface(this);
}
void mixer_track::add_effect(plugin_host* in_effect) {
g_audio_thread_hub.push_message([in_effect, this] {
in_effect->owner_tracks.push_back(this);
in_effect->channel->set_input_channel(channel_interface_->output_channel_nodes);
in_effect->channel->set_output_channel(channel_interface_->output_channel_nodes);
effects_.push_back(in_effect);
});
}
void mixer_track::remove_effect(plugin_host* in_effect) {
g_audio_thread_hub.push_message([in_effect, this] {
auto remove_effect_track = std::remove(in_effect->owner_tracks.begin(), in_effect->owner_tracks.end(), this);
in_effect->owner_tracks.erase(remove_effect_track, in_effect->owner_tracks.end());
auto remove_effect = std::remove(effects_.begin(), effects_.end(), in_effect);
effects_.erase(remove_effect, effects_.end());
});
}
void mixer_track::process(uint32_t in_frames) {
for (auto effect : effects_) effect->process(in_frames);
buffer_.multiple(get_volume());
}
auto mixer_track::get_latency() const -> int32_t {
int32_t latency = 0;
for (const auto effect : effects_) {
latency += effect->get_latency();
}
return latency;
}
void mixer_track::set_ui_buffer_latency(int32_t in_latency) {
const uint32_t block_size = g_audio_device_manager.get_buffer_size();
const int32_t block_num = std::ceil((float)in_latency / block_size) + 1;
for (auto& buffer : ui_buffers_) {
buffer.set_capacity(block_num * block_size);
buffer.push_zeros(in_latency);
}
}
instrument_track::instrument_track(plugin_host* in_instrument): mixer_track(mixer_track_type::instrument)
, instrument_(in_instrument) {}
void instrument_track::rename(const std::string& in_name) {
instrument_->name = in_name;
}
auto instrument_track::get_name() const -> std::string {
return instrument_->name;
}
auto instrument_track::get_latency() const -> int32_t {
return mixer_track::get_latency() + instrument_->get_latency() + instrument_->get_user_latency();
}
void delete_effect(mixer_track* track, plugin_host* host) {
track->remove_effect(host);
host->try_close_editor();
g_audio_thread_hub.push_message([host, track]() {
delete host;
});
}