File: sndfile_engine.c

package info (click to toggle)
alsaplayer 0.99.82-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,892 kB
  • sloc: ansic: 40,741; cpp: 14,400; makefile: 983; sh: 796; lex: 751; asm: 45; python: 29; sed: 16
file content (370 lines) | stat: -rw-r--r-- 7,486 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 *  sndfile_engine.c (C) 2002 by Andy Lo A Foe <andy@loafoe.com>
 *
 *  This file is part of AlsaPlayer.
 *
 *  AlsaPlayer 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  AlsaPlayer 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <alloca.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <sndfile.h>

#include "input_plugin.h"
#include "alsaplayer_error.h"
#include "ap_string.h"
#include "ap_unused.h"

static const int BLOCK_SAMPLES = 256;

struct sf_local_data
{
	SNDFILE* sfhandle;
	SF_INFO	 sfinfo;
	int blocksize;
	char filename[1024];
	char path[1024];
};


static int sndfile_open (input_object *obj, const char *name)
{
	struct sf_local_data *data;
	char *p;

	if (!obj)
		return 0;

	obj->local_data = malloc(sizeof(struct sf_local_data));

	if (!obj->local_data)
	{
		return 0;
	}
	obj->nr_blocks = 0;

	data = (struct sf_local_data *) obj->local_data;

	memset (&data->sfinfo, 0, sizeof (data->sfinfo));
	data->sfhandle = sf_open(name, SFM_READ, &data->sfinfo);

	if (data->sfhandle == NULL)
	{
		free(obj->local_data);
		obj->local_data = NULL;
		return 0;
	}

	if (data->sfinfo.channels > 2)
	{
		alsaplayer_error("sndfile_engine: no support for 2+ channels");
		sf_close(data->sfhandle);
		free(obj->local_data);
		obj->local_data = NULL;
		return 0;
	}

	data->blocksize = BLOCK_SAMPLES;

	p = strrchr(name, '/');
	if (p) {
		ap_strlcpy(data->filename, ++p, sizeof (data->filename));
	} else {
		ap_strlcpy(data->filename, name, sizeof (data->filename));
	}

	ap_strlcpy(data->path, name, sizeof (data->path));
	if (data->sfinfo.seekable)
		obj->flags = P_SEEK;
	return 1;
}


static void
sndfile_close (input_object *obj)
{
	if (obj == NULL)
		return;

	if (obj->local_data) {
		struct sf_local_data *data =
			(struct sf_local_data *) obj->local_data;
		sf_close(data->sfhandle);
		free(obj->local_data);
		obj->local_data = NULL;
	}

	return;
}


static int sndfile_play_block (input_object *obj, short *buf)
{
	size_t	samples_to_read;
	size_t	items_read;
	size_t	i;
	short	*buffer;

	struct sf_local_data	*data;

	if (!obj)
		return 0;

	data = (struct sf_local_data *) obj->local_data;

	if (!data)
		return 0;

	buffer = alloca(BLOCK_SAMPLES * sizeof (buffer [0]));

	if (!buffer)
		return 0;

	if (data->sfinfo.channels == 1) { /* Mono, so double */
		short *dest;
		short *src;

		samples_to_read = BLOCK_SAMPLES / 2 ;
		items_read = sf_read_short(data->sfhandle, buffer, samples_to_read);

		if (buf) {
			src = buffer;
			dest = buf;
			for (i = 0; i < items_read; i++) {
				dest [2 * i] = src [i];
				dest [2 * i + 1] =	src [i];
			}
			if (items_read < samples_to_read) {
				return 0;
			}
		}
	} else {
		samples_to_read = BLOCK_SAMPLES;

		items_read = sf_read_short(data->sfhandle, buffer, samples_to_read);
		if (buf)
			memcpy(buf, buffer, BLOCK_SAMPLES * sizeof (buffer [0]));
		else
			return 0;
		if (items_read < samples_to_read)
			return 0;
	}
	return 1;
}


static int sndfile_block_seek (input_object *obj, int block)
{
	struct sf_local_data	*data;
	sf_count_t pos;
	int result = 0;

	if (!obj)
		return result;

	data = (struct sf_local_data *) obj->local_data;

	if (data->sfhandle == NULL)
	{
		return result;
	}
	pos = (block * BLOCK_SAMPLES) / data->sfinfo.channels ;
	//alsaplayer_error("pos = %d", pos);
	if (sf_seek(data->sfhandle, pos, SEEK_SET) != pos)
		return 0;
	return block;
}


