summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/formats/wavfile.c
blob: d9668d487234c77a3b21d18da78f9728c4460be9 (plain) (blame)
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
/*********************************************************************

    wavfile.c

    Format code for wave (*.wav) files

*********************************************************************/

#include <stdio.h>
#include <assert.h>

#include "wavfile.h"
#include "cassimg.h"

static const char magic1[4] = { 'R', 'I', 'F', 'F' };
static const char magic2[4] = { 'W', 'A', 'V', 'E' };
static const char format_tag_id[4] = { 'f', 'm', 't', ' ' };
static const char data_tag_id[4] = { 'd', 'a', 't', 'a' };

#define WAV_FORMAT_PCM		1



static UINT32 get_leuint32(const void *ptr)
{
	UINT32 value;
	memcpy(&value, ptr, sizeof(value));
	return LITTLE_ENDIANIZE_INT32(value);
}



static UINT16 get_leuint16(const void *ptr)
{
	UINT16 value;
	memcpy(&value, ptr, sizeof(value));
	return LITTLE_ENDIANIZE_INT16(value);
}



static void put_leuint32(void *ptr, UINT32 value)
{
	value = LITTLE_ENDIANIZE_INT32(value);
	memcpy(ptr, &value, sizeof(value));
}



static void put_leuint16(void *ptr, UINT16 value)
{
	value = LITTLE_ENDIANIZE_INT16(value);
	memcpy(ptr, &value, sizeof(value));
}



static casserr_t wavfile_process(cassette_image *cassette, struct CassetteOptions *opts,
	int read_waveform)
{
	UINT8 file_header[12];
	UINT8 tag_header[8];
	UINT8 format_tag[16];
	UINT32 stated_size;
	UINT64 file_size;
	UINT32 tag_size;
	UINT32 tag_samples;
	UINT64 offset;
	int format_specified = FALSE;

	UINT16 format_type = 0;
	UINT32 bytes_per_second = 0;
//	UINT16 block_align = 0;
	int waveform_flags = 0;

	/* read header */
	cassette_image_read(cassette, file_header, 0, sizeof(file_header));
	offset = sizeof(file_header);

	/* check magic numbers */
	if (memcmp(&file_header[0], magic1, 4))
		return CASSETTE_ERROR_INVALIDIMAGE;
	if (memcmp(&file_header[8], magic2, 4))
		return CASSETTE_ERROR_INVALIDIMAGE;

	/* read and sanity check size */
	stated_size = get_leuint32(&file_header[4]) + 8;
	file_size = cassette_image_size(cassette);
	if (stated_size > file_size)
		stated_size = (UINT32) file_size;

	while(offset < stated_size)
	{
		cassette_image_read(cassette, tag_header, offset, sizeof(tag_header));
		tag_size = get_leuint32(&tag_header[4]);
		offset += sizeof(tag_header);

		if (!memcmp(tag_header, format_tag_id, 4))
		{
			/* format tag */
			if (format_specified || (tag_size < sizeof(format_tag)))
				return CASSETTE_ERROR_INVALIDIMAGE;
			format_specified = TRUE;

			cassette_image_read(cassette, format_tag, offset, sizeof(format_tag));

			format_type				= get_leuint16(&format_tag[0]);
			opts->channels			= get_leuint16(&format_tag[2]);
			opts->sample_frequency	= get_leuint32(&format_tag[4]);
			bytes_per_second		= get_leuint32(&format_tag[8]);
//			block_align				= get_leuint16(&format_tag[12]);
			opts->bits_per_sample	= get_leuint16(&format_tag[14]);

			if (format_type != WAV_FORMAT_PCM)
				return CASSETTE_ERROR_INVALIDIMAGE;
			if (opts->sample_frequency * opts->bits_per_sample * opts->channels / 8 != bytes_per_second)
				return CASSETTE_ERROR_INVALIDIMAGE;

			switch(opts->bits_per_sample)
			{
				case 8:
					waveform_flags = CASSETTE_WAVEFORM_8BIT;
					break;
				case 16:
					waveform_flags = CASSETTE_WAVEFORM_16BITLE;
					break;
				case 32:
					waveform_flags = CASSETTE_WAVEFORM_32BITLE;
					break;
				default:
					return CASSETTE_ERROR_INVALIDIMAGE;
			}
		}
		else if (!memcmp(tag_header, data_tag_id, 4))
		{
			/* data tag */
			if (!format_specified)
				return CASSETTE_ERROR_INVALIDIMAGE;

			if (read_waveform)
			{
				tag_samples = tag_size / (opts->bits_per_sample / 8) / opts->channels;
				cassette_read_samples(cassette, opts->channels, 0.0, tag_samples / ((double) opts->sample_frequency),
					tag_samples, offset, waveform_flags);
			}
		}
		else
		{
			/* ignore other tags */
		}
		offset += tag_size;
	}

	return CASSETTE_ERROR_SUCCESS;
}



