2009-08-09 21:16:39 +00:00
|
|
|
/*
|
2011-02-18 23:47:37 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2009-08-09 21:16:39 +00:00
|
|
|
*
|
2011-02-18 23:47:37 +00:00
|
|
|
* Copyright 2009-2011 Jörg Hermann Müller
|
2009-08-09 21:16:39 +00:00
|
|
|
*
|
|
|
|
* This file is part of AudaSpace.
|
|
|
|
*
|
2011-02-18 23:47:37 +00:00
|
|
|
* Audaspace is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
2009-08-09 21:16:39 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* AudaSpace is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2011-02-18 23:47:37 +00:00
|
|
|
* GNU General Public License for more details.
|
2009-08-09 21:16:39 +00:00
|
|
|
*
|
2011-02-18 23:47:37 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Audaspace; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-08-09 21:16:39 +00:00
|
|
|
*
|
2011-02-18 23:47:37 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2009-08-09 21:16:39 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-25 10:21:56 +00:00
|
|
|
/** \file audaspace/SDL/AUD_SDLDevice.cpp
|
|
|
|
* \ingroup audsdl
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-08-09 21:16:39 +00:00
|
|
|
#include "AUD_SDLDevice.h"
|
|
|
|
#include "AUD_IReader.h"
|
|
|
|
|
2009-08-16 14:53:11 +00:00
|
|
|
void AUD_SDLDevice::SDL_mix(void *data, Uint8* buffer, int length)
|
2009-08-09 21:16:39 +00:00
|
|
|
{
|
|
|
|
AUD_SDLDevice* device = (AUD_SDLDevice*)data;
|
2009-08-16 14:53:11 +00:00
|
|
|
|
2010-01-01 05:09:30 +00:00
|
|
|
device->mix((data_t*)buffer,length/AUD_DEVICE_SAMPLE_SIZE(device->m_specs));
|
2009-08-09 21:16:39 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 08:07:21 +00:00
|
|
|
static const char* open_error = "AUD_SDLDevice: Device couldn't be opened.";
|
|
|
|
static const char* format_error = "AUD_SDLDevice: Obtained unsupported sample "
|
|
|
|
"format.";
|
|
|
|
|
2010-01-01 05:09:30 +00:00
|
|
|
AUD_SDLDevice::AUD_SDLDevice(AUD_DeviceSpecs specs, int buffersize)
|
2009-08-09 21:16:39 +00:00
|
|
|
{
|
|
|
|
if(specs.channels == AUD_CHANNELS_INVALID)
|
|
|
|
specs.channels = AUD_CHANNELS_STEREO;
|
|
|
|
if(specs.format == AUD_FORMAT_INVALID)
|
|
|
|
specs.format = AUD_FORMAT_S16;
|
|
|
|
if(specs.rate == AUD_RATE_INVALID)
|
|
|
|
specs.rate = AUD_RATE_44100;
|
|
|
|
|
|
|
|
m_specs = specs;
|
|
|
|
|
|
|
|
SDL_AudioSpec format, obtained;
|
|
|
|
|
|
|
|
format.freq = m_specs.rate;
|
|
|
|
if(m_specs.format == AUD_FORMAT_U8)
|
|
|
|
format.format = AUDIO_U8;
|
|
|
|
else
|
|
|
|
format.format = AUDIO_S16SYS;
|
|
|
|
format.channels = m_specs.channels;
|
|
|
|
format.samples = buffersize;
|
2009-08-16 14:53:11 +00:00
|
|
|
format.callback = AUD_SDLDevice::SDL_mix;
|
2009-08-09 21:16:39 +00:00
|
|
|
format.userdata = this;
|
|
|
|
|
|
|
|
if(SDL_OpenAudio(&format, &obtained) != 0)
|
2010-08-03 08:07:21 +00:00
|
|
|
AUD_THROW(AUD_ERROR_SDL, open_error);
|
2009-08-09 21:16:39 +00:00
|
|
|
|
|
|
|
m_specs.rate = (AUD_SampleRate)obtained.freq;
|
|
|
|
m_specs.channels = (AUD_Channels)obtained.channels;
|
|
|
|
if(obtained.format == AUDIO_U8)
|
|
|
|
m_specs.format = AUD_FORMAT_U8;
|
|
|
|
else if(obtained.format == AUDIO_S16LSB || obtained.format == AUDIO_S16MSB)
|
|
|
|
m_specs.format = AUD_FORMAT_S16;
|
|
|
|
else
|
2010-08-03 08:07:21 +00:00
|
|
|
{
|
|
|
|
SDL_CloseAudio();
|
|
|
|
AUD_THROW(AUD_ERROR_SDL, format_error);
|
|
|
|
}
|
2009-08-09 21:16:39 +00:00
|
|
|
|
|
|
|
create();
|
|
|
|
}
|
|
|
|
|
|
|
|
AUD_SDLDevice::~AUD_SDLDevice()
|
|
|
|
{
|
|
|
|
lock();
|
|
|
|
SDL_CloseAudio();
|
|
|
|
unlock();
|
|
|
|
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AUD_SDLDevice::playing(bool playing)
|
|
|
|
{
|
|
|
|
SDL_PauseAudio(playing ? 0 : 1);
|
|
|
|
}
|