static int sndfile_nr_blocks (input_object *obj)
{
	struct sf_local_data    *data;
	sf_count_t samples;

	if (!obj)
		return 0;
	data = (struct sf_local_data *) obj->local_data;
	samples = data->sfinfo.frames;

	if (samples > 0)  {
		return ((int)data->sfinfo.frames * 2 / BLOCK_SAMPLES);
	}
	return 0;
}

static int64_t
sndfile_frame_count (input_object *obj)
{
	struct sf_local_data    *data;

	if (!obj)
		return 0;
	data = (struct sf_local_data *) obj->local_data;

	if (data->sfinfo.frames > 0)
		return data->sfinfo.frames;

	return -1;
}

static int sndfile_block_size (input_object * UNUSED (obj))
{
	return BLOCK_SAMPLES;
}


static int sndfile_sample_rate (input_object *obj)
{
	struct sf_local_data *data;

	if (!obj)
		return 0;

	data = (struct sf_local_data *) obj->local_data;

	return data->sfinfo.samplerate;
}


static int sndfile_channels (input_object *obj)
{
	if (!obj)
		return 0;

	return obj->nr_channels;
}


static  long sndfile_block_to_sec (input_object *obj, int block)
{
	unsigned long	result = 0;

	struct sf_local_data	*data;

	if (!obj)
		return result;

	data = (struct sf_local_data *) obj->local_data;

	if (!data)
		return result;
	result = (unsigned long) (block * BLOCK_SAMPLES /
			(data->sfinfo.samplerate * sizeof (short) / 100));

	return result;
}

static float sndfile_can_handle (const char *name)
{
	const char *fname = strrchr(name, '/');
	const char *dot;

	/* sndfile input plugin doesn't handle streaming. */
	if (strncasecmp (name, "http://", 7) == 0)
		return 0.0;

	if (!fname)
		fname = name;
	if ((dot = strrchr(fname, '.')) != NULL)
	{
		dot++;

		if (!strcasecmp(dot, "aif")
			|| !strcasecmp(dot, "aiff")
			|| !strcasecmp(dot, "au")
			|| !strcasecmp(dot, "opus")
			|| !strcasecmp(dot, "svx")
			|| !strcasecmp(dot, "voc")
			|| !strcasecmp(dot, "wav")
			)
			return 1.0;

		if (strcasecmp(dot, "flac") == 0)
		{
			SNDFILE *file;
			SF_INFO info;
			memset (&info, 0, sizeof (info));
			file = sf_open (name, SFM_READ, &info);
			sf_close (file);
			return file ? 1.0 : 0.0 ;
		}

	}
	return 0.0;
}


static int sndfile_stream_info (input_object *obj, stream_info *info)
{
	struct sf_local_data	*data;

	if (!obj || !info)
		return 0;

	data = (struct sf_local_data *) obj->local_data;

	if (data->sfhandle == NULL)
	{
		return 0;
	}


	ap_strlcpy(info->stream_type, "sndfile supported format", sizeof (info->stream_type));
	ap_strlcpy(info->title, data->filename, sizeof (info->title));
	info->status [0] = 0;
	info->artist [0] = 0;

	return 1;
}

static int sndfile_init (void)
{
	return 1;
}

static void sndfile_shutdown (void)
{
	return;
}


static input_plugin sndfile_plugin;


input_plugin *input_plugin_info (void)
{
	memset(&sndfile_plugin, 0, sizeof(input_plugin));
	sndfile_plugin.version = INPUT_PLUGIN_VERSION;
	sndfile_plugin.name = "libsndfile plugin v0.1";
	sndfile_plugin.author = "Andy Lo A Foe";
	sndfile_plugin.init = sndfile_init;
	sndfile_plugin.shutdown = sndfile_shutdown;
	sndfile_plugin.can_handle = sndfile_can_handle;
	sndfile_plugin.open = sndfile_open;
	sndfile_plugin.close = sndfile_close;
	sndfile_plugin.play_block = sndfile_play_block;
	sndfile_plugin.block_seek = sndfile_block_seek;
	sndfile_plugin.block_size = sndfile_block_size;
	sndfile_plugin.nr_blocks = sndfile_nr_blocks;
	sndfile_plugin.frame_count = sndfile_frame_count;
	sndfile_plugin.block_to_sec = sndfile_block_to_sec;
	sndfile_plugin.sample_rate = sndfile_sample_rate;
	sndfile_plugin.channels = sndfile_channels;
	sndfile_plugin.stream_info = sndfile_stream_info;
	return &sndfile_plugin;
}