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
|
/*
DeaDBeeF - ultimate music player for GNU/Linux systems with X11
Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __linux__
#define _LARGEFILE64_SOURCE
#endif
#include <string.h>
#include <sndfile.h>
#include "../../deadbeef.h"
#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))
//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(fmt,...)
static DB_decoder_t plugin;
static DB_functions_t *deadbeef;
typedef struct {
DB_fileinfo_t info;
SNDFILE *ctx;
DB_FILE *file;
int startsample;
int endsample;
int currentsample;
int bitrate;
} sndfile_info_t;
// vfs wrapper for sf
static sf_count_t
sf_vfs_get_filelen (void *user_data) {
sndfile_info_t *ctx = user_data;
return deadbeef->fgetlength (ctx->file);
}
static sf_count_t
sf_vfs_read (void *ptr, sf_count_t count, void *user_data) {
sndfile_info_t *ctx = user_data;
return deadbeef->fread (ptr, 1, count, ctx->file);
}
static sf_count_t
sf_vfs_write (const void *ptr, sf_count_t count, void *user_data) {
return -1;
}
static sf_count_t
sf_vfs_seek (sf_count_t offset, int whence, void *user_data) {
sndfile_info_t *ctx = user_data;
int ret = deadbeef->fseek (ctx->file, offset, whence);
if (!ret) {
return offset;
}
return -1;
}
static sf_count_t
sf_vfs_tell (void *user_data) {
sndfile_info_t *ctx = user_data;
return deadbeef->ftell (ctx->file);
}
static SF_VIRTUAL_IO vfs = {
.get_filelen = sf_vfs_get_filelen,
.seek = sf_vfs_seek,
.read = sf_vfs_read,
.write = sf_vfs_write,
.tell = sf_vfs_tell
};
static DB_fileinfo_t *
sndfile_open (void) {
DB_fileinfo_t *_info = malloc (sizeof (sndfile_info_t));
memset (_info, 0, sizeof (sndfile_info_t));
return _info;
}
static int
sndfile_init (DB_fileinfo_t *_info, DB_playItem_t *it) {
sndfile_info_t *info = (sndfile_info_t*)_info;
SF_INFO inf;
DB_FILE *fp = deadbeef->fopen (it->fname);
if (!fp) {
trace ("sndfile: failed to open %s\n", it->fname);
return -1;
}
int fsize = deadbeef->fgetlength (fp);
info->file = fp;
info->ctx = sf_open_virtual (&vfs, SFM_READ, &inf, info);
if (!info->ctx) {
trace ("sndfile: %s: unsupported file format\n");
return -1;
}
_info->plugin = &plugin;
_info->bps = 16;
_info->channels = inf.channels;
_info->samplerate = inf.samplerate;
_info->readpos = 0;
if (it->endsample > 0) {
info->startsample = it->startsample;
info->endsample = it->endsample;
if (plugin.seek_sample (_info, 0) < 0) {
return -1;
}
}
else {
info->startsample = 0;
info->endsample = inf.frames-1;
}
// hack bitrate
float sec = (float)(info->endsample-info->startsample) / inf.samplerate;
if (sec > 0) {
info->bitrate = fsize / sec * 8 / 1000;
}
else {
info->bitrate = -1;
}
return 0;
}
static void
sndfile_free (DB_fileinfo_t *_info) {
sndfile_info_t *info = (sndfile_info_t*)_info;
if (info->ctx) {
sf_close (info->ctx);
}
if (info->file) {
deadbeef->fclose (info->file);
}
memset (&info, 0, sizeof (info));
}
static int
sndfile_read_int16 (DB_fileinfo_t *_info, char *bytes, int size) {
sndfile_info_t *info = (sndfile_info_t*)_info;
if (size / (2 * _info->channels) + info->currentsample > info->endsample) {
size = (info->endsample - info->currentsample + 1) * 2 * _info->channels;
trace ("wv: size truncated to %d bytes, cursample=%d, endsample=%d\n", size, info->currentsample, info->endsample);
if (size <= 0) {
return 0;
}
}
int n = sf_readf_short (info->ctx, (short *)bytes, size/(2*_info->channels));
info->currentsample += n;
size = n * 2 * _info->channels;
_info->readpos = (float)(info->currentsample-info->startsample)/_info->samplerate;
if (info->bitrate > 0) {
deadbeef->streamer_set_bitrate (info->bitrate);
}
return size;
}
static int
sndfile_read_float32 (DB_fileinfo_t *_info, char *bytes, int size) {
sndfile_info_t *info = (sndfile_info_t*)_info;
if (size / (4 * _info->channels) + info->currentsample > info->endsample) {
size = (info->endsample - info->currentsample + 1) * 4 * _info->channels;
trace ("wv: size truncated to %d bytes, cursample=%d, endsample=%d\n", size, info->currentsample, info->endsample);
if (size <= 0) {
return 0;
}
}
int n = sf_readf_float (info->ctx, (float *)bytes, size/(4*_info->channels));
info->currentsample += n;
size = n * 4 * _info->channels;
_info->readpos = (float)(info->currentsample-info->startsample)/_info->samplerate;
if (info->bitrate > 0) {
deadbeef->streamer_set_bitrate (info->bitrate);
}
return size;
}
static int
sndfile_seek_sample (DB_fileinfo_t *_info, int sample) {
sndfile_info_t *info = (sndfile_info_t*)_info;
int ret = sf_seek (info->ctx, sample + info->startsample, SEEK_SET);
if (ret < 0) {
return -1;
}
info->currentsample = ret;
_info->readpos = (float)(info->currentsample - info->startsample) / _info->samplerate;
return 0;
}
static int
sndfile_seek (DB_fileinfo_t *_info, float sec) {
return sndfile_seek_sample (_info, sec * _info->samplerate);
}
static DB_playItem_t *
sndfile_insert (DB_playItem_t *after, const char *fname) {
SF_INFO inf;
sndfile_info_t info;
memset (&info, 0, sizeof (info));
info.file = deadbeef->fopen (fname);
if (!info.file) {
trace ("sndfile: failed to open %s\n", fname);
return NULL;
}
info.ctx = sf_open_virtual (&vfs, SFM_READ, &inf, &info);
if (!info.ctx) {
trace ("sndfile: sf_open failed");
return NULL;
}
int totalsamples = inf.frames;
int samplerate = inf.samplerate;
sf_close (info.ctx);
deadbeef->fclose (info.file);
float duration = (float)totalsamples / samplerate;
DB_playItem_t *it = deadbeef->pl_item_alloc ();
it->decoder_id = deadbeef->plug_get_decoder_id (plugin.plugin.id);
it->fname = strdup (fname);
it->filetype = "wav";
deadbeef->pl_set_item_duration (it, duration);
trace ("sndfile: totalsamples=%d, samplerate=%d, duration=%f\n", totalsamples, samplerate, duration);
DB_playItem_t *cue_after = deadbeef->pl_insert_cue (after, it, totalsamples, samplerate);
if (cue_after) {
deadbeef->pl_item_unref (it);
deadbeef->pl_item_unref (cue_after);
return cue_after;
}
deadbeef->pl_add_meta (it, "title", NULL);
after = deadbeef->pl_insert_item (after, it);
deadbeef->pl_item_unref (it);
return after;
}
static const char * exts[] = { "wav", "aif", "aiff", "snd", "au", "paf", "svx", "nist", "voc", "ircam", "w64", "mat4", "mat5", "pvf", "xi", "htk", "sds", "avr", "wavex", "sd2", "caf", "wve", NULL };
static const char *filetypes[] = { "wav", NULL };
// define plugin interface
static DB_decoder_t plugin = {
DB_PLUGIN_SET_API_VERSION
.plugin.version_major = 0,
.plugin.version_minor = 1,
.plugin.type = DB_PLUGIN_DECODER,
.plugin.id = "sndfile",
.plugin.name = "pcm player",
.plugin.descr = "wav/aiff player using libsndfile",
.plugin.author = "Alexey Yakovenko",
.plugin.email = "waker@users.sourceforge.net",
.plugin.website = "http://deadbeef.sf.net",
.open = sndfile_open,
.init = sndfile_init,
.free = sndfile_free,
.read_int16 = sndfile_read_int16,
.read_float32 = sndfile_read_float32,
.seek = sndfile_seek,
.seek_sample = sndfile_seek_sample,
.insert = sndfile_insert,
.exts = exts,
.filetypes = filetypes
};
DB_plugin_t *
sndfile_load (DB_functions_t *api) {
deadbeef = api;
return DB_PLUGIN (&plugin);
}
|