#include "stdafx.h" #include "AudioBackend.h" #include "Emu/system_config.h" AudioBackend::AudioBackend() {} void AudioBackend::SetErrorCallback(std::function cb) { std::lock_guard lock(m_error_cb_mutex); m_error_callback = cb; } /* * Helper methods */ u32 AudioBackend::get_sampling_rate() const { return static_cast>(m_sampling_rate); } u32 AudioBackend::get_sample_size() const { return static_cast>(m_sample_size); } u32 AudioBackend::get_channels() const { return static_cast>(m_channels); } bool AudioBackend::get_convert_to_s16() const { return m_sample_size == AudioSampleSize::S16; } void AudioBackend::convert_to_s16(u32 cnt, const f32* src, void* dst) { for (u32 i = 0; i < cnt; i++) { static_cast(dst)[i] = static_cast(std::clamp(src[i] * 32768.5f, -32768.0f, 32767.0f)); } } f32 AudioBackend::apply_volume(const VolumeParam& param, u32 sample_cnt, const f32* src, f32* dst) { ensure(param.ch_cnt > 1 && param.ch_cnt % 2 == 0); // Tends to produce faster code const f32 vol_incr = (param.target_volume - param.initial_volume) / (VOLUME_CHANGE_DURATION * param.freq); f32 crnt_vol = param.current_volume; u32 sample_idx = 0; if (vol_incr >= 0) { for (sample_idx = 0; sample_idx < sample_cnt && crnt_vol != param.target_volume; sample_idx += param.ch_cnt) { crnt_vol = std::min(param.current_volume + (sample_idx + 1) / param.ch_cnt * vol_incr, param.target_volume); for (u32 i = 0; i < param.ch_cnt; i++) { dst[sample_idx + i] = src[sample_idx + i] * crnt_vol; } } } else { for (sample_idx = 0; sample_idx < sample_cnt && crnt_vol != param.target_volume; sample_idx += param.ch_cnt) { crnt_vol = std::max(param.current_volume + (sample_idx + 1) / param.ch_cnt * vol_incr, param.target_volume); for (u32 i = 0; i < param.ch_cnt; i++) { dst[sample_idx + i] = src[sample_idx + i] * crnt_vol; } } } if (sample_cnt > sample_idx) { apply_volume_static(param.target_volume, sample_cnt - sample_idx, &src[sample_idx], &dst[sample_idx]); } return crnt_vol; } void AudioBackend::apply_volume_static(f32 vol, u32 sample_cnt, const f32* src, f32* dst) { for (u32 i = 0; i < sample_cnt; i++) { dst[i] = src[i] * vol; } } void AudioBackend::normalize(u32 sample_cnt, const f32* src, f32* dst) { for (u32 i = 0; i < sample_cnt; i++) { dst[i] = std::clamp(src[i], -1.0f, 1.0f); } }