]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_queue.cxx
performance monitor: start measurement interval with a fresh timestamp.
[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 <cstdlib>      // rand()
30 #include <cstring>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/structure/exception.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/math/SGMath.hxx>
36
37 #include "soundmgr_openal.hxx"
38 #include "sample_queue.hxx"
39
40
41 //
42 // SGSampleQueue
43 //
44
45 // empty constructor
46 SGSampleQueue::SGSampleQueue( int freq, int format ) :
47     _absolute_pos(SGVec3d::zeros()),
48     _relative_pos(SGVec3d::zeros()),
49     _direction(SGVec3d::zeros()),
50     _velocity(SGVec3f::zeros()),
51     _orientation(SGQuatd::zeros()),
52     _orivec(SGVec3f::zeros()),
53     _base_pos(SGVec3d::zeros()),
54     _rotation(SGQuatd::zeros()),
55     _refname(random_string()),
56     _format(format),
57     _freq(freq),
58     _valid_source(false),
59     _source(SGSoundMgr::NO_SOURCE),
60     _inner_angle(360.0),
61     _outer_angle(360.0),
62     _outer_gain(0.0),
63     _pitch(1.0),
64     _volume(1.0),
65     _master_volume(1.0),
66     _reference_dist(500.0),
67     _max_dist(3000.0),
68     _loop(false),
69     _playing(false),
70     _changed(true)
71 {
72     _buffers.clear();
73 }
74
75 SGSampleQueue::~SGSampleQueue() {
76     stop();
77 }
78
79 void SGSampleQueue::stop()
80 {
81     ALint num;
82     alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
83     for (int i=0; i<num; i++) {
84         ALuint buffer;
85         alSourceUnqueueBuffers(_source, 1, &buffer);
86         alDeleteBuffers(1, &buffer);
87     }
88     _buffers.clear();
89
90     _playing = false;
91     _changed = true;
92 }
93
94 void SGSampleQueue::add( const void* smp_data, size_t len )
95 {
96     const ALvoid *data = (const ALvoid *)smp_data;
97     ALuint buffer;
98     ALint num;
99
100     if ( _valid_source )
101     {
102        alGetSourcei(_source, AL_BUFFERS_PROCESSED, &num);
103        if (num > 1) {
104            alSourceUnqueueBuffers(_source, 1, &buffer);
105        } else {
106            alGenBuffers(1, &buffer);
107        }
108        alBufferData(buffer, _format, data, len, _freq);
109     }
110     else
111     {
112         alGenBuffers(1, &buffer);
113         alBufferData(buffer, _format, data, len, _freq);
114         _buffers.push_back(buffer);
115     }
116 }
117
118 void SGSampleQueue::set_source( unsigned int sid )
119 {
120     _source = sid;
121     _valid_source = true;
122     _changed = true;
123
124     ALuint num = _buffers.size();
125     for (unsigned int i=0; i < num; i++)
126     {
127         ALuint buffer = _buffers[i];
128         alSourceQueueBuffers(_source, 1, &buffer);
129     }
130     _buffers.clear();
131
132 }
133
134 string SGSampleQueue::random_string() {
135       static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz"
136                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
137       string rstr = "Queued sample: ";
138       for (int i=0; i<10; i++) {
139           rstr.push_back( r[rand() % strlen(r)] );
140       }
141
142       return rstr;
143 }