Compare commits

...

3 Commits

Author SHA1 Message Date
Squall-Leonhart
d71942f7a4 revert findsdl2.cmake 2024-01-24 11:56:45 +11:00
Squall-Leonhart
bd734fb019 fixed some errors in my usage 2024-01-24 11:08:13 +11:00
Squall Leonhart
139aff1b50 use libsamplerate for conversion 2024-01-24 11:08:13 +11:00
2 changed files with 48 additions and 15 deletions

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"
@@ -76,7 +77,7 @@ void SoundSDL::read(uint16_t* stream, int length) {
SDL_SemPost(data_read);
}
void SoundSDL::write(uint16_t * finalWave, int length) {
void SoundSDL::write(uint16_t* finalWave, int length) {
if (!initialized)
return;
@@ -88,6 +89,36 @@ 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
}
// Convert your uint16_t* data to float*
float* finalWaveFloat = new float[length];
for (int i = 0; i < length; i++) {
finalWaveFloat[i] = (float)finalWave[i];
}
// Set up the SRC_DATA struct
SRC_DATA src_data;
src_data.data_in = finalWaveFloat;
src_data.input_frames = length / 2;
src_data.data_out = finalWaveFloat;
src_data.output_frames = length / 2;
src_data.src_ratio = 1.0; // since desired_sample_rate matches 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);
@@ -110,8 +141,10 @@ void SoundSDL::write(uint16_t * finalWave, int length) {
samples_buf.write(finalWave, samples * 2);
SDL_UnlockMutex(mutex);
}
// Clean up
delete[] finalWaveFloat;
}
bool SoundSDL::init(long sampleRate) {
if (initialized) deinit();