mirror of
https://github.com/libretro/RetroArch
synced 2025-02-20 15:40:44 +00:00
Add resampler tests.
This commit is contained in:
parent
0496ffc007
commit
9bad6f2bba
14
audio/sinc.c
14
audio/sinc.c
@ -19,13 +19,23 @@
|
||||
// Only suitable as an upsampler, as there is no low-pass filter stage.
|
||||
|
||||
#include "resampler.h"
|
||||
#include "../general.h"
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef RESAMPLER_TEST
|
||||
#include "../general.h"
|
||||
#else
|
||||
#define SSNES_LOG(...)
|
||||
#endif
|
||||
|
||||
// M_PI is left out of ISO C99 :(
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846264338327
|
||||
#endif
|
||||
|
||||
#if __SSE__
|
||||
#include <xmmintrin.h>
|
||||
#endif
|
||||
@ -45,7 +55,7 @@
|
||||
#define PHASES_WRAP (1 << (PHASE_BITS + SUBPHASE_BITS))
|
||||
#define FRAMES_SHIFT (PHASE_BITS + SUBPHASE_BITS)
|
||||
|
||||
#define SIDELOBES 8
|
||||
#define SIDELOBES 16
|
||||
#define TAPS (SIDELOBES * 2)
|
||||
|
||||
#define PHASE_INDEX 0
|
||||
|
29
audio/test/Makefile
Normal file
29
audio/test/Makefile
Normal file
@ -0,0 +1,29 @@
|
||||
TESTS := test-hermite test-sinc test-snr-sinc test-snr-hermite
|
||||
|
||||
CFLAGS += -O3 -g -Wall -pedantic -std=gnu99 -DRESAMPLER_TEST -march=native
|
||||
LDFLAGS += -lm
|
||||
|
||||
all: $(TESTS)
|
||||
|
||||
test-hermite: ../hermite.o ../utils.o main.o
|
||||
$(CC) -o $@ $^ $(LDFLAGS)
|
||||
|
||||
test-sinc: ../sinc.o ../utils.o main.o
|
||||
$(CC) -o $@ $^ $(LDFLAGS)
|
||||
|
||||
test-snr-sinc: ../sinc.o ../utils.o snr.o
|
||||
$(CC) -o $@ $^ $(LDFLAGS)
|
||||
|
||||
test-snr-hermite: ../hermite.o ../utils.o snr.o
|
||||
$(CC) -o $@ $^ $(LDFLAGS)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f $(TESTS)
|
||||
rm -f *.o
|
||||
rm -f ../*.o
|
||||
|
||||
.PHONY: clean
|
||||
|
81
audio/test/main.c
Normal file
81
audio/test/main.c
Normal file
@ -0,0 +1,81 @@
|
||||
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
|
||||
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
|
||||
*
|
||||
* Some code herein may be based on code found in BSNES.
|
||||
*
|
||||
* SSNES is free software: you can redistribute it and/or modify it under the terms
|
||||
* of the GNU General Public License as published by the Free Software Found-
|
||||
* ation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* SSNES 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with SSNES.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// Resampler that reads raw S16NE/stereo from stdin and outputs to stdout in S16NE/stereo.
|
||||
// Used for testing and performance benchmarking.
|
||||
|
||||
#include "../resampler.h"
|
||||
#include "../utils.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int16_t input_i[1024];
|
||||
float input_f[1024];
|
||||
int16_t output_i[1024 * 8];
|
||||
float output_f[1024 * 8];
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <in-rate> <out-rate> (max ratio: 8.0)\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
double in_rate = strtod(argv[1], NULL);
|
||||
double out_rate = strtod(argv[2], NULL);
|
||||
|
||||
double ratio = out_rate / in_rate;
|
||||
if (ratio >= 7.99)
|
||||
{
|
||||
fprintf(stderr, "Ratio is too high.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
ssnes_resampler_t *resamp = resampler_new();
|
||||
if (!resamp)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate resampler ...\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (fread(input_i, sizeof(int16_t), 1024, stdin) != 1024)
|
||||
break;
|
||||
|
||||
audio_convert_s16_to_float(input_f, input_i, 1024);
|
||||
|
||||
struct resampler_data data = {
|
||||
.data_in = input_f,
|
||||
.data_out = output_f,
|
||||
.input_frames = sizeof(input_f) / (2 * sizeof(float)),
|
||||
.ratio = ratio,
|
||||
};
|
||||
|
||||
resampler_process(resamp, &data);
|
||||
|
||||
size_t output_samples = data.output_frames * 2;
|
||||
audio_convert_float_to_s16(output_i, output_f, output_samples);
|
||||
|
||||
if (fwrite(output_i, sizeof(int16_t), output_samples, stdout) != output_samples)
|
||||
break;
|
||||
}
|
||||
|
||||
resampler_free(resamp);
|
||||
}
|
||||
|
182
audio/test/snr.c
Normal file
182
audio/test/snr.c
Normal file
@ -0,0 +1,182 @@
|
||||
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
|
||||
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
|
||||
*
|
||||
* Some code herein may be based on code found in BSNES.
|
||||
*
|
||||
* SSNES is free software: you can redistribute it and/or modify it under the terms
|
||||
* of the GNU General Public License as published by the Free Software Found-
|
||||
* ation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* SSNES 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with SSNES.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "../resampler.h"
|
||||
#include "../utils.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <assert.h>
|
||||
|
||||
static void gen_signal(float *out, double freq, double sample_rate, double bias_phase, size_t samples)
|
||||
{
|
||||
for (size_t i = 0; i < samples; i += 2)
|
||||
{
|
||||
out[i + 0] = cos((2.0 * M_PI * freq * ((i >> 1) + bias_phase)) / sample_rate);
|
||||
out[i + 1] = out[i + 0];
|
||||
}
|
||||
}
|
||||
|
||||
static double calculate_snr(const float *orig, const float *resamp, size_t samples)
|
||||
{
|
||||
double noise = 0.0;
|
||||
double signal = 0.0;
|
||||
|
||||
for (size_t i = 0; i < samples; i += 2)
|
||||
signal += orig[i] * orig[i];
|
||||
|
||||
for (size_t i = 0; i < samples; i += 2)
|
||||
{
|
||||
double diff = resamp[i] - orig[i];
|
||||
noise += diff * diff;
|
||||
}
|
||||
|
||||
double snr = 10 * log10(signal / noise);
|
||||
|
||||
return snr;
|
||||
}
|
||||
|
||||
#define SAMPLES 0x100000
|
||||
|
||||
// This approach is kinda stupid.
|
||||
// There should be a good way to directly (and accurately) determine phase after correlating
|
||||
// the two signals.
|
||||
double find_best_snr(const float *output,
|
||||
size_t samples,
|
||||
double freq,
|
||||
double out_rate,
|
||||
uint64_t *first_offset, uint64_t *last_offset,
|
||||
uint64_t *first_subphase, uint64_t *last_subphase, uint64_t *subphases)
|
||||
{
|
||||
static float output_expected[SAMPLES];
|
||||
|
||||
double max_snr = -100.0;
|
||||
uint64_t best_offset = *first_offset;
|
||||
uint64_t best_subphase = *first_subphase;
|
||||
|
||||
for (uint64_t offset = *first_offset; offset <= *last_offset; offset += 2)
|
||||
{
|
||||
for (uint64_t subphase = *first_subphase; subphase <= *last_subphase; subphase++)
|
||||
{
|
||||
gen_signal(output_expected, freq, out_rate, (double)subphase / *subphases, samples);
|
||||
double snr = calculate_snr(output_expected, output + offset, samples);
|
||||
if (snr > max_snr)
|
||||
{
|
||||
max_snr = snr;
|
||||
best_offset = offset;
|
||||
best_subphase = subphase;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Narrow down the search area.
|
||||
uint64_t left_offset = *first_offset;
|
||||
uint64_t right_offset = *last_offset;
|
||||
if (best_offset > left_offset)
|
||||
left_offset = best_offset - 1;
|
||||
if (best_offset < right_offset)
|
||||
right_offset = best_offset + 1;
|
||||
|
||||
*first_offset = left_offset;
|
||||
*last_offset = right_offset;
|
||||
|
||||
*subphases *= 2;
|
||||
best_subphase *= 2;
|
||||
|
||||
uint64_t left_subphase = best_subphase - 2;
|
||||
uint64_t right_subphase = best_subphase + 2;
|
||||
if (best_subphase < 2)
|
||||
left_subphase = 0;
|
||||
|
||||
*first_subphase = left_subphase;
|
||||
*last_subphase = right_subphase;
|
||||
|
||||
return max_snr;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
static float input[SAMPLES];
|
||||
static float output[SAMPLES * 8];
|
||||
|
||||
if (argc != 3)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <in-rate> <out-rate> (max ratio: 8.0)\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
double in_rate = strtod(argv[1], NULL);
|
||||
double out_rate = strtod(argv[2], NULL);
|
||||
|
||||
double ratio = out_rate / in_rate;
|
||||
if (ratio >= 7.99)
|
||||
{
|
||||
fprintf(stderr, "Ratio is too high ...\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ratio < 1.0)
|
||||
{
|
||||
fprintf(stderr, "Ratio too low ...\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const float freq_list[] = {
|
||||
100, 200, 400, 600, 800, 1000,
|
||||
2000, 3000, 5000, 8000, 10000, 12000, 15000, 18000, 20000,
|
||||
};
|
||||
|
||||
for (unsigned i = 0; i < sizeof(freq_list) / sizeof(freq_list[0]); i++)
|
||||
{
|
||||
gen_signal(input, freq_list[i], in_rate, 0.0, SAMPLES);
|
||||
|
||||
struct resampler_data data = {
|
||||
.data_in = input,
|
||||
.data_out = output,
|
||||
.input_frames = SAMPLES / 2,
|
||||
.ratio = ratio,
|
||||
};
|
||||
|
||||
ssnes_resampler_t *re = resampler_new();
|
||||
assert(re);
|
||||
resampler_process(re, &data);
|
||||
resampler_free(re);
|
||||
|
||||
#define MAX_OFFSET 128
|
||||
uint64_t first_offset = 0;
|
||||
uint64_t last_offset = MAX_OFFSET - 2;
|
||||
uint64_t first_subphase = 0;
|
||||
uint64_t last_subphase = 1;
|
||||
uint64_t subphases = 2;
|
||||
|
||||
double max_snr = -100.0;
|
||||
|
||||
// Iteratively find the correct SNR value.
|
||||
for (unsigned j = 0; j < 48; j++)
|
||||
{
|
||||
double snr = find_best_snr(output, SAMPLES - MAX_OFFSET, freq_list[i], out_rate,
|
||||
&first_offset, &last_offset,
|
||||
&first_subphase, &last_subphase, &subphases);
|
||||
|
||||
if (snr > max_snr)
|
||||
max_snr = snr;
|
||||
}
|
||||
|
||||
printf("SNR @ %.0lf Hz: %lf dB\n", freq_list[i], max_snr);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user