-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwavFile.cpp
163 lines (111 loc) · 2.63 KB
/
wavFile.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
/////////////////////////////////////////////////////////
//
// Name: wavFile.hpp
//
// Copyright: Igor Baklykov (c) 2017
//
// Author: Igor Baklykov
//
// Date: 18.12.2017 17:11
//
// Description: WAVE file
//
/////////////////////////////////////////////////////////
#define UNICODE
#define _UNICODE
#include <fstream>
#include "wavFile.hpp"
// Default c-tor
wavFile::wavFile() : data(NULL), dataSize(0) {}
// Play file
void wavFile::play(bool loop) {
WAVEFORMATEX wf;
wf.wFormatTag = WAVE_FORMAT_PCM;
wf.nChannels = header.getNumOfChannels();
wf.nSamplesPerSec = header.getSampleRate();
wf.nAvgBytesPerSec = header.getByteRate();
wf.nBlockAlign = header.getBlockAlign();
wf.wBitsPerSample = header.getBitsPerSample();
wf.cbSize = 0;
wh.lpData = data + 44;
wh.dwBufferLength = header.getDataSize();
wh.dwFlags = (loop ? (WHDR_BEGINLOOP | WHDR_ENDLOOP) : 0);
wh.dwLoops = 1000;
waveOutOpen(&hWaveOut, WAVE_MAPPER, &wf, 0, 0, CALLBACK_NULL);
waveOutPrepareHeader(hWaveOut, &wh, sizeof(wh));
waveOutWrite(hWaveOut, &wh, sizeof(wh));
}
// Stop playing
void wavFile::stop() {
waveOutReset(hWaveOut);
waveOutUnprepareHeader(hWaveOut, &wh, sizeof(wh));
waveOutClose(hWaveOut);
}
// Save file on disk
void wavFile::save(const wchar_t* fileName) const {
// File name
WCHAR name[MAX_PATH];
// Generate file name
wsprintf(name, L"%s.wav", fileName);
// Open file
HANDLE hFile = CreateFileW(name,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Check if file opened successfully
if (hFile == INVALID_HANDLE_VALUE) {
MessageBoxW(NULL, L"Unable to open file!", L"Error!", MB_OK);
return;
}
// Write error checker
DWORD bytesWriten = 0;
// Write data to file
WriteFile(hFile,
data,
dataSize,
&bytesWriten,
NULL);
// Check if data readed successfully
if (bytesWriten != dataSize) {
MessageBoxW(NULL, L"Unable to write WAV!", L"Error!", MB_OK);
return;
}
// Close file
CloseHandle(hFile);
}
// Set WAV header
void wavFile::setHeader(const wavHeader &_header) {
header = _header;
}
// Set WAV data
void wavFile::setData(const char* _data, const int &size) {
delete [] data;
dataSize = size;
data = new char[size];
memcpy(data, _data, size);
}
// Get WAV header
wavHeader* wavFile::getHeader() {
return &header;
}
// Get WAV data
char* wavFile::getData() const {
return data;
}
// Get file size
size_t wavFile::getSize() const {
return dataSize;
}
// D-tor
wavFile::~wavFile() {
// Clean up
if (data) {
delete [] data;
data = NULL;
}
// Set size to zero
dataSize = 0;
}