Audio interpolation (#1176)

add audio interpolation (emulation improvement)
This commit is contained in:
Arisotura 2021-08-08 14:27:57 +02:00 committed by GitHub
parent b28a9e4d24
commit 2df6b4fdc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 180 additions and 45 deletions

View File

@ -18,6 +18,7 @@
#include <stdio.h>
#include <string.h>
#include <cmath>
#include "Platform.h"
#include "NDS.h"
#include "DSi.h"
@ -27,7 +28,6 @@
// SPU TODO
// * capture addition modes, overflow bugs
// * channel hold
// * 'length less than 4' glitch
namespace SPU
{
@ -62,6 +62,12 @@ const s16 PSGTable[8][8] =
{-0x7FFF, -0x7FFF, -0x7FFF, -0x7FFF, -0x7FFF, -0x7FFF, -0x7FFF, -0x7FFF}
};
// audio interpolation is an improvement upon the original hardware
// (which performs no interpolation)
int InterpType;
s16 InterpCos[0x100];
s16 InterpCubic[0x100][4];
const u32 OutputBufferSize = 2*2048;
s16 OutputBackbuffer[2 * OutputBufferSize];
u32 OutputBackbufferWritePosition;
@ -90,6 +96,32 @@ bool Init()
AudioLock = Platform::Mutex_Create();
InterpType = 0;
// generate interpolation tables
// values are 1:1:14 fixed-point
float m_pi = std::acos(-1.0f);
for (int i = 0; i < 0x100; i++)
{
float ratio = (i * m_pi) / 255.0f;
ratio = 1.0f - std::cos(ratio);
InterpCos[i] = (s16)(ratio * 0x2000);
}
for (int i = 0; i < 0x100; i++)
{
s32 i1 = i << 6;
s32 i2 = (i * i) >> 2;
s32 i3 = (i * i * i) >> 10;
InterpCubic[i][0] = -i3 + 2*i2 - i1;
InterpCubic[i][1] = i3 - 2*i2 + 0x4000;
InterpCubic[i][2] = -i3 + i2 + i1;
InterpCubic[i][3] = i3 - i2;
}
return true;
}
@ -148,6 +180,11 @@ void DoSavestate(Savestate* file)
}
void SetInterpolation(int type)
{
InterpType = type;
}
void SetBias(u16 bias)
{
Bias = bias;
@ -202,6 +239,7 @@ void Channel::DoSavestate(Savestate* file)
file->Var8((u8*)&KeyOn);
file->Var32(&Timer);
file->Var32((u32*)&Pos);
file->VarArray(PrevSample, sizeof(PrevSample));
file->Var16((u16*)&CurSample);
file->Var16(&NoiseVal);
@ -215,7 +253,7 @@ void Channel::DoSavestate(Savestate* file)
file->Var32(&FIFOWritePos);
file->Var32(&FIFOReadOffset);
file->Var32(&FIFOLevel);
file->VarArray(FIFO, 8*4);
file->VarArray(FIFO, sizeof(FIFO));
}
void Channel::FIFO_BufferData()
@ -269,6 +307,9 @@ void Channel::Start()
Pos = -3;
NoiseVal = 0x7FFF;
PrevSample[0] = 0;
PrevSample[1] = 0;
PrevSample[2] = 0;
CurSample = 0;
FIFOReadPos = 0;
@ -444,6 +485,16 @@ s32 Channel::Run()
{
Timer = TimerReload + (Timer - 0x10000);
// for optional interpolation: save previous samples
// the interpolated audio will be delayed by a couple samples,
// but it's easier to deal with this way
if ((type < 3) && (InterpType != 0))
{
PrevSample[2] = PrevSample[1];
PrevSample[1] = PrevSample[0];
PrevSample[0] = CurSample;
}
switch (type)
{
case 0: NextSample_PCM8(); break;
@ -455,6 +506,34 @@ s32 Channel::Run()
}
s32 val = (s32)CurSample;
// interpolation (emulation improvement, not a hardware feature)
if ((type < 3) && (InterpType != 0))
{
s32 samplepos = ((Timer - TimerReload) * 0x100) / (0x10000 - TimerReload);
if (samplepos > 0xFF) samplepos = 0xFF;
switch (InterpType)
{
case 1: // linear
val = ((val * samplepos) +
(PrevSample[0] * (0xFF-samplepos))) >> 8;
break;
case 2: // cosine
val = ((val * InterpCos[samplepos]) +
(PrevSample[0] * InterpCos[0xFF-samplepos])) >> 14;
break;
case 3: // cubic
val = ((PrevSample[2] * InterpCubic[samplepos][0]) +
(PrevSample[1] * InterpCubic[samplepos][1]) +
(PrevSample[0] * InterpCubic[samplepos][2]) +
(val * InterpCubic[samplepos][3])) >> 14;
break;
}
}
val <<= VolumeShift;
val *= Volume;
return val;

View File

@ -31,6 +31,9 @@ void Stop();
void DoSavestate(Savestate* file);
// 0=none 1=linear 2=cosine 3=cubic
void SetInterpolation(int type);
void SetBias(u16 bias);
void Mix(u32 dummy);
@ -73,6 +76,7 @@ public:
bool KeyOn;
u32 Timer;
s32 Pos;
s16 PrevSample[3];
s16 CurSample;
u16 NoiseVal;

View File

@ -38,8 +38,15 @@ AudioSettingsDialog::AudioSettingsDialog(QWidget* parent) : QDialog(parent), ui(
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
oldInterp = Config::AudioInterp;
oldVolume = Config::AudioVolume;
ui->cbInterpolation->addItem("None");
ui->cbInterpolation->addItem("Linear");
ui->cbInterpolation->addItem("Cosine");
ui->cbInterpolation->addItem("Cubic");
ui->cbInterpolation->setCurrentIndex(Config::AudioInterp);
ui->slVolume->setValue(Config::AudioVolume);
grpMicMode = new QButtonGroup(this);
@ -73,11 +80,22 @@ void AudioSettingsDialog::on_AudioSettingsDialog_accepted()
void AudioSettingsDialog::on_AudioSettingsDialog_rejected()
{
Config::AudioInterp = oldInterp;
Config::AudioVolume = oldVolume;
closeDlg();
}
void AudioSettingsDialog::on_cbInterpolation_currentIndexChanged(int idx)
{
// prevent a spurious change
if (ui->cbInterpolation->count() < 4) return;
Config::AudioInterp = ui->cbInterpolation->currentIndex();
emit updateAudioSettings();
}
void AudioSettingsDialog::on_slVolume_valueChanged(int val)
{
Config::AudioVolume = val;

View File

@ -51,10 +51,14 @@ public:
currentDlg = nullptr;
}
signals:
void updateAudioSettings();
private slots:
void on_AudioSettingsDialog_accepted();
void on_AudioSettingsDialog_rejected();
void on_cbInterpolation_currentIndexChanged(int idx);
void on_slVolume_valueChanged(int val);
void onChangeMicMode(int mode);
void on_btnMicWavBrowse_clicked();
@ -62,6 +66,7 @@ private slots:
private:
Ui::AudioSettingsDialog* ui;
int oldInterp;
int oldVolume;
QButtonGroup* grpMicMode;
};

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>482</width>
<height>230</height>
<height>256</height>
</rect>
</property>
<property name="sizePolicy">
@ -29,14 +29,14 @@
<string>Audio output</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Volume:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="1" column="1">
<widget class="QSlider" name="slVolume">
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Controls the volume of the audio output.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
@ -52,6 +52,20 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Interpolation:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="cbInterpolation">
<property name="whatsThis">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Applies interpolation to audio samples for better quality. Option &amp;quot;None&amp;quot; is accurate to DS hardware.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>

View File

@ -354,21 +354,21 @@
<item row="2" column="0">
<widget class="QCheckBox" name="chkJITBranchOptimisations">
<property name="text">
<string>Branch Optimisations</string>
<string>Branch optimisations</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="chkJITLiteralOptimisations">
<property name="text">
<string>Literal Optimisations</string>
<string>Literal optimisations</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="chkJITFastMemory">
<property name="text">
<string>Fast Memory</string>
<string>Fast memory</string>
</property>
</widget>
</item>

View File

@ -69,6 +69,7 @@ int DirectLAN;
int SavestateRelocSRAM;
int AudioInterp;
int AudioVolume;
int MicInputType;
char MicWavPath[1024];
@ -177,6 +178,7 @@ ConfigEntry PlatformConfigFile[] =
{"SavStaRelocSRAM", 0, &SavestateRelocSRAM, 0, NULL, 0},
{"AudioInterp", 0, &AudioInterp, 0, NULL, 0},
{"AudioVolume", 0, &AudioVolume, 256, NULL, 0},
{"MicInputType", 0, &MicInputType, 1, NULL, 0},
{"MicWavPath", 1, MicWavPath, 0, "", 1023},
@ -199,7 +201,7 @@ ConfigEntry PlatformConfigFile[] =
{"MouseHide", 0, &MouseHide, 0, NULL, 0},
{"MouseHideSeconds", 0, &MouseHideSeconds, 5, NULL, 0},
{"PauseLostFocus", 0, &PauseLostFocus, 0, NULL, 0},
{"", -1, NULL, 0, NULL, 0}
};

