]>
Dogcows Code - chaz/yoink/blob - src/Moof/Sound.cc
9a632ebf3ace404b31787b16f227c70fecba4d60
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 *******************************************************************************/
33 #include <SDL/SDL_sound.h>
36 #include "Mippleton.hh"
46 static ALenum
getAudioFormat(const Sound_AudioInfo
& audioInfo
)
48 if (audioInfo
.format
== AUDIO_U8
|| audioInfo
.format
== AUDIO_S8
)
50 if (audioInfo
.channels
== 1) return AL_FORMAT_MONO8
;
51 else return AL_FORMAT_STEREO8
;
55 if (audioInfo
.channels
== 1) return AL_FORMAT_MONO16
;
56 else return AL_FORMAT_STEREO16
;
60 struct Buffer
: public Mippleton
<Buffer
>
62 Buffer(const std::string
& name
) :
63 Mippleton
<Buffer
>(name
),
69 alDeleteBuffers(1, &object
);
71 void loadFromFile(const std::string
& filePath
, bool stream
)
73 if (object
!= 0) return;
75 Sound_Sample
* sound
= Sound_NewSampleFromFile(filePath
.c_str(),
80 std::cerr
<< "could not load sound from file" << std::endl
;
84 unsigned decoded
= Sound_DecodeAll(sound
);
87 std::cout
<< "decoded no bytes" << std::endl
;
90 std::cerr
<< "buffer size: " << sound
->buffer_size
<< std::endl
;
91 std::cerr
<< "channels: " << (int)sound
->actual
.channels
<< std::endl
;
92 std::cerr
<< "format: " << sound
->actual
.format
<< std::endl
;
93 std::cerr
<< "frequency: " << sound
->actual
.rate
<< std::endl
;
95 alGenBuffers(1, &object
);
96 alBufferData(object
, getAudioFormat(sound
->actual
), sound
->buffer
,
97 sound
->buffer_size
, sound
->actual
.rate
);
99 Sound_FreeSample(sound
);
104 //ALfloat location[] = {0.0f, 0.0f, 0.0f};
105 //ALfloat location2[] = {0.0f, 0.0f, 0.0f};
106 //ALfloat orient[] = {0.0f, 0.0f, -1.0f, 0.0, 1.0, 0.0};
109 //alListenerfv(AL_POSITION, location);
110 //alListenerfv(AL_VELOCITY, location);
111 //alListenerfv(AL_VELOCITY, orient);
114 Impl(const std::string
& name
, bool stream
= false) :
115 buffer_(Buffer::retain(name
), Buffer::release
)
117 if (!stream
) buffer_
->loadFromFile(Sound::getPath(name
), stream
);
118 else buffer_
->loadFromFile(SoundStream::getPath(name
), stream
);
120 ALfloat location
[] = {0.0f
, 0.0f
, 0.0f
};
122 alGenSources(1, &source_
);
123 alSourcei(source_
, AL_BUFFER
, buffer_
->object
);
124 alSourcef(source_
, AL_PITCH
, 1.0f
);
125 alSourcef(source_
, AL_GAIN
, 1.0f
);
126 alSourcefv(source_
, AL_POSITION
, location
);
127 alSourcefv(source_
, AL_VELOCITY
, location
);
128 alSourcei(source_
, AL_LOOPING
, AL_FALSE
);
133 alDeleteSources(1, &source_
);
142 boost::shared_ptr
<Buffer
> buffer_
;
147 Sound::Sound(const std::string
& name
) :
149 impl_(new Sound::Impl(name
)) {}
154 alSourceRewind(impl_
->source_
);
155 alSourcePlay(impl_
->source_
);
159 std::string
Sound::getPath(const std::string
& name
)
161 std::string path
= Resource::getPath("sounds/" + name
+ ".ogg");
166 //##############################################################################
169 SoundStream::SoundStream(const std::string
& name
)
171 //impl_(name, true) {}
173 impl_
= boost::shared_ptr
<Sound::Impl
>(new Sound::Impl(name
, true));
177 void SoundStream::update(Scalar t
, Scalar dt
)
184 std::string
SoundStream::getPath(const std::string
& name
)
186 std::string path
= Resource::getPath("sounds/" + name
+ ".xm");
193 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.039682 seconds and 4 git commands to generate.