Added GetSampleRate function

This commit is contained in:
Kevin Alexis Contreras 2022-06-14 14:04:47 -05:00
parent 9cf537eaff
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 GetDesiredBuffered(void) = 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
pa_sample_spec ss;
ss.format = PA_SAMPLE_S16LE;
ss.rate = 32000;
ss.rate = this->GetSampleRate();
ss.channels = 2;
pa_buffer_attr attr;

View File

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

View File

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