]> Dogcows Code - chaz/yoink/blob - src/Moof/Sound.cc
9a632ebf3ace404b31787b16f227c70fecba4d60
[chaz/yoink] / src / Moof / Sound.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
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.
15
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.
26
27 *******************************************************************************/
28
29 #include <iostream>
30 #include <string>
31
32 #include <SDL/SDL.h>
33 #include <SDL/SDL_sound.h>
34 #include <AL/al.h>
35
36 #include "Mippleton.hh"
37 #include "Sound.hh"
38
39
40 namespace Mf {
41
42
43 struct Sound::Impl
44 {
45
46 static ALenum getAudioFormat(const Sound_AudioInfo& audioInfo)
47 {
48 if (audioInfo.format == AUDIO_U8 || audioInfo.format == AUDIO_S8)
49 {
50 if (audioInfo.channels == 1) return AL_FORMAT_MONO8;
51 else return AL_FORMAT_STEREO8;
52 }
53 else
54 {
55 if (audioInfo.channels == 1) return AL_FORMAT_MONO16;
56 else return AL_FORMAT_STEREO16;
57 }
58 }
59
60 struct Buffer : public Mippleton<Buffer>
61 {
62 Buffer(const std::string& name) :
63 Mippleton<Buffer>(name),
64 object(0)
65 {}
66
67 ~Buffer()
68 {
69 alDeleteBuffers(1, &object);
70 }
71 void loadFromFile(const std::string& filePath, bool stream)
72 {
73 if (object != 0) return;
74
75 Sound_Sample* sound = Sound_NewSampleFromFile(filePath.c_str(),
76 NULL, 8096);
77
78 if (!sound)
79 {
80 std::cerr << "could not load sound from file" << std::endl;
81 exit(1);
82 }
83
84 unsigned decoded = Sound_DecodeAll(sound);
85 if (decoded == 0)
86 {
87 std::cout << "decoded no bytes" << std::endl;
88 exit(1);
89 }
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;
94
95 alGenBuffers(1, &object);
96 alBufferData(object, getAudioFormat(sound->actual), sound->buffer,
97 sound->buffer_size, sound->actual.rate);
98
99 Sound_FreeSample(sound);
100 }
101
102 ALuint object;
103
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};
107
108
109 //alListenerfv(AL_POSITION, location);
110 //alListenerfv(AL_VELOCITY, location);
111 //alListenerfv(AL_VELOCITY, orient);
112 };
113
114 Impl(const std::string& name, bool stream = false) :
115 buffer_(Buffer::retain(name), Buffer::release)
116 {
117 if (!stream) buffer_->loadFromFile(Sound::getPath(name), stream);
118 else buffer_->loadFromFile(SoundStream::getPath(name), stream);
119
120 ALfloat location[] = {0.0f, 0.0f, 0.0f};
121
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);
129 }
130
131 ~Impl()
132 {
133 alDeleteSources(1, &source_);
134 }
135
136
137 void update()
138 {
139 }
140
141
142 boost::shared_ptr<Buffer> buffer_;
143 ALuint source_;
144 };
145
146
147 Sound::Sound(const std::string& name) :
148 // pass through
149 impl_(new Sound::Impl(name)) {}
150
151
152 void Sound::play()
153 {
154 alSourceRewind(impl_->source_);
155 alSourcePlay(impl_->source_);
156 }
157
158
159 std::string Sound::getPath(const std::string& name)
160 {
161 std::string path = Resource::getPath("sounds/" + name + ".ogg");
162 return path;
163 }
164
165
166 //##############################################################################
167
168
169 SoundStream::SoundStream(const std::string& name)
170 // pass through
171 //impl_(name, true) {}
172 {
173 impl_ = boost::shared_ptr<Sound::Impl>(new Sound::Impl(name, true));
174 }
175
176
177 void SoundStream::update(Scalar t, Scalar dt)
178 {
179 // pass through
180 impl_->update();
181 }
182
183
184 std::string SoundStream::getPath(const std::string& name)
185 {
186 std::string path = Resource::getPath("sounds/" + name + ".xm");
187 return path;
188 }
189
190
191 } // namespace Mf
192
193 /** vim: set ts=4 sw=4 tw=80: *************************************************/
194
This page took 0.042056 seconds and 4 git commands to generate.