mirror of
https://github.com/visualboyadvance-m/visualboyadvance-m
synced 2025-10-05 23:52:49 +02:00
Compare commits
3 Commits
sfml-downg
...
libsample-
Author | SHA1 | Date | |
---|---|---|---|
|
d71942f7a4 | ||
|
bd734fb019 | ||
|
139aff1b50 |
@@ -260,4 +260,4 @@ ENDIF(SDL2_LIBRARY_TEMP)
|
||||
|
||||
INCLUDE(FindPackageHandleStandardArgs)
|
||||
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)
|
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)
|
@@ -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,42 +77,74 @@ 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;
|
||||
|
||||
SDL_LockMutex(mutex);
|
||||
|
||||
if (SDL_GetAudioDeviceStatus(sound_device) != SDL_AUDIO_PLAYING)
|
||||
SDL_PauseAudioDevice(sound_device, 0);
|
||||
SDL_PauseAudioDevice(sound_device, 0);
|
||||
|
||||
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);
|
||||
samples_buf.write(finalWave, avail * 2);
|
||||
|
||||
finalWave += avail * 2;
|
||||
samples -= avail;
|
||||
finalWave += avail * 2;
|
||||
samples -= avail;
|
||||
|
||||
SDL_UnlockMutex(mutex);
|
||||
SDL_UnlockMutex(mutex);
|
||||
|
||||
SDL_SemPost(data_available);
|
||||
SDL_SemPost(data_available);
|
||||
|
||||
if (should_wait())
|
||||
SDL_SemWait(data_read);
|
||||
else
|
||||
// Drop the remainder of the audio data
|
||||
return;
|
||||
if (should_wait())
|
||||
SDL_SemWait(data_read);
|
||||
else
|
||||
// Drop the remainder of the audio data
|
||||
return;
|
||||
|
||||
SDL_LockMutex(mutex);
|
||||
SDL_LockMutex(mutex);
|
||||
}
|
||||
|
||||
samples_buf.write(finalWave, samples * 2);
|
||||
|
||||
SDL_UnlockMutex(mutex);
|
||||
}
|
||||
|
||||
// Clean up
|
||||
delete[] finalWaveFloat;
|
||||
}
|
||||
|
||||
bool SoundSDL::init(long sampleRate) {
|
||||
if (initialized) deinit();
|
||||
|
Reference in New Issue
Block a user