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 371 372 373 374
|
/*
* audiofile_engine.cpp (C) 1999 by Michael Pruett <michael@68k.org>
*
* 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 <stdio.h>
#include <alloca.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <audiofile.h>
#include "input_plugin.h"
/*
Audio File Library frames have a different meaning than AlsaPlayer
frames. In AlsaPlayer a frame is an array of bytes that make of a
number of samples where Audio File Library sees a frame as a single
sample (or sample pair in case of stereo). Standardizing on one
definition of "frame" would be good of course. Comments?
*/
static const int FRAME_COUNT = 8192;
struct af_local_data
{
AFfilehandle filehandle;
int frameSize;
int sampleRate;
char filename[NAME_MAX];
};
/*
Take a path as input and return the last part of it as the file name.
*/
static char *getfilenamefrompath (const char *path)
{
char *p = strrchr(path, '/');
if (p != NULL)
p++;
else
p = (char *)path;
return p;
}
static void init_audiofile (void)
{
return;
}
static int audiofile_open (input_object *obj, const char *name)
{
static int audiofile_init_done = 0;
struct af_local_data *data;
#ifdef DEBUG
printf("audiofile_open: opening %s\n", name);
#endif
if (!obj)
return 0;
obj->local_data = malloc(sizeof(struct af_local_data));
if (!obj->local_data)
{
return 0;
}
data = (struct af_local_data *) obj->local_data;
data->filehandle = afOpenFile(name, "r", NULL);
strncpy(data->filename, getfilenamefrompath(name), NAME_MAX);
if (data->filehandle == AF_NULL_FILEHANDLE)
{
free(obj->local_data);
return 0;
}
if (audiofile_init_done == 0)
{
init_audiofile();
audiofile_init_done = 1;
}
data->frameSize = (int) afGetFrameSize(data->filehandle, AF_DEFAULT_TRACK, 1);
data->sampleRate = (int) afGetRate(data->filehandle, AF_DEFAULT_TRACK);
obj->nr_frames =
(afGetFrameCount(data->filehandle, AF_DEFAULT_TRACK) * data->frameSize +
FRAME_COUNT - 1) / FRAME_COUNT;
obj->nr_channels = afGetChannels(data->filehandle, AF_DEFAULT_TRACK);
obj->frame_size = FRAME_COUNT;
#ifdef DEBUG
printf("data.frameSize = %d\n", data->frameSize);
printf("data.sampleRate = %d\n", data->sampleRate);
printf("nr_frames = %d\n", obj->nr_frames);
printf("nr_channels = %d\n", obj->nr_channels);
printf("frame_size = %d\n", obj->frame_size);
#endif
obj->flags = P_SEEK;
return 1;
}
void audiofile_close (input_object *obj)
{
#ifdef DEBUG
printf("audiofile_close\n");
#endif
if (obj == NULL)
return;
if (obj->local_data)
{
struct af_local_data *data =
(struct af_local_data *) obj->local_data;
afCloseFile(data->filehandle);
free(obj->local_data);
obj->local_data = NULL;
}
}
static int audiofile_play_frame (input_object *obj, char *buf)
{
int bytes_to_read;
int frames_read;
void *buffer;
struct af_local_data *data;
if (!obj)
return 0;
data = (struct af_local_data *) obj->local_data;
if (!data)
return 0;
buffer = alloca(FRAME_COUNT * data->frameSize);
if (!buffer)
return 0;
bytes_to_read = FRAME_COUNT;
frames_read = afReadFrames(data->filehandle, AF_DEFAULT_TRACK, buffer,
bytes_to_read / data->frameSize);
if (buf)
memcpy(buf, buffer, FRAME_COUNT);
if (frames_read * data->frameSize < FRAME_COUNT)
return 0;
return 1;
}
static int audiofile_frame_seek (input_object *obj, int frame)
{
AFframecount currentFrame;
int result = 0;
struct af_local_data *data;
#ifdef DEBUG
printf("audiofile_frame_seek: %d\n", frame);
#endif
if (!obj)
return result;
data = (struct af_local_data *) obj->local_data;
if (data->filehandle == AF_NULL_FILEHANDLE)
{
return result;
}
currentFrame = afSeekFrame(data->filehandle, AF_DEFAULT_TRACK,
frame * FRAME_COUNT / data->frameSize);
result = (currentFrame >= 0);
#ifdef DEBUG
printf("audiofile_frame_seek: result %d\n", result);
#endif
return result;
}
static int audiofile_frame_size (input_object *obj)
{
return FRAME_COUNT;
}
static int audiofile_nr_frames (input_object *obj)
{
if (!obj)
return 0;
return obj->nr_frames;
}
static int audiofile_sample_rate (input_object *obj)
{
struct af_local_data *data;
if (!obj)
return 0;
data = (struct af_local_data *) obj->local_data;
return data->sampleRate;
}
static int audiofile_channels (input_object *obj)
{
if (!obj)
return 0;
return obj->nr_channels;
}
static long audiofile_frame_to_sec (input_object *obj, int frame)
{
long result = 0;
struct af_local_data *data;
#ifdef DEBUG
printf("audiofile_frame_to_sec: %d\n", frame);
#endif
if (!obj)
return result;
data = (struct af_local_data *) obj->local_data;
if (!data)
return result;
result = (frame * FRAME_COUNT /
(data->frameSize * data->sampleRate / 100));
#ifdef DEBUG
printf("audiofile_frame_to_sec: result %ld\n", result);
#endif
return result;
}
static float audiofile_can_handle (const char *name)
{
const char *fname = strrchr(name, '/');
if (!fname)
fname = name;
if (strstr(fname, ".wav") ||
strstr(fname, ".WAV") ||
strstr(fname, ".au") ||
strstr(fname, ".AU") ||
strstr(fname, ".aiff") ||
strstr(fname, ".AIFF")) {
/*
file = afOpenFile(name, "r", NULL);
if (file == AF_NULL_FILEHANDLE)
return 0.0;
afCloseFile(file);
*/
return 1.0;
}
return 0.0;
}
static int audiofile_stream_info (input_object *obj, stream_info *info)
{
int sampleWidth;
double sampleRate;
char *fileType;
struct af_local_data *data;
if (!obj || !info)
return 0;
data = (struct af_local_data *) obj->local_data;
if (data->filehandle == AF_NULL_FILEHANDLE)
{
return 0;
}
afGetSampleFormat(data->filehandle, AF_DEFAULT_TRACK, NULL, &sampleWidth);
sampleRate = afGetRate(data->filehandle, AF_DEFAULT_TRACK);
fileType = (char *) afQueryPointer(AF_QUERYTYPE_FILEFMT, AF_QUERY_NAME,
afGetFileFormat(data->filehandle, NULL), 0, 0);
sprintf(info->stream_type, "%d-bit %dkHz %s %s",
sampleWidth,
(int) (sampleRate / 1000),
obj->nr_channels == 2 ? "stereo" : "mono",
fileType);
strcpy(info->status, "");
strcpy(info->artist, "");
strcpy(info->title, data->filename);
return 1;
}
static int audiofile_init (void)
{
return 1;
}
static void audiofile_shutdown(void)
{
return;
}
input_plugin audiofile_plugin =
{
INPUT_PLUGIN_VERSION,
0,
"Audio File Library player v0.2.1",
"Michael Pruett",
NULL,
audiofile_init,
audiofile_shutdown,
NULL,
audiofile_can_handle,
audiofile_open,
audiofile_close,
audiofile_play_frame,
audiofile_frame_seek,
audiofile_frame_size,
audiofile_nr_frames,
audiofile_frame_to_sec,
audiofile_sample_rate,
audiofile_channels,
audiofile_stream_info,
NULL,
NULL
};
input_plugin *input_plugin_info (void)
{
return &audiofile_plugin;
}
|