4 * ***** BEGIN LGPL LICENSE BLOCK *****
6 * Copyright 2009 Jörg Hermann Müller
8 * This file is part of AudaSpace.
10 * AudaSpace is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * AudaSpace is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with AudaSpace. If not, see <http://www.gnu.org/licenses/>.
23 * ***** END LGPL LICENSE BLOCK *****
26 #include "AUD_SDLMixer.h"
27 #include "AUD_SDLDevice.h"
28 #include "AUD_IReader.h"
32 // this is the callback function for SDL, it only calls the class
33 void mixAudio(void *data, Uint8* buffer, int length)
35 AUD_SDLDevice* device = (AUD_SDLDevice*)data;
36 device->SDLmix((sample_t *)buffer, length);
39 AUD_SDLDevice::AUD_SDLDevice(AUD_Specs specs, int buffersize)
41 if(specs.channels == AUD_CHANNELS_INVALID)
42 specs.channels = AUD_CHANNELS_STEREO;
43 if(specs.format == AUD_FORMAT_INVALID)
44 specs.format = AUD_FORMAT_S16;
45 if(specs.rate == AUD_RATE_INVALID)
46 specs.rate = AUD_RATE_44100;
50 SDL_AudioSpec format, obtained;
52 format.freq = m_specs.rate;
53 if(m_specs.format == AUD_FORMAT_U8)
54 format.format = AUDIO_U8;
56 format.format = AUDIO_S16SYS;
57 format.channels = m_specs.channels;
58 format.samples = buffersize;
59 format.callback = &mixAudio;
60 format.userdata = this;
62 if(SDL_OpenAudio(&format, &obtained) != 0)
63 AUD_THROW(AUD_ERROR_SDL);
65 m_specs.rate = (AUD_SampleRate)obtained.freq;
66 m_specs.channels = (AUD_Channels)obtained.channels;
67 if(obtained.format == AUDIO_U8)
68 m_specs.format = AUD_FORMAT_U8;
69 else if(obtained.format == AUDIO_S16LSB || obtained.format == AUDIO_S16MSB)
70 m_specs.format = AUD_FORMAT_S16;
72 AUD_THROW(AUD_ERROR_SDL);
74 m_mixer = new AUD_SDLMixer(); AUD_NEW("mixer")
75 m_mixer->setSpecs(m_specs);
80 AUD_SDLDevice::~AUD_SDLDevice()
89 void AUD_SDLDevice::SDLmix(sample_t* buffer, int length)
91 mix(buffer, length/AUD_SAMPLE_SIZE(m_specs));
94 void AUD_SDLDevice::playing(bool playing)
96 SDL_PauseAudio(playing ? 0 : 1);