]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_openal.hxx
Add support for the MacOS variations of OpenAL.
[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 #if defined(__APPLE__)
38 # define AL_ILLEGAL_ENUM AL_INVALID_ENUM
39 # define AL_ILLEGAL_COMMAND AL_INVALID_OPERATION
40 # include <OpenAL/al.h>
41 # include <OpenAL/alut.h>
42 #else
43 # include <AL/al.h>
44 # include <AL/alut.h>
45 #endif
46
47 #include <simgear/debug/logstream.hxx>
48
49
50 /**
51  * manages everything we need to know for an individual sound sample
52  */
53
54 class SGSoundSample {
55
56 private:
57
58     // Buffers hold sound data.
59     ALuint buffer;
60
61     // Sources are points emitting sound.
62     ALuint source;
63
64     // Position of the source sound.
65     ALfloat source_pos[3];
66
67     // Velocity of the source sound.
68     ALfloat source_vel[3];
69
70     // configuration values
71     ALenum format;
72     ALsizei size;
73     ALvoid* data;
74     ALsizei freq;
75
76     double pitch;
77     double volume;
78     ALboolean loop;
79
80 public:
81
82     SGSoundSample( const char *path, const char *file );
83     SGSoundSample( unsigned char *_data, int len, int _freq );
84     ~SGSoundSample();
85
86     /**
87      * Start playing this sample.
88      *
89      * @param looped Define wether the sound should be played in a loop.
90      */
91     void play( bool _loop );
92
93     /**
94      * Stop playing this sample.
95      *
96      * @param sched A pointer to the appropriate scheduler.
97      */
98     void stop();
99
100     /**
101      * Play this sample once.
102      * @see #play
103      */
104     inline void play_once() { play(false); }
105
106     /** 
107      * Play this sample looped.
108      * @see #play
109      */
110     inline void play_looped() { play(true); }
111
112     /**
113      * Test if a sample is curretnly playing.
114      * @return true if is is playing, false otherwise.
115      */
116     inline bool is_playing( ) {
117         ALint result;
118         alGetSourcei( source, AL_SOURCE_STATE, &result );
119         if ( alGetError() != AL_NO_ERROR) {
120             SG_LOG( SG_GENERAL, SG_ALERT,
121                     "Oops AL error in sample is_playing()!" );
122         }
123         return (result == AL_PLAYING) ;
124     }
125
126     /**
127      * Get the current pitch setting of this sample.
128      */
129     inline double get_pitch() const { return pitch; }
130
131     /**
132      * Set the pitch of this sample.
133      */
134     inline void set_pitch( double p ) {
135         // clamp in the range of 0.01 to 2.0
136         if ( p < 0.01 ) { p = 0.01; }
137         if ( p > 2.0 ) { p = 2.0; }
138         pitch = p;
139         alSourcef( source, AL_PITCH, pitch );
140         if ( alGetError() != AL_NO_ERROR) {
141             SG_LOG( SG_GENERAL, SG_ALERT,
142                     "Oops AL error in sample set_pitch()! " << p );
143         }
144     }
145
146     /**
147      * Get the current volume setting of this sample.
148      */
149     inline double get_volume() const { return volume; }
150
151     /**
152      * Set the volume of this sample.
153      */
154     inline void set_volume( double v ) {
155         volume = v;
156         alSourcef( source, AL_GAIN, volume );
157         if ( alGetError() != AL_NO_ERROR) {
158             SG_LOG( SG_GENERAL, SG_ALERT,
159                     "Oops AL error in sample set_volume()!" );
160         }
161     }
162
163     /**
164      * Returns the size of the sounds sample
165      */
166     inline int get_size() {
167         return size;
168     }
169
170     /**
171      * Return a pointer to the raw data
172      */
173     inline char *get_data() {
174         return (char *)data;
175     }
176 };
177
178
179 #endif // _SG_SAMPLE_HXX
180
181