]>
Dogcows Code - chaz/yoink/blob - src/Moof/Sound.cc
368e9388bca4aee4f3665ae40b1abf1afa71f6cd
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
32 #include <SDL/SDL_sound.h>
36 #include "Mippleton.hh"
39 #define BUFFER_SIZE (8 * 4096)
47 static ALenum
getAudioFormat(const Sound_AudioInfo
& audioInfo
)
49 if (audioInfo
.format
== AUDIO_U8
|| audioInfo
.format
== AUDIO_S8
)
51 if (audioInfo
.channels
== 1) return AL_FORMAT_MONO8
;
52 else return AL_FORMAT_STEREO8
;
56 if (audioInfo
.channels
== 1) return AL_FORMAT_MONO16
;
57 else return AL_FORMAT_STEREO16
;
61 struct Buffer
: public Mippleton
<Buffer
>
63 Buffer(const std::string
& name
) :
64 Mippleton
<Buffer
>(name
)
72 alDeleteBuffers(2, objects
);
74 if (sound
) Sound_FreeSample(sound
);
78 void loadFromFile(const std::string
& filePath
, bool stream
)
80 if (objects
[0] != 0) return;
82 sound
= Sound_NewSampleFromFile(filePath
.c_str(),
87 logWarning("audio not found: %s", getName().c_str());
88 throw Exception(Exception::FILE_NOT_FOUND
);
93 unsigned decoded
= Sound_DecodeAll(sound
);
96 logWarning("decoded not bytes from %s", getName().c_str());
97 throw Exception(Exception::FILE_NOT_FOUND
);
100 alGenBuffers(2, objects
);
101 alBufferData(objects
[0], getAudioFormat(sound
->actual
), sound
->buffer
,
102 sound
->buffer_size
, sound
->actual
.rate
);
103 logDebug("buffer size: %d", sound
->buffer_size
);
104 logDebug(" channels: %d", sound
->actual
.channels
);
105 logDebug(" format: %d", sound
->actual
.format
);
106 logDebug(" frequency: %d", sound
->actual
.rate
);
108 Sound_FreeSample(sound
);
113 logDebug("buffer size: %d", sound
->buffer_size
);
114 logDebug(" channels: %d", sound
->actual
.channels
);
115 logDebug(" format: %d", sound
->actual
.format
);
116 logDebug(" frequency: %d", sound
->actual
.rate
);
117 alGenBuffers(2, objects
);
121 bool stream(ALuint buffer
)
123 int bytes
= Sound_Decode(sound
);
125 if (bytes
< BUFFER_SIZE
) return false;
127 alBufferData(buffer
, getAudioFormat(sound
->actual
), sound
->buffer
,
128 sound
->buffer_size
, sound
->actual
.rate
);
135 //ALfloat location[] = {0.0f, 0.0f, 0.0f};
136 //ALfloat location2[] = {0.0f, 0.0f, 0.0f};
137 //ALfloat orient[] = {0.0f, 0.0f, -1.0f, 0.0, 1.0, 0.0};
140 //alListenerfv(AL_POSITION, location);
141 //alListenerfv(AL_VELOCITY, location);
142 //alListenerfv(AL_VELOCITY, orient);
145 Impl(const std::string
& name
, bool stream
= false) :
146 buffer_(Buffer::getInstance(name
))
148 if (!stream
) buffer_
->loadFromFile(Sound::getPath(name
), stream
);
149 else buffer_
->loadFromFile(SoundStream::getPath(name
), stream
);
151 ALfloat location
[] = {0.0f
, 0.0f
, 0.0f
};
153 alGenSources(1, &source_
);
155 alSourcef(source_
, AL_PITCH
, 1.0f
);
156 alSourcef(source_
, AL_GAIN
, 1.0f
);
157 alSourcefv(source_
, AL_POSITION
, location
);
158 alSourcefv(source_
, AL_VELOCITY
, location
);
159 alSourcei(source_
, AL_LOOPING
, AL_FALSE
);
163 alSourcei(source_
, AL_BUFFER
, buffer_
->objects
[0]);
167 buffer_
->stream(buffer_
->objects
[0]);
168 buffer_
->stream(buffer_
->objects
[1]);
170 alSourceQueueBuffers(source_
, 2, buffer_
->objects
);
176 alDeleteSources(1, &source_
);
184 alGetSourcei(source_
, AL_BUFFERS_PROCESSED
, &finished
);
186 while (finished
-- > 0)
190 alSourceUnqueueBuffers(source_
, 1, &buffer
);
191 buffer_
->stream(buffer
);
192 alSourceQueueBuffers(source_
, 1, &buffer
);
197 boost::shared_ptr
<Buffer
> buffer_
;
203 Sound::Sound(const std::string
& name
) :
205 impl_(new Sound::Impl(name
)) {}
210 //alSourceRewind(impl_->source_);
211 alSourcePlay(impl_
->source_
);
212 impl_
->playing
= true;
217 alSourcePause(impl_
->source_
);
218 impl_
->playing
= false;
221 void Sound::togglePlayPause()
223 if (impl_
->playing
) pause();
227 void Sound::setGain(Scalar gain
)
229 alSourcef(impl_
->source_
, AL_GAIN
, gain
);
233 std::string
Sound::getPath(const std::string
& name
)
235 std::string path
= Resource::getPath("sounds/" + name
+ ".ogg");
240 //##############################################################################
243 SoundStream::SoundStream(const std::string
& name
)
245 //impl_(name, true) {}
247 impl_
= boost::shared_ptr
<Sound::Impl
>(new Sound::Impl(name
, true));
251 void SoundStream::update(Scalar t
, Scalar dt
)
258 std::string
SoundStream::getPath(const std::string
& name
)
260 std::string path
= Resource::getPath("sounds/" + name
+ ".xm");
267 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.047779 seconds and 4 git commands to generate.