From 47e2d6bd1d05859e6941c89aee4e5a01d2bda59f Mon Sep 17 00:00:00 2001 From: AlaskanEmily Date: Tue, 10 Sep 2019 00:20:08 -0700 Subject: [PATCH] Add API call to play a sound with looping, and support in dsound and openal backends --- src/cinnamon.h | 21 +++++++++++++++++++++ src/dsound/cin_dsound.cpp | 7 +++++++ src/dsound/cin_dsound_sound.cpp | 12 ++++++++++-- src/openal/cin_openal.c | 9 +++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/cinnamon.h b/src/cinnamon.h index 1a268a6..fe451b9 100755 --- a/src/cinnamon.h +++ b/src/cinnamon.h @@ -102,8 +102,29 @@ enum Cin_SoundError { */ CIN_EXPORT(unsigned) Cin_StructSoundSize(); +/** + * @brief Causes a sound to play. + * + * This will trigger the sound to play without looping. + * + * Calling play on a sound that is already playing will do nothing. To restart + * a sound, use Cin_SoundStop first. + */ CIN_EXPORT(enum Cin_SoundError) Cin_SoundPlay(struct Cin_Sound *snd); +/** + * @brief Causes a sound to play with optional looping. + * + * The sound will loop if the loop param is non-zero. + * + * Calling play on a sound that is already playing will do nothing. To restart + * a sound, use Cin_SoundStop first. + * + * @sa Cin_SoundPlay + */ +CIN_EXPORT(enum Cin_SoundError) Cin_SoundPlayLoop(struct Cin_Sound *snd, + int loop); + CIN_EXPORT(enum Cin_SoundError) Cin_SoundStop(struct Cin_Sound *snd); CIN_EXPORT(void) Cin_DestroySound(struct Cin_Sound *snd); diff --git a/src/dsound/cin_dsound.cpp b/src/dsound/cin_dsound.cpp index ff9db3f..dd53b05 100755 --- a/src/dsound/cin_dsound.cpp +++ b/src/dsound/cin_dsound.cpp @@ -65,6 +65,13 @@ CIN_EXPORT(enum Cin_SoundError) Cin_SoundPlay(Cin_Sound *snd){ /////////////////////////////////////////////////////////////////////////////// +CIN_EXPORT(enum Cin_SoundError) Cin_SoundPlayLoop(Cin_Sound *snd, int loop){ + snd->play(!!loop); + return Cin_eSoundSuccess; +} + +/////////////////////////////////////////////////////////////////////////////// + CIN_EXPORT(enum Cin_SoundError) Cin_SoundStop(Cin_Sound *snd){ snd->stop(); return Cin_eSoundSuccess; diff --git a/src/dsound/cin_dsound_sound.cpp b/src/dsound/cin_dsound_sound.cpp index 1344c0c..84bc11d 100755 --- a/src/dsound/cin_dsound_sound.cpp +++ b/src/dsound/cin_dsound_sound.cpp @@ -7,6 +7,8 @@ #include "cinnamon.h" #include "cin_soft_loader.h" +#include + #include #include #include @@ -105,6 +107,7 @@ Cin_Sound::Cin_Sound(IDirectSound8 *dsound, const Cin_Loader &ld) fmt.Format.wBitsPerSample = bytes_per_sample << 3; // Check the size of the loader data. + const unsigned byte_size = ld.bytes_placed; #ifndef NDEBUG { @@ -115,7 +118,8 @@ Cin_Sound::Cin_Sound(IDirectSound8 *dsound, const Cin_Loader &ld) assert(debug_byte_size == byte_size); } #endif - + + DSBUFFERDESC descriptor; descriptor.dwSize = sizeof(DSBUFFERDESC); descriptor.dwFlags = DSBCAPS_GLOBALFOCUS; @@ -135,7 +139,11 @@ Cin_Sound::Cin_Sound(IDirectSound8 *dsound, const Cin_Loader &ld) buffer_size == byte_size){ Cin_LoaderMemcpy(ld.first, 0, buffer_data, byte_size); - m_buffer->Unlock(buffer_data, byte_size, NULL, 0); + if(m_buffer->Unlock(buffer_data, byte_size, NULL, 0) != DS_OK){ + m_dsound->Release(); + m_buffer->Release(); + m_buffer = NULL; + } } else{ m_dsound->Release(); diff --git a/src/openal/cin_openal.c b/src/openal/cin_openal.c index c86f678..107bafa 100755 --- a/src/openal/cin_openal.c +++ b/src/openal/cin_openal.c @@ -356,6 +356,15 @@ enum Cin_SoundError Cin_SoundPlay(struct Cin_Sound *snd){ /*****************************************************************************/ +enum Cin_SoundError Cin_SoundPlayLoop(struct Cin_Sound *snd, int loop){ + alcMakeContextCurrent(snd->ctx); + alSourcei(snd->snd, AL_LOOPING, (loop==0) ? AL_FALSE : AL_TRUE); + alSourcePlay(snd->snd); + return Cin_eSoundSuccess; +} + +/*****************************************************************************/ + enum Cin_SoundError Cin_SoundStop(struct Cin_Sound *snd){ alcMakeContextCurrent(snd->ctx); alSourceStop(snd->snd); -- 2.11.0