1 /*
2  * CRRCsim - the Charles River Radio Control Club Flight Simulator Project
3  *
4  * Copyright (C) 2004 Kees Lemmens (original author)
5  * Copyright (C) 2004 Lionel Cailler
6  * Copyright (C) 2005, 2006, 2008, 2009 Jan Reucker
7  * Copyright (C) 2005, 2008 Jens Wilhelm Wulf
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2
11  * as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  */
24 
25 
26 /** \file crrc_sound.h
27  *
28  *  CRRCsim-specific stuff related to the soundserver.
29  *
30  */
31 #ifndef CRRC_SOUND_H
32 #define CRRC_SOUND_H
33 
34 #include "crrc_soundserver.h"
35 
36 /// Definition of the sound type constants
37 #define SOUND_TYPE_GLOW       (0)     ///< glow engine, sound also plays when engine is idle
38 #define SOUND_TYPE_ELECTRIC   (1)     ///< electric engine, sound stops when engine stops
39 #define SOUND_TYPE_GLIDER     (2)     ///< sound is generated by the airflow
40 
41 // Inter-thread exchange function
42 void soundUpdate3D(float flDist, float flPropFreq, float flH, float flRelV);
43 
44 
45 /** \brief The CRRCsim-specific mother of all airplane sound classes.
46  *
47  *  This class extends T_PitchVariableLoop with
48  *  some CRRCsim-specific attributes.
49  */
50 class T_AirplaneSound : public T_PitchVariableLoop
51 {
52   public:
53     /// The constructor
54     T_AirplaneSound(const char *filename, SDL_AudioSpec *fmt);
55 
56     /// The destructor
57     ~T_AirplaneSound();
58 
59     // Get a pointer to a chunk of data. Description: see implementation.
60     Uint8* getMixableData(Uint32 playpos, Uint32 *len);
61 
62     /// Get the pitch factor for this sound sample
getPitchFactor()63     double  getPitchFactor() const {return dPitchFactor;};
64     /// Set the pitch factor for this sound sample
setPitchFactor(double d)65     void    setPitchFactor(double d) {dPitchFactor = d;};
66 
67     /// Get the maximum volume for this sound sample
getMaxVolume()68     double  getMaxVolume() const {return dMaxVolume;};
69     /// Set the maximum volume for this sound sample
setMaxVolume(double v)70     void    setMaxVolume(double v) {dMaxVolume = v;};
71 
72     /// Get the type of this sound sample (SOUND_TYPE_XYZ)
getType()73     int     getType() const {return type;};
74     /// Set the type of this sound sample (SOUND_TYPE_XYZ)
setType(int t)75     void    setType(int t) {type = t;};
76 
77     /// Get the channel on which this sound sample is playing
getChannel()78     int     getChannel() const {return channel;};
79     /// Set the channel on which this sound sample is playing
setChannel(int c)80     void    setChannel(int c) {channel = c;};
81 
82     // calculate Doppler constant (see implementation for details)
83     float calculate_Doppler(float dist);
84 
85   protected:
86     double  dPitchFactor;   ///< The relation of sample pitch to e.g. motor speed
87     double  dMaxVolume;     ///< The maximum sample volume
88     int     type;           ///< Type of the sound sample (SOUND_TYPE_XYZ)
89     int     channel;        ///< The soundserver channel on which this sample is playing
90 
91   private:
92     /// Interface to the routine that calculates pitch and volume for the sample
93     virtual void calculate() = 0;
94 
95     // for Doppler calculations:
96     float  last_dist;       ///< Doppler effect: last distance to sound source
97     float  v_rel_filter;    ///< Doppler effect: filtered relative velocity
98 };
99 
100 
101 /** \brief The CRRCsim-specific engine sound class.
102  *
103  *  This class extends T_PitchVariableLoop with a
104  *  calculation routine which creates some basic
105  *  "special FX" for the engine sound: Doppler,
106  *  distance-dependant attenuation and speed-dependant
107  *  engine pitch.
108  */
109 class T_EngineSound : public T_AirplaneSound
110 {
111   public:
112     T_EngineSound(const char *filename, SDL_AudioSpec *fmt);
113     ~T_EngineSound();
114 
115   private:
116     void    calculate();
117 };
118 
119 
120 /** \brief The CRRCsim-specific glider sound class.
121  *
122  *  This class extends T_PitchVariableLoop with a
123  *  calculation routine which creates some basic
124  *  "special FX" for glider sounds: Doppler,
125  *  distance-dependant attenuation and speed-dependant
126  *  pitch.
127  */
128 class T_GliderSound : public T_AirplaneSound
129 {
130   public:
131     /// The constructor
132     T_GliderSound(const char *filename, SDL_AudioSpec *fmt);
133 
134     /// The destructor
135     ~T_GliderSound();
136 
137     /// Set the minimum relative velocity
setMinRelVelocity(float f)138     void setMinRelVelocity(float f) {flMinRelV = f;};
139 
140     /// Set the maximum relative velocity
setMaxRelVelocity(float f)141     void setMaxRelVelocity(float f) {flMaxRelV = f;};
142 
143     /// Set the maximum distance (in ft.)
setMaxDistanceFeet(float f)144     void setMaxDistanceFeet(float f) {flMaxDist = f;};
145 
146   private:
147     void calculate();
148 
149     float flMinRelV;  ///< minimal relative velocity that triggers the sound
150     float flMaxRelV;  ///< relative velocity where the sound reaches max volume
151     float flMaxDist;  ///< distance where the sound drops to min volume
152 };
153 
154 
155 /** \brief A variometer sound class.
156  *
157  *  This class implements the variometer sound for CRRCsim.
158  */
159 class T_VariometerSound : protected T_SoundSample
160 {
161   public:
162     T_VariometerSound(SDL_AudioSpec *fmt);
163     virtual ~T_VariometerSound();
164 
165     void    convert(SDL_AudioSpec *fmt);
166 
167     Uint8*  getMixableData(Uint32 playpos, Uint32 *len);
168 
169   protected:
170     void init(SDL_AudioSpec *fmt);
171     void calculate();
172 
173     std::vector<Uint8>  dyn_buffer;
174     std::vector<Sint16> sinewave;
175     int   nBeepFInc;
176     int   nBeepFIncNew;
177     int   nOnCntInit;
178     int   nVarioCnt;
179     int   nVarioOffReq;
180     int   nSinIndex;        ///< index into sinewave buffer
181     int   nSndVarioSineLen; ///< length of sine in samples
182     float flHDiffFilt;      ///< filtered vertical velocity in feet per second
183     float flHOld;           ///< old height in feet
184 
185 };
186 
187 
188 
189 #endif  // CRRC_SOUND_H
190