forked from lehmanad1/Comp441Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.cpp
160 lines (142 loc) · 5.42 KB
/
audio.cpp
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
// Programming 2D Games
// Copyright (c) 2011 by:
// Charles Kelly
// audio.cpp v1.0
#include "audio.h"
//=============================================================================
// default constructor
//=============================================================================
Audio::Audio()
{
xactEngine = NULL;
waveBank = NULL;
soundBank = NULL;
cueI = 0;
mapWaveBank = NULL; // Call UnmapViewOfFile() to release file
soundBankData = NULL;
HRESULT hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
if( SUCCEEDED( hr ) )
coInitialized = true;
else
coInitialized = false;
}
//=============================================================================
// destructor
//=============================================================================
Audio::~Audio()
{
// Shutdown XACT
if( xactEngine )
{
xactEngine->ShutDown(); // shut down XACT engine and free resources
xactEngine->Release();
}
if ( soundBankData )
delete[] soundBankData;
soundBankData = NULL;
// After xactEngine->ShutDown() returns, release memory mapped files
if( mapWaveBank )
UnmapViewOfFile( mapWaveBank );
mapWaveBank = NULL;
if( coInitialized ) // if CoInitializeEx succeeded
CoUninitialize();
}
//=============================================================================
// initialize
// This function does the following:
// 1. Initialize XACT by calling xactEngine->Initialize
// 2. Create the XACT wave bank(s) you want to use
// 3. Create the XACT sound bank(s) you want to use
// 4. Store indices to the XACT cue(s) your game uses
//=============================================================================
HRESULT Audio::initialize()
{
HRESULT result = E_FAIL;
HANDLE hFile;
DWORD fileSize;
DWORD bytesRead;
HANDLE hMapFile;
if( coInitialized == false)
return E_FAIL;
result = XACT3CreateEngine( 0, &xactEngine );
if( FAILED( result ) || xactEngine == NULL )
return E_FAIL;
// Initialize & create the XACT runtime
XACT_RUNTIME_PARAMETERS xactParams = {0};
xactParams.lookAheadTime = XACT_ENGINE_LOOKAHEAD_DEFAULT;
result = xactEngine->Initialize( &xactParams );
if( FAILED( result ) )
return result;
// Create an "in memory" XACT wave bank file using memory mapped file IO
result = E_FAIL; // default to failure code, replaced on success
hFile = CreateFile( WAVE_BANK, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
if( hFile != INVALID_HANDLE_VALUE )
{
fileSize = GetFileSize( hFile, NULL );
if( fileSize != -1 )
{
hMapFile = CreateFileMapping( hFile, NULL, PAGE_READONLY, 0, fileSize, NULL );
if( hMapFile )
{
mapWaveBank = MapViewOfFile( hMapFile, FILE_MAP_READ, 0, 0, 0 );
if( mapWaveBank )
result = xactEngine->CreateInMemoryWaveBank( mapWaveBank, fileSize, 0, 0, &waveBank );
CloseHandle( hMapFile ); // mapWaveBank maintains a handle on the file so close this unneeded handle
}
}
CloseHandle( hFile ); // mapWaveBank maintains a handle on the file so close this unneeded handle
}
if( FAILED( result ) )
return HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND );
// Read and register the sound bank file with XACT.
result = E_FAIL; // default to failure code, replaced on success
hFile = CreateFile( SOUND_BANK, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
if( hFile != INVALID_HANDLE_VALUE )
{
fileSize = GetFileSize( hFile, NULL );
if( fileSize != -1 )
{
soundBankData = new BYTE[fileSize]; // reserve memory for sound bank
if( soundBankData )
{
if( 0 != ReadFile( hFile, soundBankData, fileSize, &bytesRead, NULL ) )
result = xactEngine->CreateSoundBank( soundBankData, fileSize, 0, 0, &soundBank );
}
}
CloseHandle( hFile );
}
if( FAILED( result ) )
return HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND );
return S_OK;
}
//=============================================================================
// perform periodic sound engine tasks
//=============================================================================
void Audio::run()
{
if (xactEngine == NULL)
return;
xactEngine->DoWork();
}
//=============================================================================
// play sound specified by cue from sound bank
// if cue does not exist no error occurs, there is simply no sound played
//=============================================================================
void Audio::playCue(const char cue[])
{
if (soundBank == NULL)
return;
cueI = soundBank->GetCueIndex( cue ); // get cue index from sound bank
soundBank->Play( cueI, 0, 0, NULL );
}
//=============================================================================
// stop a playing sound specified by cue from sound bank
// if cue does not exist no error occurs
//=============================================================================
void Audio::stopCue(const char cue[])
{
if (soundBank == NULL)
return;
cueI = soundBank->GetCueIndex( cue ); // get cue index from sound bank
soundBank->Stop( cueI, XACT_FLAG_SOUNDBANK_STOP_IMMEDIATE);
}