-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundBuffer.cpp
185 lines (158 loc) · 5.91 KB
/
SoundBuffer.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
/********************************************************************************************
*********************************************************************************************
Aaron's Tic-Tac-Toe Clone
A 2 player verison of Tic-Tac-Toe game that's played on a console screen.
Copyright (C) 2012 Aaron Gagern
This file is part of Aaron's Tic-Tac-Toe Clone.
Aaron's Tic-Tac-Toe Clone is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Aaron's Tic-Tac-Toe Clone is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Aaron's Tic-Tac-Toe Clone. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************************
********************************************************************************************/
#include "SoundBuffer.h"
SoundBuffer::SoundBuffer() throw()
: isSoundBufferInitialized_(false)
{}
SoundBuffer::SoundBuffer(const SoundBuffer &sb) throw()
{
soundData_.reset();
format_ = sb.format_;
buffer_ = sb.buffer_;
if(sb.soundData_) {
soundData_ = bytePtr(new BYTE[buffer_.AudioBytes]);
CopyMemory(soundData_.get(), sb.soundData_.get(), buffer_.AudioBytes);
buffer_.pAudioData = soundData_.get();
}
}
SoundBuffer::~SoundBuffer() throw()
{
if(soundData_)
soundData_.reset();
}
SoundBuffer& SoundBuffer::operator=(const SoundBuffer &sb) throw()
{
soundData_.reset();
format_ = sb.format_;
buffer_ = sb.buffer_;
if(sb.soundData_) {
soundData_ = bytePtr(new BYTE[buffer_.AudioBytes]);
CopyMemory(soundData_.get(), sb.soundData_.get(), buffer_.AudioBytes);
buffer_.pAudioData = soundData_.get();
}
return *this;
}
void SoundBuffer::InitializeSoundBuffer()
{
soundData_.reset();
SecureZeroMemory(&format_, sizeof(format_));
SecureZeroMemory(&buffer_, sizeof(buffer_));
isSoundBufferInitialized_ = true;
}
bool SoundBuffer::LoadFile(const char *soundFile)
{
if(soundFile == NULL)
throw Exception(err.SoundBuffer_Fatal_Error, "Cannot find sound file, name is NULL.");
std::ifstream inputSoundFile(soundFile, std::ios::binary | std::ios::in);
if(inputSoundFile.bad())
throw Exception(err.SoundBuffer_Fatal_Error, "Sound file is bad or corrupted.");
DWORD dwChunkId = 0, dwFileSize = 0, dwChunkSize = 0, dwExtra = 0;
//find for 'RIFF' chunk identifier
inputSoundFile.seekg(0, std::ios::beg);
inputSoundFile.read(reinterpret_cast<char*>(&dwChunkId), sizeof(dwChunkId));
if(dwChunkId != 'FFIR') {
inputSoundFile.close();
throw Exception(err.SoundBuffer_Fatal_Error, "Couldn't find the RIFF chunk identifier.\nFile may be corrupted.");
}
//Get file size
inputSoundFile.seekg(4, std::ios::beg);
inputSoundFile.read(reinterpret_cast<char*>(&dwFileSize), sizeof(dwFileSize));
if(dwFileSize <= 16) {
inputSoundFile.close();
throw Exception(err.SoundBuffer_Fatal_Error, "Could not determine file size.\nFile may be corrupted.");
}
//Get file format
inputSoundFile.seekg(8, std::ios::beg);
inputSoundFile.read(reinterpret_cast<char*>(&dwExtra), sizeof(dwExtra));
if(dwExtra != 'EVAW') {
inputSoundFile.close();
throw Exception(err.SoundBuffer_Fatal_Error, "Could not determine file format.\nFile may be corrupted.");
}
//find 'fmt ' in the chunk id
bool isFormatOK = false;
for(unsigned int i = 12; i < dwFileSize; ) {
inputSoundFile.seekg(i, std::ios::beg);
inputSoundFile.read(reinterpret_cast<char*>(&dwChunkId), sizeof(dwChunkId));
inputSoundFile.seekg(i + 4, std::ios::beg);
inputSoundFile.read(reinterpret_cast<char*>(&dwChunkSize), sizeof(dwChunkSize));
if(dwChunkId == ' tmf') {
inputSoundFile.seekg(i + 8, std::ios::beg);
inputSoundFile.read(reinterpret_cast<char*>(&format_), sizeof(format_));
isFormatOK = true;
break;
}
dwChunkSize += 8; //add offsets of the chunk id, and chunk size data entries
dwChunkSize += 1;
dwChunkSize &= 0xfffffffe; //guarantees WORD padding alignment
i += dwChunkSize;
}
if(!isFormatOK) {
inputSoundFile.close();
throw Exception(err.SoundBuffer_Fatal_Error, "Couldn't find \'fmt\' in chunk id, sound file may be corrupted");
}
//find 'data' in the chunk id
bool isDataOK = false;
for(unsigned int i = 12; i < dwFileSize; ) {
inputSoundFile.seekg(i, std::ios::beg);
inputSoundFile.read(reinterpret_cast<char*>(&dwChunkId), sizeof(dwChunkId));
inputSoundFile.seekg(i + 4, std::ios::beg);
inputSoundFile.read(reinterpret_cast<char*>(&dwChunkSize), sizeof(dwChunkSize));
if(dwChunkId == 'atad') {
soundData_ = bytePtr(new BYTE[dwChunkSize]);
inputSoundFile.seekg((i + 8), std::ios::beg);
inputSoundFile.read(reinterpret_cast<char*>(soundData_.get()), dwChunkSize);
buffer_.AudioBytes = dwChunkSize;
buffer_.pAudioData = soundData_.get();
buffer_.PlayBegin = 0;
buffer_.PlayLength = 0;
isDataOK = true;
break;
}
dwChunkSize += 8; //add offsets of the chunk id, and chunk size data entries
dwChunkSize += 1;
dwChunkSize &= 0xfffffffe; //guarantees WORD padding alignment
i += dwChunkSize;
}
if(!isDataOK) {
inputSoundFile.close();
throw Exception(err.SoundBuffer_Fatal_Error, "Couldn't find \'data\' in chunk id, sound file may be corrupted");
}
inputSoundFile.close();
return true;
}
const xa2BufferPtr SoundBuffer::GetXA2Buffer() const
{
xa2BufferPtr temp(new XAUDIO2_BUFFER());
if(isSoundBufferInitialized_) {
*temp = buffer_;
return temp;
}
else
throw Exception(err.Invalid_Variable_Access);
}
const wFormatPtr SoundBuffer::GetWFormat() const
{
wFormatPtr temp(new WAVEFORMATEX());
if(isSoundBufferInitialized_) {
*temp = format_;
return temp;
}
else
throw Exception(err.Invalid_Variable_Access);
}