mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 06:09:50 -06:00
High Quality Resampler, using FIR filter
For more information: https://docs.google.com/document/d/1tBEgsJh7QiwNwepXI0eobfK3U8LkJButSyeuFt1degM/edit?usp=sharing removed: SSE includes (not used) added: 16bit -> float -> 16bit functions added: linear interpolator and high-quality (windowed-sinc) interpolator functions (including Resampler class) added: dithering changed: renamed variables and reformatted a few things to fit with style guide (braces, #include->const) changed: use s16, u16, s32, u32 etc changed: store samples and do all computations as floats changed: volume from 0 - 255
This commit is contained in:
@ -4,50 +4,56 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "AudioCommon/WaveFile.h"
|
||||
|
||||
// 16 bit Stereo
|
||||
#define MAX_SAMPLES (1024 * 2) // 64ms
|
||||
#define INDEX_MASK (MAX_SAMPLES * 2 - 1)
|
||||
|
||||
#define LOW_WATERMARK 1280 // 40 ms
|
||||
#define MAX_FREQ_SHIFT 200 // per 32000 Hz
|
||||
#define CONTROL_FACTOR 0.2f // in freq_shift per fifo size offset
|
||||
#define CONTROL_AVG 32
|
||||
|
||||
class CMixer {
|
||||
// Dither define
|
||||
#define DITHER_NOISE (rand() / (float) RAND_MAX - 0.5f)
|
||||
|
||||
class CMixer
|
||||
{
|
||||
public:
|
||||
CMixer(unsigned int BackendSampleRate)
|
||||
CMixer(u32 BackendSampleRate)
|
||||
: m_dma_mixer(this, 32000)
|
||||
, m_streaming_mixer(this, 48000)
|
||||
, m_wiimote_speaker_mixer(this, 3000)
|
||||
, m_sampleRate(BackendSampleRate)
|
||||
, m_sample_rate(BackendSampleRate)
|
||||
, m_log_dtk_audio(0)
|
||||
, m_log_dsp_audio(0)
|
||||
, m_speed(0)
|
||||
, m_l_dither_prev(0)
|
||||
, m_r_dither_prev(0)
|
||||
{
|
||||
INFO_LOG(AUDIO_INTERFACE, "Mixer is initialized");
|
||||
m_output_buffer.reserve(MAX_SAMPLES * 2);
|
||||
}
|
||||
|
||||
static const u32 MAX_SAMPLES = 2048;
|
||||
static const u32 INDEX_MASK = MAX_SAMPLES * 2 - 1;
|
||||
static const float LOW_WATERMARK;
|
||||
static const float MAX_FREQ_SHIFT;
|
||||
static const float CONTROL_FACTOR;
|
||||
static const float CONTROL_AVG;
|
||||
|
||||
virtual ~CMixer() {}
|
||||
|
||||
// Called from audio threads
|
||||
virtual unsigned int Mix(short* samples, unsigned int numSamples, bool consider_framelimit = true);
|
||||
u32 Mix(s16* samples, u32 numSamples, bool consider_framelimit = true);
|
||||
|
||||
// Called from main thread
|
||||
virtual void PushSamples(const short* samples, unsigned int num_samples);
|
||||
virtual void PushStreamingSamples(const short* samples, unsigned int num_samples);
|
||||
virtual void PushWiimoteSpeakerSamples(const short* samples, unsigned int num_samples, unsigned int sample_rate);
|
||||
unsigned int GetSampleRate() const { return m_sampleRate; }
|
||||
virtual void PushSamples(const s16* samples, u32 num_samples);
|
||||
virtual void PushStreamingSamples(const s16* samples, u32 num_samples);
|
||||
virtual void PushWiimoteSpeakerSamples(const s16* samples, u32 num_samples, u32 sample_rate);
|
||||
u32 GetSampleRate() const { return m_sample_rate; }
|
||||
|
||||
void SetDMAInputSampleRate(unsigned int rate);
|
||||
void SetStreamInputSampleRate(unsigned int rate);
|
||||
void SetStreamingVolume(unsigned int lvolume, unsigned int rvolume);
|
||||
void SetWiimoteSpeakerVolume(unsigned int lvolume, unsigned int rvolume);
|
||||
void SetDMAInputSampleRate(u32 rate);
|
||||
void SetStreamInputSampleRate(u32 rate);
|
||||
void SetStreamingVolume(u32 lvolume, u32 rvolume);
|
||||
void SetWiimoteSpeakerVolume(u32 lvolume, u32 rvolume);
|
||||
|
||||
virtual void StartLogDTKAudio(const std::string& filename)
|
||||
{
|
||||
@ -107,46 +113,98 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
std::mutex& MixerCritical() { return m_csMixing; }
|
||||
std::mutex& MixerCritical() { return m_cs_mixing; }
|
||||
|
||||
float GetCurrentSpeed() const { return m_speed; }
|
||||
void UpdateSpeed(volatile float val) { m_speed = val; }
|
||||
|
||||
protected:
|
||||
class MixerFifo {
|
||||
class MixerFifo
|
||||
{
|
||||
public:
|
||||
MixerFifo(CMixer *mixer, unsigned sample_rate)
|
||||
MixerFifo(CMixer* mixer, u32 sample_rate)
|
||||
: m_mixer(mixer)
|
||||
, m_input_sample_rate(sample_rate)
|
||||
, m_indexW(0)
|
||||
, m_indexR(0)
|
||||
, m_LVolume(256)
|
||||
, m_RVolume(256)
|
||||
, m_numLeftI(0.0f)
|
||||
, m_frac(0)
|
||||
, m_write_index(0)
|
||||
, m_read_index(0)
|
||||
, m_lvolume(255)
|
||||
, m_rvolume(255)
|
||||
, m_num_left_i(0.0f)
|
||||
, m_fraction(0.0f)
|
||||
{
|
||||
memset(m_buffer, 0, sizeof(m_buffer));
|
||||
srand((u32) time(nullptr));
|
||||
}
|
||||
void PushSamples(const short* samples, unsigned int num_samples);
|
||||
unsigned int Mix(short* samples, unsigned int numSamples, bool consider_framelimit = true);
|
||||
void SetInputSampleRate(unsigned int rate);
|
||||
void SetVolume(unsigned int lvolume, unsigned int rvolume);
|
||||
private:
|
||||
CMixer *m_mixer;
|
||||
unsigned m_input_sample_rate;
|
||||
short m_buffer[MAX_SAMPLES * 2];
|
||||
volatile u32 m_indexW;
|
||||
volatile u32 m_indexR;
|
||||
// Volume ranges from 0-256
|
||||
volatile s32 m_LVolume;
|
||||
volatile s32 m_RVolume;
|
||||
float m_numLeftI;
|
||||
u32 m_frac;
|
||||
virtual void Interpolate(u32 left_input_index, float* left_output, float* right_output) = 0;
|
||||
void PushSamples(const s16* samples, u32 num_samples);
|
||||
void Mix(std::vector<float>& samples, u32 numSamples, bool consider_framelimit = true);
|
||||
void SetInputSampleRate(u32 rate);
|
||||
void SetVolume(u32 lvolume, u32 rvolume);
|
||||
void GetVolume(u32* lvolume, u32* rvolume) const;
|
||||
|
||||
protected:
|
||||
CMixer* m_mixer;
|
||||
u32 m_input_sample_rate;
|
||||
|
||||
std::array<float, MAX_SAMPLES * 2> m_float_buffer;
|
||||
|
||||
volatile u32 m_write_index;
|
||||
volatile u32 m_read_index;
|
||||
|
||||
// Volume ranges from 0-255
|
||||
volatile u32 m_lvolume;
|
||||
volatile u32 m_rvolume;
|
||||
|
||||
float m_num_left_i;
|
||||
float m_fraction;
|
||||
};
|
||||
MixerFifo m_dma_mixer;
|
||||
MixerFifo m_streaming_mixer;
|
||||
MixerFifo m_wiimote_speaker_mixer;
|
||||
unsigned int m_sampleRate;
|
||||
|
||||
class LinearMixerFifo : public MixerFifo
|
||||
{
|
||||
public:
|
||||
LinearMixerFifo(CMixer* mixer, u32 sample_rate) : MixerFifo(mixer, sample_rate) {}
|
||||
void Interpolate(u32 left_input_index, float* left_output, float* right_output) override;
|
||||
};
|
||||
|
||||
class WindowedSincMixerFifo : public MixerFifo
|
||||
{
|
||||
public:
|
||||
WindowedSincMixerFifo(CMixer* mixer, u32 sample_rate) : MixerFifo(mixer, sample_rate) {}
|
||||
void Interpolate(u32 left_input_index, float* left_output, float* right_output) override;
|
||||
};
|
||||
|
||||
class Resampler
|
||||
{
|
||||
static const double LOWPASS_ROLLOFF;
|
||||
static const double KAISER_BETA;
|
||||
static const double BESSEL_EPSILON; // acceptable delta for Kaiser Window calculation
|
||||
|
||||
void PopulateFilterCoeff();
|
||||
double ModBessel0th(const double x);
|
||||
public:
|
||||
|
||||
static const u32 SAMPLES_PER_CROSSING = 4096;
|
||||
static const u32 NUM_CROSSINGS = 35;
|
||||
static const u32 WING_SIZE = SAMPLES_PER_CROSSING * (NUM_CROSSINGS - 1) / 2;
|
||||
|
||||
Resampler()
|
||||
{
|
||||
PopulateFilterCoeff();
|
||||
}
|
||||
|
||||
std::array<double, WING_SIZE> m_lowpass_filter;
|
||||
std::array<double, WING_SIZE> m_lowpass_delta;
|
||||
};
|
||||
|
||||
Resampler m_resampler;
|
||||
|
||||
WindowedSincMixerFifo m_dma_mixer;
|
||||
WindowedSincMixerFifo m_streaming_mixer;
|
||||
|
||||
// Linear interpolation seems to be the best for Wiimote 3khz -> 48khz, for now.
|
||||
// TODO: figure out why and make it work with the above FIR
|
||||
LinearMixerFifo m_wiimote_speaker_mixer;
|
||||
|
||||
u32 m_sample_rate;
|
||||
|
||||
WaveFileWriter g_wave_writer_dtk;
|
||||
WaveFileWriter g_wave_writer_dsp;
|
||||
@ -154,7 +212,26 @@ protected:
|
||||
bool m_log_dtk_audio;
|
||||
bool m_log_dsp_audio;
|
||||
|
||||
std::mutex m_csMixing;
|
||||
std::mutex m_cs_mixing;
|
||||
|
||||
volatile float m_speed; // Current rate of the emulation (1.0 = 100% speed)
|
||||
};
|
||||
|
||||
private:
|
||||
// converts [-32768, 32767] -> [-1.0, 1.0]
|
||||
static inline float Signed16ToFloat(const s16 s)
|
||||
{
|
||||
return (s > 0) ? (float) (s / (float) 0x7fff) : (float) (s / (float) 0x8000);
|
||||
}
|
||||
|
||||
// converts [-1.0, 1.0] -> [-32768, 32767]
|
||||
static inline s16 FloatToSigned16(const float f)
|
||||
{
|
||||
return (f > 0) ? (s16) (f * 0x7fff) : (s16) (f * 0x8000);
|
||||
}
|
||||
|
||||
void TriangleDither(float* l_sample, float* r_sample);
|
||||
|
||||
std::vector<float> m_output_buffer;
|
||||
float m_l_dither_prev;
|
||||
float m_r_dither_prev;
|
||||
};
|
Reference in New Issue
Block a user