]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_queue.cxx
Remove duplicate members in SGSampleQueue.
[simgear.git] / simgear / sound / sample_queue.cxx
1
2 // queue.cxx -- Audio sample encapsulation class
3 // 
4 // Written by Curtis Olson, started April 2004.
5 // Modified to match the new SoundSystem by Erik Hofman, October 2009
6 //
7 // Copyright (C) 2004  Curtis L. Olson - http://www.flightgear.org/~curt
8 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
9 //
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.
14 //
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.
19 //
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.
23 //
24 // $Id$
25
26 #ifdef HAVE_CONFIG_H
27 #  include <simgear_config.h>
28 #endif
29
30 #include <cstdlib>      // rand()
31 #include <cstring>
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/structure/exception.hxx>
35 #include <simgear/misc/sg_path.hxx>
36
37 #include "soundmgr_openal.hxx"
38 #include "sample_queue.hxx"
39 #include "soundmgr_openal_private.hxx"
40
41 using std::string;
42
43 #define ENABLE_SOUND
44
45 //
46 // SGSampleQueue
47 //
48
49 // empty constructor
50 SGSampleQueue::SGSampleQueue( int freq, int format ) :
51     _refname(random_string()),
52     _playing(false)
53 {
54     _freq = freq;
55     _format = format;
56     _buffers.clear();
57 }
58
59 SGSampleQueue::~SGSampleQueue() {
60     stop();
61 }
62
63 void SGSampleQueue::stop()
64 {
65 #ifdef ENABLE_SOUND
66     ALint num;
67     alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
68     for (int i=0; i<num; i++) {
69         ALuint buffer;
70         alSourceUnqueueBuffers(_source, 1, &buffer);
71         alDeleteBuffers(1, &buffer);
72     }
73     _buffers.clear();
74 #endif
75     _playing = false;
76     _changed = true;
77 }
78
79 void SGSampleQueue::add( const void* smp_data, size_t len )
80 {
81 #ifdef ENABLE_SOUND
82     const ALvoid *data = (const ALvoid *)smp_data;
83     ALuint buffer;
84     ALint num;
85
86     if ( _valid_source )
87     {
88        alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
89        if (num > 1) {
90            alSourceUnqueueBuffers(_source, 1, &buffer);
91        } else {
92            alGenBuffers(1, &buffer);
93        }
94        alBufferData(buffer, _format, data, len, _freq);
95     }
96     else
97     {
98         alGenBuffers(1, &buffer);
99         alBufferData(buffer, _format, data, len, _freq);
100         _buffers.push_back(buffer);
101     }
102 #endif
103 }
104
105 void SGSampleQueue::set_source( unsigned int sid )
106 {
107     SGSoundSample::set_source(sid);
108 #ifdef ENABLE_SOUND
109     ALuint num = _buffers.size();
110     for (unsigned int i=0; i < num; i++)
111     {
112         ALuint buffer = _buffers[i];
113         alSourceQueueBuffers(_source, 1, &buffer);
114     }
115     _buffers.clear();
116 #endif
117 }
118
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)] );
125       }
126
127       return rstr;
128 }