Blender V2.61 - r43446

AUD_SDLDevice.cpp

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