OSDN Git Service

Add API call to play a sound with looping, and support in dsound and openal backends
authorAlaskanEmily <emily@alaskanemily.net>
Tue, 10 Sep 2019 07:20:08 +0000 (00:20 -0700)
committerAlaskanEmily <emily@alaskanemily.net>
Tue, 10 Sep 2019 07:20:08 +0000 (00:20 -0700)
src/cinnamon.h
src/dsound/cin_dsound.cpp
src/dsound/cin_dsound_sound.cpp
src/openal/cin_openal.c

index 1a268a6..fe451b9 100755 (executable)
@@ -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);
index ff9db3f..dd53b05 100755 (executable)
@@ -65,6 +65,13 @@ CIN_EXPORT(enum Cin_SoundError) Cin_SoundPlay(Cin_Sound *snd){
 \r
 ///////////////////////////////////////////////////////////////////////////////\r
 \r
+CIN_EXPORT(enum Cin_SoundError) Cin_SoundPlayLoop(Cin_Sound *snd, int loop){\r
+    snd->play(!!loop);\r
+    return Cin_eSoundSuccess;\r
+}\r
+\r
+///////////////////////////////////////////////////////////////////////////////\r
+\r
 CIN_EXPORT(enum Cin_SoundError) Cin_SoundStop(Cin_Sound *snd){\r
     snd->stop();\r
     return Cin_eSoundSuccess;\r
index 1344c0c..84bc11d 100755 (executable)
@@ -7,6 +7,8 @@
 #include "cinnamon.h"\r
 #include "cin_soft_loader.h"\r
 \r
+#include <assert.h>\r
+\r
 #include <mmreg.h>\r
 #include <ks.h>\r
 #include <ksmedia.h>\r
@@ -105,6 +107,7 @@ Cin_Sound::Cin_Sound(IDirectSound8 *dsound, const Cin_Loader &ld)
     fmt.Format.wBitsPerSample = bytes_per_sample << 3;\r
 \r
     // Check the size of the loader data.\r
+\r
     const unsigned byte_size = ld.bytes_placed;\r
 #ifndef NDEBUG\r
     {\r
@@ -115,7 +118,8 @@ Cin_Sound::Cin_Sound(IDirectSound8 *dsound, const Cin_Loader &ld)
         assert(debug_byte_size == byte_size);\r
     }\r
 #endif\r
-\r
+    \r
+    \r
     DSBUFFERDESC descriptor;\r
     descriptor.dwSize = sizeof(DSBUFFERDESC);\r
     descriptor.dwFlags = DSBCAPS_GLOBALFOCUS;\r
@@ -135,7 +139,11 @@ Cin_Sound::Cin_Sound(IDirectSound8 *dsound, const Cin_Loader &ld)
         buffer_size == byte_size){\r
         \r
         Cin_LoaderMemcpy(ld.first, 0, buffer_data, byte_size);\r
-        m_buffer->Unlock(buffer_data, byte_size, NULL, 0);\r
+        if(m_buffer->Unlock(buffer_data, byte_size, NULL, 0) != DS_OK){\r
+            m_dsound->Release();\r
+            m_buffer->Release();\r
+            m_buffer = NULL;\r
+        }\r
     }\r
     else{\r
         m_dsound->Release();\r
index c86f678..107bafa 100755 (executable)
@@ -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);