File: SoundFile.cpp

package info (click to toggle)
wstools 0.4.8d-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,440 kB
  • ctags: 1,991
  • sloc: cpp: 13,880; makefile: 469
file content (350 lines) | stat: -rw-r--r-- 8,209 bytes parent folder | download | duplicates (2)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 *   Copyright (C) 2002-2004 by Jonathan Naylor G4KLX
 *
 *   This program 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 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "SoundFile.h"
#include "Exception.h"

#include <wx/debug.h>
#include <wx/log.h>

#ifdef __WINDOWS__

typedef unsigned char uint8;
typedef signed short  sint16;

CSoundFile::CSoundFile(const wxString& fileName, int sampleRate, int sampleWidth) :
m_fileName(fileName),
m_sampleRate(sampleRate),
m_sampleWidth(sampleWidth),
m_direction(SoundFile_Read),
m_handle(NULL),
m_parent(),
m_child()
{
	wxASSERT(sampleRate > 0);
	wxASSERT(sampleWidth == 8 || sampleWidth == 16);
}

CSoundFile::~CSoundFile()
{
}

void CSoundFile::openRead()
{
	wxASSERT(m_handle == NULL);

	m_handle = ::mmioOpen((CHAR *)m_fileName.c_str(), 0, MMIO_READ | MMIO_ALLOCBUF);

	if (m_handle == NULL)
		throw CException(wxT("Cannot open the WAV file for reading"));

	MMCKINFO parent;
	parent.fccType = mmioFOURCC('W', 'A', 'V', 'E');

	MMRESULT res = ::mmioDescend(m_handle, &parent, 0, MMIO_FINDRIFF);

	if (res != MMSYSERR_NOERROR)
		throw CException(wxT("File is not a valid WAV file"));

	MMCKINFO child;
	child.ckid = mmioFOURCC('f', 'm', 't', ' ');

	res = ::mmioDescend(m_handle, &child, &parent, MMIO_FINDCHUNK);

	if (res != MMSYSERR_NOERROR)
		throw CException(wxT("File is not a valid WAV file"));

	WAVEFORMATEX format;

	LONG len = ::mmioRead(m_handle, (char *)&format, child.cksize);

	if (len != LONG(child.cksize))
		throw CException(wxT("File is not a valid WAV file"));

	if (format.wFormatTag != WAVE_FORMAT_PCM)
		throw CException(wxT("File is not a standard WAV file"));

	if (int(format.nSamplesPerSec) != m_sampleRate) {
		wxString text;
		text.Printf(wxT("Invalid sample rate in the WAV file, found %d samples/s"), int(format.nSamplesPerSec));
		throw CException(text);
	}

	if (format.nChannels != 1)
		throw CException(wxT("More than one channel in the WAV file"));

	m_sampleWidth = format.wBitsPerSample;

	if (m_sampleWidth != 8 && m_sampleWidth != 16)
		throw CException(wxT("None standard data format in the WAV file"));

	res = ::mmioAscend(m_handle, &child, 0);

	if (res != MMSYSERR_NOERROR)
		throw CException(wxT("File is not a valid WAV file"));

	child.ckid = mmioFOURCC('d', 'a', 't', 'a');

	res = ::mmioDescend(m_handle, &child, &parent, MMIO_FINDCHUNK);

	if (res != MMSYSERR_NOERROR)
		throw CException(wxT("File is not a valid WAV file"));

	m_direction = SoundFile_Read;
}

void CSoundFile::openWrite()
{
	wxASSERT(m_handle == NULL);

	m_handle = ::mmioOpen((CHAR *)m_fileName.c_str(), 0, MMIO_WRITE | MMIO_CREATE | MMIO_ALLOCBUF);

	if (m_handle == NULL)
		throw CException(wxT("Cannot open the WAV file for writing"));

	m_parent.fccType = mmioFOURCC('W', 'A', 'V', 'E');
	m_parent.cksize  = 0;

	MMRESULT res = ::mmioCreateChunk(m_handle, &m_parent, MMIO_CREATERIFF);

	if (res != MMSYSERR_NOERROR)
		throw CException(wxT("Cannot write to the WAV file"));

	m_child.ckid   = mmioFOURCC('f', 'm', 't', ' ');
	m_child.cksize = sizeof(WAVEFORMATEX);

	res = ::mmioCreateChunk(m_handle, &m_child, 0);

	if (res != MMSYSERR_NOERROR)
		throw CException(wxT("Cannot write to the WAV file"));

	WAVEFORMATEX format;
	format.wBitsPerSample  = m_sampleWidth;
	format.wFormatTag      = WAVE_FORMAT_PCM;
	format.nChannels       = 1;
	format.nSamplesPerSec  = m_sampleRate;
	format.nAvgBytesPerSec = m_sampleRate * 1 * m_sampleWidth / 8;
	format.nBlockAlign     = 1 * m_sampleWidth / 8;
	format.cbSize          = 0;

	LONG n = ::mmioWrite(m_handle, (CHAR *)&format, sizeof(WAVEFORMATEX));

	if (n != sizeof(WAVEFORMATEX))
		throw CException(wxT("Cannot write to the WAV file"));

	::mmioAscend(m_handle, &m_child, 0);

	m_child.ckid   = mmioFOURCC('d', 'a', 't', 'a');
	m_child.cksize = 0;

	res = ::mmioCreateChunk(m_handle, &m_child, 0);

	if (res != MMSYSERR_NOERROR)
		throw CException(wxT("Cannot write to the WAV file"));

	m_direction = SoundFile_Write;
}

