]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.hxx
Clamp pitch values rather than just dumping an error message.
[simgear.git] / simgear / sound / sample_openal.hxx
1 // sample.hxx -- Sound sample encapsulation class
2 // 
3 // Written by Curtis Olson, started April 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 /**
24  * \file sample.hxx
25  * Provides a sound sample encapsulation
26  */
27
28 #ifndef _SG_SAMPLE_HXX
29 #define _SG_SAMPLE_HXX 1
30
31 #ifndef __cplusplus
32 # error This library requires C++
33 #endif
34
35 #include <simgear/compiler.h>
36
37 #include <AL/al.h>
38
39 #include <simgear/debug/logstream.hxx>
40
41
42 /**
43  * manages everything we need to know for an individual sound sample
44  */
45
46 class SGSoundSample {
47
48 private:
49
50     // Buffers hold sound data.
51     ALuint buffer;
52
53     // Sources are points emitting sound.
54     ALuint source;
55
56     // Position of the source sound.
57     ALfloat source_pos[3];
58
59     // Velocity of the source sound.
60     ALfloat source_vel[3];
61
62     // configuration values
63     ALenum format;
64     ALsizei size;
65     ALvoid* data;
66     ALsizei freq;
67
68     double pitch;
69     double volume;
70     ALboolean loop;
71
72 public:
73
74     SGSoundSample( const char *path, const char *file );
75     SGSoundSample( unsigned char *_data, int len, int _freq );
76     ~SGSoundSample();
77
78     /**
79      * Start playing this sample.
80      *
81      * @param looped Define wether the sound should be played in a loop.
82      */
83     void play( bool _loop );
84
85     /**
86      * Stop playing this sample.
87      *
88      * @param sched A pointer to the appropriate scheduler.
89      */
90     void stop();
91
92     /**
93      * Play this sample once.
94      * @see #play
95      */
96     inline void play_once() { play(false); }
97
98     /** 
99      * Play this sample looped.
100      * @see #play
101      */
102     inline void play_looped() { play(true); }
103
104     /**
105      * Test if a sample is curretnly playing.
106      * @return true if is is playing, false otherwise.
107      */
108     inline bool is_playing( ) {
109         ALint result;
110         alGetSourcei( source, AL_SOURCE_STATE, &result );
111         if ( alGetError() != AL_NO_ERROR) {
112             SG_LOG( SG_GENERAL, SG_ALERT,
113                     "Oops AL error in sample is_playing()!" );
114         }
115         return (result == AL_PLAYING) ;
116     }
117
118     /**
119      * Get the current pitch setting of this sample.
120      */
121     inline double get_pitch() const { return pitch; }
122
123     /**
124      * Set the pitch of this sample.
125      */
126     inline void set_pitch( double p ) {
127         // clamp in the range of 0.01 to 2.0
128         if ( p < 0.01 ) { p = 0.01; }
129         if ( p > 2.0 ) { p = 2.0; }
130         pitch = p;
131         alSourcef( source, AL_PITCH, pitch );
132         if ( alGetError() != AL_NO_ERROR) {
133             SG_LOG( SG_GENERAL, SG_ALERT,
134                     "Oops AL error in sample set_pitch()! " << p );
135         }
136     }
137
138     /**
139      * Get the current volume setting of this sample.
140      */
141     inline double get_volume() const { return volume; }
142
143     /**
144      * Set the volume of this sample.
145      */
146     inline void set_volume( double v ) {
147         volume = v;
148         alSourcef( source, AL_GAIN, volume );
149         if ( alGetError() != AL_NO_ERROR) {
150             SG_LOG( SG_GENERAL, SG_ALERT,
151                     "Oops AL error in sample set_volume()!" );
152         }
153     }
154
155     /**
156      * Returns the size of the sounds sample
157      */
158     inline int get_size() {
159         return size;
160     }
161
162     /**
163      * Return a pointer to the raw data
164      */
165     inline char *get_data() {
166         return (char *)data;
167     }
168 };
169
170
171 #endif // _SG_SAMPLE_HXX
172
173