]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_queue.cxx
Remove support for stereo sounds
[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 //
44 // SGSampleQueue
45 //
46
47 // empty constructor
48 SGSampleQueue::SGSampleQueue( int freq, int format ) :
49     _refname(random_string()),
50     _playing(false)
51 {
52     set_frequency( freq );
53     set_format_AL( format );
54     _buffers.clear();
55 }
56
57 SGSampleQueue::~SGSampleQueue() {
58     stop();
59 }
60
61 void SGSampleQueue::stop()
62 {
63 #ifdef ENABLE_SOUND
64     ALint num;
65     alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
66     for (int i=0; i<num; i++) {
67         ALuint buffer;
68         alSourceUnqueueBuffers(_source, 1, &buffer);
69         alDeleteBuffers(1, &buffer);
70     }
71     _buffers.clear();
72 #endif
73     _playing = false;
74     _changed = true;
75 }
76
77 void SGSampleQueue::add( const void* smp_data, size_t len )
78 {
79 #ifdef ENABLE_SOUND
80     const ALvoid *data = (const ALvoid *)smp_data;
81     ALuint buffer;
82     ALint num;
83
84     if ( _valid_source )
85     {
86        alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
87        if (num > 1) {
88            alSourceUnqueueBuffers(_source, 1, &buffer);
89        } else {
90            alGenBuffers(1, &buffer);
91        }
92        alBufferData(buffer, get_format_AL(), data, len, get_frequency());
93     }
94     else
95     {
96         alGenBuffers(1, &buffer);
97         alBufferData(buffer, get_format_AL(), data, len, get_frequency());
98         _buffers.push_back(buffer);
99     }
100 #endif
101 }
102
103 void SGSampleQueue::set_source( unsigned int sid )
104 {
105     SGSoundSample::set_source(sid);
106 #ifdef ENABLE_SOUND
107     ALuint num = _buffers.size();
108     for (unsigned int i=0; i < num; i++)
109     {
110         ALuint buffer = _buffers[i];
111         alSourceQueueBuffers(_source, 1, &buffer);
112     }
113     _buffers.clear();
114 #endif
115 }
116
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)] );
123       }
124
125       return rstr;
126 }