void CSoundFile::read(double* sample, int& len)
{
	wxASSERT(m_handle != NULL);
	wxASSERT(sample != NULL);
	wxASSERT(len > 0);

	if (m_sampleWidth == 8) {
		uint8* data = new uint8[len];

		LONG n = ::mmioRead(m_handle, (char *)data, len);

		if (n <= 0) {
			delete[] data;
			len = n;
			return;
		}

		for (int i = 0; i < n; i++)
			sample[i] = (double(data[i]) - 127.0) / 128.0;

		delete[] data;

		len = n;
	} else {
		sint16* data = new sint16[len];

		LONG n = ::mmioRead(m_handle, (char *)data, len * 2);

		if (n <= 0) {
			delete[] data;
			len = n;
			return;
		}

		n /= 2;

		for (int i = 0; i < n; i++)
			sample[i] = double(data[i]) / 32768.0;

		delete[] data;

		len = n;
	}
}

void CSoundFile::write(double* sample, int len)
{
	wxASSERT(m_handle != NULL);
	wxASSERT(sample != NULL);
	wxASSERT(len > 0);

	if (m_sampleWidth == 8) {
		uint8* data = new uint8[len];

		for (int i = 0; i < len; i++)
			data[i] = uint8(sample[i] * 128.0 + 127.0);

		LONG n = ::mmioWrite(m_handle, (char *)data, len * sizeof(uint8));

		delete[] data;

		if (n != LONG(len * sizeof(uint8)))
			throw CException(wxT("Cannot write to the WAV file"));
	} else {
		sint16* data = new sint16[len];

		for (int i = 0; i < len; i++)
			data[i] = sint16(sample[i] * 32768.0);

		LONG n = ::mmioWrite(m_handle, (char *)data, len * sizeof(sint16));

		delete[] data;

		if (n != LONG(len * sizeof(sint16)))
			throw CException(wxT("Cannot write to the WAV file"));
	}
}

void CSoundFile::close()
{
	wxASSERT(m_handle != NULL);

	if (m_direction == SoundFile_Write) {
		::mmioAscend(m_handle, &m_child, 0);
		::mmioAscend(m_handle, &m_parent, 0);
	}

	::mmioClose(m_handle, 0);
	m_handle = NULL;
}

#else

CSoundFile::CSoundFile(const wxString& fileName, int sampleRate, int sampleWidth) :
m_fileName(fileName),
m_sampleRate(sampleRate),
m_sampleWidth(sampleWidth),
m_direction(SoundFile_Read),
m_file(NULL)
{
	wxASSERT(sampleRate > 0);
	wxASSERT(sampleWidth == 8 || sampleWidth == 16);
}

CSoundFile::~CSoundFile()
{
}

void CSoundFile::openRead()
{
	wxASSERT(m_file == NULL);

	SF_INFO info;
	info.format = 0;
	m_file = ::sf_open(m_fileName.mb_str(), SFM_READ, &info);

	if (m_file == NULL)
		throw CException(wxT("Cannot open the WAV file for reading"));

	if (info.samplerate != m_sampleRate) {
		wxString text;
		text.Printf(wxT("Invalid sample rate in the WAV file, found %d samples/s"), info.samplerate);
		throw CException(text);
	}

	if (info.channels != 1)
		throw CException(wxT("More than one channel in the WAV file"));

	m_direction = SoundFile_Read;
}

void CSoundFile::openWrite()
{
	wxASSERT(m_file == NULL);

	SF_INFO info;
	info.samplerate = m_sampleRate;
	info.channels   = 1;
	info.format     = SF_FORMAT_WAV;

	if (m_sampleWidth == 8)
		info.format |= SF_FORMAT_PCM_U8;
	else
		info.format |= SF_FORMAT_PCM_16;

	int ret = ::sf_format_check(&info);

	if (!ret)
		throw CException(wxT("Mistake in setting up SF_INFO for write"));

	m_file = ::sf_open(m_fileName.mb_str(), SFM_WRITE, &info);

	if (m_file == NULL)
		throw CException(wxT("Cannot open the WAV file for writing"));

	m_direction = SoundFile_Write;
}

void CSoundFile::read(double* sample, int& len)
{
	wxASSERT(m_file != NULL);
	wxASSERT(sample != NULL);
	wxASSERT(len > 0);

	len = ::sf_read_double(m_file, sample, len);
}

void CSoundFile::write(double* sample, int len)
{
	wxASSERT(m_file != NULL);
	wxASSERT(sample != NULL);
	wxASSERT(len > 0);

	int n = ::sf_write_double(m_file, sample, len);

	if (n != len)
		throw CException(wxT("Error writing to the sound file"));
}

void CSoundFile::close()
{
	wxASSERT(m_file != NULL);

	::sf_close(m_file);
	m_file = NULL;
}

#endif