File: gmi_wav.c

package info (click to toggle)
easyh10 1.5-4
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,400 kB
  • ctags: 1,022
  • sloc: ansic: 9,050; sh: 8,387; makefile: 77
file content (276 lines) | stat: -rw-r--r-- 6,326 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
/*
 *      Tag and audio information retrieval from Riff/WAVE files.
 *
 *      Copyright (c) 2005 Nyaochi
 *
 * 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, or visit
 * http://www.gnu.org/copyleft/gpl.html .
 *
 */

/* $Id: gmi_wav.c,v 1.3 2005/07/20 02:03:48 nyaochi Exp $ */

#ifdef	HAVE_CONFIG_H
#include <config.h>
#endif/*HAVE_CONFIG_H*/

#include <os.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <ucs2char.h>
#include <h10db.h>
#include <getmediainfo.h>

#define	MIN(a, b)	((a) < (b) ? (a) : (b))

struct tag_chunk_header {
	uint32_t id;
	uint32_t size;
};
typedef struct tag_chunk_header chunk_header_t;


/* Currently, only for little endian machines. */
static int read_uint16le(uint16_t* val, FILE *fp)
{
	uint8_t v[2];
	int ret = (fread(v, sizeof(uint8_t), 2, fp) == 2) ? 0 : 1;
	*val = (uint16_t)v[1] << 8 | (uint16_t)v[0];
	return ret;
}

static int read_uint32le(uint32_t* val, FILE *fp)
{
	uint8_t v[4];
	int ret = (fread(v, sizeof(uint8_t), 4, fp) == 4) ? 0 : 1;
	*val = (uint32_t)v[3] << 24 | (uint32_t)v[2] << 16 | (uint32_t)v[1] << 8 | (uint32_t)v[0];
	return ret;
}

static int read_chunk_header(chunk_header_t* ch, FILE *fp)
{
	int ret = 0;
	ret |= read_uint32le(&ch->id, fp);
	ret |= read_uint32le(&ch->size, fp);
	return ret;
}




int parse_listinfo(media_info* info, FILE *fp, uint32_t chunksize)
{
	int ret = 0;
	char buffer[1024];
	chunk_header_t ch;

	long base = ftell(fp);
	long offset = 0;

	while (offset = ftell(fp), base <= offset && offset < base+(long)chunksize) {
		long pos = 0;
		uint32_t size;
		if (pos < 0) {
			goto parse_listinfo_error;
		}
		ret |= read_chunk_header(&ch, fp);
		if (feof(fp)) {
			break;
		}
		if (ret != 0) {
			goto parse_listinfo_error;
		}

		pos = ftell(fp);

		memset(buffer, 0, sizeof(buffer));
		size = MIN(ch.size, sizeof(buffer)-2);
		fread(buffer, sizeof(char), size, fp);

		switch (ch.id) {
		case 0x44525049:	/* "IPRD" */
			free(info->album);
			info->album = mbsdupucs2_music(buffer);
			break;
		case 0x4D414E49:	/* "INAM" */
			free(info->title);
			info->title = mbsdupucs2_music(buffer);
			break;
		case 0x54524149:	/* "IART" */
			free(info->artist);
			info->artist = mbsdupucs2_music(buffer);
			break;
		case 0x524E4749:	/* "IGNR" */
			free(info->genre);
			info->genre = mbsdupucs2_music(buffer);
			break;
		case 0x44524349:	/* "ICRD" */
			info->year = atoi(buffer);
			break;
		case 0x6974726B:	/* "itrk" */
			info->tracknumer = atoi(buffer);
			break;
		}

		/* Round out the chunk size to align word boundary. */
		if (ch.size & 0x01) {
			ch.size++;
		}

		fseek(fp, pos + ch.size, SEEK_SET);
	}

	return 0;

parse_listinfo_error:
	return 1;
}


int get_riff_audio_info(media_info* info, const ucs2_char_t *filename)
{
	int ret = 0;
	FILE *fp = NULL;
	uint32_t tmp32;
	uint16_t tmp16;
	chunk_header_t ch;
	uint16_t channels, sample_bits;
	
	/* Open the audio file. */
	fp = ucs2fopen(filename, "rb");
	if (!fp) {
		return -1;
	}

	/* Make sure RIFF format. */
	ret |= read_chunk_header(&ch, fp);
	if (ret != 0 || ch.id != 0x46464952) {
		goto get_riff_audio_info_error;
	}
	info->filesize = ch.size + 8;

	/* Make sure Riff type to be "WAVE". */
	ret |= read_uint32le(&tmp32, fp);
	if (ret != 0 || tmp32 != 0x45564157) {
		goto get_riff_audio_info_error;
	}

	for (;;) {
		long pos = 0;
		if (pos < 0) {
			goto get_riff_audio_info_error;
		}
		ret |= read_chunk_header(&ch, fp);
		if (feof(fp)) {
			break;
		}
		if (ret != 0) {
			goto get_riff_audio_info_error;
		}

		pos = ftell(fp);

		switch (ch.id) {
		case 0x20746D66:	/* "fmt " */
			/* Make sure the format to be PCM. */
			ret |= read_uint16le(&tmp16, fp);
			if (ret != 0 || tmp16 != 1) {
				goto get_riff_audio_info_error;
			}
			/* Read number of channels. */
			ret |= read_uint16le(&channels, fp);
			if (ret != 0) {
				goto get_riff_audio_info_error;					
			}
			/* Read sample rate. */
			ret |= read_uint32le(&tmp32, fp);
			if (ret != 0) {
				goto get_riff_audio_info_error;					
			}
			info->samplerate = tmp32;
			/* Skip averate bytes per second. */
			ret |= read_uint32le(&tmp32, fp);
			if (ret != 0) {
				goto get_riff_audio_info_error;					
			}
			/* Skip block align. */
			ret |= read_uint16le(&tmp16, fp);
			if (ret != 0) {
				goto get_riff_audio_info_error;					
			}
			/* Read sample bit. */
			ret |= read_uint16le(&sample_bits, fp);
			if (ret != 0) {
				goto get_riff_audio_info_error;					
			}
			/* Calculate bitrate. */
			info->bitrate = channels * sample_bits * info->samplerate;
			break;
		case 0x61746164:
			info->duration = (uint32_t)((double)ch.size * 8.0 / info->bitrate);
			break;
		case 0x5453494C:
			ret |= read_uint32le(&tmp32, fp);
			if (ret != 0) {
				goto get_riff_audio_info_error;					
			}
			if (tmp32 == 0x4F464E49) {
				ret |= parse_listinfo(info, fp, ch.size - 4);
				if (ret != 0) {
					goto get_riff_audio_info_error;					
				}
			}
			break;
		}

		fseek(fp, pos + ch.size, SEEK_SET);
	}

	fclose(fp);
	return 0;

get_riff_audio_info_error:
	fclose(fp);
	return -1;
}

int gettag_wav(media_info* info, const ucs2_char_t *path, const ucs2_char_t *file)
{
	int ret = 0;

	/* Obtain the filename. */
	ucs2_char_t pathname[MAX_PATH+1];
	ucs2cpy(pathname, path);
	ucs2cat(pathname, file);

	/* Set the pathname and filename. */
	info->pathname = ucs2dup(path);
	info->filename = ucs2dup(file);

	/* Set the filename to title in case we could not retrieve track title. */
	free(info->title);
	info->title = gettag_setfilename(file);

	ret = get_riff_audio_info(info, pathname);

	if (!info->title || !*info->title) {
		ucs2free(info->title);
		info->title = gettag_setfilename(file);
	}

	return ret;
}