static casserr_t wavfile_identify(cassette_image *cassette, struct CassetteOptions *opts)
{
	return wavfile_process(cassette, opts, FALSE);
}



static casserr_t wavfile_load(cassette_image *cassette)
{
	struct CassetteOptions opts;
	memset(&opts, 0, sizeof(opts));
	return wavfile_process(cassette, &opts, TRUE);
}



static casserr_t wavfile_save(cassette_image *cassette, const struct CassetteInfo *info)
{
	casserr_t err;
	UINT8 consolidated_header[12 + 8 + 16 + 8];
	UINT8 *header				= &consolidated_header[0];
	UINT8 *format_tag_header	= &consolidated_header[12];
	UINT8 *format_tag_data		= &consolidated_header[12 + 8];
	UINT8 *data_tag_header		= &consolidated_header[12 + 8 + 16];
	UINT32 file_size;
	UINT32 bytes_per_second;
	UINT16 bits_per_sample;
	UINT32 data_size;
	size_t bytes_per_sample = 2;
	int waveform_flags = CASSETTE_WAVEFORM_16BITLE;
	UINT16 block_align;

	bits_per_sample = (UINT16) (bytes_per_sample * 8);
	bytes_per_second = info->sample_frequency * bytes_per_sample * info->channels;
	data_size = (UINT32) (info->sample_count * bytes_per_sample * info->channels);
	file_size = data_size + sizeof(consolidated_header) - 8;
	block_align = (UINT16) (bytes_per_sample * info->channels);

	/* set up header */
	memcpy(&header[0],					magic1, 4);
	memcpy(&header[8],					magic2, 4);
	put_leuint32(&header[4],			file_size);

	/* set up format tag */
	memcpy(&format_tag_header[0],		format_tag_id, 4);
	put_leuint32(&format_tag_header[4],	16);
	put_leuint16(&format_tag_data[0],	WAV_FORMAT_PCM);
	put_leuint16(&format_tag_data[2],	info->channels);
	put_leuint32(&format_tag_data[4],	info->sample_frequency);
	put_leuint32(&format_tag_data[8],	bytes_per_second);
	put_leuint16(&format_tag_data[12],	block_align);
	put_leuint16(&format_tag_data[14],	bits_per_sample);

	/* set up data tag */
	memcpy(&data_tag_header[0],			data_tag_id, 4);
	put_leuint32(&data_tag_header[4],	data_size);

	/* write consolidated header */
	cassette_image_write(cassette, consolidated_header, 0, sizeof(consolidated_header));

	/* write out the actual data */
	err = cassette_write_samples(cassette, info->channels, 0.0, info->sample_count
		/ (double) info->sample_frequency, info->sample_count, sizeof(consolidated_header),
		waveform_flags);
	if (err)
		return err;

	return CASSETTE_ERROR_SUCCESS;
}



const struct CassetteFormat wavfile_format =
{
	"wav",
	wavfile_identify,
	wavfile_load,
	wavfile_save
};



/*********************************************************************
    wavfile_testload()

    This is a hokey function used to test the cassette wave loading
    system, specifically to test that when one loads a WAV file image
    that the resulting info queried will be the same data in the WAV.

    This code has already identified some rounding errors
*********************************************************************/

#ifdef UNUSED_FUNCTION
void wavfile_testload(const char *fname)
{
	cassette_image *cassette;
	FILE *f;
	long offset;
	int freq, samples, i;
	INT32 cassamp;
	INT16 wavsamp;

	f = fopen(fname, "rb");
	if (!f)
		return;

	if (cassette_open(f, &stdio_ioprocs, &wavfile_format, CASSETTE_FLAG_READONLY, &cassette))
	{
		fclose(f);
		return;
	}

	offset = 44;
	freq = 44100;
	samples = 5667062;

	for (i = 0; i < samples; i++)
	{
		cassette_get_sample(cassette, 0, i / (double) freq, 0.0, &cassamp);

		fseek(f, offset + i * 2, SEEK_SET);
		fread(&wavsamp, 1, 2, f);
		assert(cassamp == (((UINT32) wavsamp) << 16));
	}

	cassette_close(cassette);

	fclose(f);
}
#endif