]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_queue.cxx
Merge branch 'next' of git://gitorious.org/fg/simgear into next
[simgear.git] / simgear / sound / sample_queue.cxx
1 // queue.cxx -- Audio sample encapsulation class
2 // 
3 // Written by Curtis Olson, started April 2004.
4 // Modified to match the new SoundSystem by Erik Hofman, October 2009
5 //
6 // Copyright (C) 2004  Curtis L. Olson - http://www.flightgear.org/~curt
7 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
8 //
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.
13 //
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.
18 //
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.
22 //
23 // $Id$
24
25 #ifdef HAVE_CONFIG_H
26 #  include <simgear_config.h>
27 #endif
28
29 #include <stdlib.h>     // rand()
30
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/structure/exception.hxx>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/math/SGMath.hxx>
35
36 #include "soundmgr_openal.hxx"
37 #include "sample_queue.hxx"
38
39
40 //
41 // SGSampleQueue
42 //
43
44 // empty constructor
45 SGSampleQueue::SGSampleQueue( int freq, int format ) :
46     _absolute_pos(SGVec3d::zeros()),
47     _relative_pos(SGVec3d::zeros()),
48     _direction(SGVec3d::zeros()),
49     _velocity(SGVec3f::zeros()),
50     _orientation(SGQuatd::zeros()),
51     _orivec(SGVec3f::zeros()),
52     _base_pos(SGVec3d::zeros()),
53     _rotation(SGQuatd::zeros()),
54     _refname(random_string()),
55     _format(format),
56     _freq(freq),
57     _valid_source(false),
58     _source(SGSoundMgr::NO_SOURCE),
59     _inner_angle(360.0),
60     _outer_angle(360.0),
61     _outer_gain(0.0),
62     _pitch(1.0),
63     _volume(1.0),
64     _master_volume(1.0),
65     _reference_dist(500.0),
66     _max_dist(3000.0),
67     _playing(false),
68     _changed(true)
69 {
70     _buffers.clear();
71 }
72
73 SGSampleQueue::~SGSampleQueue() {
74     stop();
75 }
76
77 void SGSampleQueue::stop()
78 {
79     ALint num;
80     alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
81     for (int i=0; i<num; i++) {
82         ALuint buffer;
83         alSourceUnqueueBuffers(_source, 1, &buffer);
84         alDeleteBuffers(1, &buffer);
85     }
86     _buffers.clear();
87
88     _playing = false;
89     _changed = true;
90 }
91
92 void SGSampleQueue::add( const void* smp_data, size_t len )
93 {
94     const ALvoid *data = (const ALvoid *)smp_data;
95     ALuint buffer;
96     ALint num;
97
98     if ( _valid_source )
99     {
100        alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
101        if (num > 1) {
102            alSourceUnqueueBuffers(_source, 1, &buffer);
103        } else {
104            alGenBuffers(1, &buffer);
105        }
106        alBufferData(buffer, _format, data, len, _freq);
107     }
108     else
109     {
110         alGenBuffers(1, &buffer);
111         alBufferData(buffer, _format, data, len, _freq);
112         _buffers.push_back(buffer);
113     }
114 }
115
116 void SGSampleQueue::set_source( unsigned int sid )
117 {
118     _source = sid;
119     _valid_source = true;
120     _changed = true;
121
122     ALuint num = _buffers.size();
123     for (unsigned int i=0; i < num; i++)
124     {
125         ALuint buffer = _buffers[i];
126         alSourceQueueBuffers(_source, 1, &buffer);
127     }
128     _buffers.clear();
129
130 }
131
132 string SGSampleQueue::random_string() {
133       static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
134                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
135       string rstr = "Queued sample: ";
136       for (int i=0; i<10; i++) {
137           rstr.push_back( r[rand() % strlen(r)] );
138       }
139
140       return rstr;
141 }