diff options
Diffstat (limited to 'src/audio_core')
26 files changed, 0 insertions, 3017 deletions
diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt deleted file mode 100644 index 0ad86bb7a..000000000 --- a/src/audio_core/CMakeLists.txt +++ /dev/null | |||
| @@ -1,44 +0,0 @@ | |||
| 1 | set(SRCS | ||
| 2 | audio_core.cpp | ||
| 3 | codec.cpp | ||
| 4 | hle/dsp.cpp | ||
| 5 | hle/filter.cpp | ||
| 6 | hle/mixers.cpp | ||
| 7 | hle/pipe.cpp | ||
| 8 | hle/source.cpp | ||
| 9 | interpolate.cpp | ||
| 10 | sink_details.cpp | ||
| 11 | time_stretch.cpp | ||
| 12 | ) | ||
| 13 | |||
| 14 | set(HEADERS | ||
| 15 | audio_core.h | ||
| 16 | codec.h | ||
| 17 | hle/common.h | ||
| 18 | hle/dsp.h | ||
| 19 | hle/filter.h | ||
| 20 | hle/mixers.h | ||
| 21 | hle/pipe.h | ||
| 22 | hle/source.h | ||
| 23 | interpolate.h | ||
| 24 | null_sink.h | ||
| 25 | sink.h | ||
| 26 | sink_details.h | ||
| 27 | time_stretch.h | ||
| 28 | ) | ||
| 29 | |||
| 30 | if(SDL2_FOUND) | ||
| 31 | set(SRCS ${SRCS} sdl2_sink.cpp) | ||
| 32 | set(HEADERS ${HEADERS} sdl2_sink.h) | ||
| 33 | endif() | ||
| 34 | |||
| 35 | create_directory_groups(${SRCS} ${HEADERS}) | ||
| 36 | |||
| 37 | add_library(audio_core STATIC ${SRCS} ${HEADERS}) | ||
| 38 | target_link_libraries(audio_core PUBLIC common core) | ||
| 39 | target_link_libraries(audio_core PRIVATE SoundTouch) | ||
| 40 | |||
| 41 | if(SDL2_FOUND) | ||
| 42 | target_link_libraries(audio_core PRIVATE SDL2) | ||
| 43 | target_compile_definitions(audio_core PRIVATE HAVE_SDL2) | ||
| 44 | endif() | ||
diff --git a/src/audio_core/audio_core.cpp b/src/audio_core/audio_core.cpp deleted file mode 100644 index ae2b68f9c..000000000 --- a/src/audio_core/audio_core.cpp +++ /dev/null | |||
| @@ -1,61 +0,0 @@ | |||
| 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 <array> | ||
| 6 | #include <memory> | ||
| 7 | #include <string> | ||
| 8 | #include "audio_core/audio_core.h" | ||
| 9 | #include "audio_core/hle/dsp.h" | ||
| 10 | #include "audio_core/hle/pipe.h" | ||
| 11 | #include "audio_core/null_sink.h" | ||
| 12 | #include "audio_core/sink.h" | ||
| 13 | #include "audio_core/sink_details.h" | ||
| 14 | #include "common/common_types.h" | ||
| 15 | #include "core/core_timing.h" | ||
| 16 | #include "core/hle/service/dsp_dsp.h" | ||
| 17 | |||
| 18 | namespace AudioCore { | ||
| 19 | |||
| 20 | // Audio Ticks occur about every 5 miliseconds. | ||
| 21 | static CoreTiming::EventType* tick_event; ///< CoreTiming event | ||
| 22 | static constexpr u64 audio_frame_ticks = 1310252ull; ///< Units: ARM11 cycles | ||
| 23 | |||
| 24 | static void AudioTickCallback(u64 /*userdata*/, int cycles_late) { | ||
| 25 | if (DSP::HLE::Tick()) { | ||
| 26 | // TODO(merry): Signal all the other interrupts as appropriate. | ||
| 27 | Service::DSP_DSP::SignalPipeInterrupt(DSP::HLE::DspPipe::Audio); | ||
| 28 | // HACK(merry): Added to prevent regressions. Will remove soon. | ||
| 29 | Service::DSP_DSP::SignalPipeInterrupt(DSP::HLE::DspPipe::Binary); | ||
| 30 | } | ||
| 31 | |||
| 32 | // Reschedule recurrent event | ||
| 33 | CoreTiming::ScheduleEvent(audio_frame_ticks - cycles_late, tick_event); | ||
| 34 | } | ||
| 35 | |||
| 36 | void Init() { | ||
| 37 | DSP::HLE::Init(); | ||
| 38 | |||
| 39 | tick_event = CoreTiming::RegisterEvent("AudioCore::tick_event", AudioTickCallback); | ||
| 40 | CoreTiming::ScheduleEvent(audio_frame_ticks, tick_event); | ||
| 41 | } | ||
| 42 | |||
| 43 | std::array<u8, Memory::DSP_RAM_SIZE>& GetDspMemory() { | ||
| 44 | return DSP::HLE::g_dsp_memory.raw_memory; | ||
| 45 | } | ||
| 46 | |||
| 47 | void SelectSink(std::string sink_id) { | ||
| 48 | const SinkDetails& sink_details = GetSinkDetails(sink_id); | ||
| 49 | DSP::HLE::SetSink(sink_details.factory()); | ||
| 50 | } | ||
| 51 | |||
| 52 | void EnableStretching(bool enable) { | ||
| 53 | DSP::HLE::EnableStretching(enable); | ||
| 54 | } | ||
| 55 | |||
| 56 | void Shutdown() { | ||
| 57 | CoreTiming::UnscheduleEvent(tick_event, 0); | ||
| 58 | DSP::HLE::Shutdown(); | ||
| 59 | } | ||
| 60 | |||
| 61 | } // namespace AudioCore | ||
diff --git a/src/audio_core/audio_core.h b/src/audio_core/audio_core.h deleted file mode 100644 index ab323ce1f..000000000 --- a/src/audio_core/audio_core.h +++ /dev/null | |||
| @@ -1,31 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <string> | ||
| 9 | #include "common/common_types.h" | ||
| 10 | #include "core/memory.h" | ||
| 11 | |||
| 12 | namespace AudioCore { | ||
| 13 | |||
| 14 | constexpr int native_sample_rate = 32728; ///< 32kHz | ||
| 15 | |||
| 16 | /// Initialise Audio Core | ||
| 17 | void Init(); | ||
| 18 | |||
| 19 | /// Returns a reference to the array backing DSP memory | ||
| 20 | std::array<u8, Memory::DSP_RAM_SIZE>& GetDspMemory(); | ||
| 21 | |||
| 22 | /// Select the sink to use based on sink id. | ||
| 23 | void SelectSink(std::string sink_id); | ||
| 24 | |||
| 25 | /// Enable/Disable stretching. | ||
| 26 | void EnableStretching(bool enable); | ||
| 27 | |||
| 28 | /// Shutdown Audio Core | ||
| 29 | void Shutdown(); | ||
| 30 | |||
| 31 | } // namespace AudioCore | ||
diff --git a/src/audio_core/codec.cpp b/src/audio_core/codec.cpp deleted file mode 100644 index 6fba9fdae..000000000 --- a/src/audio_core/codec.cpp +++ /dev/null | |||
| @@ -1,127 +0,0 @@ | |||
| 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 <array> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstring> | ||
| 8 | #include <vector> | ||
| 9 | #include "audio_core/codec.h" | ||
| 10 | #include "common/assert.h" | ||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "common/math_util.h" | ||
| 13 | |||
| 14 | namespace Codec { | ||
| 15 | |||
| 16 | StereoBuffer16 DecodeADPCM(const u8* const data, const size_t sample_count, | ||
| 17 | const std::array<s16, 16>& adpcm_coeff, ADPCMState& state) { | ||
| 18 | // GC-ADPCM with scale factor and variable coefficients. | ||
| 19 | // Frames are 8 bytes long containing 14 samples each. | ||
| 20 | // Samples are 4 bits (one nibble) long. | ||
| 21 | |||
| 22 | constexpr size_t FRAME_LEN = 8; | ||
| 23 | constexpr size_t SAMPLES_PER_FRAME = 14; | ||
| 24 | constexpr std::array<int, 16> SIGNED_NIBBLES = { | ||
| 25 | {0, 1, 2, 3, 4, 5, 6, 7, -8, -7, -6, -5, -4, -3, -2, -1}}; | ||
| 26 | |||
| 27 | const size_t ret_size = | ||
| 28 | sample_count % 2 == 0 ? sample_count : sample_count + 1; // Ensure multiple of two. | ||
| 29 | StereoBuffer16 ret(ret_size); | ||
| 30 | |||
| 31 | int yn1 = state.yn1, yn2 = state.yn2; | ||
| 32 | |||
| 33 | const size_t NUM_FRAMES = | ||
| 34 | (sample_count + (SAMPLES_PER_FRAME - 1)) / SAMPLES_PER_FRAME; // Round up. | ||
| 35 | for (size_t framei = 0; framei < NUM_FRAMES; framei++) { | ||
| 36 | const int frame_header = data[framei * FRAME_LEN]; | ||
| 37 | const int scale = 1 << (frame_header & 0xF); | ||
| 38 | const int idx = (frame_header >> 4) & 0x7; | ||
| 39 | |||
| 40 | // Coefficients are fixed point with 11 bits fractional part. | ||
| 41 | const int coef1 = adpcm_coeff[idx * 2 + 0]; | ||
| 42 | const int coef2 = adpcm_coeff[idx * 2 + 1]; | ||
| 43 | |||
| 44 | // Decodes an audio sample. One nibble produces one sample. | ||
| 45 | const auto decode_sample = [&](const int nibble) -> s16 { | ||
| 46 | const int xn = nibble * scale; | ||
| 47 | // We first transform everything into 11 bit fixed point, perform the second order | ||
| 48 | // digital filter, then transform back. | ||
| 49 | // 0x400 == 0.5 in 11 bit fixed point. | ||
| 50 | // Filter: y[n] = x[n] + 0.5 + c1 * y[n-1] + c2 * y[n-2] | ||
| 51 | int val = ((xn << 11) + 0x400 + coef1 * yn1 + coef2 * yn2) >> 11; | ||
| 52 | // Clamp to output range. | ||
| 53 | val = MathUtil::Clamp(val, -32768, 32767); | ||
| 54 | // Advance output feedback. | ||
| 55 | yn2 = yn1; | ||
| 56 | yn1 = val; | ||
| 57 | return (s16)val; | ||
| 58 | }; | ||
| 59 | |||
| 60 | size_t outputi = framei * SAMPLES_PER_FRAME; | ||
| 61 | size_t datai = framei * FRAME_LEN + 1; | ||
| 62 | for (size_t i = 0; i < SAMPLES_PER_FRAME && outputi < sample_count; i += 2) { | ||
| 63 | const s16 sample1 = decode_sample(SIGNED_NIBBLES[data[datai] >> 4]); | ||
| 64 | ret[outputi].fill(sample1); | ||
| 65 | outputi++; | ||
| 66 | |||
| 67 | const s16 sample2 = decode_sample(SIGNED_NIBBLES[data[datai] & 0xF]); | ||
| 68 | ret[outputi].fill(sample2); | ||
| 69 | outputi++; | ||
| 70 | |||
| 71 | datai++; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | state.yn1 = yn1; | ||
| 76 | state.yn2 = yn2; | ||
| 77 | |||
| 78 | return ret; | ||
| 79 | } | ||
| 80 | |||
| 81 | static s16 SignExtendS8(u8 x) { | ||
| 82 | // The data is actually signed PCM8. | ||
| 83 | // We sign extend this to signed PCM16. | ||
| 84 | return static_cast<s16>(static_cast<s8>(x)); | ||
| 85 | } | ||
| 86 | |||
| 87 | StereoBuffer16 DecodePCM8(const unsigned num_channels, const u8* const data, | ||
| 88 | const size_t sample_count) { | ||
| 89 | ASSERT(num_channels == 1 || num_channels == 2); | ||
| 90 | |||
| 91 | StereoBuffer16 ret(sample_count); | ||
| 92 | |||
| 93 | if (num_channels == 1) { | ||
| 94 | for (size_t i = 0; i < sample_count; i++) { | ||
| 95 | ret[i].fill(SignExtendS8(data[i])); | ||
| 96 | } | ||
| 97 | } else { | ||
| 98 | for (size_t i = 0; i < sample_count; i++) { | ||
| 99 | ret[i][0] = SignExtendS8(data[i * 2 + 0]); | ||
| 100 | ret[i][1] = SignExtendS8(data[i * 2 + 1]); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | return ret; | ||
| 105 | } | ||
| 106 | |||
| 107 | StereoBuffer16 DecodePCM16(const unsigned num_channels, const u8* const data, | ||
| 108 | const size_t sample_count) { | ||
| 109 | ASSERT(num_channels == 1 || num_channels == 2); | ||
| 110 | |||
| 111 | StereoBuffer16 ret(sample_count); | ||
| 112 | |||
| 113 | if (num_channels == 1) { | ||
| 114 | for (size_t i = 0; i < sample_count; i++) { | ||
| 115 | s16 sample; | ||
| 116 | std::memcpy(&sample, data + i * sizeof(s16), sizeof(s16)); | ||
| 117 | ret[i].fill(sample); | ||
| 118 | } | ||
| 119 | } else { | ||
| 120 | for (size_t i = 0; i < sample_count; ++i) { | ||
| 121 | std::memcpy(&ret[i], data + i * sizeof(s16) * 2, 2 * sizeof(s16)); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | return ret; | ||
| 126 | } | ||
| 127 | }; | ||
diff --git a/src/audio_core/codec.h b/src/audio_core/codec.h deleted file mode 100644 index 877b2202d..000000000 --- a/src/audio_core/codec.h +++ /dev/null | |||
| @@ -1,51 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <deque> | ||
| 9 | #include "common/common_types.h" | ||
| 10 | |||
| 11 | namespace Codec { | ||
| 12 | |||
| 13 | /// A variable length buffer of signed PCM16 stereo samples. | ||
| 14 | using StereoBuffer16 = std::deque<std::array<s16, 2>>; | ||
| 15 | |||
| 16 | /// See: Codec::DecodeADPCM | ||
| 17 | struct ADPCMState { | ||
| 18 | // Two historical samples from previous processed buffer, | ||
| 19 | // required for ADPCM decoding | ||
| 20 | s16 yn1; ///< y[n-1] | ||
| 21 | s16 yn2; ///< y[n-2] | ||
| 22 | }; | ||
| 23 | |||
| 24 | /** | ||
| 25 | * @param data Pointer to buffer that contains ADPCM data to decode | ||
| 26 | * @param sample_count Length of buffer in terms of number of samples | ||
| 27 | * @param adpcm_coeff ADPCM coefficients | ||
| 28 | * @param state ADPCM state, this is updated with new state | ||
| 29 | * @return Decoded stereo signed PCM16 data, sample_count in length | ||
| 30 | */ | ||
| 31 | StereoBuffer16 DecodeADPCM(const u8* const data, const size_t sample_count, | ||
| 32 | const std::array<s16, 16>& adpcm_coeff, ADPCMState& state); | ||
| 33 | |||
| 34 | /** | ||
| 35 | * @param num_channels Number of channels | ||
| 36 | * @param data Pointer to buffer that contains PCM8 data to decode | ||
| 37 | * @param sample_count Length of buffer in terms of number of samples | ||
| 38 | * @return Decoded stereo signed PCM16 data, sample_count in length | ||
| 39 | */ | ||
| 40 | StereoBuffer16 DecodePCM8(const unsigned num_channels, const u8* const data, | ||
| 41 | const size_t sample_count); | ||
| 42 | |||
| 43 | /** | ||
| 44 | * @param num_channels Number of channels | ||
| 45 | * @param data Pointer to buffer that contains PCM16 data to decode | ||
| 46 | * @param sample_count Length of buffer in terms of number of samples | ||
| 47 | * @return Decoded stereo signed PCM16 data, sample_count in length | ||
| 48 | */ | ||
| 49 | StereoBuffer16 DecodePCM16(const unsigned num_channels, const u8* const data, | ||
| 50 | const size_t sample_count); | ||
| 51 | }; | ||
diff --git a/src/audio_core/hle/common.h b/src/audio_core/hle/common.h deleted file mode 100644 index 7fbc3ad9a..000000000 --- a/src/audio_core/hle/common.h +++ /dev/null | |||
| @@ -1,34 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <array> | ||
| 9 | #include "common/common_types.h" | ||
| 10 | |||
| 11 | namespace DSP { | ||
| 12 | namespace HLE { | ||
| 13 | |||
| 14 | constexpr int num_sources = 24; | ||
| 15 | constexpr int samples_per_frame = 160; ///< Samples per audio frame at native sample rate | ||
| 16 | |||
| 17 | /// The final output to the speakers is stereo. Preprocessing output in Source is also stereo. | ||
| 18 | using StereoFrame16 = std::array<std::array<s16, 2>, samples_per_frame>; | ||
| 19 | |||
| 20 | /// The DSP is quadraphonic internally. | ||
| 21 | using QuadFrame32 = std::array<std::array<s32, 4>, samples_per_frame>; | ||
| 22 | |||
| 23 | /** | ||
| 24 | * This performs the filter operation defined by FilterT::ProcessSample on the frame in-place. | ||
| 25 | * FilterT::ProcessSample is called sequentially on the samples. | ||
| 26 | */ | ||
| 27 | template <typename FrameT, typename FilterT> | ||
| 28 | void FilterFrame(FrameT& frame, FilterT& filter) { | ||
| 29 | std::transform(frame.begin(), frame.end(), frame.begin(), | ||
| 30 | [&filter](const auto& sample) { return filter.ProcessSample(sample); }); | ||
| 31 | } | ||
| 32 | |||
| 33 | } // namespace HLE | ||
| 34 | } // namespace DSP | ||
diff --git a/src/audio_core/hle/dsp.cpp b/src/audio_core/hle/dsp.cpp deleted file mode 100644 index 260b182ed..000000000 --- a/src/audio_core/hle/dsp.cpp +++ /dev/null | |||
| @@ -1,172 +0,0 @@ | |||
| 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 <array> | ||
| 6 | #include <memory> | ||
| 7 | #include "audio_core/hle/dsp.h" | ||
| 8 | #include "audio_core/hle/mixers.h" | ||
| 9 | #include "audio_core/hle/pipe.h" | ||
| 10 | #include "audio_core/hle/source.h" | ||
| 11 | #include "audio_core/sink.h" | ||
| 12 | #include "audio_core/time_stretch.h" | ||
| 13 | |||
| 14 | namespace DSP { | ||
| 15 | namespace HLE { | ||
| 16 | |||
| 17 | // Region management | ||
| 18 | |||
| 19 | DspMemory g_dsp_memory; | ||
| 20 | |||
| 21 | static size_t CurrentRegionIndex() { | ||
| 22 | // The region with the higher frame counter is chosen unless there is wraparound. | ||
| 23 | // This function only returns a 0 or 1. | ||
| 24 | u16 frame_counter_0 = g_dsp_memory.region_0.frame_counter; | ||
| 25 | u16 frame_counter_1 = g_dsp_memory.region_1.frame_counter; | ||
| 26 | |||
| 27 | if (frame_counter_0 == 0xFFFFu && frame_counter_1 != 0xFFFEu) { | ||
| 28 | // Wraparound has occurred. | ||
| 29 | return 1; | ||
| 30 | } | ||
| 31 | |||
| 32 | if (frame_counter_1 == 0xFFFFu && frame_counter_0 != 0xFFFEu) { | ||
| 33 | // Wraparound has occurred. | ||
| 34 | return 0; | ||
| 35 | } | ||
| 36 | |||
| 37 | return (frame_counter_0 > frame_counter_1) ? 0 : 1; | ||
| 38 | } | ||
| 39 | |||
| 40 | static SharedMemory& ReadRegion() { | ||
| 41 | return CurrentRegionIndex() == 0 ? g_dsp_memory.region_0 : g_dsp_memory.region_1; | ||
| 42 | } | ||
| 43 | |||
| 44 | static SharedMemory& WriteRegion() { | ||
| 45 | return CurrentRegionIndex() != 0 ? g_dsp_memory.region_0 : g_dsp_memory.region_1; | ||
| 46 | } | ||
| 47 | |||
| 48 | // Audio processing and mixing | ||
| 49 | |||
| 50 | static std::array<Source, num_sources> sources = { | ||
| 51 | Source(0), Source(1), Source(2), Source(3), Source(4), Source(5), Source(6), Source(7), | ||
| 52 | Source(8), Source(9), Source(10), Source(11), Source(12), Source(13), Source(14), Source(15), | ||
| 53 | Source(16), Source(17), Source(18), Source(19), Source(20), Source(21), Source(22), Source(23), | ||
| 54 | }; | ||
| 55 | static Mixers mixers; | ||
| 56 | |||
| 57 | static StereoFrame16 GenerateCurrentFrame() { | ||
| 58 | SharedMemory& read = ReadRegion(); | ||
| 59 | SharedMemory& write = WriteRegion(); | ||
| 60 | |||
| 61 | std::array<QuadFrame32, 3> intermediate_mixes = {}; | ||
| 62 | |||
| 63 | // Generate intermediate mixes | ||
| 64 | for (size_t i = 0; i < num_sources; i++) { | ||
| 65 | write.source_statuses.status[i] = | ||
| 66 | sources[i].Tick(read.source_configurations.config[i], read.adpcm_coefficients.coeff[i]); | ||
| 67 | for (size_t mix = 0; mix < 3; mix++) { | ||
| 68 | sources[i].MixInto(intermediate_mixes[mix], mix); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | // Generate final mix | ||
| 73 | write.dsp_status = mixers.Tick(read.dsp_configuration, read.intermediate_mix_samples, | ||
| 74 | write.intermediate_mix_samples, intermediate_mixes); | ||
| 75 | |||
| 76 | StereoFrame16 output_frame = mixers.GetOutput(); | ||
| 77 | |||
| 78 | // Write current output frame to the shared memory region | ||
| 79 | for (size_t samplei = 0; samplei < output_frame.size(); samplei++) { | ||
| 80 | for (size_t channeli = 0; channeli < output_frame[0].size(); channeli++) { | ||
| 81 | write.final_samples.pcm16[samplei][channeli] = s16_le(output_frame[samplei][channeli]); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | return output_frame; | ||
| 86 | } | ||
| 87 | |||
| 88 | // Audio output | ||
| 89 | |||
| 90 | static bool perform_time_stretching = true; | ||
| 91 | static std::unique_ptr<AudioCore::Sink> sink; | ||
| 92 | static AudioCore::TimeStretcher time_stretcher; | ||
| 93 | |||
| 94 | static void FlushResidualStretcherAudio() { | ||
| 95 | time_stretcher.Flush(); | ||
| 96 | while (true) { | ||
| 97 | std::vector<s16> residual_audio = time_stretcher.Process(sink->SamplesInQueue()); | ||
| 98 | if (residual_audio.empty()) | ||
| 99 | break; | ||
| 100 | sink->EnqueueSamples(residual_audio.data(), residual_audio.size() / 2); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | static void OutputCurrentFrame(const StereoFrame16& frame) { | ||
| 105 | if (perform_time_stretching) { | ||
| 106 | time_stretcher.AddSamples(&frame[0][0], frame.size()); | ||
| 107 | std::vector<s16> stretched_samples = time_stretcher.Process(sink->SamplesInQueue()); | ||
| 108 | sink->EnqueueSamples(stretched_samples.data(), stretched_samples.size() / 2); | ||
| 109 | } else { | ||
| 110 | constexpr size_t maximum_sample_latency = 2048; // about 64 miliseconds | ||
| 111 | if (sink->SamplesInQueue() > maximum_sample_latency) { | ||
| 112 | // This can occur if we're running too fast and samples are starting to back up. | ||
| 113 | // Just drop the samples. | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | sink->EnqueueSamples(&frame[0][0], frame.size()); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | void EnableStretching(bool enable) { | ||
| 122 | if (perform_time_stretching == enable) | ||
| 123 | return; | ||
| 124 | |||
| 125 | if (!enable) { | ||
| 126 | FlushResidualStretcherAudio(); | ||
| 127 | } | ||
| 128 | perform_time_stretching = enable; | ||
| 129 | } | ||
| 130 | |||
| 131 | // Public Interface | ||
| 132 | |||
| 133 | void Init() { | ||
| 134 | DSP::HLE::ResetPipes(); | ||
| 135 | |||
| 136 | for (auto& source : sources) { | ||
| 137 | source.Reset(); | ||
| 138 | } | ||
| 139 | |||
| 140 | mixers.Reset(); | ||
| 141 | |||
| 142 | time_stretcher.Reset(); | ||
| 143 | if (sink) { | ||
| 144 | time_stretcher.SetOutputSampleRate(sink->GetNativeSampleRate()); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | void Shutdown() { | ||
| 149 | if (perform_time_stretching) { | ||
| 150 | FlushResidualStretcherAudio(); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | bool Tick() { | ||
| 155 | StereoFrame16 current_frame = {}; | ||
| 156 | |||
| 157 | // TODO: Check dsp::DSP semaphore (which indicates emulated application has finished writing to | ||
| 158 | // shared memory region) | ||
| 159 | current_frame = GenerateCurrentFrame(); | ||
| 160 | |||
| 161 | OutputCurrentFrame(current_frame); | ||
| 162 | |||
| 163 | return true; | ||
| 164 | } | ||
| 165 | |||
| 166 | void SetSink(std::unique_ptr<AudioCore::Sink> sink_) { | ||
| 167 | sink = std::move(sink_); | ||
| 168 | time_stretcher.SetOutputSampleRate(sink->GetNativeSampleRate()); | ||
| 169 | } | ||
| 170 | |||
| 171 | } // namespace HLE | ||
| 172 | } // namespace DSP | ||
diff --git a/src/audio_core/hle/dsp.h b/src/audio_core/hle/dsp.h deleted file mode 100644 index 94ce48863..000000000 --- a/src/audio_core/hle/dsp.h +++ /dev/null | |||
| @@ -1,595 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <cstddef> | ||
| 9 | #include <memory> | ||
| 10 | #include <type_traits> | ||
| 11 | #include "audio_core/hle/common.h" | ||
| 12 | #include "common/bit_field.h" | ||
| 13 | #include "common/common_funcs.h" | ||
| 14 | #include "common/common_types.h" | ||
| 15 | #include "common/swap.h" | ||
| 16 | |||
| 17 | namespace AudioCore { | ||
| 18 | class Sink; | ||
| 19 | } | ||
| 20 | |||
| 21 | namespace DSP { | ||
| 22 | namespace HLE { | ||
| 23 | |||
| 24 | // The application-accessible region of DSP memory consists of two parts. Both are marked as IO and | ||
| 25 | // have Read/Write permissions. | ||
| 26 | // | ||
| 27 | // First Region: 0x1FF50000 (Size: 0x8000) | ||
| 28 | // Second Region: 0x1FF70000 (Size: 0x8000) | ||
| 29 | // | ||
| 30 | // The DSP reads from each region alternately based on the frame counter for each region much like a | ||
| 31 | // double-buffer. The frame counter is located as the very last u16 of each region and is | ||
| 32 | // incremented each audio tick. | ||
| 33 | |||
| 34 | constexpr u32 region0_offset = 0x50000; | ||
| 35 | constexpr u32 region1_offset = 0x70000; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * The DSP is native 16-bit. The DSP also appears to be big-endian. When reading 32-bit numbers from | ||
| 39 | * its memory regions, the higher and lower 16-bit halves are swapped compared to the little-endian | ||
| 40 | * layout of the ARM11. Hence from the ARM11's point of view the memory space appears to be | ||
| 41 | * middle-endian. | ||
| 42 | * | ||
| 43 | * Unusually this does not appear to be an issue for floating point numbers. The DSP makes the more | ||
| 44 | * sensible choice of keeping that little-endian. There are also some exceptions such as the | ||
| 45 | * IntermediateMixSamples structure, which is little-endian. | ||
| 46 | * | ||
| 47 | * This struct implements the conversion to and from this middle-endianness. | ||
| 48 | */ | ||
| 49 | struct u32_dsp { | ||
| 50 | u32_dsp() = default; | ||
| 51 | operator u32() const { | ||
| 52 | return Convert(storage); | ||
| 53 | } | ||
| 54 | void operator=(u32 new_value) { | ||
| 55 | storage = Convert(new_value); | ||
| 56 | } | ||
| 57 | |||
| 58 | private: | ||
| 59 | static constexpr u32 Convert(u32 value) { | ||
| 60 | return (value << 16) | (value >> 16); | ||
| 61 | } | ||
| 62 | u32_le storage; | ||
| 63 | }; | ||
| 64 | #if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER) | ||
| 65 | static_assert(std::is_trivially_copyable<u32_dsp>::value, "u32_dsp isn't trivially copyable"); | ||
| 66 | #endif | ||
| 67 | |||
| 68 | // There are 15 structures in each memory region. A table of them in the order they appear in memory | ||
| 69 | // is presented below: | ||
| 70 | // | ||
| 71 | // # First Region DSP Address Purpose Control | ||
| 72 | // 5 0x8400 DSP Status DSP | ||
| 73 | // 9 0x8410 DSP Debug Info DSP | ||
| 74 | // 6 0x8540 Final Mix Samples DSP | ||
| 75 | // 2 0x8680 Source Status [24] DSP | ||
| 76 | // 8 0x8710 Compressor Table Application | ||
| 77 | // 4 0x9430 DSP Configuration Application | ||
| 78 | // 7 0x9492 Intermediate Mix Samples DSP + App | ||
| 79 | // 1 0x9E92 Source Configuration [24] Application | ||
| 80 | // 3 0xA792 Source ADPCM Coefficients [24] Application | ||
| 81 | // 10 0xA912 Surround Sound Related | ||
| 82 | // 11 0xAA12 Surround Sound Related | ||
| 83 | // 12 0xAAD2 Surround Sound Related | ||
| 84 | // 13 0xAC52 Surround Sound Related | ||
| 85 | // 14 0xAC5C Surround Sound Related | ||
| 86 | // 0 0xBFFF Frame Counter Application | ||
| 87 | // | ||
| 88 | // #: This refers to the order in which they appear in the DspPipe::Audio DSP pipe. | ||
| 89 | // See also: DSP::HLE::PipeRead. | ||
| 90 | // | ||
| 91 | // Note that the above addresses do vary slightly between audio firmwares observed; the addresses | ||
| 92 | // are not fixed in stone. The addresses above are only an examplar; they're what this | ||
| 93 | // implementation does and provides to applications. | ||
| 94 | // | ||
| 95 | // Application requests the DSP service to convert DSP addresses into ARM11 virtual addresses using | ||
| 96 | // the ConvertProcessAddressFromDspDram service call. Applications seem to derive the addresses for | ||
| 97 | // the second region via: | ||
| 98 | // second_region_dsp_addr = first_region_dsp_addr | 0x10000 | ||
| 99 | // | ||
| 100 | // Applications maintain most of its own audio state, the memory region is used mainly for | ||
| 101 | // communication and not storage of state. | ||
| 102 | // | ||
| 103 | // In the documentation below, filter and effect transfer functions are specified in the z domain. | ||
| 104 | // (If you are more familiar with the Laplace transform, z = exp(sT). The z domain is the digital | ||
| 105 | // frequency domain, just like how the s domain is the analog frequency domain.) | ||
| 106 | |||
| 107 | #define INSERT_PADDING_DSPWORDS(num_words) INSERT_PADDING_BYTES(2 * (num_words)) | ||
| 108 | |||
| 109 | // GCC versions < 5.0 do not implement std::is_trivially_copyable. | ||
| 110 | // Excluding MSVC because it has weird behaviour for std::is_trivially_copyable. | ||
| 111 | #if (__GNUC__ >= 5) || defined(__clang__) | ||
| 112 | #define ASSERT_DSP_STRUCT(name, size) \ | ||
| 113 | static_assert(std::is_standard_layout<name>::value, \ | ||
| 114 | "DSP structure " #name " doesn't use standard layout"); \ | ||
| 115 | static_assert(std::is_trivially_copyable<name>::value, \ | ||
| 116 | "DSP structure " #name " isn't trivially copyable"); \ | ||
| 117 | static_assert(sizeof(name) == (size), "Unexpected struct size for DSP structure " #name) | ||
| 118 | #else | ||
| 119 | #define ASSERT_DSP_STRUCT(name, size) \ | ||
| 120 | static_assert(std::is_standard_layout<name>::value, \ | ||
| 121 | "DSP structure " #name " doesn't use standard layout"); \ | ||
| 122 | static_assert(sizeof(name) == (size), "Unexpected struct size for DSP structure " #name) | ||
| 123 | #endif | ||
| 124 | |||
| 125 | struct SourceConfiguration { | ||
| 126 | struct Configuration { | ||
| 127 | /// These dirty flags are set by the application when it updates the fields in this struct. | ||
| 128 | /// The DSP clears these each audio frame. | ||
| 129 | union { | ||
| 130 | u32_le dirty_raw; | ||
| 131 | |||
| 132 | BitField<0, 1, u32_le> format_dirty; | ||
| 133 | BitField<1, 1, u32_le> mono_or_stereo_dirty; | ||
| 134 | BitField<2, 1, u32_le> adpcm_coefficients_dirty; | ||
| 135 | /// Tends to be set when a looped buffer is queued. | ||
| 136 | BitField<3, 1, u32_le> partial_embedded_buffer_dirty; | ||
| 137 | BitField<4, 1, u32_le> partial_reset_flag; | ||
| 138 | |||
| 139 | BitField<16, 1, u32_le> enable_dirty; | ||
| 140 | BitField<17, 1, u32_le> interpolation_dirty; | ||
| 141 | BitField<18, 1, u32_le> rate_multiplier_dirty; | ||
| 142 | BitField<19, 1, u32_le> buffer_queue_dirty; | ||
| 143 | BitField<20, 1, u32_le> loop_related_dirty; | ||
| 144 | /// Tends to also be set when embedded buffer is updated. | ||
| 145 | BitField<21, 1, u32_le> play_position_dirty; | ||
| 146 | BitField<22, 1, u32_le> filters_enabled_dirty; | ||
| 147 | BitField<23, 1, u32_le> simple_filter_dirty; | ||
| 148 | BitField<24, 1, u32_le> biquad_filter_dirty; | ||
| 149 | BitField<25, 1, u32_le> gain_0_dirty; | ||
| 150 | BitField<26, 1, u32_le> gain_1_dirty; | ||
| 151 | BitField<27, 1, u32_le> gain_2_dirty; | ||
| 152 | BitField<28, 1, u32_le> sync_dirty; | ||
| 153 | BitField<29, 1, u32_le> reset_flag; | ||
| 154 | BitField<30, 1, u32_le> embedded_buffer_dirty; | ||
| 155 | }; | ||
| 156 | |||
| 157 | // Gain control | ||
| 158 | |||
| 159 | /** | ||
| 160 | * Gain is between 0.0-1.0. This determines how much will this source appear on each of the | ||
| 161 | * 12 channels that feed into the intermediate mixers. Each of the three intermediate mixers | ||
| 162 | * is fed two left and two right channels. | ||
| 163 | */ | ||
| 164 | float_le gain[3][4]; | ||
| 165 | |||
| 166 | // Interpolation | ||
| 167 | |||
| 168 | /// Multiplier for sample rate. Resampling occurs with the selected interpolation method. | ||
| 169 | float_le rate_multiplier; | ||
| 170 | |||
| 171 | enum class InterpolationMode : u8 { | ||
| 172 | Polyphase = 0, | ||
| 173 | Linear = 1, | ||
| 174 | None = 2, | ||
| 175 | }; | ||
| 176 | |||
| 177 | InterpolationMode interpolation_mode; | ||
| 178 | INSERT_PADDING_BYTES(1); ///< Interpolation related | ||
| 179 | |||
| 180 | // Filters | ||
| 181 | |||
| 182 | /** | ||
| 183 | * This is the simplest normalized first-order digital recursive filter. | ||
| 184 | * The transfer function of this filter is: | ||
| 185 | * H(z) = b0 / (1 - a1 z^-1) | ||
| 186 | * Note the feedbackward coefficient is negated. | ||
| 187 | * Values are signed fixed point with 15 fractional bits. | ||
| 188 | */ | ||
| 189 | struct SimpleFilter { | ||
| 190 | s16_le b0; | ||
| 191 | s16_le a1; | ||
| 192 | }; | ||
| 193 | |||
| 194 | /** | ||
| 195 | * This is a normalised biquad filter (second-order). | ||
| 196 | * The transfer function of this filter is: | ||
| 197 | * H(z) = (b0 + b1 z^-1 + b2 z^-2) / (1 - a1 z^-1 - a2 z^-2) | ||
| 198 | * Nintendo chose to negate the feedbackward coefficients. This differs from standard | ||
| 199 | * notation as in: https://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html | ||
| 200 | * Values are signed fixed point with 14 fractional bits. | ||
| 201 | */ | ||
| 202 | struct BiquadFilter { | ||
| 203 | s16_le a2; | ||
| 204 | s16_le a1; | ||
| 205 | s16_le b2; | ||
| 206 | s16_le b1; | ||
| 207 | s16_le b0; | ||
| 208 | }; | ||
| 209 | |||
| 210 | union { | ||
| 211 | u16_le filters_enabled; | ||
| 212 | BitField<0, 1, u16_le> simple_filter_enabled; | ||
| 213 | BitField<1, 1, u16_le> biquad_filter_enabled; | ||
| 214 | }; | ||
| 215 | |||
| 216 | SimpleFilter simple_filter; | ||
| 217 | BiquadFilter biquad_filter; | ||
| 218 | |||
| 219 | // Buffer Queue | ||
| 220 | |||
| 221 | /// A buffer of audio data from the application, along with metadata about it. | ||
| 222 | struct Buffer { | ||
| 223 | /// Physical memory address of the start of the buffer | ||
| 224 | u32_dsp physical_address; | ||
| 225 | |||
| 226 | /// This is length in terms of samples. | ||
| 227 | /// Note that in different buffer formats a sample takes up different number of bytes. | ||
| 228 | u32_dsp length; | ||
| 229 | |||
| 230 | /// ADPCM Predictor (4 bits) and Scale (4 bits) | ||
| 231 | union { | ||
| 232 | u16_le adpcm_ps; | ||
| 233 | BitField<0, 4, u16_le> adpcm_scale; | ||
| 234 | BitField<4, 4, u16_le> adpcm_predictor; | ||
| 235 | }; | ||
| 236 | |||
| 237 | /// ADPCM Historical Samples (y[n-1] and y[n-2]) | ||
| 238 | u16_le adpcm_yn[2]; | ||
| 239 | |||
| 240 | /// This is non-zero when the ADPCM values above are to be updated. | ||
| 241 | u8 adpcm_dirty; | ||
| 242 | |||
| 243 | /// Is a looping buffer. | ||
| 244 | u8 is_looping; | ||
| 245 | |||
| 246 | /// This value is shown in SourceStatus::previous_buffer_id when this buffer has | ||
| 247 | /// finished. This allows the emulated application to tell what buffer is currently | ||
| 248 | /// playing. | ||
| 249 | u16_le buffer_id; | ||
| 250 | |||
| 251 | INSERT_PADDING_DSPWORDS(1); | ||
| 252 | }; | ||
| 253 | |||
| 254 | u16_le buffers_dirty; ///< Bitmap indicating which buffers are dirty (bit i -> buffers[i]) | ||
| 255 | Buffer buffers[4]; ///< Queued Buffers | ||
| 256 | |||
| 257 | // Playback controls | ||
| 258 | |||
| 259 | u32_dsp loop_related; | ||
| 260 | u8 enable; | ||
| 261 | INSERT_PADDING_BYTES(1); | ||
| 262 | u16_le sync; ///< Application-side sync (See also: SourceStatus::sync) | ||
| 263 | u32_dsp play_position; ///< Position. (Units: number of samples) | ||
| 264 | INSERT_PADDING_DSPWORDS(2); | ||
| 265 | |||
| 266 | // Embedded Buffer | ||
| 267 | // This buffer is often the first buffer to be used when initiating audio playback, | ||
| 268 | // after which the buffer queue is used. | ||
| 269 | |||
| 270 | u32_dsp physical_address; | ||
| 271 | |||
| 272 | /// This is length in terms of samples. | ||
| 273 | /// Note a sample takes up different number of bytes in different buffer formats. | ||
| 274 | u32_dsp length; | ||
| 275 | |||
| 276 | enum class MonoOrStereo : u16_le { | ||
| 277 | Mono = 1, | ||
| 278 | Stereo = 2, | ||
| 279 | }; | ||
| 280 | |||
| 281 | enum class Format : u16_le { | ||
| 282 | PCM8 = 0, | ||
| 283 | PCM16 = 1, | ||
| 284 | ADPCM = 2, | ||
| 285 | }; | ||
| 286 | |||
| 287 | union { | ||
| 288 | u16_le flags1_raw; | ||
| 289 | BitField<0, 2, MonoOrStereo> mono_or_stereo; | ||
| 290 | BitField<2, 2, Format> format; | ||
| 291 | BitField<5, 1, u16_le> fade_in; | ||
| 292 | }; | ||
| 293 | |||
| 294 | /// ADPCM Predictor (4 bit) and Scale (4 bit) | ||
| 295 | union { | ||
| 296 | u16_le adpcm_ps; | ||
| 297 | BitField<0, 4, u16_le> adpcm_scale; | ||
| 298 | BitField<4, 4, u16_le> adpcm_predictor; | ||
| 299 | }; | ||
| 300 | |||
| 301 | /// ADPCM Historical Samples (y[n-1] and y[n-2]) | ||
| 302 | u16_le adpcm_yn[2]; | ||
| 303 | |||
| 304 | union { | ||
| 305 | u16_le flags2_raw; | ||
| 306 | BitField<0, 1, u16_le> adpcm_dirty; ///< Has the ADPCM info above been changed? | ||
| 307 | BitField<1, 1, u16_le> is_looping; ///< Is this a looping buffer? | ||
| 308 | }; | ||
| 309 | |||
| 310 | /// Buffer id of embedded buffer (used as a buffer id in SourceStatus to reference this | ||
| 311 | /// buffer). | ||
| 312 | u16_le buffer_id; | ||
| 313 | }; | ||
| 314 | |||
| 315 | Configuration config[num_sources]; | ||
| 316 | }; | ||
| 317 | ASSERT_DSP_STRUCT(SourceConfiguration::Configuration, 192); | ||
| 318 | ASSERT_DSP_STRUCT(SourceConfiguration::Configuration::Buffer, 20); | ||
| 319 | |||
| 320 | struct SourceStatus { | ||
| 321 | struct Status { | ||
| 322 | u8 is_enabled; ///< Is this channel enabled? (Doesn't have to be playing anything.) | ||
| 323 | u8 current_buffer_id_dirty; ///< Non-zero when current_buffer_id changes | ||
| 324 | u16_le sync; ///< Is set by the DSP to the value of SourceConfiguration::sync | ||
| 325 | u32_dsp buffer_position; ///< Number of samples into the current buffer | ||
| 326 | u16_le current_buffer_id; ///< Updated when a buffer finishes playing | ||
| 327 | INSERT_PADDING_DSPWORDS(1); | ||
| 328 | }; | ||
| 329 | |||
| 330 | Status status[num_sources]; | ||
| 331 | }; | ||
| 332 | ASSERT_DSP_STRUCT(SourceStatus::Status, 12); | ||
| 333 | |||
| 334 | struct DspConfiguration { | ||
| 335 | /// These dirty flags are set by the application when it updates the fields in this struct. | ||
| 336 | /// The DSP clears these each audio frame. | ||
| 337 | union { | ||
| 338 | u32_le dirty_raw; | ||
| 339 | |||
| 340 | BitField<8, 1, u32_le> mixer1_enabled_dirty; | ||
| 341 | BitField<9, 1, u32_le> mixer2_enabled_dirty; | ||
| 342 | BitField<10, 1, u32_le> delay_effect_0_dirty; | ||
| 343 | BitField<11, 1, u32_le> delay_effect_1_dirty; | ||
| 344 | BitField<12, 1, u32_le> reverb_effect_0_dirty; | ||
| 345 | BitField<13, 1, u32_le> reverb_effect_1_dirty; | ||
| 346 | |||
| 347 | BitField<16, 1, u32_le> volume_0_dirty; | ||
| 348 | |||
| 349 | BitField<24, 1, u32_le> volume_1_dirty; | ||
| 350 | BitField<25, 1, u32_le> volume_2_dirty; | ||
| 351 | BitField<26, 1, u32_le> output_format_dirty; | ||
| 352 | BitField<27, 1, u32_le> limiter_enabled_dirty; | ||
| 353 | BitField<28, 1, u32_le> headphones_connected_dirty; | ||
| 354 | }; | ||
| 355 | |||
| 356 | /// The DSP has three intermediate audio mixers. This controls the volume level (0.0-1.0) for | ||
| 357 | /// each at the final mixer. | ||
| 358 | float_le volume[3]; | ||
| 359 | |||
| 360 | INSERT_PADDING_DSPWORDS(3); | ||
| 361 | |||
| 362 | enum class OutputFormat : u16_le { | ||
| 363 | Mono = 0, | ||
| 364 | Stereo = 1, | ||
| 365 | Surround = 2, | ||
| 366 | }; | ||
| 367 | |||
| 368 | OutputFormat output_format; | ||
| 369 | |||
| 370 | u16_le limiter_enabled; ///< Not sure of the exact gain equation for the limiter. | ||
| 371 | u16_le headphones_connected; ///< Application updates the DSP on headphone status. | ||
| 372 | INSERT_PADDING_DSPWORDS(4); ///< TODO: Surround sound related | ||
| 373 | INSERT_PADDING_DSPWORDS(2); ///< TODO: Intermediate mixer 1/2 related | ||
| 374 | u16_le mixer1_enabled; | ||
| 375 | u16_le mixer2_enabled; | ||
| 376 | |||
| 377 | /** | ||
| 378 | * This is delay with feedback. | ||
| 379 | * Transfer function: | ||
| 380 | * H(z) = a z^-N / (1 - b z^-1 + a g z^-N) | ||
| 381 | * where | ||
| 382 | * N = frame_count * samples_per_frame | ||
| 383 | * g, a and b are fixed point with 7 fractional bits | ||
| 384 | */ | ||
| 385 | struct DelayEffect { | ||
| 386 | /// These dirty flags are set by the application when it updates the fields in this struct. | ||
| 387 | /// The DSP clears these each audio frame. | ||
| 388 | union { | ||
| 389 | u16_le dirty_raw; | ||
| 390 | BitField<0, 1, u16_le> enable_dirty; | ||
| 391 | BitField<1, 1, u16_le> work_buffer_address_dirty; | ||
| 392 | BitField<2, 1, u16_le> other_dirty; ///< Set when anything else has been changed | ||
| 393 | }; | ||
| 394 | |||
| 395 | u16_le enable; | ||
| 396 | INSERT_PADDING_DSPWORDS(1); | ||
| 397 | u16_le outputs; | ||
| 398 | /// The application allocates a block of memory for the DSP to use as a work buffer. | ||
| 399 | u32_dsp work_buffer_address; | ||
| 400 | /// Frames to delay by | ||
| 401 | u16_le frame_count; | ||
| 402 | |||
| 403 | // Coefficients | ||
| 404 | s16_le g; ///< Fixed point with 7 fractional bits | ||
| 405 | s16_le a; ///< Fixed point with 7 fractional bits | ||
| 406 | s16_le b; ///< Fixed point with 7 fractional bits | ||
| 407 | }; | ||
| 408 | |||
| 409 | DelayEffect delay_effect[2]; | ||
| 410 | |||
| 411 | struct ReverbEffect { | ||
| 412 | INSERT_PADDING_DSPWORDS(26); ///< TODO | ||
| 413 | }; | ||
| 414 | |||
| 415 | ReverbEffect reverb_effect[2]; | ||
| 416 | |||
| 417 | INSERT_PADDING_DSPWORDS(4); | ||
| 418 | }; | ||
| 419 | ASSERT_DSP_STRUCT(DspConfiguration, 196); | ||
| 420 | ASSERT_DSP_STRUCT(DspConfiguration::DelayEffect, 20); | ||
| 421 | ASSERT_DSP_STRUCT(DspConfiguration::ReverbEffect, 52); | ||
| 422 | |||
| 423 | struct AdpcmCoefficients { | ||
| 424 | /// Coefficients are signed fixed point with 11 fractional bits. | ||
| 425 | /// Each source has 16 coefficients associated with it. | ||
| 426 | s16_le coeff[num_sources][16]; | ||
| 427 | }; | ||
| 428 | ASSERT_DSP_STRUCT(AdpcmCoefficients, 768); | ||
| 429 | |||
| 430 | struct DspStatus { | ||
| 431 | u16_le unknown; | ||
| 432 | u16_le dropped_frames; | ||
| 433 | INSERT_PADDING_DSPWORDS(0xE); | ||
| 434 | }; | ||
| 435 | ASSERT_DSP_STRUCT(DspStatus, 32); | ||
| 436 | |||
| 437 | /// Final mixed output in PCM16 stereo format, what you hear out of the speakers. | ||
| 438 | /// When the application writes to this region it has no effect. | ||
| 439 | struct FinalMixSamples { | ||
| 440 | s16_le pcm16[samples_per_frame][2]; | ||
| 441 | }; | ||
| 442 | ASSERT_DSP_STRUCT(FinalMixSamples, 640); | ||
| 443 | |||
| 444 | /// DSP writes output of intermediate mixers 1 and 2 here. | ||
| 445 | /// Writes to this region by the application edits the output of the intermediate mixers. | ||
| 446 | /// This seems to be intended to allow the application to do custom effects on the ARM11. | ||
| 447 | /// Values that exceed s16 range will be clipped by the DSP after further processing. | ||
| 448 | struct IntermediateMixSamples { | ||
| 449 | struct Samples { | ||
| 450 | s32_le pcm32[4][samples_per_frame]; ///< Little-endian as opposed to DSP middle-endian. | ||
| 451 | }; | ||
| 452 | |||
| 453 | Samples mix1; | ||
| 454 | Samples mix2; | ||
| 455 | }; | ||
| 456 | ASSERT_DSP_STRUCT(IntermediateMixSamples, 5120); | ||
| 457 | |||
| 458 | /// Compressor table | ||
| 459 | struct Compressor { | ||
| 460 | INSERT_PADDING_DSPWORDS(0xD20); ///< TODO | ||
| 461 | }; | ||
| 462 | |||
| 463 | /// There is no easy way to implement this in a HLE implementation. | ||
| 464 | struct DspDebug { | ||
| 465 | INSERT_PADDING_DSPWORDS(0x130); | ||
| 466 | }; | ||
| 467 | ASSERT_DSP_STRUCT(DspDebug, 0x260); | ||
| 468 | |||
| 469 | struct SharedMemory { | ||
| 470 | /// Padding | ||
| 471 | INSERT_PADDING_DSPWORDS(0x400); | ||
| 472 | |||
| 473 | DspStatus dsp_status; | ||
| 474 | |||
| 475 | DspDebug dsp_debug; | ||
| 476 | |||
| 477 | FinalMixSamples final_samples; | ||
| 478 | |||
| 479 | SourceStatus source_statuses; | ||
| 480 | |||
| 481 | Compressor compressor; | ||
| 482 | |||
| 483 | DspConfiguration dsp_configuration; | ||
| 484 | |||
| 485 | IntermediateMixSamples intermediate_mix_samples; | ||
| 486 | |||
| 487 | SourceConfiguration source_configurations; | ||
| 488 | |||
| 489 | AdpcmCoefficients adpcm_coefficients; | ||
| 490 | |||
| 491 | struct { | ||
| 492 | INSERT_PADDING_DSPWORDS(0x100); | ||
| 493 | } unknown10; | ||
| 494 | |||
| 495 | struct { | ||
| 496 | INSERT_PADDING_DSPWORDS(0xC0); | ||
| 497 | } unknown11; | ||
| 498 | |||
| 499 | struct { | ||
| 500 | INSERT_PADDING_DSPWORDS(0x180); | ||
| 501 | } unknown12; | ||
| 502 | |||
| 503 | struct { | ||
| 504 | INSERT_PADDING_DSPWORDS(0xA); | ||
| 505 | } unknown13; | ||
| 506 | |||
| 507 | struct { | ||
| 508 | INSERT_PADDING_DSPWORDS(0x13A3); | ||
| 509 | } unknown14; | ||
| 510 | |||
| 511 | u16_le frame_counter; | ||
| 512 | }; | ||
| 513 | ASSERT_DSP_STRUCT(SharedMemory, 0x8000); | ||
| 514 | |||
| 515 | union DspMemory { | ||
| 516 | std::array<u8, 0x80000> raw_memory; | ||
| 517 | struct { | ||
| 518 | u8 unused_0[0x50000]; | ||
| 519 | SharedMemory region_0; | ||
| 520 | u8 unused_1[0x18000]; | ||
| 521 | SharedMemory region_1; | ||
| 522 | u8 unused_2[0x8000]; | ||
| 523 | }; | ||
| 524 | }; | ||
| 525 | static_assert(offsetof(DspMemory, region_0) == region0_offset, | ||
| 526 | "DSP region 0 is at the wrong offset"); | ||
| 527 | static_assert(offsetof(DspMemory, region_1) == region1_offset, | ||
| 528 | "DSP region 1 is at the wrong offset"); | ||
| 529 | |||
| 530 | extern DspMemory g_dsp_memory; | ||
| 531 | |||
| 532 | // Structures must have an offset that is a multiple of two. | ||
| 533 | static_assert(offsetof(SharedMemory, frame_counter) % 2 == 0, | ||
| 534 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 535 | static_assert(offsetof(SharedMemory, source_configurations) % 2 == 0, | ||
| 536 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 537 | static_assert(offsetof(SharedMemory, source_statuses) % 2 == 0, | ||
| 538 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 539 | static_assert(offsetof(SharedMemory, adpcm_coefficients) % 2 == 0, | ||
| 540 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 541 | static_assert(offsetof(SharedMemory, dsp_configuration) % 2 == 0, | ||
| 542 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 543 | static_assert(offsetof(SharedMemory, dsp_status) % 2 == 0, | ||
| 544 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 545 | static_assert(offsetof(SharedMemory, final_samples) % 2 == 0, | ||
| 546 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 547 | static_assert(offsetof(SharedMemory, intermediate_mix_samples) % 2 == 0, | ||
| 548 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 549 | static_assert(offsetof(SharedMemory, compressor) % 2 == 0, | ||
| 550 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 551 | static_assert(offsetof(SharedMemory, dsp_debug) % 2 == 0, | ||
| 552 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 553 | static_assert(offsetof(SharedMemory, unknown10) % 2 == 0, | ||
| 554 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 555 | static_assert(offsetof(SharedMemory, unknown11) % 2 == 0, | ||
| 556 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 557 | static_assert(offsetof(SharedMemory, unknown12) % 2 == 0, | ||
| 558 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 559 | static_assert(offsetof(SharedMemory, unknown13) % 2 == 0, | ||
| 560 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 561 | static_assert(offsetof(SharedMemory, unknown14) % 2 == 0, | ||
| 562 | "Structures in DSP::HLE::SharedMemory must be 2-byte aligned"); | ||
| 563 | |||
| 564 | #undef INSERT_PADDING_DSPWORDS | ||
| 565 | #undef ASSERT_DSP_STRUCT | ||
| 566 | |||
| 567 | /// Initialize DSP hardware | ||
| 568 | void Init(); | ||
| 569 | |||
| 570 | /// Shutdown DSP hardware | ||
| 571 | void Shutdown(); | ||
| 572 | |||
| 573 | /** | ||
| 574 | * Perform processing and updates state of current shared memory buffer. | ||
| 575 | * This function is called every audio tick before triggering the audio interrupt. | ||
| 576 | * @return Whether an audio interrupt should be triggered this frame. | ||
| 577 | */ | ||
| 578 | bool Tick(); | ||
| 579 | |||
| 580 | /** | ||
| 581 | * Set the output sink. This must be called before calling Tick(). | ||
| 582 | * @param sink The sink to which audio will be output to. | ||
| 583 | */ | ||
| 584 | void SetSink(std::unique_ptr<AudioCore::Sink> sink); | ||
| 585 | |||
| 586 | /** | ||
| 587 | * Enables/Disables audio-stretching. | ||
| 588 | * Audio stretching is an enhancement that stretches audio to match emulation | ||
| 589 | * speed to prevent stuttering at the cost of some audio latency. | ||
| 590 | * @param enable true to enable, false to disable. | ||
| 591 | */ | ||
| 592 | void EnableStretching(bool enable); | ||
| 593 | |||
| 594 | } // namespace HLE | ||
| 595 | } // namespace DSP | ||
diff --git a/src/audio_core/hle/filter.cpp b/src/audio_core/hle/filter.cpp deleted file mode 100644 index b24a79b89..000000000 --- a/src/audio_core/hle/filter.cpp +++ /dev/null | |||
| @@ -1,117 +0,0 @@ | |||
| 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 <array> | ||
| 6 | #include <cstddef> | ||
| 7 | #include "audio_core/hle/common.h" | ||
| 8 | #include "audio_core/hle/dsp.h" | ||
| 9 | #include "audio_core/hle/filter.h" | ||
| 10 | #include "common/common_types.h" | ||
| 11 | #include "common/math_util.h" | ||
| 12 | |||
| 13 | namespace DSP { | ||
| 14 | namespace HLE { | ||
| 15 | |||
| 16 | void SourceFilters::Reset() { | ||
| 17 | Enable(false, false); | ||
| 18 | } | ||
| 19 | |||
| 20 | void SourceFilters::Enable(bool simple, bool biquad) { | ||
| 21 | simple_filter_enabled = simple; | ||
| 22 | biquad_filter_enabled = biquad; | ||
| 23 | |||
| 24 | if (!simple) | ||
| 25 | simple_filter.Reset(); | ||
| 26 | if (!biquad) | ||
| 27 | biquad_filter.Reset(); | ||
| 28 | } | ||
| 29 | |||
| 30 | void SourceFilters::Configure(SourceConfiguration::Configuration::SimpleFilter config) { | ||
| 31 | simple_filter.Configure(config); | ||
| 32 | } | ||
| 33 | |||
| 34 | void SourceFilters::Configure(SourceConfiguration::Configuration::BiquadFilter config) { | ||
| 35 | biquad_filter.Configure(config); | ||
| 36 | } | ||
| 37 | |||
| 38 | void SourceFilters::ProcessFrame(StereoFrame16& frame) { | ||
| 39 | if (!simple_filter_enabled && !biquad_filter_enabled) | ||
| 40 | return; | ||
| 41 | |||
| 42 | if (simple_filter_enabled) { | ||
| 43 | FilterFrame(frame, simple_filter); | ||
| 44 | } | ||
| 45 | |||
| 46 | if (biquad_filter_enabled) { | ||
| 47 | FilterFrame(frame, biquad_filter); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | // SimpleFilter | ||
| 52 | |||
| 53 | void SourceFilters::SimpleFilter::Reset() { | ||
| 54 | y1.fill(0); | ||
| 55 | // Configure as passthrough. | ||
| 56 | a1 = 0; | ||
| 57 | b0 = 1 << 15; | ||
| 58 | } | ||
| 59 | |||
| 60 | void SourceFilters::SimpleFilter::Configure( | ||
| 61 | SourceConfiguration::Configuration::SimpleFilter config) { | ||
| 62 | |||
| 63 | a1 = config.a1; | ||
| 64 | b0 = config.b0; | ||
| 65 | } | ||
| 66 | |||
| 67 | std::array<s16, 2> SourceFilters::SimpleFilter::ProcessSample(const std::array<s16, 2>& x0) { | ||
| 68 | std::array<s16, 2> y0; | ||
| 69 | for (size_t i = 0; i < 2; i++) { | ||
| 70 | const s32 tmp = (b0 * x0[i] + a1 * y1[i]) >> 15; | ||
| 71 | y0[i] = MathUtil::Clamp(tmp, -32768, 32767); | ||
| 72 | } | ||
| 73 | |||
| 74 | y1 = y0; | ||
| 75 | |||
| 76 | return y0; | ||
| 77 | } | ||
| 78 | |||
| 79 | // BiquadFilter | ||
| 80 | |||
| 81 | void SourceFilters::BiquadFilter::Reset() { | ||
| 82 | x1.fill(0); | ||
| 83 | x2.fill(0); | ||
| 84 | y1.fill(0); | ||
| 85 | y2.fill(0); | ||
| 86 | // Configure as passthrough. | ||
| 87 | a1 = a2 = b1 = b2 = 0; | ||
| 88 | b0 = 1 << 14; | ||
| 89 | } | ||
| 90 | |||
| 91 | void SourceFilters::BiquadFilter::Configure( | ||
| 92 | SourceConfiguration::Configuration::BiquadFilter config) { | ||
| 93 | |||
| 94 | a1 = config.a1; | ||
| 95 | a2 = config.a2; | ||
| 96 | b0 = config.b0; | ||
| 97 | b1 = config.b1; | ||
| 98 | b2 = config.b2; | ||
| 99 | } | ||
| 100 | |||
| 101 | std::array<s16, 2> SourceFilters::BiquadFilter::ProcessSample(const std::array<s16, 2>& x0) { | ||
| 102 | std::array<s16, 2> y0; | ||
| 103 | for (size_t i = 0; i < 2; i++) { | ||
| 104 | const s32 tmp = (b0 * x0[i] + b1 * x1[i] + b2 * x2[i] + a1 * y1[i] + a2 * y2[i]) >> 14; | ||
| 105 | y0[i] = MathUtil::Clamp(tmp, -32768, 32767); | ||
| 106 | } | ||
| 107 | |||
| 108 | x2 = x1; | ||
| 109 | x1 = x0; | ||
| 110 | y2 = y1; | ||
| 111 | y1 = y0; | ||
| 112 | |||
| 113 | return y0; | ||
| 114 | } | ||
| 115 | |||
| 116 | } // namespace HLE | ||
| 117 | } // namespace DSP | ||
diff --git a/src/audio_core/hle/filter.h b/src/audio_core/hle/filter.h deleted file mode 100644 index 5350e2857..000000000 --- a/src/audio_core/hle/filter.h +++ /dev/null | |||
| @@ -1,117 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include "audio_core/hle/common.h" | ||
| 9 | #include "audio_core/hle/dsp.h" | ||
| 10 | #include "common/common_types.h" | ||
| 11 | |||
| 12 | namespace DSP { | ||
| 13 | namespace HLE { | ||
| 14 | |||
| 15 | /// Preprocessing filters. There is an independent set of filters for each Source. | ||
| 16 | class SourceFilters final { | ||
| 17 | public: | ||
| 18 | SourceFilters() { | ||
| 19 | Reset(); | ||
| 20 | } | ||
| 21 | |||
| 22 | /// Reset internal state. | ||
| 23 | void Reset(); | ||
| 24 | |||
| 25 | /** | ||
| 26 | * Enable/Disable filters | ||
| 27 | * See also: SourceConfiguration::Configuration::simple_filter_enabled, | ||
| 28 | * SourceConfiguration::Configuration::biquad_filter_enabled. | ||
| 29 | * @param simple If true, enables the simple filter. If false, disables it. | ||
| 30 | * @param biquad If true, enables the biquad filter. If false, disables it. | ||
| 31 | */ | ||
| 32 | void Enable(bool simple, bool biquad); | ||
| 33 | |||
| 34 | /** | ||
| 35 | * Configure simple filter. | ||
| 36 | * @param config Configuration from DSP shared memory. | ||
| 37 | */ | ||
| 38 | void Configure(SourceConfiguration::Configuration::SimpleFilter config); | ||
| 39 | |||
| 40 | /** | ||
| 41 | * Configure biquad filter. | ||
| 42 | * @param config Configuration from DSP shared memory. | ||
| 43 | */ | ||
| 44 | void Configure(SourceConfiguration::Configuration::BiquadFilter config); | ||
| 45 | |||
| 46 | /** | ||
| 47 | * Processes a frame in-place. | ||
| 48 | * @param frame Audio samples to process. Modified in-place. | ||
| 49 | */ | ||
| 50 | void ProcessFrame(StereoFrame16& frame); | ||
| 51 | |||
| 52 | private: | ||
| 53 | bool simple_filter_enabled; | ||
| 54 | bool biquad_filter_enabled; | ||
| 55 | |||
| 56 | struct SimpleFilter { | ||
| 57 | SimpleFilter() { | ||
| 58 | Reset(); | ||
| 59 | } | ||
| 60 | |||
| 61 | /// Resets internal state. | ||
| 62 | void Reset(); | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Configures this filter with application settings. | ||
| 66 | * @param config Configuration from DSP shared memory. | ||
| 67 | */ | ||
| 68 | void Configure(SourceConfiguration::Configuration::SimpleFilter config); | ||
| 69 | |||
| 70 | /** | ||
| 71 | * Processes a single stereo PCM16 sample. | ||
| 72 | * @param x0 Input sample | ||
| 73 | * @return Output sample | ||
| 74 | */ | ||
| 75 | std::array<s16, 2> ProcessSample(const std::array<s16, 2>& x0); | ||
| 76 | |||
| 77 | private: | ||
| 78 | // Configuration | ||
| 79 | s32 a1, b0; | ||
| 80 | // Internal state | ||
| 81 | std::array<s16, 2> y1; | ||
| 82 | } simple_filter; | ||
| 83 | |||
| 84 | struct BiquadFilter { | ||
| 85 | BiquadFilter() { | ||
| 86 | Reset(); | ||
| 87 | } | ||
| 88 | |||
| 89 | /// Resets internal state. | ||
| 90 | void Reset(); | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Configures this filter with application settings. | ||
| 94 | * @param config Configuration from DSP shared memory. | ||
| 95 | */ | ||
| 96 | void Configure(SourceConfiguration::Configuration::BiquadFilter config); | ||
| 97 | |||
| 98 | /** | ||
| 99 | * Processes a single stereo PCM16 sample. | ||
| 100 | * @param x0 Input sample | ||
| 101 | * @return Output sample | ||
| 102 | */ | ||
| 103 | std::array<s16, 2> ProcessSample(const std::array<s16, 2>& x0); | ||
| 104 | |||
| 105 | private: | ||
| 106 | // Configuration | ||
| 107 | s32 a1, a2, b0, b1, b2; | ||
| 108 | // Internal state | ||
| 109 | std::array<s16, 2> x1; | ||
| 110 | std::array<s16, 2> x2; | ||
| 111 | std::array<s16, 2> y1; | ||
| 112 | std::array<s16, 2> y2; | ||
| 113 | } biquad_filter; | ||
| 114 | }; | ||
| 115 | |||
| 116 | } // namespace HLE | ||
| 117 | } // namespace DSP | ||
diff --git a/src/audio_core/hle/mixers.cpp b/src/audio_core/hle/mixers.cpp deleted file mode 100644 index 6cc81dfca..000000000 --- a/src/audio_core/hle/mixers.cpp +++ /dev/null | |||
| @@ -1,210 +0,0 @@ | |||
| 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 | #include "common/assert.h" | ||
| 11 | #include "common/logging/log.h" | ||
| 12 | #include "common/math_util.h" | ||
| 13 | |||
| 14 | namespace DSP { | ||
| 15 | namespace HLE { | ||
| 16 | |||
| 17 | void Mixers::Reset() { | ||
| 18 | current_frame.fill({}); | ||
| 19 | state = {}; | ||
| 20 | } | ||
| 21 | |||
| 22 | DspStatus Mixers::Tick(DspConfiguration& config, const IntermediateMixSamples& read_samples, | ||
| 23 | IntermediateMixSamples& write_samples, | ||
| 24 | const std::array<QuadFrame32, 3>& input) { | ||
| 25 | ParseConfig(config); | ||
| 26 | |||
| 27 | AuxReturn(read_samples); | ||
| 28 | AuxSend(write_samples, input); | ||
| 29 | |||
| 30 | MixCurrentFrame(); | ||
| 31 | |||
| 32 | return GetCurrentStatus(); | ||
| 33 | } | ||
| 34 | |||
| 35 | void Mixers::ParseConfig(DspConfiguration& config) { | ||
| 36 | if (!config.dirty_raw) { | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | |||
| 40 | if (config.mixer1_enabled_dirty) { | ||
| 41 | config.mixer1_enabled_dirty.Assign(0); | ||
| 42 | state.mixer1_enabled = config.mixer1_enabled != 0; | ||
| 43 | LOG_TRACE(Audio_DSP, "mixers mixer1_enabled = %hu", config.mixer1_enabled); | ||
| 44 | } | ||
| 45 | |||
| 46 | if (config.mixer2_enabled_dirty) { | ||
| 47 | config.mixer2_enabled_dirty.Assign(0); | ||
| 48 | state.mixer2_enabled = config.mixer2_enabled != 0; | ||
| 49 | LOG_TRACE(Audio_DSP, "mixers mixer2_enabled = %hu", config.mixer2_enabled); | ||
| 50 | } | ||
| 51 | |||
| 52 | if (config.volume_0_dirty) { | ||
| 53 | config.volume_0_dirty.Assign(0); | ||
| 54 | state.intermediate_mixer_volume[0] = config.volume[0]; | ||
| 55 | LOG_TRACE(Audio_DSP, "mixers volume[0] = %f", config.volume[0]); | ||
| 56 | } | ||
| 57 | |||
| 58 | if (config.volume_1_dirty) { | ||
| 59 | config.volume_1_dirty.Assign(0); | ||
| 60 | state.intermediate_mixer_volume[1] = config.volume[1]; | ||
| 61 | LOG_TRACE(Audio_DSP, "mixers volume[1] = %f", config.volume[1]); | ||
| 62 | } | ||
| 63 | |||
| 64 | if (config.volume_2_dirty) { | ||
| 65 | config.volume_2_dirty.Assign(0); | ||
| 66 | state.intermediate_mixer_volume[2] = config.volume[2]; | ||
| 67 | LOG_TRACE(Audio_DSP, "mixers volume[2] = %f", config.volume[2]); | ||
| 68 | } | ||
| 69 | |||
| 70 | if (config.output_format_dirty) { | ||
| 71 | config.output_format_dirty.Assign(0); | ||
| 72 | state.output_format = config.output_format; | ||
| 73 | LOG_TRACE(Audio_DSP, "mixers output_format = %zu", | ||
| 74 | static_cast<size_t>(config.output_format)); | ||
| 75 | } | ||
| 76 | |||
| 77 | if (config.headphones_connected_dirty) { | ||
| 78 | config.headphones_connected_dirty.Assign(0); | ||
| 79 | // Do nothing. (Note: Whether headphones are connected does affect coefficients used for | ||
| 80 | // surround sound.) | ||
| 81 | LOG_TRACE(Audio_DSP, "mixers headphones_connected=%hu", config.headphones_connected); | ||
| 82 | } | ||
| 83 | |||
| 84 | if (config.dirty_raw) { | ||
| 85 | LOG_DEBUG(Audio_DSP, "mixers remaining_dirty=%x", config.dirty_raw); | ||
| 86 | } | ||
| 87 | |||
| 88 | config.dirty_raw = 0; | ||
| 89 | } | ||
| 90 | |||
| 91 | static s16 ClampToS16(s32 value) { | ||
| 92 | return static_cast<s16>(MathUtil::Clamp(value, -32768, 32767)); | ||
| 93 | } | ||
| 94 | |||
| 95 | static std::array<s16, 2> AddAndClampToS16(const std::array<s16, 2>& a, | ||
| 96 | const std::array<s16, 2>& b) { | ||
| 97 | return {ClampToS16(static_cast<s32>(a[0]) + static_cast<s32>(b[0])), | ||
| 98 | ClampToS16(static_cast<s32>(a[1]) + static_cast<s32>(b[1]))}; | ||
| 99 | } | ||
| 100 | |||
| 101 | void Mixers::DownmixAndMixIntoCurrentFrame(float gain, const QuadFrame32& samples) { | ||
| 102 | // TODO(merry): Limiter. (Currently we're performing final mixing assuming a disabled limiter.) | ||
| 103 | |||
| 104 | switch (state.output_format) { | ||
| 105 | case OutputFormat::Mono: | ||
| 106 | std::transform( | ||
| 107 | current_frame.begin(), current_frame.end(), samples.begin(), current_frame.begin(), | ||
| 108 | [gain](const std::array<s16, 2>& accumulator, | ||
| 109 | const std::array<s32, 4>& sample) -> std::array<s16, 2> { | ||
| 110 | // Downmix to mono | ||
| 111 | s16 mono = ClampToS16(static_cast<s32>( | ||
| 112 | (gain * sample[0] + gain * sample[1] + gain * sample[2] + gain * sample[3]) / | ||
| 113 | 2)); | ||
| 114 | // Mix into current frame | ||
| 115 | return AddAndClampToS16(accumulator, {mono, mono}); | ||
| 116 | }); | ||
| 117 | return; | ||
| 118 | |||
| 119 | case OutputFormat::Surround: | ||
| 120 | // TODO(merry): Implement surround sound. | ||
| 121 | // fallthrough | ||
| 122 | |||
| 123 | case OutputFormat::Stereo: | ||
| 124 | std::transform( | ||
| 125 | current_frame.begin(), current_frame.end(), samples.begin(), current_frame.begin(), | ||
| 126 | [gain](const std::array<s16, 2>& accumulator, | ||
| 127 | const std::array<s32, 4>& sample) -> std::array<s16, 2> { | ||
| 128 | // Downmix to stereo | ||
| 129 | s16 left = ClampToS16(static_cast<s32>(gain * sample[0] + gain * sample[2])); | ||
| 130 | s16 right = ClampToS16(static_cast<s32>(gain * sample[1] + gain * sample[3])); | ||
| 131 | // Mix into current frame | ||
| 132 | return AddAndClampToS16(accumulator, {left, right}); | ||
| 133 | }); | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | |||
| 137 | UNREACHABLE_MSG("Invalid output_format %zu", static_cast<size_t>(state.output_format)); | ||
| 138 | } | ||
| 139 | |||
| 140 | void Mixers::AuxReturn(const IntermediateMixSamples& read_samples) { | ||
| 141 | // NOTE: read_samples.mix{1,2}.pcm32 annoyingly have their dimensions in reverse order to | ||
| 142 | // QuadFrame32. | ||
| 143 | |||
| 144 | if (state.mixer1_enabled) { | ||
| 145 | for (size_t sample = 0; sample < samples_per_frame; sample++) { | ||
| 146 | for (size_t channel = 0; channel < 4; channel++) { | ||
| 147 | state.intermediate_mix_buffer[1][sample][channel] = | ||
| 148 | read_samples.mix1.pcm32[channel][sample]; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | if (state.mixer2_enabled) { | ||
| 154 | for (size_t sample = 0; sample < samples_per_frame; sample++) { | ||
| 155 | for (size_t channel = 0; channel < 4; channel++) { | ||
| 156 | state.intermediate_mix_buffer[2][sample][channel] = | ||
| 157 | read_samples.mix2.pcm32[channel][sample]; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | void Mixers::AuxSend(IntermediateMixSamples& write_samples, | ||
| 164 | const std::array<QuadFrame32, 3>& input) { | ||
| 165 | // NOTE: read_samples.mix{1,2}.pcm32 annoyingly have their dimensions in reverse order to | ||
| 166 | // QuadFrame32. | ||
| 167 | |||
| 168 | state.intermediate_mix_buffer[0] = input[0]; | ||
| 169 | |||
| 170 | if (state.mixer1_enabled) { | ||
| 171 | for (size_t sample = 0; sample < samples_per_frame; sample++) { | ||
| 172 | for (size_t channel = 0; channel < 4; channel++) { | ||
| 173 | write_samples.mix1.pcm32[channel][sample] = input[1][sample][channel]; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } else { | ||
| 177 | state.intermediate_mix_buffer[1] = input[1]; | ||
| 178 | } | ||
| 179 | |||
| 180 | if (state.mixer2_enabled) { | ||
| 181 | for (size_t sample = 0; sample < samples_per_frame; sample++) { | ||
| 182 | for (size_t channel = 0; channel < 4; channel++) { | ||
| 183 | write_samples.mix2.pcm32[channel][sample] = input[2][sample][channel]; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } else { | ||
| 187 | state.intermediate_mix_buffer[2] = input[2]; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | void Mixers::MixCurrentFrame() { | ||
| 192 | current_frame.fill({}); | ||
| 193 | |||
| 194 | for (size_t mix = 0; mix < 3; mix++) { | ||
| 195 | DownmixAndMixIntoCurrentFrame(state.intermediate_mixer_volume[mix], | ||
| 196 | state.intermediate_mix_buffer[mix]); | ||
| 197 | } | ||
| 198 | |||
| 199 | // TODO(merry): Compressor. (We currently assume a disabled compressor.) | ||
| 200 | } | ||
| 201 | |||
| 202 | DspStatus Mixers::GetCurrentStatus() const { | ||
| 203 | DspStatus status; | ||
| 204 | status.unknown = 0; | ||
| 205 | status.dropped_frames = 0; | ||
| 206 | return status; | ||
| 207 | } | ||
| 208 | |||
| 209 | } // namespace HLE | ||
| 210 | } // namespace DSP | ||
diff --git a/src/audio_core/hle/mixers.h b/src/audio_core/hle/mixers.h deleted file mode 100644 index bf4e865ae..000000000 --- a/src/audio_core/hle/mixers.h +++ /dev/null | |||
| @@ -1,61 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include "audio_core/hle/common.h" | ||
| 9 | #include "audio_core/hle/dsp.h" | ||
| 10 | |||
| 11 | namespace DSP { | ||
| 12 | namespace HLE { | ||
| 13 | |||
| 14 | class Mixers final { | ||
| 15 | public: | ||
| 16 | Mixers() { | ||
| 17 | Reset(); | ||
| 18 | } | ||
| 19 | |||
| 20 | void Reset(); | ||
| 21 | |||
| 22 | DspStatus Tick(DspConfiguration& config, const IntermediateMixSamples& read_samples, | ||
| 23 | IntermediateMixSamples& write_samples, const std::array<QuadFrame32, 3>& input); | ||
| 24 | |||
| 25 | StereoFrame16 GetOutput() const { | ||
| 26 | return current_frame; | ||
| 27 | } | ||
| 28 | |||
| 29 | private: | ||
| 30 | StereoFrame16 current_frame = {}; | ||
| 31 | |||
| 32 | using OutputFormat = DspConfiguration::OutputFormat; | ||
| 33 | |||
| 34 | struct { | ||
| 35 | std::array<float, 3> intermediate_mixer_volume = {}; | ||
| 36 | |||
| 37 | bool mixer1_enabled = false; | ||
| 38 | bool mixer2_enabled = false; | ||
| 39 | std::array<QuadFrame32, 3> intermediate_mix_buffer = {}; | ||
| 40 | |||
| 41 | OutputFormat output_format = OutputFormat::Stereo; | ||
| 42 | |||
| 43 | } state; | ||
| 44 | |||
| 45 | /// INTERNAL: Update our internal state based on the current config. | ||
| 46 | void ParseConfig(DspConfiguration& config); | ||
| 47 | /// INTERNAL: Read samples from shared memory that have been modified by the ARM11. | ||
| 48 | void AuxReturn(const IntermediateMixSamples& read_samples); | ||
| 49 | /// INTERNAL: Write samples to shared memory for the ARM11 to modify. | ||
| 50 | void AuxSend(IntermediateMixSamples& write_samples, const std::array<QuadFrame32, 3>& input); | ||
| 51 | /// INTERNAL: Mix current_frame. | ||
| 52 | void MixCurrentFrame(); | ||
| 53 | /// INTERNAL: Downmix from quadraphonic to stereo based on status.output_format and accumulate | ||
| 54 | /// into current_frame. | ||
| 55 | void DownmixAndMixIntoCurrentFrame(float gain, const QuadFrame32& samples); | ||
| 56 | /// INTERNAL: Generate DspStatus based on internal state. | ||
| 57 | DspStatus GetCurrentStatus() const; | ||
| 58 | }; | ||
| 59 | |||
| 60 | } // namespace HLE | ||
| 61 | } // namespace DSP | ||
diff --git a/src/audio_core/hle/pipe.cpp b/src/audio_core/hle/pipe.cpp deleted file mode 100644 index 24074a514..000000000 --- a/src/audio_core/hle/pipe.cpp +++ /dev/null | |||
| @@ -1,177 +0,0 @@ | |||
| 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 <array> | ||
| 6 | #include <vector> | ||
| 7 | #include "audio_core/hle/dsp.h" | ||
| 8 | #include "audio_core/hle/pipe.h" | ||
| 9 | #include "common/assert.h" | ||
| 10 | #include "common/common_types.h" | ||
| 11 | #include "common/logging/log.h" | ||
| 12 | #include "core/hle/service/dsp_dsp.h" | ||
| 13 | |||
| 14 | namespace DSP { | ||
| 15 | namespace HLE { | ||
| 16 | |||
| 17 | static DspState dsp_state = DspState::Off; | ||
| 18 | |||
| 19 | static std::array<std::vector<u8>, NUM_DSP_PIPE> pipe_data; | ||
| 20 | |||
| 21 | void ResetPipes() { | ||
| 22 | for (auto& data : pipe_data) { | ||
| 23 | data.clear(); | ||
| 24 | } | ||
| 25 | dsp_state = DspState::Off; | ||
| 26 | } | ||
| 27 | |||
| 28 | std::vector<u8> PipeRead(DspPipe pipe_number, u32 length) { | ||
| 29 | const size_t pipe_index = static_cast<size_t>(pipe_number); | ||
| 30 | |||
| 31 | if (pipe_index >= NUM_DSP_PIPE) { | ||
| 32 | LOG_ERROR(Audio_DSP, "pipe_number = %zu invalid", pipe_index); | ||
| 33 | return {}; | ||
| 34 | } | ||
| 35 | |||
| 36 | if (length > UINT16_MAX) { // Can only read at most UINT16_MAX from the pipe | ||
| 37 | LOG_ERROR(Audio_DSP, "length of %u greater than max of %u", length, UINT16_MAX); | ||
| 38 | return {}; | ||
| 39 | } | ||
| 40 | |||
| 41 | std::vector<u8>& data = pipe_data[pipe_index]; | ||
| 42 | |||
| 43 | if (length > data.size()) { | ||
| 44 | LOG_WARNING( | ||
| 45 | Audio_DSP, | ||
| 46 | "pipe_number = %zu is out of data, application requested read of %u but %zu remain", | ||
| 47 | pipe_index, length, data.size()); | ||
| 48 | length = static_cast<u32>(data.size()); | ||
| 49 | } | ||
| 50 | |||
| 51 | if (length == 0) | ||
| 52 | return {}; | ||
| 53 | |||
| 54 | std::vector<u8> ret(data.begin(), data.begin() + length); | ||
| 55 | data.erase(data.begin(), data.begin() + length); | ||
| 56 | return ret; | ||
| 57 | } | ||
| 58 | |||
| 59 | size_t GetPipeReadableSize(DspPipe pipe_number) { | ||
| 60 | const size_t pipe_index = static_cast<size_t>(pipe_number); | ||
| 61 | |||
| 62 | if (pipe_index >= NUM_DSP_PIPE) { | ||
| 63 | LOG_ERROR(Audio_DSP, "pipe_number = %zu invalid", pipe_index); | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | return pipe_data[pipe_index].size(); | ||
| 68 | } | ||
| 69 | |||
| 70 | static void WriteU16(DspPipe pipe_number, u16 value) { | ||
| 71 | const size_t pipe_index = static_cast<size_t>(pipe_number); | ||
| 72 | |||
| 73 | std::vector<u8>& data = pipe_data.at(pipe_index); | ||
| 74 | // Little endian | ||
| 75 | data.emplace_back(value & 0xFF); | ||
| 76 | data.emplace_back(value >> 8); | ||
| 77 | } | ||
| 78 | |||
| 79 | static void AudioPipeWriteStructAddresses() { | ||
| 80 | // These struct addresses are DSP dram addresses. | ||
| 81 | // See also: DSP_DSP::ConvertProcessAddressFromDspDram | ||
| 82 | static const std::array<u16, 15> struct_addresses = { | ||
| 83 | 0x8000 + offsetof(SharedMemory, frame_counter) / 2, | ||
| 84 | 0x8000 + offsetof(SharedMemory, source_configurations) / 2, | ||
| 85 | 0x8000 + offsetof(SharedMemory, source_statuses) / 2, | ||
| 86 | 0x8000 + offsetof(SharedMemory, adpcm_coefficients) / 2, | ||
| 87 | 0x8000 + offsetof(SharedMemory, dsp_configuration) / 2, | ||
| 88 | 0x8000 + offsetof(SharedMemory, dsp_status) / 2, | ||
| 89 | 0x8000 + offsetof(SharedMemory, final_samples) / 2, | ||
| 90 | 0x8000 + offsetof(SharedMemory, intermediate_mix_samples) / 2, | ||
| 91 | 0x8000 + offsetof(SharedMemory, compressor) / 2, | ||
| 92 | 0x8000 + offsetof(SharedMemory, dsp_debug) / 2, | ||
| 93 | 0x8000 + offsetof(SharedMemory, unknown10) / 2, | ||
| 94 | 0x8000 + offsetof(SharedMemory, unknown11) / 2, | ||
| 95 | 0x8000 + offsetof(SharedMemory, unknown12) / 2, | ||
| 96 | 0x8000 + offsetof(SharedMemory, unknown13) / 2, | ||
| 97 | 0x8000 + offsetof(SharedMemory, unknown14) / 2, | ||
| 98 | }; | ||
| 99 | |||
| 100 | // Begin with a u16 denoting the number of structs. | ||
| 101 | WriteU16(DspPipe::Audio, static_cast<u16>(struct_addresses.size())); | ||
| 102 | // Then write the struct addresses. | ||
| 103 | for (u16 addr : struct_addresses) { | ||
| 104 | WriteU16(DspPipe::Audio, addr); | ||
| 105 | } | ||
| 106 | // Signal that we have data on this pipe. | ||
| 107 | Service::DSP_DSP::SignalPipeInterrupt(DspPipe::Audio); | ||
| 108 | } | ||
| 109 | |||
| 110 | void PipeWrite(DspPipe pipe_number, const std::vector<u8>& buffer) { | ||
| 111 | switch (pipe_number) { | ||
| 112 | case DspPipe::Audio: { | ||
| 113 | if (buffer.size() != 4) { | ||
| 114 | LOG_ERROR(Audio_DSP, "DspPipe::Audio: Unexpected buffer length %zu was written", | ||
| 115 | buffer.size()); | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | enum class StateChange { | ||
| 120 | Initialize = 0, | ||
| 121 | Shutdown = 1, | ||
| 122 | Wakeup = 2, | ||
| 123 | Sleep = 3, | ||
| 124 | }; | ||
| 125 | |||
| 126 | // The difference between Initialize and Wakeup is that Input state is maintained | ||
| 127 | // when sleeping but isn't when turning it off and on again. (TODO: Implement this.) | ||
| 128 | // Waking up from sleep garbles some of the structs in the memory region. (TODO: | ||
| 129 | // Implement this.) Applications store away the state of these structs before | ||
| 130 | // sleeping and reset it back after wakeup on behalf of the DSP. | ||
| 131 | |||
| 132 | switch (static_cast<StateChange>(buffer[0])) { | ||
| 133 | case StateChange::Initialize: | ||
| 134 | LOG_INFO(Audio_DSP, "Application has requested initialization of DSP hardware"); | ||
| 135 | ResetPipes(); | ||
| 136 | AudioPipeWriteStructAddresses(); | ||
| 137 | dsp_state = DspState::On; | ||
| 138 | break; | ||
| 139 | case StateChange::Shutdown: | ||
| 140 | LOG_INFO(Audio_DSP, "Application has requested shutdown of DSP hardware"); | ||
| 141 | dsp_state = DspState::Off; | ||
| 142 | break; | ||
| 143 | case StateChange::Wakeup: | ||
| 144 | LOG_INFO(Audio_DSP, "Application has requested wakeup of DSP hardware"); | ||
| 145 | ResetPipes(); | ||
| 146 | AudioPipeWriteStructAddresses(); | ||
| 147 | dsp_state = DspState::On; | ||
| 148 | break; | ||
| 149 | case StateChange::Sleep: | ||
| 150 | LOG_INFO(Audio_DSP, "Application has requested sleep of DSP hardware"); | ||
| 151 | UNIMPLEMENTED(); | ||
| 152 | dsp_state = DspState::Sleeping; | ||
| 153 | break; | ||
| 154 | default: | ||
| 155 | LOG_ERROR(Audio_DSP, | ||
| 156 | "Application has requested unknown state transition of DSP hardware %hhu", | ||
| 157 | buffer[0]); | ||
| 158 | dsp_state = DspState::Off; | ||
| 159 | break; | ||
| 160 | } | ||
| 161 | |||
| 162 | return; | ||
| 163 | } | ||
| 164 | default: | ||
| 165 | LOG_CRITICAL(Audio_DSP, "pipe_number = %zu unimplemented", | ||
| 166 | static_cast<size_t>(pipe_number)); | ||
| 167 | UNIMPLEMENTED(); | ||
| 168 | return; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | DspState GetDspState() { | ||
| 173 | return dsp_state; | ||
| 174 | } | ||
| 175 | |||
| 176 | } // namespace HLE | ||
| 177 | } // namespace DSP | ||
diff --git a/src/audio_core/hle/pipe.h b/src/audio_core/hle/pipe.h deleted file mode 100644 index ac053c029..000000000 --- a/src/audio_core/hle/pipe.h +++ /dev/null | |||
| @@ -1,63 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <cstddef> | ||
| 8 | #include <vector> | ||
| 9 | #include "common/common_types.h" | ||
| 10 | |||
| 11 | namespace DSP { | ||
| 12 | namespace HLE { | ||
| 13 | |||
| 14 | /// Reset the pipes by setting pipe positions back to the beginning. | ||
| 15 | void ResetPipes(); | ||
| 16 | |||
| 17 | enum class DspPipe { | ||
| 18 | Debug = 0, | ||
| 19 | Dma = 1, | ||
| 20 | Audio = 2, | ||
| 21 | Binary = 3, | ||
| 22 | }; | ||
| 23 | constexpr size_t NUM_DSP_PIPE = 8; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * Reads `length` bytes from the DSP pipe identified with `pipe_number`. | ||
| 27 | * @note Can read up to the maximum value of a u16 in bytes (65,535). | ||
| 28 | * @note IF an error is encoutered with either an invalid `pipe_number` or `length` value, an empty | ||
| 29 | * vector will be returned. | ||
| 30 | * @note IF `length` is set to 0, an empty vector will be returned. | ||
| 31 | * @note IF `length` is greater than the amount of data available, this function will only read the | ||
| 32 | * available amount. | ||
| 33 | * @param pipe_number a `DspPipe` | ||
| 34 | * @param length the number of bytes to read. The max is 65,535 (max of u16). | ||
| 35 | * @returns a vector of bytes from the specified pipe. On error, will be empty. | ||
| 36 | */ | ||
| 37 | std::vector<u8> PipeRead(DspPipe pipe_number, u32 length); | ||
| 38 | |||
| 39 | /** | ||
| 40 | * How much data is left in pipe | ||
| 41 | * @param pipe_number The Pipe ID | ||
| 42 | * @return The amount of data remaning in the pipe. This is the maximum length PipeRead will return. | ||
| 43 | */ | ||
| 44 | size_t GetPipeReadableSize(DspPipe pipe_number); | ||
| 45 | |||
| 46 | /** | ||
| 47 | * Write to a DSP pipe. | ||
| 48 | * @param pipe_number The Pipe ID | ||
| 49 | * @param buffer The data to write to the pipe. | ||
| 50 | */ | ||
| 51 | void PipeWrite(DspPipe pipe_number, const std::vector<u8>& buffer); | ||
| 52 | |||
| 53 | enum class DspState { | ||
| 54 | Off, | ||
| 55 | On, | ||
| 56 | Sleeping, | ||
| 57 | }; | ||
| 58 | |||
| 59 | /// Get the state of the DSP | ||
| 60 | DspState GetDspState(); | ||
| 61 | |||
| 62 | } // namespace HLE | ||
| 63 | } // namespace DSP | ||
diff --git a/src/audio_core/hle/source.cpp b/src/audio_core/hle/source.cpp deleted file mode 100644 index c12287700..000000000 --- a/src/audio_core/hle/source.cpp +++ /dev/null | |||
| @@ -1,349 +0,0 @@ | |||
| 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 <algorithm> | ||
| 6 | #include <array> | ||
| 7 | #include "audio_core/codec.h" | ||
| 8 | #include "audio_core/hle/common.h" | ||
| 9 | #include "audio_core/hle/source.h" | ||
| 10 | #include "audio_core/interpolate.h" | ||
| 11 | #include "common/assert.h" | ||
| 12 | #include "common/logging/log.h" | ||
| 13 | #include "core/memory.h" | ||
| 14 | |||
| 15 | namespace DSP { | ||
| 16 | namespace HLE { | ||
| 17 | |||
| 18 | SourceStatus::Status Source::Tick(SourceConfiguration::Configuration& config, | ||
| 19 | const s16_le (&adpcm_coeffs)[16]) { | ||
| 20 | ParseConfig(config, adpcm_coeffs); | ||
| 21 | |||
| 22 | if (state.enabled) { | ||
| 23 | GenerateFrame(); | ||
| 24 | } | ||
| 25 | |||
| 26 | return GetCurrentStatus(); | ||
| 27 | } | ||
| 28 | |||
| 29 | void Source::MixInto(QuadFrame32& dest, size_t intermediate_mix_id) const { | ||
| 30 | if (!state.enabled) | ||
| 31 | return; | ||
| 32 | |||
| 33 | const std::array<float, 4>& gains = state.gain.at(intermediate_mix_id); | ||
| 34 | for (size_t samplei = 0; samplei < samples_per_frame; samplei++) { | ||
| 35 | // Conversion from stereo (current_frame) to quadraphonic (dest) occurs here. | ||
| 36 | dest[samplei][0] += static_cast<s32>(gains[0] * current_frame[samplei][0]); | ||
| 37 | dest[samplei][1] += static_cast<s32>(gains[1] * current_frame[samplei][1]); | ||
| 38 | dest[samplei][2] += static_cast<s32>(gains[2] * current_frame[samplei][0]); | ||
| 39 | dest[samplei][3] += static_cast<s32>(gains[3] * current_frame[samplei][1]); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | void Source::Reset() { | ||
| 44 | current_frame.fill({}); | ||
| 45 | state = {}; | ||
| 46 | } | ||
| 47 | |||
| 48 | void Source::ParseConfig(SourceConfiguration::Configuration& config, | ||
| 49 | const s16_le (&adpcm_coeffs)[16]) { | ||
| 50 | if (!config.dirty_raw) { | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | if (config.reset_flag) { | ||
| 55 | config.reset_flag.Assign(0); | ||
| 56 | Reset(); | ||
| 57 | LOG_TRACE(Audio_DSP, "source_id=%zu reset", source_id); | ||
| 58 | } | ||
| 59 | |||
| 60 | if (config.partial_reset_flag) { | ||
| 61 | config.partial_reset_flag.Assign(0); | ||
| 62 | state.input_queue = std::priority_queue<Buffer, std::vector<Buffer>, BufferOrder>{}; | ||
| 63 | LOG_TRACE(Audio_DSP, "source_id=%zu partial_reset", source_id); | ||
| 64 | } | ||
| 65 | |||
| 66 | if (config.enable_dirty) { | ||
| 67 | config.enable_dirty.Assign(0); | ||
| 68 | state.enabled = config.enable != 0; | ||
| 69 | LOG_TRACE(Audio_DSP, "source_id=%zu enable=%d", source_id, state.enabled); | ||
| 70 | } | ||
| 71 | |||
| 72 | if (config.sync_dirty) { | ||
| 73 | config.sync_dirty.Assign(0); | ||
| 74 | state.sync = config.sync; | ||
| 75 | LOG_TRACE(Audio_DSP, "source_id=%zu sync=%u", source_id, state.sync); | ||
| 76 | } | ||
| 77 | |||
| 78 | if (config.rate_multiplier_dirty) { | ||
| 79 | config.rate_multiplier_dirty.Assign(0); | ||
| 80 | state.rate_multiplier = config.rate_multiplier; | ||
| 81 | LOG_TRACE(Audio_DSP, "source_id=%zu rate=%f", source_id, state.rate_multiplier); | ||
| 82 | |||
| 83 | if (state.rate_multiplier <= 0) { | ||
| 84 | LOG_ERROR(Audio_DSP, "Was given an invalid rate multiplier: source_id=%zu rate=%f", | ||
| 85 | source_id, state.rate_multiplier); | ||
| 86 | state.rate_multiplier = 1.0f; | ||
| 87 | // Note: Actual firmware starts producing garbage if this occurs. | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | if (config.adpcm_coefficients_dirty) { | ||
| 92 | config.adpcm_coefficients_dirty.Assign(0); | ||
| 93 | std::transform(adpcm_coeffs, adpcm_coeffs + state.adpcm_coeffs.size(), | ||
| 94 | state.adpcm_coeffs.begin(), | ||
| 95 | [](const auto& coeff) { return static_cast<s16>(coeff); }); | ||
| 96 | LOG_TRACE(Audio_DSP, "source_id=%zu adpcm update", source_id); | ||
| 97 | } | ||
| 98 | |||
| 99 | if (config.gain_0_dirty) { | ||
| 100 | config.gain_0_dirty.Assign(0); | ||
| 101 | std::transform(config.gain[0], config.gain[0] + state.gain[0].size(), state.gain[0].begin(), | ||
| 102 | [](const auto& coeff) { return static_cast<float>(coeff); }); | ||
| 103 | LOG_TRACE(Audio_DSP, "source_id=%zu gain 0 update", source_id); | ||
| 104 | } | ||
| 105 | |||
| 106 | if (config.gain_1_dirty) { | ||
| 107 | config.gain_1_dirty.Assign(0); | ||
| 108 | std::transform(config.gain[1], config.gain[1] + state.gain[1].size(), state.gain[1].begin(), | ||
| 109 | [](const auto& coeff) { return static_cast<float>(coeff); }); | ||
| 110 | LOG_TRACE(Audio_DSP, "source_id=%zu gain 1 update", source_id); | ||
| 111 | } | ||
| 112 | |||
| 113 | if (config.gain_2_dirty) { | ||
| 114 | config.gain_2_dirty.Assign(0); | ||
| 115 | std::transform(config.gain[2], config.gain[2] + state.gain[2].size(), state.gain[2].begin(), | ||
| 116 | [](const auto& coeff) { return static_cast<float>(coeff); }); | ||
| 117 | LOG_TRACE(Audio_DSP, "source_id=%zu gain 2 update", source_id); | ||
| 118 | } | ||
| 119 | |||
| 120 | if (config.filters_enabled_dirty) { | ||
| 121 | config.filters_enabled_dirty.Assign(0); | ||
| 122 | state.filters.Enable(config.simple_filter_enabled.ToBool(), | ||
| 123 | config.biquad_filter_enabled.ToBool()); | ||
| 124 | LOG_TRACE(Audio_DSP, "source_id=%zu enable_simple=%hu enable_biquad=%hu", source_id, | ||
| 125 | config.simple_filter_enabled.Value(), config.biquad_filter_enabled.Value()); | ||
| 126 | } | ||
| 127 | |||
| 128 | if (config.simple_filter_dirty) { | ||
| 129 | config.simple_filter_dirty.Assign(0); | ||
| 130 | state.filters.Configure(config.simple_filter); | ||
| 131 | LOG_TRACE(Audio_DSP, "source_id=%zu simple filter update", source_id); | ||
| 132 | } | ||
| 133 | |||
| 134 | if (config.biquad_filter_dirty) { | ||
| 135 | config.biquad_filter_dirty.Assign(0); | ||
| 136 | state.filters.Configure(config.biquad_filter); | ||
| 137 | LOG_TRACE(Audio_DSP, "source_id=%zu biquad filter update", source_id); | ||
| 138 | } | ||
| 139 | |||
| 140 | if (config.interpolation_dirty) { | ||
| 141 | config.interpolation_dirty.Assign(0); | ||
| 142 | state.interpolation_mode = config.interpolation_mode; | ||
| 143 | LOG_TRACE(Audio_DSP, "source_id=%zu interpolation_mode=%zu", source_id, | ||
| 144 | static_cast<size_t>(state.interpolation_mode)); | ||
| 145 | } | ||
| 146 | |||
| 147 | if (config.format_dirty || config.embedded_buffer_dirty) { | ||
| 148 | config.format_dirty.Assign(0); | ||
| 149 | state.format = config.format; | ||
| 150 | LOG_TRACE(Audio_DSP, "source_id=%zu format=%zu", source_id, | ||
| 151 | static_cast<size_t>(state.format)); | ||
| 152 | } | ||
| 153 | |||
| 154 | if (config.mono_or_stereo_dirty || config.embedded_buffer_dirty) { | ||
| 155 | config.mono_or_stereo_dirty.Assign(0); | ||
| 156 | state.mono_or_stereo = config.mono_or_stereo; | ||
| 157 | LOG_TRACE(Audio_DSP, "source_id=%zu mono_or_stereo=%zu", source_id, | ||
| 158 | static_cast<size_t>(state.mono_or_stereo)); | ||
| 159 | } | ||
| 160 | |||
| 161 | u32_dsp play_position = {}; | ||
| 162 | if (config.play_position_dirty && config.play_position != 0) { | ||
| 163 | config.play_position_dirty.Assign(0); | ||
| 164 | play_position = config.play_position; | ||
| 165 | // play_position applies only to the embedded buffer, and defaults to 0 w/o a dirty bit | ||
| 166 | // This will be the starting sample for the first time the buffer is played. | ||
| 167 | } | ||
| 168 | |||
| 169 | if (config.embedded_buffer_dirty) { | ||
| 170 | config.embedded_buffer_dirty.Assign(0); | ||
| 171 | state.input_queue.emplace(Buffer{ | ||
| 172 | config.physical_address, | ||
| 173 | config.length, | ||
| 174 | static_cast<u8>(config.adpcm_ps), | ||
| 175 | {config.adpcm_yn[0], config.adpcm_yn[1]}, | ||
| 176 | config.adpcm_dirty.ToBool(), | ||
| 177 | config.is_looping.ToBool(), | ||
| 178 | config.buffer_id, | ||
| 179 | state.mono_or_stereo, | ||
| 180 | state.format, | ||
| 181 | false, | ||
| 182 | play_position, | ||
| 183 | false, | ||
| 184 | }); | ||
| 185 | LOG_TRACE(Audio_DSP, "enqueuing embedded addr=0x%08x len=%u id=%hu start=%u", | ||
| 186 | config.physical_address, config.length, config.buffer_id, | ||
| 187 | static_cast<u32>(config.play_position)); | ||
| 188 | } | ||
| 189 | |||
| 190 | if (config.loop_related_dirty && config.loop_related != 0) { | ||
| 191 | config.loop_related_dirty.Assign(0); | ||
| 192 | LOG_WARNING(Audio_DSP, "Unhandled complex loop with loop_related=0x%08x", | ||
| 193 | static_cast<u32>(config.loop_related)); | ||
| 194 | } | ||
| 195 | |||
| 196 | if (config.buffer_queue_dirty) { | ||
| 197 | config.buffer_queue_dirty.Assign(0); | ||
| 198 | for (size_t i = 0; i < 4; i++) { | ||
| 199 | if (config.buffers_dirty & (1 << i)) { | ||
| 200 | const auto& b = config.buffers[i]; | ||
| 201 | state.input_queue.emplace(Buffer{ | ||
| 202 | b.physical_address, | ||
| 203 | b.length, | ||
| 204 | static_cast<u8>(b.adpcm_ps), | ||
| 205 | {b.adpcm_yn[0], b.adpcm_yn[1]}, | ||
| 206 | b.adpcm_dirty != 0, | ||
| 207 | b.is_looping != 0, | ||
| 208 | b.buffer_id, | ||
| 209 | state.mono_or_stereo, | ||
| 210 | state.format, | ||
| 211 | true, | ||
| 212 | {}, // 0 in u32_dsp | ||
| 213 | false, | ||
| 214 | }); | ||
| 215 | LOG_TRACE(Audio_DSP, "enqueuing queued %zu addr=0x%08x len=%u id=%hu", i, | ||
| 216 | b.physical_address, b.length, b.buffer_id); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | config.buffers_dirty = 0; | ||
| 220 | } | ||
| 221 | |||
| 222 | if (config.dirty_raw) { | ||
| 223 | LOG_DEBUG(Audio_DSP, "source_id=%zu remaining_dirty=%x", source_id, config.dirty_raw); | ||
| 224 | } | ||
| 225 | |||
| 226 | config.dirty_raw = 0; | ||
| 227 | } | ||
| 228 | |||
| 229 | void Source::GenerateFrame() { | ||
| 230 | current_frame.fill({}); | ||
| 231 | |||
| 232 | if (state.current_buffer.empty() && !DequeueBuffer()) { | ||
| 233 | state.enabled = false; | ||
| 234 | state.buffer_update = true; | ||
| 235 | state.current_buffer_id = 0; | ||
| 236 | return; | ||
| 237 | } | ||
| 238 | |||
| 239 | size_t frame_position = 0; | ||
| 240 | |||
| 241 | state.current_sample_number = state.next_sample_number; | ||
| 242 | while (frame_position < current_frame.size()) { | ||
| 243 | if (state.current_buffer.empty() && !DequeueBuffer()) { | ||
| 244 | break; | ||
| 245 | } | ||
| 246 | |||
| 247 | switch (state.interpolation_mode) { | ||
| 248 | case InterpolationMode::None: | ||
| 249 | AudioInterp::None(state.interp_state, state.current_buffer, state.rate_multiplier, | ||
| 250 | current_frame, frame_position); | ||
| 251 | break; | ||
| 252 | case InterpolationMode::Linear: | ||
| 253 | AudioInterp::Linear(state.interp_state, state.current_buffer, state.rate_multiplier, | ||
| 254 | current_frame, frame_position); | ||
| 255 | break; | ||
| 256 | case InterpolationMode::Polyphase: | ||
| 257 | // TODO(merry): Implement polyphase interpolation | ||
| 258 | LOG_DEBUG(Audio_DSP, "Polyphase interpolation unimplemented; falling back to linear"); | ||
| 259 | AudioInterp::Linear(state.interp_state, state.current_buffer, state.rate_multiplier, | ||
| 260 | current_frame, frame_position); | ||
| 261 | break; | ||
| 262 | default: | ||
| 263 | UNIMPLEMENTED(); | ||
| 264 | break; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | state.next_sample_number += static_cast<u32>(frame_position); | ||
| 268 | |||
| 269 | state.filters.ProcessFrame(current_frame); | ||
| 270 | } | ||
| 271 | |||
| 272 | bool Source::DequeueBuffer() { | ||
| 273 | ASSERT_MSG(state.current_buffer.empty(), | ||
| 274 | "Shouldn't dequeue; we still have data in current_buffer"); | ||
| 275 | |||
| 276 | if (state.input_queue.empty()) | ||
| 277 | return false; | ||
| 278 | |||
| 279 | Buffer buf = state.input_queue.top(); | ||
| 280 | |||
| 281 | // if we're in a loop, the current sound keeps playing afterwards, so leave the queue alone | ||
| 282 | if (!buf.is_looping) { | ||
| 283 | state.input_queue.pop(); | ||
| 284 | } | ||
| 285 | |||
| 286 | if (buf.adpcm_dirty) { | ||
| 287 | state.adpcm_state.yn1 = buf.adpcm_yn[0]; | ||
| 288 | state.adpcm_state.yn2 = buf.adpcm_yn[1]; | ||
| 289 | } | ||
| 290 | |||
| 291 | const u8* const memory = Memory::GetPhysicalPointer(buf.physical_address); | ||
| 292 | if (memory) { | ||
| 293 | const unsigned num_channels = buf.mono_or_stereo == MonoOrStereo::Stereo ? 2 : 1; | ||
| 294 | switch (buf.format) { | ||
| 295 | case Format::PCM8: | ||
| 296 | state.current_buffer = Codec::DecodePCM8(num_channels, memory, buf.length); | ||
| 297 | break; | ||
| 298 | case Format::PCM16: | ||
| 299 | state.current_buffer = Codec::DecodePCM16(num_channels, memory, buf.length); | ||
| 300 | break; | ||
| 301 | case Format::ADPCM: | ||
| 302 | DEBUG_ASSERT(num_channels == 1); | ||
| 303 | state.current_buffer = | ||
| 304 | Codec::DecodeADPCM(memory, buf.length, state.adpcm_coeffs, state.adpcm_state); | ||
| 305 | break; | ||
| 306 | default: | ||
| 307 | UNIMPLEMENTED(); | ||
| 308 | break; | ||
| 309 | } | ||
| 310 | } else { | ||
| 311 | LOG_WARNING(Audio_DSP, | ||
| 312 | "source_id=%zu buffer_id=%hu length=%u: Invalid physical address 0x%08X", | ||
| 313 | source_id, buf.buffer_id, buf.length, buf.physical_address); | ||
| 314 | state.current_buffer.clear(); | ||
| 315 | return true; | ||
| 316 | } | ||
| 317 | |||
| 318 | // the first playthrough starts at play_position, loops start at the beginning of the buffer | ||
| 319 | state.current_sample_number = (!buf.has_played) ? buf.play_position : 0; | ||
| 320 | state.next_sample_number = state.current_sample_number; | ||
| 321 | state.current_buffer_id = buf.buffer_id; | ||
| 322 | state.buffer_update = buf.from_queue && !buf.has_played; | ||
| 323 | |||
| 324 | buf.has_played = true; | ||
| 325 | |||
| 326 | LOG_TRACE(Audio_DSP, "source_id=%zu buffer_id=%hu from_queue=%s current_buffer.size()=%zu", | ||
| 327 | source_id, buf.buffer_id, buf.from_queue ? "true" : "false", | ||
| 328 | state.current_buffer.size()); | ||
| 329 | return true; | ||
| 330 | } | ||
| 331 | |||
| 332 | SourceStatus::Status Source::GetCurrentStatus() { | ||
| 333 | SourceStatus::Status ret; | ||
| 334 | |||
| 335 | // Applications depend on the correct emulation of | ||
| 336 | // current_buffer_id_dirty and current_buffer_id to synchronise | ||
| 337 | // audio with video. | ||
| 338 | ret.is_enabled = state.enabled; | ||
| 339 | ret.current_buffer_id_dirty = state.buffer_update ? 1 : 0; | ||
| 340 | state.buffer_update = false; | ||
| 341 | ret.current_buffer_id = state.current_buffer_id; | ||
| 342 | ret.buffer_position = state.current_sample_number; | ||
| 343 | ret.sync = state.sync; | ||
| 344 | |||
| 345 | return ret; | ||
| 346 | } | ||
| 347 | |||
| 348 | } // namespace HLE | ||
| 349 | } // namespace DSP | ||
diff --git a/src/audio_core/hle/source.h b/src/audio_core/hle/source.h deleted file mode 100644 index c4d2debc2..000000000 --- a/src/audio_core/hle/source.h +++ /dev/null | |||
| @@ -1,149 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <queue> | ||
| 9 | #include <vector> | ||
| 10 | #include "audio_core/codec.h" | ||
| 11 | #include "audio_core/hle/common.h" | ||
| 12 | #include "audio_core/hle/dsp.h" | ||
| 13 | #include "audio_core/hle/filter.h" | ||
| 14 | #include "audio_core/interpolate.h" | ||
| 15 | #include "common/common_types.h" | ||
| 16 | |||
| 17 | namespace DSP { | ||
| 18 | namespace HLE { | ||
| 19 | |||
| 20 | /** | ||
| 21 | * This module performs: | ||
| 22 | * - Buffer management | ||
| 23 | * - Decoding of buffers | ||
| 24 | * - Buffer resampling and interpolation | ||
| 25 | * - Per-source filtering (SimpleFilter, BiquadFilter) | ||
| 26 | * - Per-source gain | ||
| 27 | * - Other per-source processing | ||
| 28 | */ | ||
| 29 | class Source final { | ||
| 30 | public: | ||
| 31 | explicit Source(size_t source_id_) : source_id(source_id_) { | ||
| 32 | Reset(); | ||
| 33 | } | ||
| 34 | |||
| 35 | /// Resets internal state. | ||
| 36 | void Reset(); | ||
| 37 | |||
| 38 | /** | ||
| 39 | * This is called once every audio frame. This performs per-source processing every frame. | ||
| 40 | * @param config The new configuration we've got for this Source from the application. | ||
| 41 | * @param adpcm_coeffs ADPCM coefficients to use if config tells us to use them (may contain | ||
| 42 | * invalid values otherwise). | ||
| 43 | * @return The current status of this Source. This is given back to the emulated application via | ||
| 44 | * SharedMemory. | ||
| 45 | */ | ||
| 46 | SourceStatus::Status Tick(SourceConfiguration::Configuration& config, | ||
| 47 | const s16_le (&adpcm_coeffs)[16]); | ||
| 48 | |||
| 49 | /** | ||
| 50 | * Mix this source's output into dest, using the gains for the `intermediate_mix_id`-th | ||
| 51 | * intermediate mixer. | ||
| 52 | * @param dest The QuadFrame32 to mix into. | ||
| 53 | * @param intermediate_mix_id The id of the intermediate mix whose gains we are using. | ||
| 54 | */ | ||
| 55 | void MixInto(QuadFrame32& dest, size_t intermediate_mix_id) const; | ||
| 56 | |||
| 57 | private: | ||
| 58 | const size_t source_id; | ||
| 59 | StereoFrame16 current_frame; | ||
| 60 | |||
| 61 | using Format = SourceConfiguration::Configuration::Format; | ||
| 62 | using InterpolationMode = SourceConfiguration::Configuration::InterpolationMode; | ||
| 63 | using MonoOrStereo = SourceConfiguration::Configuration::MonoOrStereo; | ||
| 64 | |||
| 65 | /// Internal representation of a buffer for our buffer queue | ||
| 66 | struct Buffer { | ||
| 67 | PAddr physical_address; | ||
| 68 | u32 length; | ||
| 69 | u8 adpcm_ps; | ||
| 70 | std::array<u16, 2> adpcm_yn; | ||
| 71 | bool adpcm_dirty; | ||
| 72 | bool is_looping; | ||
| 73 | u16 buffer_id; | ||
| 74 | |||
| 75 | MonoOrStereo mono_or_stereo; | ||
| 76 | Format format; | ||
| 77 | |||
| 78 | bool from_queue; | ||
| 79 | u32_dsp play_position; // = 0; | ||
| 80 | bool has_played; // = false; | ||
| 81 | }; | ||
| 82 | |||
| 83 | struct BufferOrder { | ||
| 84 | bool operator()(const Buffer& a, const Buffer& b) const { | ||
| 85 | // Lower buffer_id comes first. | ||
| 86 | return a.buffer_id > b.buffer_id; | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | struct { | ||
| 91 | |||
| 92 | // State variables | ||
| 93 | |||
| 94 | bool enabled = false; | ||
| 95 | u16 sync = 0; | ||
| 96 | |||
| 97 | // Mixing | ||
| 98 | |||
| 99 | std::array<std::array<float, 4>, 3> gain = {}; | ||
| 100 | |||
| 101 | // Buffer queue | ||
| 102 | |||
| 103 | std::priority_queue<Buffer, std::vector<Buffer>, BufferOrder> input_queue; | ||
| 104 | MonoOrStereo mono_or_stereo = MonoOrStereo::Mono; | ||
| 105 | Format format = Format::ADPCM; | ||
| 106 | |||
| 107 | // Current buffer | ||
| 108 | |||
| 109 | u32 current_sample_number = 0; | ||
| 110 | u32 next_sample_number = 0; | ||
| 111 | AudioInterp::StereoBuffer16 current_buffer; | ||
| 112 | |||
| 113 | // buffer_id state | ||
| 114 | |||
| 115 | bool buffer_update = false; | ||
| 116 | u32 current_buffer_id = 0; | ||
| 117 | |||
| 118 | // Decoding state | ||
| 119 | |||
| 120 | std::array<s16, 16> adpcm_coeffs = {}; | ||
| 121 | Codec::ADPCMState adpcm_state = {}; | ||
| 122 | |||
| 123 | // Resampling state | ||
| 124 | |||
| 125 | float rate_multiplier = 1.0; | ||
| 126 | InterpolationMode interpolation_mode = InterpolationMode::Polyphase; | ||
| 127 | AudioInterp::State interp_state = {}; | ||
| 128 | |||
| 129 | // Filter state | ||
| 130 | |||
| 131 | SourceFilters filters; | ||
| 132 | |||
| 133 | } state; | ||
| 134 | |||
| 135 | // Internal functions | ||
| 136 | |||
| 137 | /// INTERNAL: Update our internal state based on the current config. | ||
| 138 | void ParseConfig(SourceConfiguration::Configuration& config, const s16_le (&adpcm_coeffs)[16]); | ||
| 139 | /// INTERNAL: Generate the current audio output for this frame based on our internal state. | ||
| 140 | void GenerateFrame(); | ||
| 141 | /// INTERNAL: Dequeues a buffer and does preprocessing on it (decoding, resampling). Puts it | ||
| 142 | /// into current_buffer. | ||
| 143 | bool DequeueBuffer(); | ||
| 144 | /// INTERNAL: Generates a SourceStatus::Status based on our internal state. | ||
| 145 | SourceStatus::Status GetCurrentStatus(); | ||
| 146 | }; | ||
| 147 | |||
| 148 | } // namespace HLE | ||
| 149 | } // namespace DSP | ||
diff --git a/src/audio_core/interpolate.cpp b/src/audio_core/interpolate.cpp deleted file mode 100644 index 83573d772..000000000 --- a/src/audio_core/interpolate.cpp +++ /dev/null | |||
| @@ -1,76 +0,0 @@ | |||
| 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 "audio_core/interpolate.h" | ||
| 6 | #include "common/assert.h" | ||
| 7 | #include "common/math_util.h" | ||
| 8 | |||
| 9 | namespace AudioInterp { | ||
| 10 | |||
| 11 | // Calculations are done in fixed point with 24 fractional bits. | ||
| 12 | // (This is not verified. This was chosen for minimal error.) | ||
| 13 | constexpr u64 scale_factor = 1 << 24; | ||
| 14 | constexpr u64 scale_mask = scale_factor - 1; | ||
| 15 | |||
| 16 | /// Here we step over the input in steps of rate, until we consume all of the input. | ||
| 17 | /// Three adjacent samples are passed to fn each step. | ||
| 18 | template <typename Function> | ||
| 19 | static void StepOverSamples(State& state, StereoBuffer16& input, float rate, | ||
| 20 | DSP::HLE::StereoFrame16& output, size_t& outputi, Function fn) { | ||
| 21 | ASSERT(rate > 0); | ||
| 22 | |||
| 23 | if (input.empty()) | ||
| 24 | return; | ||
| 25 | |||
| 26 | input.insert(input.begin(), {state.xn2, state.xn1}); | ||
| 27 | |||
| 28 | const u64 step_size = static_cast<u64>(rate * scale_factor); | ||
| 29 | u64 fposition = state.fposition; | ||
| 30 | size_t inputi = 0; | ||
| 31 | |||
| 32 | while (outputi < output.size()) { | ||
| 33 | inputi = static_cast<size_t>(fposition / scale_factor); | ||
| 34 | |||
| 35 | if (inputi + 2 >= input.size()) { | ||
| 36 | inputi = input.size() - 2; | ||
| 37 | break; | ||
| 38 | } | ||
| 39 | |||
| 40 | u64 fraction = fposition & scale_mask; | ||
| 41 | output[outputi++] = fn(fraction, input[inputi], input[inputi + 1], input[inputi + 2]); | ||
| 42 | |||
| 43 | fposition += step_size; | ||
| 44 | } | ||
| 45 | |||
| 46 | state.xn2 = input[inputi]; | ||
| 47 | state.xn1 = input[inputi + 1]; | ||
| 48 | state.fposition = fposition - inputi * scale_factor; | ||
| 49 | |||
| 50 | input.erase(input.begin(), std::next(input.begin(), inputi + 2)); | ||
| 51 | } | ||
| 52 | |||
| 53 | void None(State& state, StereoBuffer16& input, float rate, DSP::HLE::StereoFrame16& output, | ||
| 54 | size_t& outputi) { | ||
| 55 | StepOverSamples( | ||
| 56 | state, input, rate, output, outputi, | ||
| 57 | [](u64 fraction, const auto& x0, const auto& x1, const auto& x2) { return x0; }); | ||
| 58 | } | ||
| 59 | |||
| 60 | void Linear(State& state, StereoBuffer16& input, float rate, DSP::HLE::StereoFrame16& output, | ||
| 61 | size_t& outputi) { | ||
| 62 | // Note on accuracy: Some values that this produces are +/- 1 from the actual firmware. | ||
| 63 | StepOverSamples(state, input, rate, output, outputi, | ||
| 64 | [](u64 fraction, const auto& x0, const auto& x1, const auto& x2) { | ||
| 65 | // This is a saturated subtraction. (Verified by black-box fuzzing.) | ||
| 66 | s64 delta0 = MathUtil::Clamp<s64>(x1[0] - x0[0], -32768, 32767); | ||
| 67 | s64 delta1 = MathUtil::Clamp<s64>(x1[1] - x0[1], -32768, 32767); | ||
| 68 | |||
| 69 | return std::array<s16, 2>{ | ||
| 70 | static_cast<s16>(x0[0] + fraction * delta0 / scale_factor), | ||
| 71 | static_cast<s16>(x0[1] + fraction * delta1 / scale_factor), | ||
| 72 | }; | ||
| 73 | }); | ||
| 74 | } | ||
| 75 | |||
| 76 | } // namespace AudioInterp | ||
diff --git a/src/audio_core/interpolate.h b/src/audio_core/interpolate.h deleted file mode 100644 index 8dff6111a..000000000 --- a/src/audio_core/interpolate.h +++ /dev/null | |||
| @@ -1,49 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <deque> | ||
| 9 | #include "audio_core/hle/common.h" | ||
| 10 | #include "common/common_types.h" | ||
| 11 | |||
| 12 | namespace AudioInterp { | ||
| 13 | |||
| 14 | /// A variable length buffer of signed PCM16 stereo samples. | ||
| 15 | using StereoBuffer16 = std::deque<std::array<s16, 2>>; | ||
| 16 | |||
| 17 | struct State { | ||
| 18 | /// Two historical samples. | ||
| 19 | std::array<s16, 2> xn1 = {}; ///< x[n-1] | ||
| 20 | std::array<s16, 2> xn2 = {}; ///< x[n-2] | ||
| 21 | /// Current fractional position. | ||
| 22 | u64 fposition = 0; | ||
| 23 | }; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * No interpolation. This is equivalent to a zero-order hold. There is a two-sample predelay. | ||
| 27 | * @param state Interpolation state. | ||
| 28 | * @param input Input buffer. | ||
| 29 | * @param rate Stretch factor. Must be a positive non-zero value. | ||
| 30 | * rate > 1.0 performs decimation and rate < 1.0 performs upsampling. | ||
| 31 | * @param output The resampled audio buffer. | ||
| 32 | * @param outputi The index of output to start writing to. | ||
| 33 | */ | ||
| 34 | void None(State& state, StereoBuffer16& input, float rate, DSP::HLE::StereoFrame16& output, | ||
| 35 | size_t& outputi); | ||
| 36 | |||
| 37 | /** | ||
| 38 | * Linear interpolation. This is equivalent to a first-order hold. There is a two-sample predelay. | ||
| 39 | * @param state Interpolation state. | ||
| 40 | * @param input Input buffer. | ||
| 41 | * @param rate Stretch factor. Must be a positive non-zero value. | ||
| 42 | * rate > 1.0 performs decimation and rate < 1.0 performs upsampling. | ||
| 43 | * @param output The resampled audio buffer. | ||
| 44 | * @param outputi The index of output to start writing to. | ||
| 45 | */ | ||
| 46 | void Linear(State& state, StereoBuffer16& input, float rate, DSP::HLE::StereoFrame16& output, | ||
| 47 | size_t& outputi); | ||
| 48 | |||
| 49 | } // namespace AudioInterp | ||
diff --git a/src/audio_core/null_sink.h b/src/audio_core/null_sink.h deleted file mode 100644 index c732926a2..000000000 --- a/src/audio_core/null_sink.h +++ /dev/null | |||
| @@ -1,34 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <cstddef> | ||
| 8 | #include "audio_core/audio_core.h" | ||
| 9 | #include "audio_core/sink.h" | ||
| 10 | |||
| 11 | namespace AudioCore { | ||
| 12 | |||
| 13 | class NullSink final : public Sink { | ||
| 14 | public: | ||
| 15 | ~NullSink() override = default; | ||
| 16 | |||
| 17 | unsigned int GetNativeSampleRate() const override { | ||
| 18 | return native_sample_rate; | ||
| 19 | } | ||
| 20 | |||
| 21 | void EnqueueSamples(const s16*, size_t) override {} | ||
| 22 | |||
| 23 | size_t SamplesInQueue() const override { | ||
| 24 | return 0; | ||
| 25 | } | ||
| 26 | |||
| 27 | void SetDevice(int device_id) override {} | ||
| 28 | |||
| 29 | std::vector<std::string> GetDeviceList() const override { | ||
| 30 | return {}; | ||
| 31 | } | ||
| 32 | }; | ||
| 33 | |||
| 34 | } // namespace AudioCore | ||
diff --git a/src/audio_core/sdl2_sink.cpp b/src/audio_core/sdl2_sink.cpp deleted file mode 100644 index 933c5f16d..000000000 --- a/src/audio_core/sdl2_sink.cpp +++ /dev/null | |||
| @@ -1,147 +0,0 @@ | |||
| 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 <list> | ||
| 6 | #include <numeric> | ||
| 7 | #include <SDL.h> | ||
| 8 | #include "audio_core/audio_core.h" | ||
| 9 | #include "audio_core/sdl2_sink.h" | ||
| 10 | #include "common/assert.h" | ||
| 11 | #include "common/logging/log.h" | ||
| 12 | #include "core/settings.h" | ||
| 13 | |||
| 14 | namespace AudioCore { | ||
| 15 | |||
| 16 | struct SDL2Sink::Impl { | ||
| 17 | unsigned int sample_rate = 0; | ||
| 18 | |||
| 19 | SDL_AudioDeviceID audio_device_id = 0; | ||
| 20 | |||
| 21 | std::list<std::vector<s16>> queue; | ||
| 22 | |||
| 23 | static void Callback(void* impl_, u8* buffer, int buffer_size_in_bytes); | ||
| 24 | }; | ||
| 25 | |||
| 26 | SDL2Sink::SDL2Sink() : impl(std::make_unique<Impl>()) { | ||
| 27 | if (SDL_Init(SDL_INIT_AUDIO) < 0) { | ||
| 28 | LOG_CRITICAL(Audio_Sink, "SDL_Init(SDL_INIT_AUDIO) failed with: %s", SDL_GetError()); | ||
| 29 | impl->audio_device_id = 0; | ||
| 30 | return; | ||
| 31 | } | ||
| 32 | |||
| 33 | SDL_AudioSpec desired_audiospec; | ||
| 34 | SDL_zero(desired_audiospec); | ||
| 35 | desired_audiospec.format = AUDIO_S16; | ||
| 36 | desired_audiospec.channels = 2; | ||
| 37 | desired_audiospec.freq = native_sample_rate; | ||
| 38 | desired_audiospec.samples = 512; | ||
| 39 | desired_audiospec.userdata = impl.get(); | ||
| 40 | desired_audiospec.callback = &Impl::Callback; | ||
| 41 | |||
| 42 | SDL_AudioSpec obtained_audiospec; | ||
| 43 | SDL_zero(obtained_audiospec); | ||
| 44 | |||
| 45 | int device_count = SDL_GetNumAudioDevices(0); | ||
| 46 | device_list.clear(); | ||
| 47 | for (int i = 0; i < device_count; ++i) { | ||
| 48 | device_list.push_back(SDL_GetAudioDeviceName(i, 0)); | ||
| 49 | } | ||
| 50 | |||
| 51 | const char* device = nullptr; | ||
| 52 | |||
| 53 | if (device_count >= 1 && Settings::values.audio_device_id != "auto" && | ||
| 54 | !Settings::values.audio_device_id.empty()) { | ||
| 55 | device = Settings::values.audio_device_id.c_str(); | ||
| 56 | } | ||
| 57 | |||
| 58 | impl->audio_device_id = SDL_OpenAudioDevice(device, false, &desired_audiospec, | ||
| 59 | &obtained_audiospec, SDL_AUDIO_ALLOW_ANY_CHANGE); | ||
| 60 | if (impl->audio_device_id <= 0) { | ||
| 61 | LOG_CRITICAL(Audio_Sink, "SDL_OpenAudioDevice failed with code %d for device \"%s\"", | ||
| 62 | impl->audio_device_id, Settings::values.audio_device_id.c_str()); | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | |||
| 66 | impl->sample_rate = obtained_audiospec.freq; | ||
| 67 | |||
| 68 | // SDL2 audio devices start out paused, unpause it: | ||
| 69 | SDL_PauseAudioDevice(impl->audio_device_id, 0); | ||
| 70 | } | ||
| 71 | |||
| 72 | SDL2Sink::~SDL2Sink() { | ||
| 73 | if (impl->audio_device_id <= 0) | ||
| 74 | return; | ||
| 75 | |||
| 76 | SDL_CloseAudioDevice(impl->audio_device_id); | ||
| 77 | } | ||
| 78 | |||
| 79 | unsigned int SDL2Sink::GetNativeSampleRate() const { | ||
| 80 | if (impl->audio_device_id <= 0) | ||
| 81 | return native_sample_rate; | ||
| 82 | |||
| 83 | return impl->sample_rate; | ||
| 84 | } | ||
| 85 | |||
| 86 | std::vector<std::string> SDL2Sink::GetDeviceList() const { | ||
| 87 | return device_list; | ||
| 88 | } | ||
| 89 | |||
| 90 | void SDL2Sink::EnqueueSamples(const s16* samples, size_t sample_count) { | ||
| 91 | if (impl->audio_device_id <= 0) | ||
| 92 | return; | ||
| 93 | |||
| 94 | SDL_LockAudioDevice(impl->audio_device_id); | ||
| 95 | impl->queue.emplace_back(samples, samples + sample_count * 2); | ||
| 96 | SDL_UnlockAudioDevice(impl->audio_device_id); | ||
| 97 | } | ||
| 98 | |||
| 99 | size_t SDL2Sink::SamplesInQueue() const { | ||
| 100 | if (impl->audio_device_id <= 0) | ||
| 101 | return 0; | ||
| 102 | |||
| 103 | SDL_LockAudioDevice(impl->audio_device_id); | ||
| 104 | |||
| 105 | size_t total_size = std::accumulate(impl->queue.begin(), impl->queue.end(), | ||
| 106 | static_cast<size_t>(0), [](size_t sum, const auto& buffer) { | ||
| 107 | // Division by two because each stereo sample is made of | ||
| 108 | // two s16. | ||
| 109 | return sum + buffer.size() / 2; | ||
| 110 | }); | ||
| 111 | |||
| 112 | SDL_UnlockAudioDevice(impl->audio_device_id); | ||
| 113 | |||
| 114 | return total_size; | ||
| 115 | } | ||
| 116 | |||
| 117 | void SDL2Sink::SetDevice(int device_id) { | ||
| 118 | this->device_id = device_id; | ||
| 119 | } | ||
| 120 | |||
| 121 | void SDL2Sink::Impl::Callback(void* impl_, u8* buffer, int buffer_size_in_bytes) { | ||
| 122 | Impl* impl = reinterpret_cast<Impl*>(impl_); | ||
| 123 | |||
| 124 | size_t remaining_size = static_cast<size_t>(buffer_size_in_bytes) / | ||
| 125 | sizeof(s16); // Keep track of size in 16-bit increments. | ||
| 126 | |||
| 127 | while (remaining_size > 0 && !impl->queue.empty()) { | ||
| 128 | if (impl->queue.front().size() <= remaining_size) { | ||
| 129 | memcpy(buffer, impl->queue.front().data(), impl->queue.front().size() * sizeof(s16)); | ||
| 130 | buffer += impl->queue.front().size() * sizeof(s16); | ||
| 131 | remaining_size -= impl->queue.front().size(); | ||
| 132 | impl->queue.pop_front(); | ||
| 133 | } else { | ||
| 134 | memcpy(buffer, impl->queue.front().data(), remaining_size * sizeof(s16)); | ||
| 135 | buffer += remaining_size * sizeof(s16); | ||
| 136 | impl->queue.front().erase(impl->queue.front().begin(), | ||
| 137 | impl->queue.front().begin() + remaining_size); | ||
| 138 | remaining_size = 0; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | if (remaining_size > 0) { | ||
| 143 | memset(buffer, 0, remaining_size * sizeof(s16)); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | } // namespace AudioCore | ||
diff --git a/src/audio_core/sdl2_sink.h b/src/audio_core/sdl2_sink.h deleted file mode 100644 index bcc725369..000000000 --- a/src/audio_core/sdl2_sink.h +++ /dev/null | |||
| @@ -1,34 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <cstddef> | ||
| 8 | #include <memory> | ||
| 9 | #include "audio_core/sink.h" | ||
| 10 | |||
| 11 | namespace AudioCore { | ||
| 12 | |||
| 13 | class SDL2Sink final : public Sink { | ||
| 14 | public: | ||
| 15 | SDL2Sink(); | ||
| 16 | ~SDL2Sink() override; | ||
| 17 | |||
| 18 | unsigned int GetNativeSampleRate() const override; | ||
| 19 | |||
| 20 | void EnqueueSamples(const s16* samples, size_t sample_count) override; | ||
| 21 | |||
| 22 | size_t SamplesInQueue() const override; | ||
| 23 | |||
| 24 | std::vector<std::string> GetDeviceList() const override; | ||
| 25 | void SetDevice(int device_id) override; | ||
| 26 | |||
| 27 | private: | ||
| 28 | struct Impl; | ||
| 29 | std::unique_ptr<Impl> impl; | ||
| 30 | int device_id; | ||
| 31 | std::vector<std::string> device_list; | ||
| 32 | }; | ||
| 33 | |||
| 34 | } // namespace AudioCore | ||
diff --git a/src/audio_core/sink.h b/src/audio_core/sink.h deleted file mode 100644 index c69cb2c74..000000000 --- a/src/audio_core/sink.h +++ /dev/null | |||
| @@ -1,45 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <vector> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | |||
| 10 | namespace AudioCore { | ||
| 11 | |||
| 12 | /** | ||
| 13 | * This class is an interface for an audio sink. An audio sink accepts samples in stereo signed | ||
| 14 | * PCM16 format to be output. Sinks *do not* handle resampling and expect the correct sample rate. | ||
| 15 | * They are dumb outputs. | ||
| 16 | */ | ||
| 17 | class Sink { | ||
| 18 | public: | ||
| 19 | virtual ~Sink() = default; | ||
| 20 | |||
| 21 | /// The native rate of this sink. The sink expects to be fed samples that respect this. (Units: | ||
| 22 | /// samples/sec) | ||
| 23 | virtual unsigned int GetNativeSampleRate() const = 0; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * Feed stereo samples to sink. | ||
| 27 | * @param samples Samples in interleaved stereo PCM16 format. | ||
| 28 | * @param sample_count Number of samples. | ||
| 29 | */ | ||
| 30 | virtual void EnqueueSamples(const s16* samples, size_t sample_count) = 0; | ||
| 31 | |||
| 32 | /// Samples enqueued that have not been played yet. | ||
| 33 | virtual std::size_t SamplesInQueue() const = 0; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * Sets the desired output device. | ||
| 37 | * @param device_id ID of the desired device. | ||
| 38 | */ | ||
| 39 | virtual void SetDevice(int device_id) = 0; | ||
| 40 | |||
| 41 | /// Returns the list of available devices. | ||
| 42 | virtual std::vector<std::string> GetDeviceList() const = 0; | ||
| 43 | }; | ||
| 44 | |||
| 45 | } // namespace | ||
diff --git a/src/audio_core/sink_details.cpp b/src/audio_core/sink_details.cpp deleted file mode 100644 index 6972395af..000000000 --- a/src/audio_core/sink_details.cpp +++ /dev/null | |||
| @@ -1,42 +0,0 @@ | |||
| 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 <algorithm> | ||
| 6 | #include <memory> | ||
| 7 | #include <vector> | ||
| 8 | #include "audio_core/null_sink.h" | ||
| 9 | #include "audio_core/sink_details.h" | ||
| 10 | #ifdef HAVE_SDL2 | ||
| 11 | #include "audio_core/sdl2_sink.h" | ||
| 12 | #endif | ||
| 13 | #include "common/logging/log.h" | ||
| 14 | |||
| 15 | namespace AudioCore { | ||
| 16 | |||
| 17 | // g_sink_details is ordered in terms of desirability, with the best choice at the top. | ||
| 18 | const std::vector<SinkDetails> g_sink_details = { | ||
| 19 | #ifdef HAVE_SDL2 | ||
| 20 | {"sdl2", []() { return std::make_unique<SDL2Sink>(); }}, | ||
| 21 | #endif | ||
| 22 | {"null", []() { return std::make_unique<NullSink>(); }}, | ||
| 23 | }; | ||
| 24 | |||
| 25 | const SinkDetails& GetSinkDetails(std::string sink_id) { | ||
| 26 | auto iter = | ||
| 27 | std::find_if(g_sink_details.begin(), g_sink_details.end(), | ||
| 28 | [sink_id](const auto& sink_detail) { return sink_detail.id == sink_id; }); | ||
| 29 | |||
| 30 | if (sink_id == "auto" || iter == g_sink_details.end()) { | ||
| 31 | if (sink_id != "auto") { | ||
| 32 | LOG_ERROR(Audio, "AudioCore::SelectSink given invalid sink_id %s", sink_id.c_str()); | ||
| 33 | } | ||
| 34 | // Auto-select. | ||
| 35 | // g_sink_details is ordered in terms of desirability, with the best choice at the front. | ||
| 36 | iter = g_sink_details.begin(); | ||
| 37 | } | ||
| 38 | |||
| 39 | return *iter; | ||
| 40 | } | ||
| 41 | |||
| 42 | } // namespace AudioCore | ||
diff --git a/src/audio_core/sink_details.h b/src/audio_core/sink_details.h deleted file mode 100644 index 9d3735171..000000000 --- a/src/audio_core/sink_details.h +++ /dev/null | |||
| @@ -1,29 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <functional> | ||
| 8 | #include <memory> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | namespace AudioCore { | ||
| 12 | |||
| 13 | class Sink; | ||
| 14 | |||
| 15 | struct SinkDetails { | ||
| 16 | SinkDetails(const char* id_, std::function<std::unique_ptr<Sink>()> factory_) | ||
| 17 | : id(id_), factory(factory_) {} | ||
| 18 | |||
| 19 | /// Name for this sink. | ||
| 20 | const char* id; | ||
| 21 | /// A method to call to construct an instance of this type of sink. | ||
| 22 | std::function<std::unique_ptr<Sink>()> factory; | ||
| 23 | }; | ||
| 24 | |||
| 25 | extern const std::vector<SinkDetails> g_sink_details; | ||
| 26 | |||
| 27 | const SinkDetails& GetSinkDetails(std::string sink_id); | ||
| 28 | |||
| 29 | } // namespace AudioCore | ||
diff --git a/src/audio_core/time_stretch.cpp b/src/audio_core/time_stretch.cpp deleted file mode 100644 index 437cf9752..000000000 --- a/src/audio_core/time_stretch.cpp +++ /dev/null | |||
| @@ -1,143 +0,0 @@ | |||
| 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 <chrono> | ||
| 6 | #include <cmath> | ||
| 7 | #include <vector> | ||
| 8 | #include <SoundTouch.h> | ||
| 9 | #include "audio_core/audio_core.h" | ||
| 10 | #include "audio_core/time_stretch.h" | ||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "common/logging/log.h" | ||
| 13 | #include "common/math_util.h" | ||
| 14 | |||
| 15 | using steady_clock = std::chrono::steady_clock; | ||
| 16 | |||
| 17 | namespace AudioCore { | ||
| 18 | |||
| 19 | constexpr double MIN_RATIO = 0.1; | ||
| 20 | constexpr double MAX_RATIO = 100.0; | ||
| 21 | |||
| 22 | static double ClampRatio(double ratio) { | ||
| 23 | return MathUtil::Clamp(ratio, MIN_RATIO, MAX_RATIO); | ||
| 24 | } | ||
| 25 | |||
| 26 | constexpr double MIN_DELAY_TIME = 0.05; // Units: seconds | ||
| 27 | constexpr double MAX_DELAY_TIME = 0.25; // Units: seconds | ||
| 28 | constexpr size_t DROP_FRAMES_SAMPLE_DELAY = 16000; // Units: samples | ||
| 29 | |||
| 30 | constexpr double SMOOTHING_FACTOR = 0.007; | ||
| 31 | |||
| 32 | struct TimeStretcher::Impl { | ||
| 33 | soundtouch::SoundTouch soundtouch; | ||
| 34 | |||
| 35 | steady_clock::time_point frame_timer = steady_clock::now(); | ||
| 36 | size_t samples_queued = 0; | ||
| 37 | |||
| 38 | double smoothed_ratio = 1.0; | ||
| 39 | |||
| 40 | double sample_rate = static_cast<double>(native_sample_rate); | ||
| 41 | }; | ||
| 42 | |||
| 43 | std::vector<s16> TimeStretcher::Process(size_t samples_in_queue) { | ||
| 44 | // This is a very simple algorithm without any fancy control theory. It works and is stable. | ||
| 45 | |||
| 46 | double ratio = CalculateCurrentRatio(); | ||
| 47 | ratio = CorrectForUnderAndOverflow(ratio, samples_in_queue); | ||
| 48 | impl->smoothed_ratio = | ||
| 49 | (1.0 - SMOOTHING_FACTOR) * impl->smoothed_ratio + SMOOTHING_FACTOR * ratio; | ||
| 50 | impl->smoothed_ratio = ClampRatio(impl->smoothed_ratio); | ||
| 51 | |||
| 52 | // SoundTouch's tempo definition the inverse of our ratio definition. | ||
| 53 | impl->soundtouch.setTempo(1.0 / impl->smoothed_ratio); | ||
| 54 | |||
| 55 | std::vector<s16> samples = GetSamples(); | ||
| 56 | if (samples_in_queue >= DROP_FRAMES_SAMPLE_DELAY) { | ||
| 57 | samples.clear(); | ||
| 58 | LOG_TRACE(Audio, "Dropping frames!"); | ||
| 59 | } | ||
| 60 | return samples; | ||
| 61 | } | ||
| 62 | |||
| 63 | TimeStretcher::TimeStretcher() : impl(std::make_unique<Impl>()) { | ||
| 64 | impl->soundtouch.setPitch(1.0); | ||
| 65 | impl->soundtouch.setChannels(2); | ||
| 66 | impl->soundtouch.setSampleRate(native_sample_rate); | ||
| 67 | Reset(); | ||
| 68 | } | ||
| 69 | |||
| 70 | TimeStretcher::~TimeStretcher() { | ||
| 71 | impl->soundtouch.clear(); | ||
| 72 | } | ||
| 73 | |||
| 74 | void TimeStretcher::SetOutputSampleRate(unsigned int sample_rate) { | ||
| 75 | impl->sample_rate = static_cast<double>(sample_rate); | ||
| 76 | impl->soundtouch.setRate(static_cast<double>(native_sample_rate) / impl->sample_rate); | ||
| 77 | } | ||
| 78 | |||
| 79 | void TimeStretcher::AddSamples(const s16* buffer, size_t num_samples) { | ||
| 80 | impl->soundtouch.putSamples(buffer, static_cast<uint>(num_samples)); | ||
| 81 | impl->samples_queued += num_samples; | ||
| 82 | } | ||
| 83 | |||
| 84 | void TimeStretcher::Flush() { | ||
| 85 | impl->soundtouch.flush(); | ||
| 86 | } | ||
| 87 | |||
| 88 | void TimeStretcher::Reset() { | ||
| 89 | impl->soundtouch.setTempo(1.0); | ||
| 90 | impl->soundtouch.clear(); | ||
| 91 | impl->smoothed_ratio = 1.0; | ||
| 92 | impl->frame_timer = steady_clock::now(); | ||
| 93 | impl->samples_queued = 0; | ||
| 94 | SetOutputSampleRate(native_sample_rate); | ||
| 95 | } | ||
| 96 | |||
| 97 | double TimeStretcher::CalculateCurrentRatio() { | ||
| 98 | const steady_clock::time_point now = steady_clock::now(); | ||
| 99 | const std::chrono::duration<double> duration = now - impl->frame_timer; | ||
| 100 | |||
| 101 | const double expected_time = | ||
| 102 | static_cast<double>(impl->samples_queued) / static_cast<double>(native_sample_rate); | ||
| 103 | const double actual_time = duration.count(); | ||
| 104 | |||
| 105 | double ratio; | ||
| 106 | if (expected_time != 0) { | ||
| 107 | ratio = ClampRatio(actual_time / expected_time); | ||
| 108 | } else { | ||
| 109 | ratio = impl->smoothed_ratio; | ||
| 110 | } | ||
| 111 | |||
| 112 | impl->frame_timer = now; | ||
| 113 | impl->samples_queued = 0; | ||
| 114 | |||
| 115 | return ratio; | ||
| 116 | } | ||
| 117 | |||
| 118 | double TimeStretcher::CorrectForUnderAndOverflow(double ratio, size_t sample_delay) const { | ||
| 119 | const size_t min_sample_delay = static_cast<size_t>(MIN_DELAY_TIME * impl->sample_rate); | ||
| 120 | const size_t max_sample_delay = static_cast<size_t>(MAX_DELAY_TIME * impl->sample_rate); | ||
| 121 | |||
| 122 | if (sample_delay < min_sample_delay) { | ||
| 123 | // Make the ratio bigger. | ||
| 124 | ratio = ratio > 1.0 ? ratio * ratio : sqrt(ratio); | ||
| 125 | } else if (sample_delay > max_sample_delay) { | ||
| 126 | // Make the ratio smaller. | ||
| 127 | ratio = ratio > 1.0 ? sqrt(ratio) : ratio * ratio; | ||
| 128 | } | ||
| 129 | |||
| 130 | return ClampRatio(ratio); | ||
| 131 | } | ||
| 132 | |||
| 133 | std::vector<s16> TimeStretcher::GetSamples() { | ||
| 134 | uint available = impl->soundtouch.numSamples(); | ||
| 135 | |||
| 136 | std::vector<s16> output(static_cast<size_t>(available) * 2); | ||
| 137 | |||
| 138 | impl->soundtouch.receiveSamples(output.data(), available); | ||
| 139 | |||
| 140 | return output; | ||
| 141 | } | ||
| 142 | |||
| 143 | } // namespace AudioCore | ||
diff --git a/src/audio_core/time_stretch.h b/src/audio_core/time_stretch.h deleted file mode 100644 index c98b16705..000000000 --- a/src/audio_core/time_stretch.h +++ /dev/null | |||
| @@ -1,60 +0,0 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <cstddef> | ||
| 8 | #include <memory> | ||
| 9 | #include <vector> | ||
| 10 | #include "common/common_types.h" | ||
| 11 | |||
| 12 | namespace AudioCore { | ||
| 13 | |||
| 14 | class TimeStretcher final { | ||
| 15 | public: | ||
| 16 | TimeStretcher(); | ||
| 17 | ~TimeStretcher(); | ||
| 18 | |||
| 19 | /** | ||
| 20 | * Set sample rate for the samples that Process returns. | ||
| 21 | * @param sample_rate The sample rate. | ||
| 22 | */ | ||
| 23 | void SetOutputSampleRate(unsigned int sample_rate); | ||
| 24 | |||
| 25 | /** | ||
| 26 | * Add samples to be processed. | ||
| 27 | * @param sample_buffer Buffer of samples in interleaved stereo PCM16 format. | ||
| 28 | * @param num_samples Number of samples. | ||
| 29 | */ | ||
| 30 | void AddSamples(const s16* sample_buffer, size_t num_samples); | ||
| 31 | |||
| 32 | /// Flush audio remaining in internal buffers. | ||
| 33 | void Flush(); | ||
| 34 | |||
| 35 | /// Resets internal state and clears buffers. | ||
| 36 | void Reset(); | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Does audio stretching and produces the time-stretched samples. | ||
| 40 | * Timer calculations use sample_delay to determine how much of a margin we have. | ||
| 41 | * @param sample_delay How many samples are buffered downstream of this module and haven't been | ||
| 42 | * played yet. | ||
| 43 | * @return Samples to play in interleaved stereo PCM16 format. | ||
| 44 | */ | ||
| 45 | std::vector<s16> Process(size_t sample_delay); | ||
| 46 | |||
| 47 | private: | ||
| 48 | struct Impl; | ||
| 49 | std::unique_ptr<Impl> impl; | ||
| 50 | |||
| 51 | /// INTERNAL: ratio = wallclock time / emulated time | ||
| 52 | double CalculateCurrentRatio(); | ||
| 53 | /// INTERNAL: If we have too many or too few samples downstream, nudge ratio in the appropriate | ||
| 54 | /// direction. | ||
| 55 | double CorrectForUnderAndOverflow(double ratio, size_t sample_delay) const; | ||
| 56 | /// INTERNAL: Gets the time-stretched samples from SoundTouch. | ||
| 57 | std::vector<s16> GetSamples(); | ||
| 58 | }; | ||
| 59 | |||
| 60 | } // namespace AudioCore | ||