12 #include <sys/types.h>
30 #define LIBDAX_AUDIOXTR_H_INTERNAL 1
33 #define LIBDAX_AUDIOXTR_H_PUBLIC 1
40 struct libdax_audioxtr *o;
42 o= *xtr= (
struct libdax_audioxtr *) calloc(1,
sizeof(
struct libdax_audioxtr));
48 strcpy(o->fmt,
"unidentified");
55 o->bits_per_sample= 0;
58 o->wav_data_location= 44;
59 o->wav_subchunk2_size= 0;
61 o->au_data_location= 0;
62 o->au_data_size= 0xffffffff;
66 {ret= -2*(ret<0);
goto failure;}
77 struct libdax_audioxtr *o;
82 if(o->fd>=0 && strcmp(o->path,
"-")!=0)
95 if(strcmp(o->path,
"-")==0)
98 o->fd= open(o->path, O_RDONLY |
O_BINARY);
100 sprintf(msg,
"Cannot open audio source file : %s",o->path);
108 sprintf(msg,
"Audio source file has unsuitable format : %s",o->path);
116 sprintf(msg,
"Failed to prepare reading of audio data : %s",o->path);
133 static char buf[256];
135 if((flag & 1) || o->fd == 0) {
136 while(pos - *old_pos > 0) {
137 to_read= pos - *old_pos;
138 if(to_read >
sizeof(buf))
139 to_read=
sizeof(buf);
140 ret= read(o->fd, buf, to_read);
141 if(ret < (
int) to_read)
146 ret= lseek(o->fd, pos, SEEK_SET);
156 int ret, fmt_seen= 0, data_seen= 0;
157 off_t pos= 0, old_pos= 0, riff_end= 0;
166 ubuf= (
unsigned char *) buf;
173 ret= read(o->fd, buf, 8);
178 if(pos > 0xffffffff || pos - old_pos < 4)
180 if(strncmp(buf,
"RIFF", 4) == 0)
183 if(strncmp(buf,
"INFO", 4) == 0 ||
184 strncmp(buf,
"CSET", 4) == 0 ||
185 strncmp(buf,
"JUNK", 4) == 0 ||
186 strncmp(buf,
"PAD ", 4) == 0)
192 ret= read(o->fd, buf, 4);
196 if(strncmp(buf,
"WAVE", 4) != 0)
202 while(old_pos < riff_end) {
206 ret= read(o->fd, buf, 8);
212 if(strncmp(buf,
"fmt ", 4) == 0) {
213 if(pos - old_pos < 16)
215 ret= read(o->fd, buf, 16);
219 if(buf[0]!=1 || buf[1]!=0)
226 ".wav , num_channels=%d , sample_rate=%d , bits_per_sample=%d",
227 o->num_channels, o->sample_rate, o->bits_per_sample);
230 }
else if(strncmp(buf,
"data", 4) == 0) {
231 o->wav_data_location= old_pos;
232 o->wav_subchunk2_size= pos - old_pos;
233 o->data_size= o->wav_subchunk2_size;
236 if(fmt_seen && data_seen) {
237 strcpy(o->fmt,
".wav");
254 ret= lseek(o->fd,0,SEEK_SET);
258 ret= read(o->fd, buf, 24);
262 if(strncmp(buf,
".snd",4)!=0)
264 strcpy(o->fmt,
".au");
270 o->bits_per_sample= 8;
272 o->bits_per_sample= 16;
274 o->bits_per_sample= 24;
276 o->bits_per_sample= 32;
278 o->bits_per_sample= -encoding;
281 if(o->au_data_size!=0xffffffff)
282 o->data_size= o->au_data_size;
286 ".au , num_channels=%d , sample_rate=%d , bits_per_sample=%d",
287 o->num_channels,o->sample_rate,o->bits_per_sample);
288 return(o->bits_per_sample>0);
310 unsigned char *bytes,
int len,
int flag)
316 for(i= 0; i<len; i++)
317 ret= ret*256+bytes[i];
319 for(i= len-1; i>=0; i--)
320 ret= ret*256+bytes[i];
335 if(strcmp(o->fmt,
".wav")==0)
336 ret= lseek(o->fd, o->wav_data_location, SEEK_SET);
337 else if(strcmp(o->fmt,
".au")==0)
338 ret= lseek(o->fd,o->au_data_location,SEEK_SET);
349 char **fmt,
char **fmt_info,
350 int *num_channels,
int *sample_rate,
int *bits_per_sample,
351 int *msb_first,
int flag)
354 *fmt_info= o->fmt_info;
355 *num_channels= o->num_channels;
356 *sample_rate= o->sample_rate;
357 *bits_per_sample= o->bits_per_sample;
358 *msb_first= o->msb_first;
371 char buffer[],
int buffer_size,
int flag)
375 if(buffer_size<=0 || o->fd<0)
377 if(o->data_size>0 && !(flag&1))
378 if(buffer_size > o->data_size - o->extract_count)
379 buffer_size= o->data_size - o->extract_count;
382 ret= read(o->fd,
buffer,buffer_size);
384 o->extract_count+= ret;
393 if(strcmp(o->fmt,
".wav")!=0 && strcmp(o->fmt,
".au")!=0)
399 if(*fd>=0 && strcmp(o->path,
"-")!=0)
#define LIBDAX_AUDIOXTR_STRLEN
static unsigned libdax_audioxtr_to_int(struct libdax_audioxtr *o, unsigned char *bytes, int len, int flag)
static int libdax_audioxtr_skip(struct libdax_audioxtr *o, off_t *old_pos, off_t pos, int flag)
static int libdax_audioxtr_identify_au(struct libdax_audioxtr *o, int flag)
int libdax_audioxtr_get_size(struct libdax_audioxtr *o, off_t *size, int flag)
static int libdax_audioxtr_identify_wav(struct libdax_audioxtr *o, int flag)
int libdax_audioxtr_destroy(struct libdax_audioxtr **xtr, int flag)
int libdax_audioxtr_read(struct libdax_audioxtr *o, char buffer[], int buffer_size, int flag)
struct libdax_msgs * libdax_messenger
int libdax_audioxtr_new(struct libdax_audioxtr **xtr, char *path, int flag)
static int libdax_audioxtr_open(struct libdax_audioxtr *o, int flag)
static int libdax_audioxtr_identify(struct libdax_audioxtr *o, int flag)
int libdax_audioxtr_get_id(struct libdax_audioxtr *o, char **fmt, char **fmt_info, int *num_channels, int *sample_rate, int *bits_per_sample, int *msb_first, int flag)
int libdax_audioxtr_detach_fd(struct libdax_audioxtr *o, int *fd, int flag)
static int libdax_audioxtr_init_reading(struct libdax_audioxtr *o, int flag)
int libdax_msgs_submit(struct libdax_msgs *m, int origin, int error_code, int severity, int priority, char *msg_text, int os_errno, int flag)
#define LIBDAX_MSGS_PRIO_HIGH
#define LIBDAX_MSGS_SEV_SORRY