-
Notifications
You must be signed in to change notification settings - Fork 226
/
WaveFileWriter.cpp
253 lines (197 loc) · 6.89 KB
/
WaveFileWriter.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
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
//--------------------------------------------------------------------------------------
// WaveFileGenerator.cpp
//
// Demonstrates how to write a WAV file.
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "WaveFileWriter.h"
//
// CWaveFileWriter()
//
CWaveFileWriter::CWaveFileWriter() :
m_hFile(nullptr),
m_pwfxFormat(nullptr),
m_dwFormatSize(0),
m_pvWaveHeader(nullptr),
m_dwWaveHeaderSize(0),
m_dwWritten(0),
m_dwLoopSegmentSize(0)
{
}
//
// ~CWaveFileWriter()
//
CWaveFileWriter::~CWaveFileWriter()
{
Close();
}
//--------------------------------------------------------------------------------------
// Name: Open
// Desc: Creates or overwrites the given file and writes WAV header information
//--------------------------------------------------------------------------------------
HRESULT CWaveFileWriter::Open(LPCWSTR pFileName, LPCWAVEFORMATEX pwfxFormat)
{
HRESULT hr = S_OK;
m_hFile = CreateFile2(pFileName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
CREATE_ALWAYS,
nullptr);
if(m_hFile == INVALID_HANDLE_VALUE)
{
hr = E_FAIL;
}
// Make a copy of the format
if(SUCCEEDED(hr))
{
m_dwFormatSize = GetFormatSize(pwfxFormat);
m_pwfxFormat = (LPWAVEFORMATEX)new BYTE[m_dwFormatSize];
hr = HRFROMP(m_pwfxFormat);
}
if(SUCCEEDED(hr))
{
CopyMemory(m_pwfxFormat, pwfxFormat, m_dwFormatSize);
}
// Allocate the wave header
if(SUCCEEDED(hr))
{
m_dwWaveHeaderSize = GetWaveHeader(m_pwfxFormat, 0, 0, nullptr, 0);
m_pvWaveHeader = new BYTE[m_dwWaveHeaderSize];
hr = HRFROMP(m_pvWaveHeader);
}
// Write the header
if(SUCCEEDED(hr))
{
hr = Commit();
}
return hr;
}
//--------------------------------------------------------------------------------------
// Name: Commit
// Desc: Updates the wave header
//--------------------------------------------------------------------------------------
HRESULT CWaveFileWriter::Commit()
{
BOOL bResult = false;
LARGE_INTEGER liZero = {};
GetWaveHeader(m_pwfxFormat, m_dwLoopSegmentSize, m_dwWritten, m_pvWaveHeader, m_dwWaveHeaderSize);
bResult = SetEndOfFile(m_hFile);
if(bResult)
{
//Move pointer to the beginning of the file
DWORD dwReturn = SetFilePointer(m_hFile, liZero.LowPart, &liZero.HighPart, FILE_BEGIN);
if(dwReturn == INVALID_SET_FILE_POINTER)
{
bResult = false;
}
}
if(bResult)
{
//Write header
bResult = WriteFile(m_hFile, m_pvWaveHeader, m_dwWaveHeaderSize, nullptr, nullptr);
}
if(bResult)
{
//Move pointer to the end of the file
SetFilePointer(m_hFile, liZero.LowPart, &liZero.HighPart, FILE_END);
}
return bResult? S_OK : E_FAIL;
}
//--------------------------------------------------------------------------------------
// Name: Close
// Desc: Commits and frees the object's resources.
//--------------------------------------------------------------------------------------
HRESULT CWaveFileWriter::Close()
{
HRESULT hr = S_OK;
// Go back and write the header
if(m_hFile)
{
hr = Commit();
}
// Close the file
if(m_hFile)
{
CloseHandle(m_hFile);
m_hFile = nullptr;
}
// Free memory and reset the object
delete[] m_pwfxFormat;
delete[] m_pvWaveHeader;
m_dwFormatSize = 0;
m_dwWaveHeaderSize = 0;
m_dwWritten = 0;
return hr;
}
//--------------------------------------------------------------------------------------
// Name: WriteSample
// Desc: Writes wave data to the file.
//--------------------------------------------------------------------------------------
HRESULT CWaveFileWriter::WriteSample(LPVOID pvBuffer, DWORD dwBufferSize, LPDWORD pdwWritten)
{
BOOL bResult;
bResult = WriteFile(m_hFile, pvBuffer, dwBufferSize, pdwWritten, nullptr);
if(bResult)
{
m_dwWritten += pdwWritten ? *pdwWritten : dwBufferSize;
}
return bResult? S_OK : E_FAIL;
}
//--------------------------------------------------------------------------------------
// Name: GetWaveHeader
// Desc: Creates a wave file header.
//--------------------------------------------------------------------------------------
DWORD CWaveFileWriter::GetWaveHeader(LPCWAVEFORMATEX pwfxFormat, DWORD dwLoopSegmentSize, DWORD dwDataSegmentSize, LPVOID pvBuffer, DWORD dwBufferSize)
{
LPRIFFHEADER pChunk;
LPDWORD pdwFileType;
LPWAVEFORMATEX pwfxDstFormat;
DWORD dwRiffChunkSize;
DWORD dwRiffDataSize;
DWORD dwFormatChunkSize;
DWORD dwFormatDataSize;
DWORD dwDataChunkSize;
DWORD dwReqBufferSize;
dwRiffDataSize = sizeof(DWORD);
dwFormatDataSize = GetFormatSize(pwfxFormat);
dwRiffChunkSize = sizeof(RIFFHEADER) + dwRiffDataSize;
dwFormatChunkSize = sizeof(RIFFHEADER) + dwFormatDataSize;
dwDataChunkSize = sizeof(RIFFHEADER);
dwReqBufferSize = dwRiffChunkSize + dwFormatChunkSize + dwDataChunkSize;
if(dwBufferSize >= dwReqBufferSize)
{
pChunk = (LPRIFFHEADER)pvBuffer;
pChunk->dwChunkId = WAVELDR_FOURCC_RIFF;
pChunk->dwDataSize = dwRiffDataSize + dwFormatChunkSize + dwDataChunkSize + dwLoopSegmentSize + dwDataSegmentSize;
pdwFileType = (LPDWORD)(pChunk + 1);
*pdwFileType = WAVELDR_FOURCC_WAVE;
pChunk = (LPRIFFHEADER)(pdwFileType + 1);
pChunk->dwChunkId = WAVELDR_FOURCC_FORMAT;
pChunk->dwDataSize = dwFormatDataSize;
pwfxDstFormat = (LPWAVEFORMATEX)(pChunk + 1);
CopyMemory(pwfxDstFormat, pwfxFormat, dwFormatDataSize);
pChunk = (LPRIFFHEADER)((LPBYTE)pChunk + dwFormatChunkSize);
pChunk->dwChunkId = WAVELDR_FOURCC_DATA;
pChunk->dwDataSize = dwDataSegmentSize;
}
return dwReqBufferSize;
}
//--------------------------------------------------------------------------------------
// Name: CreateWaveHeader
// Desc: Creates a wave file header
//--------------------------------------------------------------------------------------
HRESULT CWaveFileWriter::CreateWaveHeader(LPCWAVEFORMATEX pwfxFormat, DWORD dwLoopSegmentSize, DWORD dwDataSegmentSize, LPVOID* ppvBuffer, LPDWORD pdwBufferSize)
{
HRESULT hr;
*pdwBufferSize = GetWaveHeader(pwfxFormat, dwLoopSegmentSize, dwDataSegmentSize, nullptr, 0);
*ppvBuffer = new BYTE[*pdwBufferSize];
hr = HRFROMP(*ppvBuffer);
if(SUCCEEDED(hr))
{
GetWaveHeader(pwfxFormat, dwLoopSegmentSize, dwDataSegmentSize, *ppvBuffer, *pdwBufferSize);
}
return hr;
}