summaryrefslogtreecommitdiff
path: root/src/device_sdl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/device_sdl.cpp')
-rw-r--r--src/device_sdl.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/device_sdl.cpp b/src/device_sdl.cpp
new file mode 100644
index 0000000..042c4dd
--- /dev/null
+++ b/src/device_sdl.cpp
@@ -0,0 +1,84 @@
+#include <algorithm>
+#include <string>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "device_sdl.h"
+#include "debug.h"
+
+
+namespace audiere {
+
+ SDLAudioDevice*
+ SDLAudioDevice::create(const ParameterList& parameters) {
+
+ SDL_AudioSpec aspec;
+ aspec.format = AUDIO_S16LSB;
+ aspec.freq = 44100;
+ aspec.channels = 2;
+ aspec.samples = 1024;
+ aspec.callback = &SDLAudioDevice::outputCallback;
+
+ SDLAudioDevice* device = new SDLAudioDevice(aspec.freq, 4096);
+ aspec.userdata = device;
+
+ if (SDL_Init(SDL_INIT_AUDIO) < 0) {
+ ADR_LOG("Unable init sdl audio subsystem.\n");
+ delete device;
+ return 0;
+ }
+
+ SDL_AudioSpec obtained;
+ if (SDL_OpenAudio(&aspec, &obtained) < 0) {
+ ADR_LOG("Unable open sdl audio subsystem.\n");
+ delete device;
+ return 0;
+ }
+ if (aspec.format != obtained.format ||
+ aspec.freq != obtained.freq ||
+ aspec.channels != obtained.channels) {
+ ADR_LOG("Unsupported audio parameters.\n");
+ delete device;
+ return 0;
+ }
+
+ SDL_PauseAudio(0);
+ return device;
+ }
+
+ void SDLAudioDevice::outputCallback(void *user_data, Uint8 *stream, int len)
+ {
+ SDLAudioDevice *device = (SDLAudioDevice*)user_data;
+ device->read(len / 4, stream);
+ }
+
+ SDLAudioDevice::SDLAudioDevice(int rate, int buffer_size)
+ : MixerDevice(rate)
+ {
+ m_buffer_size = buffer_size;
+ m_buffer = new char [buffer_size];
+ }
+
+
+ SDLAudioDevice::~SDLAudioDevice() {
+ ADR_GUARD("SDLAudioDevice::~SDLAudioDevice");
+
+ SDL_CloseAudio();
+ SDL_QuitSubSystem(SDL_INIT_AUDIO);
+
+ delete [] m_buffer;
+ }
+
+
+ void ADR_CALL
+ SDLAudioDevice::update() {
+ AI_Sleep(1);
+ }
+
+
+ const char* ADR_CALL
+ SDLAudioDevice::getName() {
+ return "sdl";
+ }
+
+}