2 // queue.cxx -- Audio sample encapsulation class
4 // Written by Curtis Olson, started April 2004.
5 // Modified to match the new SoundSystem by Erik Hofman, October 2009
7 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
8 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software Foundation,
22 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 # include <simgear_config.h>
30 #include <cstdlib> // rand()
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/structure/exception.hxx>
35 #include <simgear/misc/sg_path.hxx>
37 #include "soundmgr_openal.hxx"
38 #include "sample_queue.hxx"
39 #include "soundmgr_openal_private.hxx"
48 SGSampleQueue::SGSampleQueue( int freq, int format ) :
49 _refname(random_string()),
57 SGSampleQueue::~SGSampleQueue() {
61 void SGSampleQueue::stop()
65 alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
66 for (int i=0; i<num; i++) {
68 alSourceUnqueueBuffers(_source, 1, &buffer);
69 alDeleteBuffers(1, &buffer);
77 void SGSampleQueue::add( const void* smp_data, size_t len )
80 const ALvoid *data = (const ALvoid *)smp_data;
86 alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
88 alSourceUnqueueBuffers(_source, 1, &buffer);
90 alGenBuffers(1, &buffer);
92 alBufferData(buffer, _format, data, len, _freq);
96 alGenBuffers(1, &buffer);
97 alBufferData(buffer, _format, data, len, _freq);
98 _buffers.push_back(buffer);
103 void SGSampleQueue::set_source( unsigned int sid )
105 SGSoundSample::set_source(sid);
107 ALuint num = _buffers.size();
108 for (unsigned int i=0; i < num; i++)
110 ALuint buffer = _buffers[i];
111 alSourceQueueBuffers(_source, 1, &buffer);
117 string SGSampleQueue::random_string() {
118 static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
119 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
120 string rstr = "Queued sample: ";
121 for (int i=0; i<10; i++) {
122 rstr.push_back( r[rand() % strlen(r)] );