Audio: Replace blip_buf usage with a hermite interpolation resampler
Improves sound - blip_buf was altering the audio output quite a bit compared to the DSP's raw output
This commit is contained in:
parent
c6dfcd1900
commit
206214ce2b
15 changed files with 155 additions and 497 deletions
|
@ -1,6 +1,7 @@
|
|||
#include "stdafx.h"
|
||||
#include "PcmReader.h"
|
||||
#include "../Utilities/VirtualFile.h"
|
||||
#include "../Utilities/HermiteResampler.h"
|
||||
|
||||
PcmReader::PcmReader()
|
||||
{
|
||||
|
@ -10,15 +11,11 @@ PcmReader::PcmReader()
|
|||
_prevRight = 0;
|
||||
_loopOffset = 8;
|
||||
_sampleRate = 0;
|
||||
_blipLeft = blip_new(10000);
|
||||
_blipRight = blip_new(10000);
|
||||
_outputBuffer = new int16_t[20000];
|
||||
}
|
||||
|
||||
PcmReader::~PcmReader()
|
||||
{
|
||||
blip_delete(_blipLeft);
|
||||
blip_delete(_blipRight);
|
||||
delete[] _outputBuffer;
|
||||
}
|
||||
|
||||
|
@ -51,8 +48,9 @@ bool PcmReader::Init(string filename, bool loop, uint32_t startOffset)
|
|||
_fileOffset = startOffset;
|
||||
_file.seekg(_fileOffset, ios::beg);
|
||||
|
||||
blip_clear(_blipLeft);
|
||||
blip_clear(_blipRight);
|
||||
_leftoverSampleCount = 0;
|
||||
_pcmBuffer.clear();
|
||||
_resampler.Reset();
|
||||
|
||||
return true;
|
||||
} else {
|
||||
|
@ -71,10 +69,7 @@ void PcmReader::SetSampleRate(uint32_t sampleRate)
|
|||
if(sampleRate != _sampleRate) {
|
||||
_sampleRate = sampleRate;
|
||||
|
||||
blip_clear(_blipLeft);
|
||||
blip_clear(_blipRight);
|
||||
blip_set_rates(_blipLeft, PcmReader::PcmSampleRate, _sampleRate);
|
||||
blip_set_rates(_blipRight, PcmReader::PcmSampleRate, _sampleRate);
|
||||
_resampler.SetSampleRates(PcmReader::PcmSampleRate, _sampleRate);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -104,8 +99,8 @@ void PcmReader::LoadSamples(uint32_t samplesToLoad)
|
|||
for(uint32_t i = _fileOffset; i < _fileSize && samplesRead < samplesToLoad; i+=4) {
|
||||
ReadSample(left, right);
|
||||
|
||||
blip_add_delta(_blipLeft, samplesRead, left - _prevLeft);
|
||||
blip_add_delta(_blipRight, samplesRead, right - _prevRight);
|
||||
_pcmBuffer.push_back(left);
|
||||
_pcmBuffer.push_back(right);
|
||||
|
||||
_prevLeft = left;
|
||||
_prevRight = right;
|
||||
|
@ -123,9 +118,6 @@ void PcmReader::LoadSamples(uint32_t samplesToLoad)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
blip_end_frame(_blipLeft, samplesRead);
|
||||
blip_end_frame(_blipRight, samplesRead);
|
||||
}
|
||||
|
||||
void PcmReader::ApplySamples(int16_t *buffer, size_t sampleCount, uint8_t volume)
|
||||
|
@ -134,18 +126,26 @@ void PcmReader::ApplySamples(int16_t *buffer, size_t sampleCount, uint8_t volume
|
|||
return;
|
||||
}
|
||||
|
||||
int32_t samplesNeeded = (int32_t)sampleCount - blip_samples_avail(_blipLeft);
|
||||
int32_t samplesNeeded = (int32_t)sampleCount - _leftoverSampleCount;
|
||||
if(samplesNeeded > 0) {
|
||||
uint32_t samplesToLoad = samplesNeeded * PcmReader::PcmSampleRate / _sampleRate + 1;
|
||||
uint32_t samplesToLoad = samplesNeeded * PcmReader::PcmSampleRate / _sampleRate + 2;
|
||||
LoadSamples(samplesToLoad);
|
||||
}
|
||||
|
||||
int samplesRead = blip_read_samples(_blipLeft, _outputBuffer, (int)sampleCount, 1);
|
||||
blip_read_samples(_blipRight, _outputBuffer + 1, (int)sampleCount, 1);
|
||||
uint32_t samplesRead = _resampler.Resample(_pcmBuffer.data(), (uint32_t)_pcmBuffer.size() / 2, _outputBuffer + _leftoverSampleCount*2);
|
||||
_pcmBuffer.clear();
|
||||
|
||||
for(size_t i = 0, len = samplesRead * 2; i < len; i++) {
|
||||
uint32_t samplesToProcess = std::min<uint32_t>((uint32_t)sampleCount * 2, (samplesRead + _leftoverSampleCount) * 2);
|
||||
for(uint32_t i = 0; i < samplesToProcess; i++) {
|
||||
buffer[i] += (int16_t)((int32_t)_outputBuffer[i] * volume / 255);
|
||||
}
|
||||
|
||||
//Calculate count of extra samples that couldn't be mixed with the rest of the audio and copy them to the beginning of the buffer
|
||||
//These will be mixed on the next call to ApplySamples
|
||||
_leftoverSampleCount = std::max(0, (int32_t)(samplesRead + _leftoverSampleCount) - (int32_t)sampleCount);
|
||||
for(uint32_t i = 0; i < _leftoverSampleCount*2; i++) {
|
||||
_outputBuffer[i] = _outputBuffer[samplesToProcess + i];
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t PcmReader::GetOffset()
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
#include "stdafx.h"
|
||||
#include "../Utilities/stb_vorbis.h"
|
||||
#include "../Utilities/blip_buf.h"
|
||||
#include "../Utilities/HermiteResampler.h"
|
||||
|
||||
class PcmReader
|
||||
{
|
||||
|
@ -22,8 +22,9 @@ private:
|
|||
bool _loop;
|
||||
bool _done;
|
||||
|
||||
blip_t* _blipLeft;
|
||||
blip_t* _blipRight;
|
||||
HermiteResampler _resampler;
|
||||
vector<int16_t> _pcmBuffer;
|
||||
uint32_t _leftoverSampleCount = 0;
|
||||
|
||||
uint32_t _sampleRate;
|
||||
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
#include "Spc.h"
|
||||
#include "Msu1.h"
|
||||
#include "../Utilities/Equalizer.h"
|
||||
#include "../Utilities/blip_buf.h"
|
||||
|
||||
SoundMixer::SoundMixer(Console *console)
|
||||
{
|
||||
|
|
|
@ -5,19 +5,15 @@
|
|||
#include "EmuSettings.h"
|
||||
#include "SoundMixer.h"
|
||||
#include "VideoRenderer.h"
|
||||
#include "../Utilities/blip_buf.h"
|
||||
#include "../Utilities/HermiteResampler.h"
|
||||
|
||||
SoundResampler::SoundResampler(Console *console)
|
||||
{
|
||||
_console = console;
|
||||
_blipBufLeft = blip_new(Spc::SpcSampleRate);
|
||||
_blipBufRight = blip_new(Spc::SpcSampleRate);
|
||||
}
|
||||
|
||||
SoundResampler::~SoundResampler()
|
||||
{
|
||||
blip_delete(_blipBufLeft);
|
||||
blip_delete(_blipBufRight);
|
||||
}
|
||||
|
||||
double SoundResampler::GetRateAdjustment()
|
||||
|
@ -76,45 +72,26 @@ double SoundResampler::GetTargetRateAdjustment()
|
|||
|
||||
void SoundResampler::UpdateTargetSampleRate(uint32_t sampleRate)
|
||||
{
|
||||
uint32_t spcSampleRate = Spc::SpcSampleRate;
|
||||
double spcSampleRate = Spc::SpcSampleRate;
|
||||
if(_console->GetSettings()->GetVideoConfig().IntegerFpsMode) {
|
||||
//Adjust sample rate when running at 60.0 fps instead of 60.1
|
||||
switch(_console->GetRegion()) {
|
||||
default:
|
||||
case ConsoleRegion::Ntsc: spcSampleRate = (uint32_t)(Spc::SpcSampleRate * (60.0 / 60.0988118623484)); break;
|
||||
case ConsoleRegion::Pal: spcSampleRate = (uint32_t)(Spc::SpcSampleRate * (50.0 / 50.00697796826829)); break;
|
||||
case ConsoleRegion::Ntsc: spcSampleRate = Spc::SpcSampleRate * (60.0 / 60.0988118623484); break;
|
||||
case ConsoleRegion::Pal: spcSampleRate = Spc::SpcSampleRate * (50.0 / 50.00697796826829); break;
|
||||
}
|
||||
}
|
||||
|
||||
double targetRate = sampleRate * GetTargetRateAdjustment();
|
||||
if(targetRate != _previousTargetRate || spcSampleRate != _prevSpcSampleRate) {
|
||||
blip_set_rates(_blipBufLeft, spcSampleRate, targetRate);
|
||||
blip_set_rates(_blipBufRight, spcSampleRate, targetRate);
|
||||
_previousTargetRate = targetRate;
|
||||
_prevSpcSampleRate = spcSampleRate;
|
||||
_resampler.SetSampleRates(spcSampleRate, targetRate);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t SoundResampler::Resample(int16_t *inSamples, uint32_t sampleCount, uint32_t sampleRate, int16_t *outSamples)
|
||||
{
|
||||
UpdateTargetSampleRate(sampleRate);
|
||||
|
||||
blip_add_delta(_blipBufLeft, 0, inSamples[0] - _lastSampleLeft);
|
||||
blip_add_delta(_blipBufRight, 0, inSamples[1] - _lastSampleRight);
|
||||
|
||||
for(uint32_t i = 1; i < sampleCount; i++) {
|
||||
blip_add_delta(_blipBufLeft, i, inSamples[i * 2] - inSamples[i * 2 - 2]);
|
||||
blip_add_delta(_blipBufRight, i, inSamples[i * 2 + 1] - inSamples[i * 2 - 1]);
|
||||
}
|
||||
|
||||
_lastSampleLeft = inSamples[(sampleCount - 1) * 2];
|
||||
_lastSampleRight = inSamples[(sampleCount - 1) * 2 + 1];
|
||||
|
||||
blip_end_frame(_blipBufLeft, sampleCount);
|
||||
blip_end_frame(_blipBufRight, sampleCount);
|
||||
|
||||
uint32_t resampledCount = blip_read_samples(_blipBufLeft, outSamples, Spc::SpcSampleRate, true);
|
||||
blip_read_samples(_blipBufRight, outSamples + 1, Spc::SpcSampleRate, true);
|
||||
|
||||
return resampledCount;
|
||||
return _resampler.Resample(inSamples, sampleCount, outSamples);
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
#pragma once
|
||||
#include "stdafx.h"
|
||||
#include "../Utilities/HermiteResampler.h"
|
||||
|
||||
class Console;
|
||||
struct blip_t;
|
||||
|
||||
class SoundResampler
|
||||
{
|
||||
|
@ -11,13 +11,10 @@ private:
|
|||
|
||||
double _rateAdjustment = 1.0;
|
||||
double _previousTargetRate = 0;
|
||||
uint32_t _prevSpcSampleRate = 0;
|
||||
double _prevSpcSampleRate = 0;
|
||||
int32_t _underTarget = 0;
|
||||
|
||||
blip_t *_blipBufLeft = nullptr;
|
||||
blip_t *_blipBufRight = nullptr;
|
||||
int16_t _lastSampleLeft = 0;
|
||||
int16_t _lastSampleRight = 0;
|
||||
HermiteResampler _resampler;
|
||||
|
||||
double GetTargetRateAdjustment();
|
||||
void UpdateTargetSampleRate(uint32_t sampleRate);
|
||||
|
|
|
@ -126,23 +126,23 @@ SOURCES_CXX := $(LIBRETRO_DIR)/libretro.cpp \
|
|||
$(UTIL_DIR)/AutoResetEvent.cpp \
|
||||
$(UTIL_DIR)/AviRecorder.cpp \
|
||||
$(UTIL_DIR)/AviWriter.cpp \
|
||||
$(UTIL_DIR)/blip_buf.cpp \
|
||||
$(UTIL_DIR)/BpsPatcher.cpp \
|
||||
$(UTIL_DIR)/CamstudioCodec.cpp \
|
||||
$(UTIL_DIR)/CRC32.cpp \
|
||||
$(UTIL_DIR)/Equalizer.cpp \
|
||||
$(UTIL_DIR)/FolderUtilities.cpp \
|
||||
$(UTIL_DIR)/GifRecorder.cpp \
|
||||
$(UTIL_DIR)/HermiteResampler.cpp \
|
||||
$(UTIL_DIR)/HexUtilities.cpp \
|
||||
$(UTIL_DIR)/IpsPatcher.cpp \
|
||||
$(UTIL_DIR)/md5.cpp \
|
||||
$(UTIL_DIR)/miniz.cpp \
|
||||
$(UTIL_DIR)/snes_ntsc.cpp \
|
||||
$(UTIL_DIR)/PlatformUtilities.cpp \
|
||||
$(UTIL_DIR)/PNGHelper.cpp \
|
||||
$(UTIL_DIR)/Serializer.cpp \
|
||||
$(UTIL_DIR)/sha1.cpp \
|
||||
$(UTIL_DIR)/SimpleLock.cpp \
|
||||
$(UTIL_DIR)/snes_ntsc.cpp \
|
||||
$(UTIL_DIR)/Socket.cpp \
|
||||
$(UTIL_DIR)/stb_vorbis.cpp \
|
||||
$(UTIL_DIR)/stdafx.cpp \
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include "LibretroMessageManager.h"
|
||||
#include "libretro.h"
|
||||
#include "../Core/Console.h"
|
||||
#include "../Core/Spc.h"
|
||||
#include "../Core/BaseCartridge.h"
|
||||
#include "../Core/MemoryManager.h"
|
||||
#include "../Core/VideoDecoder.h"
|
||||
|
@ -81,7 +82,7 @@ extern "C" {
|
|||
|
||||
AudioConfig audioConfig = _console->GetSettings()->GetAudioConfig();
|
||||
audioConfig.DisableDynamicSampleRate = true;
|
||||
audioConfig.SampleRate = 32000;
|
||||
audioConfig.SampleRate = Spc::SpcSampleRate;
|
||||
_console->GetSettings()->SetAudioConfig(audioConfig);
|
||||
|
||||
PreferencesConfig preferences = _console->GetSettings()->GetPreferences();
|
||||
|
|
76
Utilities/HermiteResampler.cpp
Normal file
76
Utilities/HermiteResampler.cpp
Normal file
|
@ -0,0 +1,76 @@
|
|||
#include "stdafx.h"
|
||||
#include "HermiteResampler.h"
|
||||
|
||||
//Adapted from http://paulbourke.net/miscellaneous/interpolation/
|
||||
//Original author: Paul Bourke ("Any source code found here may be freely used provided credits are given to the author.")
|
||||
int16_t HermiteResampler::HermiteInterpolate(double values[4], double mu)
|
||||
{
|
||||
constexpr double tension = 0; //Tension: 1 is high, 0 normal, -1 is low
|
||||
constexpr double bias = 0; //Bias: 0 is even, positive is towards first segment, negative towards the other
|
||||
|
||||
double m0, m1, mu2, mu3;
|
||||
double a0, a1, a2, a3;
|
||||
|
||||
mu2 = mu * mu;
|
||||
mu3 = mu2 * mu;
|
||||
m0 = (values[1] - values[0]) * (1 + bias) * (1 - tension) / 2;
|
||||
m0 += (values[2] - values[1]) * (1 - bias) * (1 - tension) / 2;
|
||||
m1 = (values[2] - values[1]) * (1 + bias) * (1 - tension) / 2;
|
||||
m1 += (values[3] - values[2]) * (1 - bias) * (1 - tension) / 2;
|
||||
a0 = 2 * mu3 - 3 * mu2 + 1;
|
||||
a1 = mu3 - 2 * mu2 + mu;
|
||||
a2 = mu3 - mu2;
|
||||
a3 = -2 * mu3 + 3 * mu2;
|
||||
|
||||
double output = a0 * values[1] + a1 * m0 + a2 * m1 + a3 * values[2];
|
||||
return (int16_t)std::max(std::min(output, 32767.0), -32768.0);
|
||||
}
|
||||
|
||||
void HermiteResampler::PushSample(double prevValues[4], int16_t sample)
|
||||
{
|
||||
prevValues[0] = prevValues[1];
|
||||
prevValues[1] = prevValues[2];
|
||||
prevValues[2] = prevValues[3];
|
||||
prevValues[3] = (double)sample;
|
||||
}
|
||||
|
||||
void HermiteResampler::Reset()
|
||||
{
|
||||
for(int i = 0; i < 4; i++) {
|
||||
_prevLeft[i] = 0.0;
|
||||
_prevRight[i] = 0.0;
|
||||
}
|
||||
_fraction = 0.0;
|
||||
}
|
||||
|
||||
void HermiteResampler::SetSampleRates(double srcRate, double dstRate)
|
||||
{
|
||||
_rateRatio = srcRate / dstRate;
|
||||
}
|
||||
|
||||
uint32_t HermiteResampler::Resample(int16_t* in, uint32_t inSampleCount, int16_t* out)
|
||||
{
|
||||
if(_rateRatio == 1.0) {
|
||||
memcpy(out, in, inSampleCount * 2 * sizeof(int16_t));
|
||||
return inSampleCount;
|
||||
}
|
||||
|
||||
uint32_t outPos = 0;
|
||||
|
||||
for(uint32_t i = 0; i < inSampleCount * 2; i += 2) {
|
||||
while(_fraction <= 1.0) {
|
||||
//Generate interpolated samples until we have enough samples for the current source sample
|
||||
out[outPos] = HermiteInterpolate(_prevLeft, _fraction);
|
||||
out[outPos + 1] = HermiteInterpolate(_prevRight, _fraction);
|
||||
outPos += 2;
|
||||
_fraction += _rateRatio;
|
||||
}
|
||||
|
||||
//Move to the next source sample
|
||||
PushSample(_prevLeft, in[i]);
|
||||
PushSample(_prevRight, in[i + 1]);
|
||||
_fraction -= 1.0;
|
||||
}
|
||||
|
||||
return outPos / 2;
|
||||
}
|
20
Utilities/HermiteResampler.h
Normal file
20
Utilities/HermiteResampler.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
#pragma once
|
||||
#include "stdafx.h"
|
||||
|
||||
class HermiteResampler
|
||||
{
|
||||
private:
|
||||
double _prevLeft[4] = {};
|
||||
double _prevRight[4] = {};
|
||||
double _rateRatio = 1.0;
|
||||
double _fraction = 0.0;
|
||||
|
||||
__forceinline int16_t HermiteInterpolate(double values[4], double mu);
|
||||
__forceinline void PushSample(double prevValues[4], int16_t sample);
|
||||
|
||||
public:
|
||||
void Reset();
|
||||
|
||||
void SetSampleRates(double srcRate, double dstRate);
|
||||
uint32_t Resample(int16_t* in, uint32_t inSampleCount, int16_t* out);
|
||||
};
|
|
@ -423,7 +423,6 @@
|
|||
<ClInclude Include="AviRecorder.h" />
|
||||
<ClInclude Include="AviWriter.h" />
|
||||
<ClInclude Include="Base64.h" />
|
||||
<ClInclude Include="blip_buf.h" />
|
||||
<ClInclude Include="BpsPatcher.h" />
|
||||
<ClInclude Include="CamstudioCodec.h" />
|
||||
<ClInclude Include="CRC32.h" />
|
||||
|
@ -448,6 +447,7 @@
|
|||
<ClInclude Include="PlatformUtilities.h" />
|
||||
<ClInclude Include="PNGHelper.h" />
|
||||
<ClInclude Include="RawCodec.h" />
|
||||
<ClInclude Include="HermiteResampler.h" />
|
||||
<ClInclude Include="Scale2x\scale2x.h" />
|
||||
<ClInclude Include="Scale2x\scale3x.h" />
|
||||
<ClInclude Include="Scale2x\scalebit.h" />
|
||||
|
@ -477,7 +477,6 @@
|
|||
<ClCompile Include="ArchiveReader.cpp" />
|
||||
<ClCompile Include="AviRecorder.cpp" />
|
||||
<ClCompile Include="AviWriter.cpp" />
|
||||
<ClCompile Include="blip_buf.cpp" />
|
||||
<ClCompile Include="BpsPatcher.cpp" />
|
||||
<ClCompile Include="CamstudioCodec.cpp" />
|
||||
<ClCompile Include="CRC32.cpp" />
|
||||
|
@ -575,6 +574,7 @@
|
|||
<ClCompile Include="PlatformUtilities.cpp" />
|
||||
<ClCompile Include="PNGHelper.cpp" />
|
||||
<ClCompile Include="AutoResetEvent.cpp" />
|
||||
<ClCompile Include="HermiteResampler.cpp" />
|
||||
<ClCompile Include="Scale2x\scale2x.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='PGO Profile|Win32'">NotUsing</PrecompiledHeader>
|
||||
|
|
|
@ -175,9 +175,6 @@
|
|||
<ClInclude Include="Equalizer.h">
|
||||
<Filter>Audio</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="blip_buf.h">
|
||||
<Filter>Audio</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Serializer.h">
|
||||
<Filter>Misc</Filter>
|
||||
</ClInclude>
|
||||
|
@ -196,6 +193,9 @@
|
|||
<ClInclude Include="IVideoRecorder.h">
|
||||
<Filter>Avi</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="HermiteResampler.h">
|
||||
<Filter>Audio</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="xBRZ\xbrz.cpp">
|
||||
|
@ -279,9 +279,6 @@
|
|||
<ClCompile Include="miniz.cpp">
|
||||
<Filter>Archives</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="blip_buf.cpp">
|
||||
<Filter>Audio</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Equalizer.cpp">
|
||||
<Filter>Audio</Filter>
|
||||
</ClCompile>
|
||||
|
@ -330,5 +327,8 @@
|
|||
<ClCompile Include="GifRecorder.cpp">
|
||||
<Filter>Avi</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="HermiteResampler.cpp">
|
||||
<Filter>Audio</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -1,345 +0,0 @@
|
|||
/* blip_buf 1.1.0. http://www.slack.net/~ant/ */
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "blip_buf.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Library Copyright (C) 2003-2009 Shay Green. This library is free software;
|
||||
you can redistribute it and/or modify it under the terms of the GNU Lesser
|
||||
General Public License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version. This
|
||||
library is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||
A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
||||
details. You should have received a copy of the GNU Lesser General Public
|
||||
License along with this module; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
|
||||
|
||||
#if defined (BLARGG_TEST) && BLARGG_TEST
|
||||
#include "blargg_test.h"
|
||||
#endif
|
||||
|
||||
/* Equivalent to ULONG_MAX >= 0xFFFFFFFF00000000.
|
||||
Avoids constants that don't fit in 32 bits. */
|
||||
#if ULONG_MAX/0xFFFFFFFF > 0xFFFFFFFF
|
||||
typedef unsigned long fixed_t;
|
||||
enum { pre_shift = 32 };
|
||||
|
||||
#elif defined(ULLONG_MAX)
|
||||
typedef unsigned long long fixed_t;
|
||||
enum { pre_shift = 32 };
|
||||
|
||||
#else
|
||||
typedef unsigned fixed_t;
|
||||
enum { pre_shift = 0 };
|
||||
|
||||
#endif
|
||||
|
||||
enum { time_bits = pre_shift + 20 };
|
||||
|
||||
static fixed_t const time_unit = (fixed_t) 1 << time_bits;
|
||||
|
||||
enum { bass_shift = 9 }; /* affects high-pass filter breakpoint frequency */
|
||||
enum { end_frame_extra = 2 }; /* allows deltas slightly after frame length */
|
||||
|
||||
enum { half_width = 8 };
|
||||
enum { buf_extra = half_width*2 + end_frame_extra };
|
||||
enum { phase_bits = 5 };
|
||||
enum { phase_count = 1 << phase_bits };
|
||||
enum { delta_bits = 15 };
|
||||
enum { delta_unit = 1 << delta_bits };
|
||||
enum { frac_bits = time_bits - pre_shift };
|
||||
|
||||
/* We could eliminate avail and encode whole samples in offset, but that would
|
||||
limit the total buffered samples to blip_max_frame. That could only be
|
||||
increased by decreasing time_bits, which would reduce resample ratio accuracy.
|
||||
*/
|
||||
|
||||
/** Sample buffer that resamples to output rate and accumulates samples
|
||||
until they're read out */
|
||||
struct blip_t
|
||||
{
|
||||
fixed_t factor;
|
||||
fixed_t offset;
|
||||
int avail;
|
||||
int size;
|
||||
int integrator;
|
||||
};
|
||||
|
||||
typedef int buf_t;
|
||||
|
||||
/* probably not totally portable */
|
||||
#define SAMPLES( buf ) ((buf_t*) ((buf) + 1))
|
||||
|
||||
/* Arithmetic (sign-preserving) right shift */
|
||||
#define ARITH_SHIFT( n, shift ) \
|
||||
((n) >> (shift))
|
||||
|
||||
enum { max_sample = +32767 };
|
||||
enum { min_sample = -32768 };
|
||||
|
||||
#define CLAMP( n ) \
|
||||
{\
|
||||
if ( (short) n != n )\
|
||||
n = ARITH_SHIFT( n, 16 ) ^ max_sample;\
|
||||
}
|
||||
|
||||
static void check_assumptions( void )
|
||||
{
|
||||
int n;
|
||||
|
||||
#if INT_MAX < 0x7FFFFFFF || UINT_MAX < 0xFFFFFFFF
|
||||
#error "int must be at least 32 bits"
|
||||
#endif
|
||||
|
||||
assert( (-3 >> 1) == -2 ); /* right shift must preserve sign */
|
||||
|
||||
n = max_sample * 2;
|
||||
CLAMP( n );
|
||||
assert( n == max_sample );
|
||||
|
||||
n = min_sample * 2;
|
||||
CLAMP( n );
|
||||
assert( n == min_sample );
|
||||
|
||||
assert( blip_max_ratio <= time_unit );
|
||||
assert( blip_max_frame <= (fixed_t) -1 >> time_bits );
|
||||
}
|
||||
|
||||
blip_t* blip_new( int size )
|
||||
{
|
||||
blip_t* m;
|
||||
assert( size >= 0 );
|
||||
|
||||
m = (blip_t*) malloc( sizeof *m + (size + buf_extra) * sizeof (buf_t) );
|
||||
if ( m )
|
||||
{
|
||||
m->factor = time_unit / blip_max_ratio;
|
||||
m->size = size;
|
||||
blip_clear( m );
|
||||
check_assumptions();
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
void blip_delete( blip_t* m )
|
||||
{
|
||||
if ( m != NULL )
|
||||
{
|
||||
/* Clear fields in case user tries to use after freeing */
|
||||
memset( m, 0, sizeof *m );
|
||||
free( m );
|
||||
}
|
||||
}
|
||||
|
||||
void blip_set_rates( blip_t* m, double clock_rate, double sample_rate )
|
||||
{
|
||||
double factor = time_unit * sample_rate / clock_rate;
|
||||
m->factor = (fixed_t) factor;
|
||||
|
||||
/* Fails if clock_rate exceeds maximum, relative to sample_rate */
|
||||
assert( 0 <= factor - m->factor && factor - m->factor < 1 );
|
||||
|
||||
/* Avoid requiring math.h. Equivalent to
|
||||
m->factor = (int) ceil( factor ) */
|
||||
if ( m->factor < factor )
|
||||
m->factor++;
|
||||
|
||||
/* At this point, factor is most likely rounded up, but could still
|
||||
have been rounded down in the floating-point calculation. */
|
||||
}
|
||||
|
||||
void blip_clear( blip_t* m )
|
||||
{
|
||||
/* We could set offset to 0, factor/2, or factor-1. 0 is suitable if
|
||||
factor is rounded up. factor-1 is suitable if factor is rounded down.
|
||||
Since we don't know rounding direction, factor/2 accommodates either,
|
||||
with the slight loss of showing an error in half the time. Since for
|
||||
a 64-bit factor this is years, the halving isn't a problem. */
|
||||
|
||||
m->offset = m->factor / 2;
|
||||
m->avail = 0;
|
||||
m->integrator = 0;
|
||||
memset( SAMPLES( m ), 0, (m->size + buf_extra) * sizeof (buf_t) );
|
||||
}
|
||||
|
||||
int blip_clocks_needed( const blip_t* m, int samples )
|
||||
{
|
||||
fixed_t needed;
|
||||
|
||||
/* Fails if buffer can't hold that many more samples */
|
||||
assert( samples >= 0 && m->avail + samples <= m->size );
|
||||
|
||||
needed = (fixed_t) samples * time_unit;
|
||||
if ( needed < m->offset )
|
||||
return 0;
|
||||
|
||||
return (int)((needed - m->offset + m->factor - 1) / m->factor);
|
||||
}
|
||||
|
||||
void blip_end_frame( blip_t* m, unsigned t )
|
||||
{
|
||||
fixed_t off = t * m->factor + m->offset;
|
||||
m->avail += off >> time_bits;
|
||||
m->offset = off & (time_unit - 1);
|
||||
|
||||
/* Fails if buffer size was exceeded */
|
||||
assert( m->avail <= m->size );
|
||||
}
|
||||
|
||||
int blip_samples_avail( const blip_t* m )
|
||||
{
|
||||
return m->avail;
|
||||
}
|
||||
|
||||
static void remove_samples( blip_t* m, int count )
|
||||
{
|
||||
buf_t* buf = SAMPLES( m );
|
||||
int remain = m->avail + buf_extra - count;
|
||||
m->avail -= count;
|
||||
|
||||
memmove( &buf [0], &buf [count], remain * sizeof buf [0] );
|
||||
memset( &buf [remain], 0, count * sizeof buf [0] );
|
||||
}
|
||||
|
||||
int blip_read_samples( blip_t* m, short out [], int count, int stereo )
|
||||
{
|
||||
assert( count >= 0 );
|
||||
|
||||
if ( count > m->avail )
|
||||
count = m->avail;
|
||||
|
||||
if ( count )
|
||||
{
|
||||
int const step = stereo ? 2 : 1;
|
||||
buf_t const* in = SAMPLES( m );
|
||||
buf_t const* end = in + count;
|
||||
int sum = m->integrator;
|
||||
do
|
||||
{
|
||||
/* Eliminate fraction */
|
||||
int s = ARITH_SHIFT( sum, delta_bits );
|
||||
|
||||
sum += *in++;
|
||||
|
||||
CLAMP( s );
|
||||
|
||||
*out = s;
|
||||
out += step;
|
||||
|
||||
/* High-pass filter */
|
||||
sum -= s << (delta_bits - bass_shift);
|
||||
}
|
||||
while ( in != end );
|
||||
m->integrator = sum;
|
||||
|
||||
remove_samples( m, count );
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Things that didn't help performance on x86:
|
||||
__attribute__((aligned(128)))
|
||||
#define short int
|
||||
restrict
|
||||
*/
|
||||
|
||||
/* Sinc_Generator( 0.9, 0.55, 4.5 ) */
|
||||
static short const bl_step [phase_count + 1] [half_width] =
|
||||
{
|
||||
{ 43, -115, 350, -488, 1136, -914, 5861,21022},
|
||||
{ 44, -118, 348, -473, 1076, -799, 5274,21001},
|
||||
{ 45, -121, 344, -454, 1011, -677, 4706,20936},
|
||||
{ 46, -122, 336, -431, 942, -549, 4156,20829},
|
||||
{ 47, -123, 327, -404, 868, -418, 3629,20679},
|
||||
{ 47, -122, 316, -375, 792, -285, 3124,20488},
|
||||
{ 47, -120, 303, -344, 714, -151, 2644,20256},
|
||||
{ 46, -117, 289, -310, 634, -17, 2188,19985},
|
||||
{ 46, -114, 273, -275, 553, 117, 1758,19675},
|
||||
{ 44, -108, 255, -237, 471, 247, 1356,19327},
|
||||
{ 43, -103, 237, -199, 390, 373, 981,18944},
|
||||
{ 42, -98, 218, -160, 310, 495, 633,18527},
|
||||
{ 40, -91, 198, -121, 231, 611, 314,18078},
|
||||
{ 38, -84, 178, -81, 153, 722, 22,17599},
|
||||
{ 36, -76, 157, -43, 80, 824, -241,17092},
|
||||
{ 34, -68, 135, -3, 8, 919, -476,16558},
|
||||
{ 32, -61, 115, 34, -60, 1006, -683,16001},
|
||||
{ 29, -52, 94, 70, -123, 1083, -862,15422},
|
||||
{ 27, -44, 73, 106, -184, 1152,-1015,14824},
|
||||
{ 25, -36, 53, 139, -239, 1211,-1142,14210},
|
||||
{ 22, -27, 34, 170, -290, 1261,-1244,13582},
|
||||
{ 20, -20, 16, 199, -335, 1301,-1322,12942},
|
||||
{ 18, -12, -3, 226, -375, 1331,-1376,12293},
|
||||
{ 15, -4, -19, 250, -410, 1351,-1408,11638},
|
||||
{ 13, 3, -35, 272, -439, 1361,-1419,10979},
|
||||
{ 11, 9, -49, 292, -464, 1362,-1410,10319},
|
||||
{ 9, 16, -63, 309, -483, 1354,-1383, 9660},
|
||||
{ 7, 22, -75, 322, -496, 1337,-1339, 9005},
|
||||
{ 6, 26, -85, 333, -504, 1312,-1280, 8355},
|
||||
{ 4, 31, -94, 341, -507, 1278,-1205, 7713},
|
||||
{ 3, 35, -102, 347, -506, 1238,-1119, 7082},
|
||||
{ 1, 40, -110, 350, -499, 1190,-1021, 6464},
|
||||
{ 0, 43, -115, 350, -488, 1136, -914, 5861}
|
||||
};
|
||||
|
||||
/* Shifting by pre_shift allows calculation using unsigned int rather than
|
||||
possibly-wider fixed_t. On 32-bit platforms, this is likely more efficient.
|
||||
And by having pre_shift 32, a 32-bit platform can easily do the shift by
|
||||
simply ignoring the low half. */
|
||||
|
||||
void blip_add_delta( blip_t* m, unsigned time, int delta )
|
||||
{
|
||||
unsigned fixed = (unsigned) ((time * m->factor + m->offset) >> pre_shift);
|
||||
buf_t* out = SAMPLES( m ) + m->avail + (fixed >> frac_bits);
|
||||
|
||||
int const phase_shift = frac_bits - phase_bits;
|
||||
int phase = fixed >> phase_shift & (phase_count - 1);
|
||||
short const* in = bl_step [phase];
|
||||
short const* rev = bl_step [phase_count - phase];
|
||||
|
||||
int interp = fixed >> (phase_shift - delta_bits) & (delta_unit - 1);
|
||||
int delta2 = (delta * interp) >> delta_bits;
|
||||
delta -= delta2;
|
||||
|
||||
/* Fails if buffer size was exceeded */
|
||||
assert( out <= &SAMPLES( m ) [m->size + end_frame_extra] );
|
||||
|
||||
out [0] += in[0]*delta + in[half_width+0]*delta2;
|
||||
out [1] += in[1]*delta + in[half_width+1]*delta2;
|
||||
out [2] += in[2]*delta + in[half_width+2]*delta2;
|
||||
out [3] += in[3]*delta + in[half_width+3]*delta2;
|
||||
out [4] += in[4]*delta + in[half_width+4]*delta2;
|
||||
out [5] += in[5]*delta + in[half_width+5]*delta2;
|
||||
out [6] += in[6]*delta + in[half_width+6]*delta2;
|
||||
out [7] += in[7]*delta + in[half_width+7]*delta2;
|
||||
|
||||
in = rev;
|
||||
out [ 8] += in[7]*delta + in[7-half_width]*delta2;
|
||||
out [ 9] += in[6]*delta + in[6-half_width]*delta2;
|
||||
out [10] += in[5]*delta + in[5-half_width]*delta2;
|
||||
out [11] += in[4]*delta + in[4-half_width]*delta2;
|
||||
out [12] += in[3]*delta + in[3-half_width]*delta2;
|
||||
out [13] += in[2]*delta + in[2-half_width]*delta2;
|
||||
out [14] += in[1]*delta + in[1-half_width]*delta2;
|
||||
out [15] += in[0]*delta + in[0-half_width]*delta2;
|
||||
}
|
||||
|
||||
void blip_add_delta_fast( blip_t* m, unsigned time, int delta )
|
||||
{
|
||||
unsigned fixed = (unsigned) ((time * m->factor + m->offset) >> pre_shift);
|
||||
buf_t* out = SAMPLES( m ) + m->avail + (fixed >> frac_bits);
|
||||
|
||||
int interp = fixed >> (frac_bits - delta_bits) & (delta_unit - 1);
|
||||
int delta2 = delta * interp;
|
||||
|
||||
/* Fails if buffer size was exceeded */
|
||||
assert( out <= &SAMPLES( m ) [m->size + end_frame_extra] );
|
||||
|
||||
out [7] += delta * delta_unit - delta2;
|
||||
out [8] += delta2;
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
#pragma once
|
||||
#include "stdafx.h"
|
||||
|
||||
/** \file
|
||||
Sample buffer that resamples from input clock rate to output sample rate */
|
||||
|
||||
/* blip_buf 1.1.0 */
|
||||
#ifndef BLIP_BUF_H
|
||||
#define BLIP_BUF_H
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define EXPORT
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** First parameter of most functions is blip_t*, or const blip_t* if nothing
|
||||
is changed. */
|
||||
typedef struct blip_t blip_t;
|
||||
|
||||
/** Creates new buffer that can hold at most sample_count samples. Sets rates
|
||||
so that there are blip_max_ratio clocks per sample. Returns pointer to new
|
||||
buffer, or NULL if insufficient memory. */
|
||||
EXPORT blip_t* blip_new( int sample_count );
|
||||
|
||||
/** Sets approximate input clock rate and output sample rate. For every
|
||||
clock_rate input clocks, approximately sample_rate samples are generated. */
|
||||
EXPORT void blip_set_rates( blip_t*, double clock_rate, double sample_rate );
|
||||
|
||||
enum { /** Maximum clock_rate/sample_rate ratio. For a given sample_rate,
|
||||
clock_rate must not be greater than sample_rate*blip_max_ratio. */
|
||||
blip_max_ratio = 1 << 20 };
|
||||
|
||||
/** Clears entire buffer. Afterwards, blip_samples_avail() == 0. */
|
||||
EXPORT void blip_clear( blip_t* );
|
||||
|
||||
/** Adds positive/negative delta into buffer at specified clock time. */
|
||||
EXPORT void blip_add_delta( blip_t*, unsigned int clock_time, int delta );
|
||||
|
||||
/** Same as blip_add_delta(), but uses faster, lower-quality synthesis. */
|
||||
void blip_add_delta_fast( blip_t*, unsigned int clock_time, int delta );
|
||||
|
||||
/** Length of time frame, in clocks, needed to make sample_count additional
|
||||
samples available. */
|
||||
int blip_clocks_needed( const blip_t*, int sample_count );
|
||||
|
||||
enum { /** Maximum number of samples that can be generated from one time frame. */
|
||||
blip_max_frame = 4000 };
|
||||
|
||||
/** Makes input clocks before clock_duration available for reading as output
|
||||
samples. Also begins new time frame at clock_duration, so that clock time 0 in
|
||||
the new time frame specifies the same clock as clock_duration in the old time
|
||||
frame specified. Deltas can have been added slightly past clock_duration (up to
|
||||
however many clocks there are in two output samples). */
|
||||
EXPORT void blip_end_frame( blip_t*, unsigned int clock_duration );
|
||||
|
||||
/** Number of buffered samples available for reading. */
|
||||
int blip_samples_avail( const blip_t* );
|
||||
|
||||
/** Reads and removes at most 'count' samples and writes them to 'out'. If
|
||||
'stereo' is true, writes output to every other element of 'out', allowing easy
|
||||
interleaving of two buffers into a stereo sample stream. Outputs 16-bit signed
|
||||
samples. Returns number of samples actually read. */
|
||||
EXPORT int blip_read_samples( blip_t*, short out [], int count, int stereo );
|
||||
|
||||
/** Frees buffer. No effect if NULL is passed. */
|
||||
EXPORT void blip_delete( blip_t* );
|
||||
|
||||
|
||||
/* Deprecated */
|
||||
typedef blip_t blip_buffer_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -248,6 +248,9 @@
|
|||
#define __forceinline
|
||||
#define alloca __builtin_alloca
|
||||
#elif !defined(_MSC_VER)
|
||||
#ifdef __forceinline
|
||||
#undef __forceinline
|
||||
#endif
|
||||
#if __GNUC__
|
||||
#define __forceinline inline
|
||||
#else
|
||||
|
|
|
@ -21,4 +21,14 @@ using std::istream;
|
|||
using std::string;
|
||||
using std::vector;
|
||||
using std::atomic;
|
||||
using std::atomic_flag;
|
||||
using std::atomic_flag;
|
||||
|
||||
#ifndef __MINGW32__
|
||||
#ifdef __clang__
|
||||
#define __forceinline __attribute__((always_inline)) inline
|
||||
#else
|
||||
#ifdef __GNUC__
|
||||
#define __forceinline __attribute__((always_inline)) inline
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
Loading…
Add table
Reference in a new issue