diff options
Diffstat (limited to 'src/audio_core/hle/mixers.cpp')
| -rw-r--r-- | src/audio_core/hle/mixers.cpp | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/src/audio_core/hle/mixers.cpp b/src/audio_core/hle/mixers.cpp new file mode 100644 index 000000000..18335f7f0 --- /dev/null +++ b/src/audio_core/hle/mixers.cpp | |||
| @@ -0,0 +1,201 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | |||
| 7 | #include "audio_core/hle/common.h" | ||
| 8 | #include "audio_core/hle/dsp.h" | ||
| 9 | #include "audio_core/hle/mixers.h" | ||
| 10 | |||
| 11 | #include "common/assert.h" | ||
| 12 | #include "common/logging/log.h" | ||
| 13 | #include "common/math_util.h" | ||
| 14 | |||
| 15 | namespace DSP { | ||
| 16 | namespace HLE { | ||
| 17 | |||
| 18 | void Mixers::Reset() { | ||
| 19 | current_frame.fill({}); | ||
| 20 | state = {}; | ||
| 21 | } | ||
| 22 | |||
| 23 | DspStatus Mixers::Tick(DspConfiguration& config, | ||
| 24 | const IntermediateMixSamples& read_samples, | ||
| 25 | IntermediateMixSamples& write_samples, | ||
| 26 | const std::array<QuadFrame32, 3>& input) | ||
| 27 | { | ||
| 28 | ParseConfig(config); | ||
| 29 | |||
| 30 | AuxReturn(read_samples); | ||
| 31 | AuxSend(write_samples, input); | ||
| 32 | |||
| 33 | MixCurrentFrame(); | ||
| 34 | |||
| 35 | return GetCurrentStatus(); | ||
| 36 | } | ||
| 37 | |||
| 38 | void Mixers::ParseConfig(DspConfiguration& config) { | ||
| 39 | if (!config.dirty_raw) { | ||
| 40 | return; | ||
| 41 | } | ||
| 42 | |||
| 43 | if (config.mixer1_enabled_dirty) { | ||
| 44 | config.mixer1_enabled_dirty.Assign(0); | ||
| 45 | state.mixer1_enabled = config.mixer1_enabled != 0; | ||
| 46 | LOG_TRACE(Audio_DSP, "mixers mixer1_enabled = %hu", config.mixer1_enabled); | ||
| 47 | } | ||
| 48 | |||
| 49 | if (config.mixer2_enabled_dirty) { | ||
| 50 | config.mixer2_enabled_dirty.Assign(0); | ||
| 51 | state.mixer2_enabled = config.mixer2_enabled != 0; | ||
| 52 | LOG_TRACE(Audio_DSP, "mixers mixer2_enabled = %hu", config.mixer2_enabled); | ||
| 53 | } | ||
| 54 | |||
| 55 | if (config.volume_0_dirty) { | ||
| 56 | config.volume_0_dirty.Assign(0); | ||
| 57 | state.intermediate_mixer_volume[0] = config.volume[0]; | ||
| 58 | LOG_TRACE(Audio_DSP, "mixers volume[0] = %f", config.volume[0]); | ||
| 59 | } | ||
| 60 | |||
| 61 | if (config.volume_1_dirty) { | ||
| 62 | config.volume_1_dirty.Assign(0); | ||
| 63 | state.intermediate_mixer_volume[1] = config.volume[1]; | ||
| 64 | LOG_TRACE(Audio_DSP, "mixers volume[1] = %f", config.volume[1]); | ||
| 65 | } | ||
| 66 | |||
| 67 | if (config.volume_2_dirty) { | ||
| 68 | config.volume_2_dirty.Assign(0); | ||
| 69 | state.intermediate_mixer_volume[2] = config.volume[2]; | ||
| 70 | LOG_TRACE(Audio_DSP, "mixers volume[2] = %f", config.volume[2]); | ||
| 71 | } | ||
| 72 | |||
| 73 | if (config.output_format_dirty) { | ||
| 74 | config.output_format_dirty.Assign(0); | ||
| 75 | state.output_format = config.output_format; | ||
| 76 | LOG_TRACE(Audio_DSP, "mixers output_format = %zu", static_cast<size_t>(config.output_format)); | ||
| 77 | } | ||
| 78 | |||
| 79 | if (config.headphones_connected_dirty) { | ||
| 80 | config.headphones_connected_dirty.Assign(0); | ||
| 81 | // Do nothing. | ||
| 82 | // (Note: Whether headphones are connected does affect coefficients used for surround sound.) | ||
| 83 | LOG_TRACE(Audio_DSP, "mixers headphones_connected=%hu", config.headphones_connected); | ||
| 84 | } | ||
| 85 | |||
| 86 | if (config.dirty_raw) { | ||
| 87 | LOG_DEBUG(Audio_DSP, "mixers remaining_dirty=%x", config.dirty_raw); | ||
| 88 | } | ||
| 89 | |||
| 90 | config.dirty_raw = 0; | ||
| 91 | } | ||
| 92 | |||
| 93 | static s16 ClampToS16(s32 value) { | ||
| 94 | return static_cast<s16>(MathUtil::Clamp(value, -32768, 32767)); | ||
| 95 | } | ||
| 96 | |||
| 97 | static std::array<s16, 2> AddAndClampToS16(const std::array<s16, 2>& a, const std::array<s16, 2>& b) { | ||
| 98 | return { | ||
| 99 | ClampToS16(static_cast<s32>(a[0]) + static_cast<s32>(b[0])), | ||
| 100 | ClampToS16(static_cast<s32>(a[1]) + static_cast<s32>(b[1])) | ||
| 101 | }; | ||
| 102 | } | ||
| 103 | |||
| 104 | void Mixers::DownmixAndMixIntoCurrentFrame(float gain, const QuadFrame32& samples) { | ||
| 105 | // TODO(merry): Limiter. (Currently we're performing final mixing assuming a disabled limiter.) | ||
| 106 | |||
| 107 | switch (state.output_format) { | ||
| 108 | case OutputFormat::Mono: | ||
| 109 | std::transform(current_frame.begin(), current_frame.end(), samples.begin(), current_frame.begin(), | ||
| 110 | [gain](const std::array<s16, 2>& accumulator, const std::array<s32, 4>& sample) -> std::array<s16, 2> { | ||
| 111 | // Downmix to mono | ||
| 112 | s16 mono = ClampToS16(static_cast<s32>((gain * sample[0] + gain * sample[1] + gain * sample[2] + gain * sample[3]) / 2)); | ||
| 113 | // Mix into current frame | ||
| 114 | return AddAndClampToS16(accumulator, { mono, mono }); | ||
| 115 | }); | ||
| 116 | return; | ||
| 117 | |||
| 118 | case OutputFormat::Surround: | ||
| 119 | // TODO(merry): Implement surround sound. | ||
| 120 | // fallthrough | ||
| 121 | |||
| 122 | case OutputFormat::Stereo: | ||
| 123 | std::transform(current_frame.begin(), current_frame.end(), samples.begin(), current_frame.begin(), | ||
| 124 | [gain](const std::array<s16, 2>& accumulator, const std::array<s32, 4>& sample) -> std::array<s16, 2> { | ||
| 125 | // Downmix to stereo | ||
| 126 | s16 left = ClampToS16(static_cast<s32>(gain * sample[0] + gain * sample[2])); | ||
| 127 | s16 right = ClampToS16(static_cast<s32>(gain * sample[1] + gain * sample[3])); | ||
| 128 | // Mix into current frame | ||
| 129 | return AddAndClampToS16(accumulator, { left, right }); | ||
| 130 | }); | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | UNREACHABLE_MSG("Invalid output_format %zu", static_cast<size_t>(state.output_format)); | ||
| 135 | } | ||
| 136 | |||
| 137 | void Mixers::AuxReturn(const IntermediateMixSamples& read_samples) { | ||
| 138 | // NOTE: read_samples.mix{1,2}.pcm32 annoyingly have their dimensions in reverse order to QuadFrame32. | ||
| 139 | |||
| 140 | if (state.mixer1_enabled) { | ||
| 141 | for (size_t sample = 0; sample < samples_per_frame; sample++) { | ||
| 142 | for (size_t channel = 0; channel < 4; channel++) { | ||
| 143 | state.intermediate_mix_buffer[1][sample][channel] = read_samples.mix1.pcm32[channel][sample]; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | if (state.mixer2_enabled) { | ||
| 149 | for (size_t sample = 0; sample < samples_per_frame; sample++) { | ||
| 150 | for (size_t channel = 0; channel < 4; channel++) { | ||
| 151 | state.intermediate_mix_buffer[2][sample][channel] = read_samples.mix2.pcm32[channel][sample]; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | void Mixers::AuxSend(IntermediateMixSamples& write_samples, const std::array<QuadFrame32, 3>& input) { | ||
| 158 | // NOTE: read_samples.mix{1,2}.pcm32 annoyingly have their dimensions in reverse order to QuadFrame32. | ||
| 159 | |||
| 160 | state.intermediate_mix_buffer[0] = input[0]; | ||
| 161 | |||
| 162 | if (state.mixer1_enabled) { | ||
| 163 | for (size_t sample = 0; sample < samples_per_frame; sample++) { | ||
| 164 | for (size_t channel = 0; channel < 4; channel++) { | ||
| 165 | write_samples.mix1.pcm32[channel][sample] = input[1][sample][channel]; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } else { | ||
| 169 | state.intermediate_mix_buffer[1] = input[1]; | ||
| 170 | } | ||
| 171 | |||
| 172 | if (state.mixer2_enabled) { | ||
| 173 | for (size_t sample = 0; sample < samples_per_frame; sample++) { | ||
| 174 | for (size_t channel = 0; channel < 4; channel++) { | ||
| 175 | write_samples.mix2.pcm32[channel][sample] = input[2][sample][channel]; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | } else { | ||
| 179 | state.intermediate_mix_buffer[2] = input[2]; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | void Mixers::MixCurrentFrame() { | ||
| 184 | current_frame.fill({}); | ||
| 185 | |||
| 186 | for (size_t mix = 0; mix < 3; mix++) { | ||
| 187 | DownmixAndMixIntoCurrentFrame(state.intermediate_mixer_volume[mix], state.intermediate_mix_buffer[mix]); | ||
| 188 | } | ||
| 189 | |||
| 190 | // TODO(merry): Compressor. (We currently assume a disabled compressor.) | ||
| 191 | } | ||
| 192 | |||
| 193 | DspStatus Mixers::GetCurrentStatus() const { | ||
| 194 | DspStatus status; | ||
| 195 | status.unknown = 0; | ||
| 196 | status.dropped_frames = 0; | ||
| 197 | return status; | ||
| 198 | } | ||
| 199 | |||
| 200 | } // namespace HLE | ||
| 201 | } // namespace DSP | ||