1 // queue.cxx -- Audio sample encapsulation class
3 // Written by Curtis Olson, started April 2004.
4 // Modified to match the new SoundSystem by Erik Hofman, October 2009
6 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
7 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software Foundation,
21 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 # include <simgear_config.h>
29 #include <cstdlib> // rand()
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/structure/exception.hxx>
34 #include <simgear/misc/sg_path.hxx>
36 #include "soundmgr_openal.hxx"
37 #include "sample_queue.hxx"
38 #include "soundmgr_openal_private.hxx"
47 SGSampleQueue::SGSampleQueue( int freq, int format ) :
48 _absolute_pos(SGVec3d::zeros()),
49 _relative_pos(SGVec3d::zeros()),
50 _direction(SGVec3d::zeros()),
51 _velocity(SGVec3f::zeros()),
52 _orientation(SGQuatd::zeros()),
53 _orivec(SGVec3f::zeros()),
54 _base_pos(SGVec3d::zeros()),
55 _rotation(SGQuatd::zeros()),
56 _refname(random_string()),
60 _source(SGSoundMgr::NO_SOURCE),
67 _reference_dist(500.0),
76 SGSampleQueue::~SGSampleQueue() {
80 void SGSampleQueue::stop()
83 alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
84 for (int i=0; i<num; i++) {
86 alSourceUnqueueBuffers(_source, 1, &buffer);
87 alDeleteBuffers(1, &buffer);
95 void SGSampleQueue::add( const void* smp_data, size_t len )
97 const ALvoid *data = (const ALvoid *)smp_data;
103 alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
105 alSourceUnqueueBuffers(_source, 1, &buffer);
107 alGenBuffers(1, &buffer);
109 alBufferData(buffer, _format, data, len, _freq);
113 alGenBuffers(1, &buffer);
114 alBufferData(buffer, _format, data, len, _freq);
115 _buffers.push_back(buffer);
119 void SGSampleQueue::set_source( unsigned int sid )
122 _valid_source = true;
125 ALuint num = _buffers.size();
126 for (unsigned int i=0; i < num; i++)
128 ALuint buffer = _buffers[i];
129 alSourceQueueBuffers(_source, 1, &buffer);
135 string SGSampleQueue::random_string() {
136 static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
137 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
138 string rstr = "Queued sample: ";
139 for (int i=0; i<10; i++) {
140 rstr.push_back( r[rand() % strlen(r)] );