Use audio buffer

This commit is contained in:
Christian Semmler 2024-12-18 13:50:10 -07:00
parent a9f6b97373
commit 61c760d9d4
2 changed files with 41 additions and 36 deletions

View File

@ -90,13 +90,14 @@ class MxWavePresenter : public MxSoundPresenter {
WaveFormat* m_waveFormat; // 0x54 WaveFormat* m_waveFormat; // 0x54
// [library:audio] // [library:audio]
// If MxDSAction::looping is set, we keep the entire audio in memory and use the paged buffer `m_pb`. // If MxDSAction::looping is set, we keep the entire audio in memory and use `m_ab`.
// In (most) other cases, data is streamed through the ring buffer `m_rb`. // In (most) other cases, data is streamed through the ring buffer `m_rb`.
ma_pcm_rb m_rb; ma_pcm_rb m_rb;
struct { struct {
ma_paged_audio_buffer buffer; ma_audio_buffer buffer;
ma_paged_audio_buffer_data data; MxU8* data;
} m_pb; MxU32 offset;
} m_ab;
ma_sound m_sound; ma_sound m_sound;
MxU32 m_chunkLength; // 0x5c MxU32 m_chunkLength; // 0x5c

View File

@ -20,7 +20,7 @@ void MxWavePresenter::Init()
{ {
m_waveFormat = NULL; m_waveFormat = NULL;
SDL_zero(m_rb); SDL_zero(m_rb);
SDL_zero(m_pb); SDL_zero(m_ab);
SDL_zero(m_sound); SDL_zero(m_sound);
m_chunkLength = 0; m_chunkLength = 0;
m_started = FALSE; m_started = FALSE;
@ -40,9 +40,9 @@ MxResult MxWavePresenter::AddToManager()
void MxWavePresenter::Destroy(MxBool p_fromDestructor) void MxWavePresenter::Destroy(MxBool p_fromDestructor)
{ {
ma_sound_uninit(&m_sound); ma_sound_uninit(&m_sound);
ma_paged_audio_buffer_uninit(&m_pb.buffer);
ma_paged_audio_buffer_data_uninit(&m_pb.data, NULL);
ma_pcm_rb_uninit(&m_rb); ma_pcm_rb_uninit(&m_rb);
ma_audio_buffer_uninit(&m_ab.buffer);
delete[] m_ab.data;
if (m_waveFormat) { if (m_waveFormat) {
delete[] ((MxU8*) m_waveFormat); delete[] ((MxU8*) m_waveFormat);
@ -59,16 +59,8 @@ void MxWavePresenter::Destroy(MxBool p_fromDestructor)
MxBool MxWavePresenter::WriteToSoundBuffer(void* p_audioPtr, MxU32 p_length) MxBool MxWavePresenter::WriteToSoundBuffer(void* p_audioPtr, MxU32 p_length)
{ {
if (m_action->IsLooping()) { if (m_action->IsLooping()) {
// MA_API ma_result ma_paged_audio_buffer_data_allocate_and_append_page(ma_paged_audio_buffer_data* pData, memcpy(m_ab.data + m_ab.offset, p_audioPtr, p_length);
// ma_uint32 pageSizeInFrames, const void* pInitialData, const ma_allocation_callbacks* pAllocationCallbacks); m_ab.offset += p_length;
ma_uint32 bpF = ma_get_bytes_per_frame(m_pb.data.format, m_pb.data.channels);
char asd[123];
sprintf(asd, "bpF is: %d and length is: %d which makes for %d\n", bpF, p_length, p_length / bpF);
OutputDebugString(asd);
ma_paged_audio_buffer_data_allocate_and_append_page(&m_pb.data, p_length / bpF, p_audioPtr, NULL);
return TRUE; return TRUE;
} }
else { else {
@ -134,28 +126,31 @@ void MxWavePresenter::StartingTickle()
m_silenceData = 0; m_silenceData = 0;
} }
if (m_action->IsLooping()) { ma_format format = m_waveFormat->m_bitsPerSample == 16 ? ma_format_s16 : ma_format_u8;
if (ma_paged_audio_buffer_data_init( ma_uint32 channels = m_waveFormat->m_channels;
m_waveFormat->m_bitsPerSample == 16 ? ma_format_s16 : ma_format_u8, ma_uint32 sampleRate = m_waveFormat->m_samplesPerSec;
m_waveFormat->m_channels,
&m_pb.data
) != MA_SUCCESS) {
goto done;
}
ma_paged_audio_buffer_config config = ma_paged_audio_buffer_config_init(&m_pb.data); if (m_action->IsLooping()) {
if (ma_paged_audio_buffer_init(&config, &m_pb.buffer) != MA_SUCCESS) { ma_uint32 sizeInFrames = ma_calculate_buffer_size_in_frames_from_milliseconds(
m_action->GetDuration() / m_action->GetLoopCount(),
sampleRate
);
m_ab.data = new MxU8[ma_get_bytes_per_frame(format, channels) * sizeInFrames];
ma_audio_buffer_config config =
ma_audio_buffer_config_init(format, channels, sizeInFrames, m_ab.data, NULL);
config.sampleRate = sampleRate;
if (ma_audio_buffer_init(&config, &m_ab.buffer) != MA_SUCCESS) {
goto done; goto done;
} }
} }
else { else {
if (ma_pcm_rb_init( if (ma_pcm_rb_init(
m_waveFormat->m_bitsPerSample == 16 ? ma_format_s16 : ma_format_u8, format,
m_waveFormat->m_channels, channels,
ma_calculate_buffer_size_in_frames_from_milliseconds( ma_calculate_buffer_size_in_frames_from_milliseconds(g_rbSizeInMilliseconds, sampleRate),
g_rbSizeInMilliseconds,
m_waveFormat->m_samplesPerSec
),
NULL, NULL,
NULL, NULL,
&m_rb &m_rb
@ -163,12 +158,12 @@ void MxWavePresenter::StartingTickle()
goto done; goto done;
} }
ma_pcm_rb_set_sample_rate(&m_rb, m_waveFormat->m_samplesPerSec); ma_pcm_rb_set_sample_rate(&m_rb, sampleRate);
} }
if (ma_sound_init_from_data_source( if (ma_sound_init_from_data_source(
MSoundManager()->GetEngine(), MSoundManager()->GetEngine(),
m_action->IsLooping() ? &m_pb.buffer.ds : &m_rb.ds, m_action->IsLooping() ? (ma_data_source*) &m_ab.buffer : (ma_data_source*) &m_rb,
m_is3d ? 0 : MA_SOUND_FLAG_NO_SPATIALIZATION, m_is3d ? 0 : MA_SOUND_FLAG_NO_SPATIALIZATION,
NULL, NULL,
&m_sound &m_sound
@ -352,7 +347,16 @@ void MxWavePresenter::Resume()
{ {
if (m_paused) { if (m_paused) {
if (ma_sound_get_engine(&m_sound) && m_started) { if (ma_sound_get_engine(&m_sound) && m_started) {
ma_sound_start(&m_sound); switch (m_currentTickleState) {
case e_streaming:
case e_repeating:
ma_sound_start(&m_sound);
break;
case e_done:
if (!ma_sound_at_end(&m_sound)) {
ma_sound_start(&m_sound);
}
}
} }
m_paused = FALSE; m_paused = FALSE;