Blender  V2.59
AUD_SDLDevice.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: AUD_SDLDevice.cpp 35141 2011-02-25 10:21:56Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * Copyright 2009-2011 Jörg Hermann Müller
00007  *
00008  * This file is part of AudaSpace.
00009  *
00010  * Audaspace is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * AudaSpace is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with Audaspace; if not, write to the Free Software Foundation,
00022  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00023  *
00024  * ***** END GPL LICENSE BLOCK *****
00025  */
00026 
00032 #include "AUD_SDLDevice.h"
00033 #include "AUD_IReader.h"
00034 
00035 void AUD_SDLDevice::SDL_mix(void *data, Uint8* buffer, int length)
00036 {
00037         AUD_SDLDevice* device = (AUD_SDLDevice*)data;
00038 
00039         device->mix((data_t*)buffer,length/AUD_DEVICE_SAMPLE_SIZE(device->m_specs));
00040 }
00041 
00042 static const char* open_error = "AUD_SDLDevice: Device couldn't be opened.";
00043 static const char* format_error = "AUD_SDLDevice: Obtained unsupported sample "
00044                                                                   "format.";
00045 
00046 AUD_SDLDevice::AUD_SDLDevice(AUD_DeviceSpecs specs, int buffersize)
00047 {
00048         if(specs.channels == AUD_CHANNELS_INVALID)
00049                 specs.channels = AUD_CHANNELS_STEREO;
00050         if(specs.format == AUD_FORMAT_INVALID)
00051                 specs.format = AUD_FORMAT_S16;
00052         if(specs.rate == AUD_RATE_INVALID)
00053                 specs.rate = AUD_RATE_44100;
00054 
00055         m_specs = specs;
00056 
00057         SDL_AudioSpec format, obtained;
00058 
00059         format.freq = m_specs.rate;
00060         if(m_specs.format == AUD_FORMAT_U8)
00061                 format.format = AUDIO_U8;
00062         else
00063                 format.format = AUDIO_S16SYS;
00064         format.channels = m_specs.channels;
00065         format.samples = buffersize;
00066         format.callback = AUD_SDLDevice::SDL_mix;
00067         format.userdata = this;
00068 
00069         if(SDL_OpenAudio(&format, &obtained) != 0)
00070                 AUD_THROW(AUD_ERROR_SDL, open_error);
00071 
00072         m_specs.rate = (AUD_SampleRate)obtained.freq;
00073         m_specs.channels = (AUD_Channels)obtained.channels;
00074         if(obtained.format == AUDIO_U8)
00075                 m_specs.format = AUD_FORMAT_U8;
00076         else if(obtained.format == AUDIO_S16LSB || obtained.format == AUDIO_S16MSB)
00077                 m_specs.format = AUD_FORMAT_S16;
00078         else
00079         {
00080                 SDL_CloseAudio();
00081                 AUD_THROW(AUD_ERROR_SDL, format_error);
00082         }
00083 
00084         create();
00085 }
00086 
00087 AUD_SDLDevice::~AUD_SDLDevice()
00088 {
00089         lock();
00090         SDL_CloseAudio();
00091         unlock();
00092 
00093         destroy();
00094 }
00095 
00096 void AUD_SDLDevice::playing(bool playing)
00097 {
00098         SDL_PauseAudio(playing ? 0 : 1);
00099 }