View File

@ -85,6 +85,7 @@ extern int DirectLAN;
extern int SavestateRelocSRAM;
extern int AudioInterp;
extern int AudioVolume;
extern int MicInputType;
extern char MicWavPath[1024];

View File

@ -34,7 +34,7 @@ QString IntToHex(u64 num)
QString QStringBytes(u64 num)
{
return (QString::number(num) + " Bytes");
return (QString::number(num) + " bytes");
}
ROMInfoDialog* ROMInfoDialog::currentDlg = nullptr;
@ -51,7 +51,7 @@ ROMInfoDialog::ROMInfoDialog(QWidget* parent) : QDialog(parent), ui(new Ui::ROMI
ui->iconImage->setPixmap(QPixmap::fromImage(iconImage));
if (NDSCart::Banner.Version == 0x103)
{
{
u32 animatedIconData[32 * 32 * 64] = {0};
Frontend::AnimatedROMIcon(NDSCart::Banner.DSiIcon, NDSCart::Banner.DSiPalette, NDSCart::Banner.DSiSequence, animatedIconData, animatedSequence);
@ -87,7 +87,7 @@ ROMInfoDialog::ROMInfoDialog(QWidget* parent) : QDialog(parent), ui(new Ui::ROMI
ui->chineseTitle->setText(QString::fromUtf16(NDSCart::Banner.ChineseTitle));
else
ui->chineseTitle->setText("None");
if (NDSCart::Banner.Version > 2)
ui->koreanTitle->setText(QString::fromUtf16(NDSCart::Banner.KoreanTitle));
else
@ -107,12 +107,12 @@ ROMInfoDialog::ROMInfoDialog(QWidget* parent) : QDialog(parent), ui(new Ui::ROMI
ui->arm7EntryAddress->setText(IntToHex(NDSCart::Header.ARM7EntryAddress));
ui->arm7RamAddress->setText(IntToHex(NDSCart::Header.ARM7RAMAddress));
ui->arm7Size->setText(QStringBytes(NDSCart::Header.ARM7Size));
ui->fntOffset->setText(IntToHex(NDSCart::Header.FNTOffset));
ui->fntSize->setText(QStringBytes(NDSCart::Header.FNTSize));
ui->fatOffset->setText(IntToHex(NDSCart::Header.FATOffset));
ui->fatSize->setText(QStringBytes(NDSCart::Header.FATSize));
}
ROMInfoDialog::~ROMInfoDialog()

View File

@ -17,7 +17,7 @@
</sizepolicy>
</property>
<property name="windowTitle">
<string>ROM Info - melonDS</string>
<string>ROM info - melonDS</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
@ -40,7 +40,7 @@
</font>
</property>
<property name="text">
<string>Japanese Title:</string>
<string>Japanese title:</string>
</property>
</widget>
</item>
@ -59,7 +59,7 @@
</font>
</property>
<property name="text">
<string>English Title:</string>
<string>English title:</string>
</property>
</widget>
</item>
@ -78,7 +78,7 @@
</font>
</property>
<property name="text">
<string>French Title:</string>
<string>French title:</string>
</property>
</widget>
</item>
@ -97,7 +97,7 @@
</font>
</property>
<property name="text">
<string>German Title:</string>
<string>German title:</string>
</property>
</widget>
</item>
@ -116,7 +116,7 @@
</font>
</property>
<property name="text">
<string>Italian Title:</string>
<string>Italian title:</string>
</property>
</widget>
</item>
@ -135,7 +135,7 @@
</font>
</property>
<property name="text">
<string>Spanish Title:</string>
<string>Spanish title:</string>
</property>
</widget>
</item>
@ -154,7 +154,7 @@
</font>
</property>
<property name="text">
<string>Chinese Title:</string>
<string>Chinese title:</string>
</property>
</widget>
</item>
@ -173,7 +173,7 @@
</font>
</property>
<property name="text">
<string>Korean Title:</string>
<string>Korean title:</string>
</property>
</widget>
</item>
@ -201,7 +201,7 @@
</font>
</property>
<property name="text">
<string>ARM9 ROM Offset: </string>
<string>ARM9 ROM offset: </string>
</property>
</widget>
</item>
@ -220,7 +220,7 @@
</font>
</property>
<property name="text">
<string>ARM9 Entry Address:</string>
<string>ARM9 entry address:</string>
</property>
</widget>
</item>
@ -239,7 +239,7 @@
</font>
</property>
<property name="text">
<string>ARM9 RAM Address:</string>
<string>ARM9 RAM address:</string>
</property>
</widget>
</item>
@ -258,7 +258,7 @@
</font>
</property>
<property name="text">
<string>ARM9 Size:</string>
<string>ARM9 size:</string>
</property>
</widget>
</item>
@ -277,7 +277,7 @@
</font>
</property>
<property name="text">
<string>ARM7 ROM Offset: </string>
<string>ARM7 ROM offset: </string>
</property>
</widget>
</item>
@ -296,7 +296,7 @@
</font>
</property>
<property name="text">
<string>ARM7 Entry Address:</string>
<string>ARM7 entry address:</string>
</property>
</widget>
</item>
@ -315,7 +315,7 @@
</font>
</property>
<property name="text">
<string>ARM7 RAM Address:</string>
<string>ARM7 RAM address:</string>
</property>
</widget>
</item>
@ -334,7 +334,7 @@
</font>
</property>
<property name="text">
<string>ARM7 Size:</string>
<string>ARM7 size:</string>
</property>
</widget>
</item>
@ -368,7 +368,7 @@
</font>
</property>
<property name="text">
<string>FNT Offset:</string>
<string>FNT offset:</string>
</property>
</widget>
</item>
@ -387,7 +387,7 @@
</font>
</property>
<property name="text">
<string>FNT Size:</string>
<string>FNT size:</string>
</property>
</widget>
</item>
@ -406,7 +406,7 @@
</font>
</property>
<property name="text">
<string>FAT Offset:</string>
<string>FAT offset:</string>
</property>
</widget>
</item>
@ -425,7 +425,7 @@
</font>
</property>
<property name="text">
<string>FAT Size:</string>
<string>FAT size:</string>
</property>
</widget>
</item>
@ -448,7 +448,7 @@
</sizepolicy>
</property>
<property name="title">
<string>General Info</string>
<string>General info</string>
</property>
<layout class="QFormLayout" name="formLayout_3">
<item row="0" column="0">
@ -459,7 +459,7 @@
</font>
</property>
<property name="text">
<string>Game Title:</string>
<string>Game title:</string>
</property>
</widget>
</item>
@ -478,7 +478,7 @@
</font>
</property>
<property name="text">
<string>Game Code:</string>
<string>Game code:</string>
</property>
</widget>
</item>
@ -497,7 +497,7 @@
</font>
</property>
<property name="text">
<string>Maker Code:</string>
<string>Maker code:</string>
</property>
</widget>
</item>
@ -516,7 +516,7 @@
</font>
</property>
<property name="text">
<string>Card Size:</string>
<string>Card size:</string>
</property>
</widget>
</item>
@ -784,7 +784,7 @@
<item row="1" column="1">
<widget class="QPushButton" name="saveIconButton">
<property name="text">
<string>Save Icon</string>
<string>Save icon</string>
</property>
</widget>
</item>

View File

@ -144,12 +144,12 @@ void WifiSettingsDialog::on_cbxDirectAdapter_currentIndexChanged(int sel)
LAN_PCap::AdapterData* adapter = &LAN_PCap::Adapters[sel];
char tmp[64];
sprintf(tmp, "MAC: %02X:%02X:%02X:%02X:%02X:%02X",
sprintf(tmp, "%02X:%02X:%02X:%02X:%02X:%02X",
adapter->MAC[0], adapter->MAC[1], adapter->MAC[2],
adapter->MAC[3], adapter->MAC[4], adapter->MAC[5]);
ui->lblAdapterMAC->setText(QString(tmp));
sprintf(tmp, "IP: %d.%d.%d.%d",
sprintf(tmp, "%d.%d.%d.%d",
adapter->IP_v4[0], adapter->IP_v4[1],
adapter->IP_v4[2], adapter->IP_v4[3]);
ui->lblAdapterIP->setText(QString(tmp));

View File

@ -61,7 +61,7 @@
<item row="3" column="0" rowspan="3" colspan="2">
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Direct Mode Settings</string>
<string>Direct mode settings</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
@ -127,7 +127,7 @@
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Indirect mode uses libslirp. It requires no extra setup and is easy to use.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Indirect Mode (uses libslirp, recommended)</string>
<string>Indirect mode (uses libslirp, recommended)</string>
</property>
</widget>
</item>

View File

@ -399,6 +399,8 @@ void EmuThread::run()
GPU::InitRenderer(videoRenderer);
GPU::SetRenderSettings(videoRenderer, videoSettings);
SPU::SetInterpolation(Config::AudioInterp);
Input::Init();
u32 nframes = 0;
@ -2424,13 +2426,21 @@ void MainWindow::onOpenVideoSettings()
void MainWindow::onOpenAudioSettings()
{
AudioSettingsDialog* dlg = AudioSettingsDialog::openDlg(this);
connect(dlg, &AudioSettingsDialog::updateAudioSettings, this, &MainWindow::onUpdateAudioSettings);
connect(dlg, &AudioSettingsDialog::finished, this, &MainWindow::onAudioSettingsFinished);
}
void MainWindow::onUpdateAudioSettings()
{
SPU::SetInterpolation(Config::AudioInterp);
}
void MainWindow::onAudioSettingsFinished(int res)
{
micClose();
SPU::SetInterpolation(Config::AudioInterp);
if (Config::MicInputType == 3)
{
micLoadWav(Config::MicWavPath);
@ -2750,6 +2760,7 @@ int main(int argc, char** argv)
);
SANITIZE(Config::ScreenVSyncInterval, 1, 20);
SANITIZE(Config::GL_ScaleFactor, 1, 16);
SANITIZE(Config::AudioInterp, 0, 3);
SANITIZE(Config::AudioVolume, 0, 256);
SANITIZE(Config::MicInputType, 0, 3);
SANITIZE(Config::ScreenRotation, 0, 3);

View File

@ -243,6 +243,7 @@ private slots:
void onInputConfigFinished(int res);
void onOpenVideoSettings();
void onOpenAudioSettings();
void onUpdateAudioSettings();
void onAudioSettingsFinished(int res);
void onOpenWifiSettings();
void onWifiSettingsFinished(int res);