]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/sound/snd_efxfile.cpp
hello world
[icculus/iodoom3.git] / neo / sound / snd_efxfile.cpp
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28 #include "../idlib/precompiled.h"
29 #pragma hdrstop
30
31 #include "snd_local.h"
32
33 /*
34 ===============
35 idEFXFile::idEFXFile
36 ===============
37 */
38 idEFXFile::idEFXFile( void ) { }
39
40 /*
41 ===============
42 idEFXFile::Clear
43 ===============
44 */
45 void idEFXFile::Clear( void ) {
46         effects.DeleteContents( true );
47 }
48
49 /*
50 ===============
51 idEFXFile::~idEFXFile
52 ===============
53 */
54 idEFXFile::~idEFXFile( void ) {
55         Clear();
56 }
57
58 /*
59 ===============
60 idEFXFile::FindEffect
61 ===============
62 */
63 bool idEFXFile::FindEffect( idStr &name, idSoundEffect **effect, int *index ) {
64         int i;
65
66         for ( i = 0; i < effects.Num(); i++ ) {
67                 if ( ( effects[i] ) && ( effects[i]->name == name ) ) {
68                         *effect = effects[i];
69                         *index = i;
70                         return true;
71                 }
72         }
73         return false;
74 }
75
76 /*
77 ===============
78 idEFXFile::ReadEffect
79 ===============
80 */
81 bool idEFXFile::ReadEffect( idLexer &src, idSoundEffect *effect ) {
82         idToken name, token;
83         
84         if ( !src.ReadToken( &token ) )
85                 return false;
86
87         // reverb effect
88         if ( token == "reverb" ) {
89                 EAXREVERBPROPERTIES *reverb = ( EAXREVERBPROPERTIES * )Mem_Alloc( sizeof( EAXREVERBPROPERTIES ) );
90                 if ( reverb ) {
91                         src.ReadTokenOnLine( &token );
92                         name = token;
93                                 
94                         if ( !src.ReadToken( &token ) ) {
95                                 Mem_Free( reverb );
96                                 return false;
97                         }
98                         
99                         if ( token != "{" ) {
100                                 src.Error( "idEFXFile::ReadEffect: { not found, found %s", token.c_str() );
101                                 Mem_Free( reverb );
102                                 return false;
103                         }
104                         
105                         do {
106                                 if ( !src.ReadToken( &token ) ) {
107                                         src.Error( "idEFXFile::ReadEffect: EOF without closing brace" );
108                                         Mem_Free( reverb );
109                                         return false;
110                                 }
111
112                                 if ( token == "}" ) {
113                                         effect->name = name;
114                                         effect->data = ( void * )reverb;
115                                         effect->datasize = sizeof( EAXREVERBPROPERTIES );
116                                         break;
117                                 }
118
119                                 if ( token == "environment" ) {
120                                         src.ReadTokenOnLine( &token );
121                                         reverb->ulEnvironment = token.GetUnsignedLongValue();
122                                 } else if ( token == "environment size" ) {
123                                         reverb->flEnvironmentSize = src.ParseFloat();
124                                 } else if ( token == "environment diffusion" ) {
125                                         reverb->flEnvironmentDiffusion = src.ParseFloat();
126                                 } else if ( token == "room" ) {
127                                         reverb->lRoom = src.ParseInt();
128                                 } else if ( token == "room hf" ) {
129                                         reverb->lRoomHF = src.ParseInt();
130                                 } else if ( token == "room lf" ) {
131                                         reverb->lRoomLF = src.ParseInt();
132                                 } else if ( token == "decay time" ) {
133                                         reverb->flDecayTime = src.ParseFloat();
134                                 } else if ( token == "decay hf ratio" ) {
135                                         reverb->flDecayHFRatio = src.ParseFloat();
136                                 } else if ( token == "decay lf ratio" ) {
137                                         reverb->flDecayLFRatio = src.ParseFloat();
138                                 } else if ( token == "reflections" ) {
139                                         reverb->lReflections = src.ParseInt();
140                                 } else if ( token == "reflections delay" ) {
141                                         reverb->flReflectionsDelay = src.ParseFloat();
142                                 } else if ( token == "reflections pan" ) {
143                                         reverb->vReflectionsPan.x = src.ParseFloat();
144                                         reverb->vReflectionsPan.y = src.ParseFloat();
145                                         reverb->vReflectionsPan.z = src.ParseFloat();
146                                 } else if ( token == "reverb" ) {
147                                         reverb->lReverb = src.ParseInt();
148                                 } else if ( token == "reverb delay" ) {
149                                         reverb->flReverbDelay = src.ParseFloat();
150                                 } else if ( token == "reverb pan" ) {
151                                         reverb->vReverbPan.x = src.ParseFloat();
152                                         reverb->vReverbPan.y = src.ParseFloat();
153                                         reverb->vReverbPan.z = src.ParseFloat();
154                                 } else if ( token == "echo time" ) {
155                                         reverb->flEchoTime = src.ParseFloat();
156                                 } else if ( token == "echo depth" ) {
157                                         reverb->flEchoDepth = src.ParseFloat();
158                                 } else if ( token == "modulation time" ) {
159                                         reverb->flModulationTime = src.ParseFloat();
160                                 } else if ( token == "modulation depth" ) {
161                                         reverb->flModulationDepth = src.ParseFloat();
162                                 } else if ( token == "air absorption hf" ) {
163                                         reverb->flAirAbsorptionHF = src.ParseFloat();
164                                 } else if ( token == "hf reference" ) {
165                                         reverb->flHFReference = src.ParseFloat();
166                                 } else if ( token == "lf reference" ) {
167                                         reverb->flLFReference = src.ParseFloat();
168                                 } else if ( token == "room rolloff factor" ) {
169                                         reverb->flRoomRolloffFactor = src.ParseFloat();
170                                 } else if ( token == "flags" ) {
171                                         src.ReadTokenOnLine( &token );
172                                         reverb->ulFlags = token.GetUnsignedLongValue();
173                                 } else {
174                                         src.ReadTokenOnLine( &token );
175                                         src.Error( "idEFXFile::ReadEffect: Invalid parameter in reverb definition" );
176                                         Mem_Free( reverb );
177                                 }
178                         } while ( 1 );
179
180                         return true;
181                 }
182         } else {
183                 // other effect (not supported at the moment)
184                 src.Error( "idEFXFile::ReadEffect: Unknown effect definition" );
185         }
186
187         return false;
188 }
189
190
191 /*
192 ===============
193 idEFXFile::LoadFile
194 ===============
195 */
196 bool idEFXFile::LoadFile( const char *filename, bool OSPath ) {
197         idLexer src( LEXFL_NOSTRINGCONCAT );
198         idToken token;
199
200         src.LoadFile( filename, OSPath );
201         if ( !src.IsLoaded() ) {
202                 return false;
203         }
204
205         if ( !src.ExpectTokenString( "Version" ) ) {
206                 return NULL;
207         }
208
209         if ( src.ParseInt() != 1 ) {
210                 src.Error( "idEFXFile::LoadFile: Unknown file version" );
211                 return false;
212         }
213         
214         while ( !src.EndOfFile() ) {
215                 idSoundEffect *effect = new idSoundEffect;
216                 if ( ReadEffect( src, effect ) ) {
217                         effects.Append( effect );
218                 }
219         };
220
221         return true;
222 }
223
224
225 /*
226 ===============
227 idEFXFile::UnloadFile
228 ===============
229 */
230 void idEFXFile::UnloadFile( void ) {
231         Clear();
232 }