Added GetSampleRate function

This commit is contained in:
Kevin Alexis Contreras 2022-06-14 14:04:47 -05:00
commit 3dc33b6f84
4 changed files with 19 additions and 18 deletions

View file

@ -11,5 +11,6 @@ namespace Ship {
virtual int Buffered(void) = 0; virtual int Buffered(void) = 0;
virtual int GetDesiredBuffered(void) = 0; virtual int GetDesiredBuffered(void) = 0;
virtual void Play(const uint8_t* buf, uint32_t len) = 0; virtual void Play(const uint8_t* buf, uint32_t len) = 0;
constexpr int GetSampleRate() const { return 44100; }
}; };
} }

View file

@ -74,7 +74,7 @@ namespace Ship
// Create stream // Create stream
pa_sample_spec ss; pa_sample_spec ss;
ss.format = PA_SAMPLE_S16LE; ss.format = PA_SAMPLE_S16LE;
ss.rate = 32000; ss.rate = this->GetSampleRate();
ss.channels = 2; ss.channels = 2;
pa_buffer_attr attr; pa_buffer_attr attr;

View file

@ -2,40 +2,40 @@
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
namespace Ship { namespace Ship {
bool SDLAudioPlayer::Init(void) { bool SDLAudioPlayer::Init(void) {
if (SDL_Init(SDL_INIT_AUDIO) != 0) { if (SDL_Init(SDL_INIT_AUDIO) != 0) {
SPDLOG_ERROR("SDL init error: %s\n", SDL_GetError()); SPDLOG_ERROR("SDL init error: %s\n", SDL_GetError());
return false; return false;
} }
SDL_AudioSpec want, have; SDL_AudioSpec want, have;
SDL_zero(want); SDL_zero(want);
want.freq = 44000; want.freq = this->GetSampleRate();
want.format = AUDIO_S16; want.format = AUDIO_S16;
want.channels = 2; want.channels = 2;
want.samples = 1024; want.samples = 1024;
want.callback = NULL; want.callback = NULL;
Device = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0); Device = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
if (Device == 0) { if (Device == 0) {
SPDLOG_ERROR("SDL_OpenAudio error: {}", SDL_GetError()); SPDLOG_ERROR("SDL_OpenAudio error: {}", SDL_GetError());
return false; return false;
} }
SDL_PauseAudioDevice(Device, 0); SDL_PauseAudioDevice(Device, 0);
return true; return true;
} }
int SDLAudioPlayer::Buffered(void) { int SDLAudioPlayer::Buffered(void) {
// 4 is sizeof(int16_t) * num_channels (2 for stereo) // 4 is sizeof(int16_t) * num_channels (2 for stereo)
return SDL_GetQueuedAudioSize(Device) / 4; return SDL_GetQueuedAudioSize(Device) / 4;
} }
int SDLAudioPlayer::GetDesiredBuffered(void) { int SDLAudioPlayer::GetDesiredBuffered(void) {
return 1680; return 1680;
} }
void SDLAudioPlayer::Play(const uint8_t* Buffer, uint32_t BufferLen) { void SDLAudioPlayer::Play(const uint8_t* Buffer, uint32_t BufferLen) {
if (Buffered() < 6000) { if (Buffered() < 6000) {
// Don't fill the audio buffer too much in case this happens // Don't fill the audio buffer too much in case this happens
SDL_QueueAudio(Device, Buffer, BufferLen); SDL_QueueAudio(Device, Buffer, BufferLen);
} }
} }
} }

View file

@ -30,8 +30,8 @@ namespace Ship {
WAVEFORMATEX desired; WAVEFORMATEX desired;
desired.wFormatTag = WAVE_FORMAT_PCM; desired.wFormatTag = WAVE_FORMAT_PCM;
desired.nChannels = 2; desired.nChannels = 2;
desired.nSamplesPerSec = 44100; desired.nSamplesPerSec = this->GetSampleRate();
desired.nAvgBytesPerSec = 44100 * 2 * 2; desired.nAvgBytesPerSec = desired.nSamplesPerSec * 2 * 2;
desired.nBlockAlign = 4; desired.nBlockAlign = 4;
desired.wBitsPerSample = 16; desired.wBitsPerSample = 16;
desired.cbSize = 0; desired.cbSize = 0;