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"
50 SGSampleQueue::SGSampleQueue( int freq, int format ) :
51 _refname(random_string()),
59 SGSampleQueue::~SGSampleQueue() {
63 void SGSampleQueue::stop()
67 alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
68 for (int i=0; i<num; i++) {
70 alSourceUnqueueBuffers(_source, 1, &buffer);
71 alDeleteBuffers(1, &buffer);
79 void SGSampleQueue::add( const void* smp_data, size_t len )
82 const ALvoid *data = (const ALvoid *)smp_data;
88 alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
90 alSourceUnqueueBuffers(_source, 1, &buffer);
92 alGenBuffers(1, &buffer);
94 alBufferData(buffer, _format, data, len, _freq);
98 alGenBuffers(1, &buffer);
99 alBufferData(buffer, _format, data, len, _freq);
100 _buffers.push_back(buffer);
105 void SGSampleQueue::set_source( unsigned int sid )
107 SGSoundSample::set_source(sid);
109 ALuint num = _buffers.size();
110 for (unsigned int i=0; i < num; i++)
112 ALuint buffer = _buffers[i];
113 alSourceQueueBuffers(_source, 1, &buffer);
119 string SGSampleQueue::random_string() {
120 static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
121 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
122 string rstr = "Queued sample: ";
123 for (int i=0; i<10; i++) {
124 rstr.push_back( r[rand() % strlen(r)] );