aboutsummaryrefslogtreecommitdiffstats
path: root/examples/common
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2013-06-05 19:44:42 -0700
committerChris Robinson <[email protected]>2013-06-05 19:50:18 -0700
commit056fa2a47443eb40cf40eadae5721c847015813e (patch)
tree4e32387995d755b03e2835dc65f4a5a79eccd537 /examples/common
parentfe5428dc49f7bd0cade45784e4c4fa65c565f14b (diff)
Use SDL_sound for the alstream example
Diffstat (limited to 'examples/common')
-rw-r--r--examples/common/alhelpers.h4
-rw-r--r--examples/common/sdl_sound.c149
-rw-r--r--examples/common/sdl_sound.h43
3 files changed, 196 insertions, 0 deletions
diff --git a/examples/common/alhelpers.h b/examples/common/alhelpers.h
index eda8925e..62ed5be2 100644
--- a/examples/common/alhelpers.h
+++ b/examples/common/alhelpers.h
@@ -9,6 +9,10 @@
#include <windows.h>
#endif
+#include "AL/alc.h"
+#include "AL/al.h"
+#include "AL/alext.h"
+
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
diff --git a/examples/common/sdl_sound.c b/examples/common/sdl_sound.c
new file mode 100644
index 00000000..9e9f1d62
--- /dev/null
+++ b/examples/common/sdl_sound.c
@@ -0,0 +1,149 @@
+/*
+ * SDL_sound Decoder Helpers
+ *
+ * Copyright (c) 2013 by Chris Robinson <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/* This file contains routines for helping to decode audio using SDL_sound.
+ * There's very little OpenAL-specific code here.
+ */
+#include "sdl_sound.h"
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <signal.h>
+#include <assert.h>
+
+#include <SDL_sound.h>
+
+#include "AL/al.h"
+#include "AL/alc.h"
+#include "AL/alext.h"
+
+#include "alhelpers.h"
+
+
+static int done_init = 0;
+
+FilePtr openAudioFile(const char *fname, size_t buftime_ms)
+{
+ FilePtr file;
+ ALuint rate;
+ Uint32 bufsize;
+ ALenum chans, type;
+
+ /* We need to make sure SDL_sound is initialized. */
+ if(!done_init)
+ {
+ Sound_Init();
+ done_init = 1;
+ }
+
+ file = Sound_NewSampleFromFile(fname, NULL, 0);
+ if(!file)
+ {
+ fprintf(stderr, "Failed to open %s: %s\n", fname, Sound_GetError());
+ return NULL;
+ }
+
+ if(getAudioInfo(file, &rate, &chans, &type) != 0)
+ {
+ Sound_FreeSample(file);
+ return NULL;
+ }
+
+ bufsize = FramesToBytes((ALsizei)(buftime_ms/1000.0*rate), chans, type);
+ if(Sound_SetBufferSize(file, bufsize) == 0)
+ {
+ fprintf(stderr, "Failed to set buffer size to %u bytes: %s\n", bufsize, Sound_GetError());
+ Sound_FreeSample(file);
+ return NULL;
+ }
+
+ return file;
+}
+
+void closeAudioFile(FilePtr file)
+{
+ if(file)
+ Sound_FreeSample(file);
+}
+
+
+int getAudioInfo(FilePtr file, ALuint *rate, ALenum *channels, ALenum *type)
+{
+ if(file->actual.channels == 1)
+ *channels = AL_MONO_SOFT;
+ else if(file->actual.channels == 2)
+ *channels = AL_STEREO_SOFT;
+ else
+ {
+ fprintf(stderr, "Unsupported channel count: %d\n", file->actual.channels);
+ return 1;
+ }
+
+ if(file->actual.format == AUDIO_U8)
+ *type = AL_UNSIGNED_BYTE_SOFT;
+ else if(file->actual.format == AUDIO_S8)
+ *type = AL_BYTE_SOFT;
+ else if(file->actual.format == AUDIO_U16SYS)
+ *type = AL_UNSIGNED_SHORT_SOFT;
+ else if(file->actual.format == AUDIO_S16SYS)
+ *type = AL_SHORT_SOFT;
+ else
+ {
+ fprintf(stderr, "Unsupported sample format: 0x%04x\n", file->actual.format);
+ return 1;
+ }
+
+ *rate = file->actual.rate;
+
+ return 0;
+}
+
+
+uint8_t *getAudioData(FilePtr file, size_t *length)
+{
+ *length = Sound_Decode(file);
+ if(*length == 0)
+ return NULL;
+ return file->buffer;
+}
+
+void *decodeAudioStream(FilePtr file, size_t *length)
+{
+ Uint32 got;
+ char *mem;
+
+ got = Sound_DecodeAll(file);
+ if(got == 0)
+ {
+ *length = 0;
+ return NULL;
+ }
+
+ mem = malloc(got);
+ memcpy(mem, file->buffer, got);
+
+ *length = got;
+ return mem;
+}
diff --git a/examples/common/sdl_sound.h b/examples/common/sdl_sound.h
new file mode 100644
index 00000000..e93ab92b
--- /dev/null
+++ b/examples/common/sdl_sound.h
@@ -0,0 +1,43 @@
+#ifndef EXAMPLES_SDL_SOUND_H
+#define EXAMPLES_SDL_SOUND_H
+
+#include "AL/al.h"
+
+#include <SDL_sound.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/* Opaque handles to files and streams. Apps don't need to concern themselves
+ * with the internals */
+typedef Sound_Sample *FilePtr;
+
+/* Opens a file with SDL_sound, and specifies the size of the sample buffer in
+ * milliseconds. */
+FilePtr openAudioFile(const char *fname, size_t buftime_ms);
+
+/* Closes/frees an opened file */
+void closeAudioFile(FilePtr file);
+
+/* Returns information about the given audio stream. Returns 0 on success. */
+int getAudioInfo(FilePtr file, ALuint *rate, ALenum *channels, ALenum *type);
+
+/* Returns a pointer to the next available chunk of decoded audio. The size (in
+ * bytes) of the returned data buffer is stored in 'length', and the returned
+ * pointer is only valid until the next call to getAudioData. */
+uint8_t *getAudioData(FilePtr file, size_t *length);
+
+/* Decodes all remaining data from the stream and returns a buffer containing
+ * the audio data, with the size stored in 'length'. The returned pointer must
+ * be freed with a call to free(). Note that since this decodes the whole
+ * stream, using it on lengthy streams (eg, music) will use a lot of memory.
+ * Such streams are better handled using getAudioData to keep smaller chunks in
+ * memory at any given time. */
+void *decodeAudioStream(FilePtr, size_t *length);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* EXAMPLES_SDL_SOUND_H */