aboutsummaryrefslogtreecommitdiff
path: root/src/audio_core
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio_core')
-rw-r--r--src/audio_core/CMakeLists.txt5
-rw-r--r--src/audio_core/codec.cpp122
-rw-r--r--src/audio_core/codec.h50
-rw-r--r--src/audio_core/hle/common.h35
-rw-r--r--src/audio_core/hle/dsp.h17
-rw-r--r--src/audio_core/hle/filter.cpp115
-rw-r--r--src/audio_core/hle/filter.h112
7 files changed, 449 insertions, 7 deletions
diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt
index b0d1c7eb6..869da5e83 100644
--- a/src/audio_core/CMakeLists.txt
+++ b/src/audio_core/CMakeLists.txt
@@ -1,12 +1,17 @@
set(SRCS
audio_core.cpp
+ codec.cpp
hle/dsp.cpp
+ hle/filter.cpp
hle/pipe.cpp
)
set(HEADERS
audio_core.h
+ codec.h
+ hle/common.h
hle/dsp.h
+ hle/filter.h
hle/pipe.h
sink.h
)
diff --git a/src/audio_core/codec.cpp b/src/audio_core/codec.cpp
new file mode 100644
index 000000000..ab65514b7
--- /dev/null
+++ b/src/audio_core/codec.cpp
@@ -0,0 +1,122 @@
+// Copyright 2016 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <array>
+#include <cstddef>
+#include <cstring>
+#include <vector>
+
+#include "audio_core/codec.h"
+
+#include "common/assert.h"
+#include "common/common_types.h"
+#include "common/math_util.h"
+
+namespace Codec {
+
+StereoBuffer16 DecodeADPCM(const u8* const data, const size_t sample_count, const std::array<s16, 16>& adpcm_coeff, ADPCMState& state) {
+ // GC-ADPCM with scale factor and variable coefficients.
+ // Frames are 8 bytes long containing 14 samples each.
+ // Samples are 4 bits (one nibble) long.
+
+ constexpr size_t FRAME_LEN = 8;
+ constexpr size_t SAMPLES_PER_FRAME = 14;
+ constexpr std::array<int, 16> SIGNED_NIBBLES {{ 0, 1, 2, 3, 4, 5, 6, 7, -8, -7, -6, -5, -4, -3, -2, -1 }};
+
+ const size_t ret_size = sample_count % 2 == 0 ? sample_count : sample_count + 1; // Ensure multiple of two.
+ StereoBuffer16 ret(ret_size);
+
+ int yn1 = state.yn1,
+ yn2 = state.yn2;
+
+ const size_t NUM_FRAMES = (sample_count + (SAMPLES_PER_FRAME - 1)) / SAMPLES_PER_FRAME; // Round up.
+ for (size_t framei = 0; framei < NUM_FRAMES; framei++) {
+ const int frame_header = data[framei * FRAME_LEN];
+ const int scale = 1 << (frame_header & 0xF);
+ const int idx = (frame_header >> 4) & 0x7;
+
+ // Coefficients are fixed point with 11 bits fractional part.
+ const int coef1 = adpcm_coeff[idx * 2 + 0];
+ const int coef2 = adpcm_coeff[idx * 2 + 1];
+
+ // Decodes an audio sample. One nibble produces one sample.
+ const auto decode_sample = [&](const int nibble) -> s16 {
+ const int xn = nibble * scale;
+ // We first transform everything into 11 bit fixed point, perform the second order digital filter, then transform back.
+ // 0x400 == 0.5 in 11 bit fixed point.
+ // Filter: y[n] = x[n] + 0.5 + c1 * y[n-1] + c2 * y[n-2]
+ int val = ((xn << 11) + 0x400 + coef1 * yn1 + coef2 * yn2) >> 11;
+ // Clamp to output range.
+ val = MathUtil::Clamp(val, -32768, 32767);
+ // Advance output feedback.
+ yn2 = yn1;
+ yn1 = val;
+ return (s16)val;
+ };
+
+ size_t outputi = framei * SAMPLES_PER_FRAME;
+ size_t datai = framei * FRAME_LEN + 1;
+ for (size_t i = 0; i < SAMPLES_PER_FRAME && outputi < sample_count; i += 2) {
+ const s16 sample1 = decode_sample(SIGNED_NIBBLES[data[datai] & 0xF]);
+ ret[outputi].fill(sample1);
+ outputi++;
+
+ const s16 sample2 = decode_sample(SIGNED_NIBBLES[data[datai] >> 4]);
+ ret[outputi].fill(sample2);
+ outputi++;
+
+ datai++;
+ }
+ }
+
+ state.yn1 = yn1;
+ state.yn2 = yn2;
+
+ return ret;
+}
+
+static s16 SignExtendS8(u8 x) {
+ // The data is actually signed PCM8.
+ // We sign extend this to signed PCM16.
+ return static_cast<s16>(static_cast<s8>(x));
+}
+
+StereoBuffer16 DecodePCM8(const unsigned num_channels, const u8* const data, const size_t sample_count) {
+ ASSERT(num_channels == 1 || num_channels == 2);
+
+ StereoBuffer16 ret(sample_count);
+
+ if (num_channels == 1) {
+ for (size_t i = 0; i < sample_count; i++) {
+ ret[i].fill(SignExtendS8(data[i]));
+ }
+ } else {
+ for (size_t i = 0; i < sample_count; i++) {
+ ret[i][0] = SignExtendS8(data[i * 2 + 0]);
+ ret[i][1] = SignExtendS8(data[i * 2 + 1]);
+ }
+ }
+
+ return ret;
+}
+
+StereoBuffer16 DecodePCM16(const unsigned num_channels, const u8* const data, const size_t sample_count) {
+ ASSERT(num_channels == 1 || num_channels == 2);
+
+ StereoBuffer16 ret(sample_count);
+
+ if (num_channels == 1) {
+ for (size_t i = 0; i < sample_count; i++) {
+ s16 sample;
+ std::memcpy(&sample, data + i * sizeof(s16), sizeof(s16));
+ ret[i].fill(sample);
+ }
+ } else {
+ std::memcpy(ret.data(), data, sample_count * 2 * sizeof(u16));
+ }
+
+ return ret;
+}
+
+};
diff --git a/src/audio_core/codec.h b/src/audio_core/codec.h
new file mode 100644
index 000000000..e695f2edc
--- /dev/null
+++ b/src/audio_core/codec.h
@@ -0,0 +1,50 @@
+// Copyright 2016 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <array>
+#include <vector>
+
+#include "common/common_types.h"
+
+namespace Codec {
+
+/// A variable length buffer of signed PCM16 stereo samples.
+using StereoBuffer16 = std::vector<std::array<s16, 2>>;
+
+/// See: Codec::DecodeADPCM
+struct ADPCMState {
+ // Two historical samples from previous processed buffer,
+ // required for ADPCM decoding
+ s16 yn1; ///< y[n-1]
+ s16 yn2; ///< y[n-2]
+};
+
+/**
+ * @param data Pointer to buffer that contains ADPCM data to decode
+ * @param sample_count Length of buffer in terms of number of samples
+ * @param adpcm_coeff ADPCM coefficients
+ * @param state ADPCM state, this is updated with new state
+ * @return Decoded stereo signed PCM16 data, sample_count in length
+ */
+StereoBuffer16 DecodeADPCM(const u8* const data, const size_t sample_count, const std::array<s16, 16>& adpcm_coeff, ADPCMState& state);
+
+/**
+ * @param num_channels Number of channels
+ * @param data Pointer to buffer that contains PCM8 data to decode
+ * @param sample_count Length of buffer in terms of number of samples
+ * @return Decoded stereo signed PCM16 data, sample_count in length
+ */
+StereoBuffer16 DecodePCM8(const unsigned num_channels, const u8* const data, const size_t sample_count);
+
+/**
+ * @param num_channels Number of channels
+ * @param data Pointer to buffer that contains PCM16 data to decode
+ * @param sample_count Length of buffer in terms of number of samples
+ * @return Decoded stereo signed PCM16 data, sample_count in length
+ */
+StereoBuffer16 DecodePCM16(const unsigned num_channels, const u8* const data, const size_t sample_count);
+
+};
diff --git a/src/audio_core/hle/common.h b/src/audio_core/hle/common.h
new file mode 100644
index 000000000..37d441eb2
--- /dev/null
+++ b/src/audio_core/hle/common.h
@@ -0,0 +1,35 @@
+// Copyright 2016 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <algorithm>
+#include <array>
+
+#include "audio_core/audio_core.h"
+
+#include "common/common_types.h"
+
+namespace DSP {
+namespace HLE {
+
+/// The final output to the speakers is stereo. Preprocessing output in Source is also stereo.
+using StereoFrame16 = std::array<std::array<s16, 2>, AudioCore::samples_per_frame>;
+
+/// The DSP is quadraphonic internally.
+using QuadFrame32 = std::array<std::array<s32, 4>, AudioCore::samples_per_frame>;
+
+/**
+ * This performs the filter operation defined by FilterT::ProcessSample on the frame in-place.
+ * FilterT::ProcessSample is called sequentially on the samples.
+ */
+template<typename FrameT, typename FilterT>
+void FilterFrame(FrameT& frame, FilterT& filter) {
+ std::transform(frame.begin(), frame.end(), frame.begin(), [&filter](const typename FrameT::value_type& sample) {
+ return filter.ProcessSample(sample);
+ });
+}
+
+} // namespace HLE
+} // namespace DSP
diff --git a/src/audio_core/hle/dsp.h b/src/audio_core/hle/dsp.h
index 376436c29..c15ef0b7a 100644
--- a/src/audio_core/hle/dsp.h
+++ b/src/audio_core/hle/dsp.h
@@ -126,8 +126,11 @@ struct SourceConfiguration {
union {
u32_le dirty_raw;
+ BitField<0, 1, u32_le> format_dirty;
+ BitField<1, 1, u32_le> mono_or_stereo_dirty;
BitField<2, 1, u32_le> adpcm_coefficients_dirty;
BitField<3, 1, u32_le> partial_embedded_buffer_dirty; ///< Tends to be set when a looped buffer is queued.
+ BitField<4, 1, u32_le> partial_reset_flag;
BitField<16, 1, u32_le> enable_dirty;
BitField<17, 1, u32_le> interpolation_dirty;
@@ -143,8 +146,7 @@ struct SourceConfiguration {
BitField<27, 1, u32_le> gain_2_dirty;
BitField<28, 1, u32_le> sync_dirty;
BitField<29, 1, u32_le> reset_flag;
-
- BitField<31, 1, u32_le> embedded_buffer_dirty;
+ BitField<30, 1, u32_le> embedded_buffer_dirty;
};
// Gain control
@@ -175,7 +177,8 @@ struct SourceConfiguration {
/**
* This is the simplest normalized first-order digital recursive filter.
* The transfer function of this filter is:
- * H(z) = b0 / (1 + a1 z^-1)
+ * H(z) = b0 / (1 - a1 z^-1)
+ * Note the feedbackward coefficient is negated.
* Values are signed fixed point with 15 fractional bits.
*/
struct SimpleFilter {
@@ -192,11 +195,11 @@ struct SourceConfiguration {
* Values are signed fixed point with 14 fractional bits.
*/
struct BiquadFilter {
- s16_le b0;
- s16_le b1;
- s16_le b2;
- s16_le a1;
s16_le a2;
+ s16_le a1;
+ s16_le b2;
+ s16_le b1;
+ s16_le b0;
};
union {
diff --git a/src/audio_core/hle/filter.cpp b/src/audio_core/hle/filter.cpp
new file mode 100644
index 000000000..2c65ef026
--- /dev/null
+++ b/src/audio_core/hle/filter.cpp
@@ -0,0 +1,115 @@
+// Copyright 2016 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <array>
+#include <cstddef>
+
+#include "audio_core/hle/common.h"
+#include "audio_core/hle/dsp.h"
+#include "audio_core/hle/filter.h"
+
+#include "common/common_types.h"
+#include "common/math_util.h"
+
+namespace DSP {
+namespace HLE {
+
+void SourceFilters::Reset() {
+ Enable(false, false);
+}
+
+void SourceFilters::Enable(bool simple, bool biquad) {
+ simple_filter_enabled = simple;
+ biquad_filter_enabled = biquad;
+
+ if (!simple)
+ simple_filter.Reset();
+ if (!biquad)
+ biquad_filter.Reset();
+}
+
+void SourceFilters::Configure(SourceConfiguration::Configuration::SimpleFilter config) {
+ simple_filter.Configure(config);
+}
+
+void SourceFilters::Configure(SourceConfiguration::Configuration::BiquadFilter config) {
+ biquad_filter.Configure(config);
+}
+
+void SourceFilters::ProcessFrame(StereoFrame16& frame) {
+ if (!simple_filter_enabled && !biquad_filter_enabled)
+ return;
+
+ if (simple_filter_enabled) {
+ FilterFrame(frame, simple_filter);
+ }
+
+ if (biquad_filter_enabled) {
+ FilterFrame(frame, biquad_filter);
+ }
+}
+
+// SimpleFilter
+
+void SourceFilters::SimpleFilter::Reset() {
+ y1.fill(0);
+ // Configure as passthrough.
+ a1 = 0;
+ b0 = 1 << 15;
+}
+
+void SourceFilters::SimpleFilter::Configure(SourceConfiguration::Configuration::SimpleFilter config) {
+ a1 = config.a1;
+ b0 = config.b0;
+}
+
+std::array<s16, 2> SourceFilters::SimpleFilter::ProcessSample(const std::array<s16, 2>& x0) {
+ std::array<s16, 2> y0;
+ for (size_t i = 0; i < 2; i++) {
+ const s32 tmp = (b0 * x0[i] + a1 * y1[i]) >> 15;
+ y0[i] = MathUtil::Clamp(tmp, -32768, 32767);
+ }
+
+ y1 = y0;
+
+ return y0;
+}
+
+// BiquadFilter
+
+void SourceFilters::BiquadFilter::Reset() {
+ x1.fill(0);
+ x2.fill(0);
+ y1.fill(0);
+ y2.fill(0);
+ // Configure as passthrough.
+ a1 = a2 = b1 = b2 = 0;
+ b0 = 1 << 14;
+}
+
+void SourceFilters::BiquadFilter::Configure(SourceConfiguration::Configuration::BiquadFilter config) {
+ a1 = config.a1;
+ a2 = config.a2;
+ b0 = config.b0;
+ b1 = config.b1;
+ b2 = config.b2;
+}
+
+std::array<s16, 2> SourceFilters::BiquadFilter::ProcessSample(const std::array<s16, 2>& x0) {
+ std::array<s16, 2> y0;
+ for (size_t i = 0; i < 2; i++) {
+ const s32 tmp = (b0 * x0[i] + b1 * x1[i] + b2 * x2[i] + a1 * y1[i] + a2 * y2[i]) >> 14;
+ y0[i] = MathUtil::Clamp(tmp, -32768, 32767);
+ }
+
+ x2 = x1;
+ x1 = x0;
+ y2 = y1;
+ y1 = y0;
+
+ return y0;
+}
+
+} // namespace HLE
+} // namespace DSP
diff --git a/src/audio_core/hle/filter.h b/src/audio_core/hle/filter.h
new file mode 100644
index 000000000..75738f600
--- /dev/null
+++ b/src/audio_core/hle/filter.h
@@ -0,0 +1,112 @@
+// Copyright 2016 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <array>
+
+#include "audio_core/hle/common.h"
+#include "audio_core/hle/dsp.h"
+
+#include "common/common_types.h"
+
+namespace DSP {
+namespace HLE {
+
+/// Preprocessing filters. There is an independent set of filters for each Source.
+class SourceFilters final {
+ SourceFilters() { Reset(); }
+
+ /// Reset internal state.
+ void Reset();
+
+ /**
+ * Enable/Disable filters
+ * See also: SourceConfiguration::Configuration::simple_filter_enabled,
+ * SourceConfiguration::Configuration::biquad_filter_enabled.
+ * @param simple If true, enables the simple filter. If false, disables it.
+ * @param simple If true, enables the biquad filter. If false, disables it.
+ */
+ void Enable(bool simple, bool biquad);
+
+ /**
+ * Configure simple filter.
+ * @param config Configuration from DSP shared memory.
+ */
+ void Configure(SourceConfiguration::Configuration::SimpleFilter config);
+
+ /**
+ * Configure biquad filter.
+ * @param config Configuration from DSP shared memory.
+ */
+ void Configure(SourceConfiguration::Configuration::BiquadFilter config);
+
+ /**
+ * Processes a frame in-place.
+ * @param frame Audio samples to process. Modified in-place.
+ */
+ void ProcessFrame(StereoFrame16& frame);
+
+private:
+ bool simple_filter_enabled;
+ bool biquad_filter_enabled;
+
+ struct SimpleFilter {
+ SimpleFilter() { Reset(); }
+
+ /// Resets internal state.
+ void Reset();
+
+ /**
+ * Configures this filter with application settings.
+ * @param config Configuration from DSP shared memory.
+ */
+ void Configure(SourceConfiguration::Configuration::SimpleFilter config);
+
+ /**
+ * Processes a single stereo PCM16 sample.
+ * @param x0 Input sample
+ * @return Output sample
+ */
+ std::array<s16, 2> ProcessSample(const std::array<s16, 2>& x0);
+
+ private:
+ // Configuration
+ s32 a1, b0;
+ // Internal state
+ std::array<s16, 2> y1;
+ } simple_filter;
+
+ struct BiquadFilter {
+ BiquadFilter() { Reset(); }
+
+ /// Resets internal state.
+ void Reset();
+
+ /**
+ * Configures this filter with application settings.
+ * @param config Configuration from DSP shared memory.
+ */
+ void Configure(SourceConfiguration::Configuration::BiquadFilter config);
+
+ /**
+ * Processes a single stereo PCM16 sample.
+ * @param x0 Input sample
+ * @return Output sample
+ */
+ std::array<s16, 2> ProcessSample(const std::array<s16, 2>& x0);
+
+ private:
+ // Configuration
+ s32 a1, a2, b0, b1, b2;
+ // Internal state
+ std::array<s16, 2> x1;
+ std::array<s16, 2> x2;
+ std::array<s16, 2> y1;
+ std::array<s16, 2> y2;
+ } biquad_filter;
+};
+
+} // namespace HLE
+} // namespace DSP