use libsamplerate for conversion

This commit is contained in:
Squall Leonhart 2024-01-20 05:13:21 +11:00 committed by Squall-Leonhart
parent f915ec5972
commit 139aff1b50
2 changed files with 52 additions and 27 deletions

View File

@ -204,6 +204,7 @@ IF(SDL2_LIBRARY_TEMP)
# Add libsamplerate with vcpkg
if(CMAKE_TOOLCHAIN_FILE MATCHES "vcpkg")
find_package(SampleRate CONFIG REQUIRED)
if(WIN32)
unset(arch_suffix)
unset(path_prefix)
@ -215,9 +216,9 @@ IF(SDL2_LIBRARY_TEMP)
endif()
set(installed_prefix ${_VCPKG_INSTALLED_DIR}/${WINARCH}-windows${arch_suffix}/${path_prefix})
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${installed_prefix}/lib/samplerate.lib)
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} SampleRate::samplerate)
else()
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} -lsamplerate)
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} SampleRate::samplerate)
endif()
endif()

View File

@ -18,6 +18,7 @@
#include <cmath>
#include <iostream>
#include <SDL_events.h>
#include <samplerate.h>
#include "SoundSDL.h"
#include "../gba/Globals.h"
#include "../gba/Sound.h"
@ -88,6 +89,30 @@ void SoundSDL::write(uint16_t * finalWave, int length) {
std::size_t samples = length / 4;
std::size_t avail;
// Create a new SRC_STATE object for the resampling
int error;
SRC_STATE* src_state = src_new(SRC_SINC_FASTEST, 2, &error);
if (!src_state) {
// Handle the error
}
// Set up the SRC_DATA struct
SRC_DATA src_data;
src_data.data_in = finalWave;
src_data.input_frames = length / 2;
src_data.data_out = finalWave;
src_data.output_frames = length / 2;
src_data.src_ratio = (double)desired_sample_rate / current_sample_rate;
// Perform the resampling
error = src_process(src_state, &src_data);
if (error) {
// Handle the error
}
// Clean up
src_delete(src_state);
while ((avail = samples_buf.avail() / 2) < samples) {
samples_buf.write(finalWave, avail * 2);
@ -112,7 +137,6 @@ void SoundSDL::write(uint16_t * finalWave, int length) {
SDL_UnlockMutex(mutex);
}
bool SoundSDL::init(long sampleRate) {
if (initialized) deinit();