1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
#include "DXUT.h"
#ifdef ENABLE_FMOD
#include <fmod.hpp>
#include <fmod_errors.h>
#endif
#include <math.h>
#include <stdio.h>
#include <assert.h>
#include "SFX_TABLE.h"
#include "NVAudio.h"
#define MAX_SOUNDEFFECTS 100
#pragma warning (disable: 4996 4505)
BOOL NVAudio;
#ifdef ENABLE_FMOD
// FMOD vars
FMOD_RESULT result;
FMOD::System *AUDIOsystem;
// SOUNDS
FMOD::Sound *MusicTrack;
// channels / voices
FMOD::Channel *MusicChannel; // pull one aside for MUSIC (background track)
FMOD::Channel *AudioChannels[NVAUDIO_MAX_CHANNELS];
float MasterVolumes[NVAUDIO_MAX_CHANNELS];
typedef struct {
BOOL loaded;
FMOD::Sound *Wave;
char Filename[100];
} SoundStruct;
SoundStruct SoundEffects[ MAX_SOUNDEFFECTS ];
#endif // ENABLE_FMOD
/*
typedef struct {
FMOD::Channel Channel;
} ChannelStruct;
ChannelStruct AudioChannels[ NVAUDIO_MAX_CHANNELS ];
*/
// --------------------------------------------------------------------
// LOADS a sample into wave format and processes it
// - Filename ( the filename )
// - EffectNumber ( the reference number of the sound effect ), Build an enum table and refer to htat
// - CompressionFlag ( Force to remain in native format if set to NVAUDIO_REMAINCOMPRESSED (ideal for mp3/ogg) )
// --------------------------------------------------------------------
void NVAudioLoadSoundEffect( char*filename, int EffectNumber , int CompressionFlag, bool loop, float frequencyVar, float volumeVar )
{
assert(CompressionFlag==NVAUDIO_REMAINCOMPRESSED || CompressionFlag==NVAUDIO_UNCOMPRESSED);
OutputDebugStringA( "\r\nTrying to Load ->" );
OutputDebugStringA( filename );
#ifdef ENABLE_FMOD
if (NVAudio && SoundEffects[ EffectNumber ].loaded!=TRUE )
{
int cmpr = FMOD_DEFAULT;
if (CompressionFlag==NVAUDIO_REMAINCOMPRESSED)
cmpr = FMOD_CREATECOMPRESSEDSAMPLE;
if (loop)
cmpr |= FMOD_LOOP_NORMAL;
result = AUDIOsystem->createSound(filename, cmpr , 0, &SoundEffects[ EffectNumber ].Wave); // FMOD_DEFAULT uses the defaults. These are the same as FMOD_LOOP_OFF | FMOD_2D | FMOD_HARDWARE.
if(result==FMOD_OK)
{
if(CompressionFlag==NVAUDIO_REMAINCOMPRESSED)
{
OutputDebugStringA("->REMAINING COMPRESSED->LOADED OK");
}
else
{
OutputDebugStringA("->DECODED->LOADED\r\n");
}
SoundEffects[ EffectNumber ].loaded = TRUE;
result = SoundEffects[ EffectNumber ].Wave->setVariations(frequencyVar, volumeVar, 0);
assert(result == FMOD_OK);
}
else
{
OutputDebugStringA("->FAILED\r\n");
SoundEffects[ EffectNumber ].loaded = FALSE;
}
}
#endif // ENABLE_FMOD
}
// ----------------------------
// quick error decode function
// ----------------------------
#ifdef ENABLE_FMOD
void NVAudioDecodeErrorAndOutput( FMOD_RESULT r )
{
char DbugTxt[200];
if (NVAudio)
{
sprintf(&DbugTxt[0],"\r\nAUDIO %d errorcode \r\n", r);
OutputDebugStringA(DbugTxt);
}
}
#endif // ENABLE_FMOD
// non public audio Test Function.
void NVAUDIOLoadMusicTrack()
{
#ifdef ENABLE_FMOD
if(NVAudio)
{
OutputDebugStringA("\r\nLoading background track\r\n");
const char* trackName = "Media\\Audio\\BoatInTheSeaSoundsLooping.wav";
result = AUDIOsystem->createSound(trackName, FMOD_DEFAULT | FMOD_LOOP_NORMAL, 0, &MusicTrack); // FMOD_DEFAULT uses the defaults. These are the same as FMOD_LOOP_OFF | FMOD_2D | FMOD_HARDWARE.
if(result != FMOD_OK)
NVAudioDecodeErrorAndOutput( result );
OutputDebugStringA("\r\nLoaded background Track\r\n");
}
#endif // ENABLE_FMOD
}
void NVAUDIOPlayMusicTrack( void )
{
#ifdef ENABLE_FMOD
if(MusicTrack)
{
OutputDebugStringA("\r\n\r\nStarting Backround Sync track");
result = AUDIOsystem->playSound(FMOD_CHANNEL_REUSE , MusicTrack, false, &AudioChannels[ MUSIC_VOICE ] );
OutputDebugStringA("\r\nBackground track syncronised and started\r\n");
}
#endif // ENABLE_FMOD
}
void NVAudioPlaySound( int effect, int channel )
{
assert(channel < NVAUDIO_MAX_CHANNELS);
char string[100];
sprintf(string,"\r\n%d Effect Number, %d channel\r\n", effect, channel );
OutputDebugStringA( string );
#ifdef ENABLE_FMOD
bool bIsPlaying;
if(NVAudio)
{
AudioChannels[ channel ]->isPlaying( &bIsPlaying );
if(bIsPlaying)
{
OutputDebugStringA( "It is Playing" );
AudioChannels[ channel ]->stop();
}
result = AUDIOsystem->playSound( FMOD_CHANNEL_FREE , SoundEffects[effect].Wave , false, &AudioChannels[ channel ] );
}
#endif // ENABLE_FMOD
}
void NVAudioSetVolume(int channel, float v)
{
#ifdef ENABLE_FMOD
assert(channel < NVAUDIO_MAX_CHANNELS);
AudioChannels[ channel ]->setVolume(MasterVolumes[channel] * v);
#endif // ENABLE_FMOD
}
float NVAudioGetVolume(int channel)
{
assert(channel < NVAUDIO_MAX_CHANNELS);
float result=0;
#ifdef ENABLE_FMOD
AudioChannels[ channel ]->getVolume(&result);
#endif // ENABLE_FMOD
return result;
}
void NVAudioSetFrequency(int channel, float f)
{
assert(channel < NVAUDIO_MAX_CHANNELS);
#ifdef ENABLE_FMOD
AudioChannels[ channel ]->setFrequency(f);
#endif // ENABLE_FMOD
}
float NVAudioGetFrequency(int channel)
{
assert(channel < NVAUDIO_MAX_CHANNELS);
float result=0;
#ifdef ENABLE_FMOD
AudioChannels[ channel ]->getFrequency(&result);
#endif // ENABLE_FMOD
return result;
}
void NVAudioSetChannelMasterVolume(int channel, float v)
{
}
void NVAudioStopSound( int channel )
{
#ifdef ENABLE_FMOD
assert(channel < NVAUDIO_MAX_CHANNELS);
bool bIsPlaying;
AudioChannels[ channel ]->isPlaying( &bIsPlaying );
if(bIsPlaying)
{
OutputDebugStringA( "It is Playing" );
AudioChannels[ channel ]->stop();
}
else
{
OutputDebugStringA( "NOT PLAYING" );
}
#endif // ENABLE_FMOD
}
void NVAudioPauseSound( int channel )
{
assert(channel < NVAUDIO_MAX_CHANNELS);
#ifdef ENABLE_FMOD
AudioChannels[ channel ]->setPaused(true);
#endif // ENABLE_FMOD
}
void NVAudioResumeSound( int channel )
{
assert(channel < NVAUDIO_MAX_CHANNELS);
#ifdef ENABLE_FMOD
AudioChannels[ channel ]->setPaused(false);
#endif // ENABLE_FMOD
}
BOOL NVAudioIsPlaying( int channel )
{
#ifdef ENABLE_FMOD
assert(channel < NVAUDIO_MAX_CHANNELS);
bool bIsPlaying;
AudioChannels[ channel ]->isPlaying( &bIsPlaying );
if(bIsPlaying)
{
return TRUE;
}
#endif // ENABLE_FMOD
return FALSE;
}
// ---------------------------------------------
// Call this to initialise the engine.
// ---------------------------------------------
BOOL NVAudioInit(float defaultMasterVolume)
{
NVAudio = FALSE;
#ifdef ENABLE_FMOD
result = FMOD::System_Create(&AUDIOsystem); // Create the main system object.
if (result != FMOD_OK)
{
OutputDebugStringA("Failed to create Audio System");
return FALSE;
}
result = AUDIOsystem->init(100, FMOD_INIT_NORMAL, 0); // Initialize FMOD.
if (result != FMOD_OK)
{
OutputDebugStringA("Failed to initialise Audio Channels");
return FALSE;
}
for (int c=0; c!=NVAUDIO_MAX_CHANNELS; ++c)
MasterVolumes[c] = defaultMasterVolume;
// This sets up all the voices so we can control where they get played.
/*
OutputDebugStringA("\r\nFinding Voices\r\n");
int c;
for(c =0;c<100;c++)
{
result = AUDIOsystem->getChannel( 0, &AudioChannels[ c ] );
if(result== FMOD_OK)
{
char dbg[100];
sprintf(dbg,"%d ",c);
OutputDebugStringA(dbg);
}
}
OutputDebugStringA("\r\nFinding Voices Completed\r\n");
*/
#endif // ENABLE_FMOD
NVAudio = TRUE;
return TRUE;
}
// ---------------------------------------------
// FUNCTION to load all the assets for the demo.
// ---------------------------------------------
void NVLoadAudioAssets()
{
#ifdef ENABLE_FMOD
if(NVAudio)
{
OutputDebugStringA("\r\nSample Loading\r\n");
// load the music track seperately. (this does not launch t5
NVAUDIOLoadMusicTrack();
// finished loading sound effects.
}
#endif ENABLE_FMOD
}
// ---------------------------------------------
// Call this EACH FRAME exactly once.
// ---------------------------------------------
void NVAudioTick( void )
{
#ifdef ENABLE_FMOD
if(NVAudio)
{
AUDIOsystem->update();
}
#endif // ENABLE_FMOD
}
// ---------------------------------------------
// Call this BEFORE EXITING the app.
// ---------------------------------------------
void NVAudioTerminate( void )
{
if(NVAudio)
{
OutputDebugStringA("\r\nAudio Closed Down\r\n");
#ifdef ENABLE_FMOD
AUDIOsystem->release();
#endif // ENABLE_FMOD
NVAudio = FALSE;
}
}
|