-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio.cpp
executable file
·189 lines (160 loc) · 5.38 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
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
// Programming 2D Games
// Copyright (c) 2011 by:
// Charles Kelly
// audio.cpp v1.2
#include "audio.h"
//==================================================
// default constructor
//=================================================
Audio::Audio()
{
mute = false;
// remove this line in release (debug purposes)
// mute = true;
}
//==================================================
// default 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();
}
//==================================================
// initalise audio
//
//=================================================
HRESULT Audio::initialise()
{
HRESULT result = E_FAIL;
HANDLE hFile;
DWORD fileSize;
DWORD bytesRead;
HANDLE hMapFile;
result = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (SUCCEEDED(result))
result = XACT3CreateEngine(0, &xactEngine);
if (FAILED(result) || xactEngine == NULL)
return E_FAIL;
result = XACT3CreateEngine(0, &xactEngine);
if (FAILED(result) || xactEngine == NULL)
return E_FAIL;
// Load the global settings file and pass it into XACTInitialize
VOID* pGlobalSettingsData = NULL;
DWORD dwGlobalSettingsFileSize = 0;
bool bSuccess = false;
hFile = CreateFile(XGS_FILE, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile) {
dwGlobalSettingsFileSize = GetFileSize(hFile, NULL);
if (dwGlobalSettingsFileSize != INVALID_FILE_SIZE) {
pGlobalSettingsData = CoTaskMemAlloc(dwGlobalSettingsFileSize);
if (pGlobalSettingsData) {
if (0 != ReadFile(hFile, pGlobalSettingsData, dwGlobalSettingsFileSize, &bytesRead, NULL)) {
bSuccess = true;
}
}
}
CloseHandle(hFile);
}
if (!bSuccess) {
if (pGlobalSettingsData)
CoTaskMemFree(pGlobalSettingsData);
pGlobalSettingsData = NULL;
dwGlobalSettingsFileSize = 0;
}
// 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);
// mapWaveBank maintains a handle on the file
// Close this unneeded handle
CloseHandle(hMapFile);
}
}
// mapWaveBank maintains a handle on the file so close this unneeded // handle
CloseHandle(hFile);
}
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];
if (soundBankData)
// Reserve memory for
// sound bank
{
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
if (!mute)
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);
}