44 #define WAV_BUF_SIZE 320
46 #define WAV_HEADER_SIZE 44
58 #if __BYTE_ORDER == __LITTLE_ENDIAN
64 #if __BYTE_ORDER == __BIG_ENDIAN
66 (((((b) ) & 0xFF) << 24) | \
67 ((( (b) >> 8) & 0xFF) << 16) | \
68 ((( (b) >> 16) & 0xFF) << 8) | \
69 ((( (b) >> 24) & 0xFF) ))
71 (((((b) ) & 0xFF) << 8) | \
72 ((( (b) >> 8) & 0xFF) ))
73 #define ltohl(b) htoll(b)
74 #define ltohs(b) htols(b)
76 #error "Endianess not defined"
81 static int check_header_fmt(FILE *f,
int hsize,
int hz)
83 unsigned short format,
chans, bysam, bisam;
84 unsigned int freq, bysec;
86 ast_log(LOG_WARNING,
"Unexpected header size %d\n", hsize);
89 if (fread(&format, 1, 2, f) != 2) {
90 ast_log(LOG_WARNING,
"Read failed (format)\n");
93 if (ltohs(format) != 1) {
94 ast_log(LOG_WARNING,
"Not a supported wav file format (%d). Only PCM encoded, 16 bit, mono, 8kHz/16kHz files are supported with a lowercase '.wav' extension.\n", ltohs(format));
97 if (fread(&chans, 1, 2, f) != 2) {
98 ast_log(LOG_WARNING,
"Read failed (format)\n");
101 if (ltohs(chans) != 1) {
102 ast_log(LOG_WARNING,
"Not in mono %d\n", ltohs(chans));
105 if (fread(&freq, 1, 4, f) != 4) {
106 ast_log(LOG_WARNING,
"Read failed (freq)\n");
110 if ((freq != 8000 && freq != 16000) || freq != hz) {
111 ast_log(LOG_WARNING,
"Unexpected frequency mismatch %d (expecting %d)\n", freq, hz);
115 if (fread(&bysec, 1, 4, f) != 4) {
116 ast_log(LOG_WARNING,
"Read failed (BYTES_PER_SECOND)\n");
120 if (fread(&bysam, 1, 2, f) != 2) {
121 ast_log(LOG_WARNING,
"Read failed (BYTES_PER_SAMPLE)\n");
124 if (ltohs(bysam) != 2) {
125 ast_log(LOG_WARNING,
"Can only handle 16bits per sample: %d\n", ltohs(bysam));
128 if (fread(&bisam, 1, 2, f) != 2) {
129 ast_log(LOG_WARNING,
"Read failed (Bits Per Sample): %d\n", ltohs(bisam));
133 if (fseek(f,hsize-16,SEEK_CUR) == -1 ) {
134 ast_log(LOG_WARNING,
"Failed to skip remaining header bytes: %d\n", hsize-16 );
140 static int check_header(FILE *f,
int hz)
142 int type, size, formtype;
144 if (fread(&type, 1, 4, f) != 4) {
145 ast_log(LOG_WARNING,
"Read failed (type)\n");
148 if (fread(&size, 1, 4, f) != 4) {
149 ast_log(LOG_WARNING,
"Read failed (size)\n");
152 #if __BYTE_ORDER == __BIG_ENDIAN
155 if (fread(&formtype, 1, 4, f) != 4) {
156 ast_log(LOG_WARNING,
"Read failed (formtype)\n");
159 if (memcmp(&type,
"RIFF", 4)) {
160 ast_log(LOG_WARNING,
"Does not begin with RIFF\n");
163 if (memcmp(&formtype,
"WAVE", 4)) {
164 ast_log(LOG_WARNING,
"Does not contain WAVE\n");
173 if (fread(&buf, 1, 4, f) != 4) {
174 ast_log(LOG_WARNING,
"Read failed (block header format)\n");
178 if (fread(&data, 1, 4, f) != 4) {
179 ast_log(LOG_WARNING,
"Read failed (block '%.4s' header length)\n", buf);
182 #if __BYTE_ORDER == __BIG_ENDIAN
185 if (memcmp(&buf,
"fmt ", 4) == 0) {
186 if (check_header_fmt(f, data, hz))
190 if(memcmp(buf,
"data", 4) == 0 )
192 ast_debug(1,
"Skipping unknown block '%.4s'\n", buf);
193 if (fseek(f,data,SEEK_CUR) == -1 ) {
194 ast_log(LOG_WARNING,
"Failed to skip '%.4s' block: %d\n", buf, data);
199 curpos = lseek(fd, 0, SEEK_CUR);
200 truelength = lseek(fd, 0, SEEK_END);
201 lseek(fd, curpos, SEEK_SET);
202 truelength -= curpos;
207 static int update_header(FILE *f)
210 int datalen,filelen,bytes;
213 fseek(f, 0, SEEK_END);
217 datalen = htoll(bytes);
219 filelen = htoll(36 + bytes);
222 ast_log(LOG_WARNING,
"Unable to find our position\n");
225 if (fseek(f, 4, SEEK_SET)) {
226 ast_log(LOG_WARNING,
"Unable to set our position\n");
229 if (fwrite(&filelen, 1, 4, f) != 4) {
230 ast_log(LOG_WARNING,
"Unable to set write file size\n");
233 if (fseek(f, 40, SEEK_SET)) {
234 ast_log(LOG_WARNING,
"Unable to set our position\n");
237 if (fwrite(&datalen, 1, 4, f) != 4) {
238 ast_log(LOG_WARNING,
"Unable to set write datalen\n");
241 if (fseeko(f, cur, SEEK_SET)) {
242 ast_log(LOG_WARNING,
"Unable to return to position\n");
248 static int write_header(FILE *f,
int writehz)
252 unsigned int hs = htoll(16);
253 unsigned short fmt = htols(1);
254 unsigned short chans = htols(1);
255 unsigned short bysam = htols(2);
256 unsigned short bisam = htols(16);
257 unsigned int size = htoll(0);
259 if (writehz == 16000) {
268 if (fwrite(
"RIFF", 1, 4, f) != 4) {
269 ast_log(LOG_WARNING,
"Unable to write header\n");
272 if (fwrite(&size, 1, 4, f) != 4) {
273 ast_log(LOG_WARNING,
"Unable to write header\n");
276 if (fwrite(
"WAVEfmt ", 1, 8, f) != 8) {
277 ast_log(LOG_WARNING,
"Unable to write header\n");
280 if (fwrite(&hs, 1, 4, f) != 4) {
281 ast_log(LOG_WARNING,
"Unable to write header\n");
284 if (fwrite(&fmt, 1, 2, f) != 2) {
285 ast_log(LOG_WARNING,
"Unable to write header\n");
288 if (fwrite(&chans, 1, 2, f) != 2) {
289 ast_log(LOG_WARNING,
"Unable to write header\n");
292 if (fwrite(&hz, 1, 4, f) != 4) {
293 ast_log(LOG_WARNING,
"Unable to write header\n");
296 if (fwrite(&bhz, 1, 4, f) != 4) {
297 ast_log(LOG_WARNING,
"Unable to write header\n");
300 if (fwrite(&bysam, 1, 2, f) != 2) {
301 ast_log(LOG_WARNING,
"Unable to write header\n");
304 if (fwrite(&bisam, 1, 2, f) != 2) {
305 ast_log(LOG_WARNING,
"Unable to write header\n");
308 if (fwrite(
"data", 1, 4, f) != 4) {
309 ast_log(LOG_WARNING,
"Unable to write header\n");
312 if (fwrite(&size, 1, 4, f) != 4) {
313 ast_log(LOG_WARNING,
"Unable to write header\n");
327 tmp->maxlen = check_header(s->f, sample_rate);
328 if (tmp->maxlen < 0) {
332 tmp->hz = sample_rate;
336 static int wav_rewrite(
struct ast_filestream *s,
const char *comment)
344 if (write_header(s->f,tmp->hz))
354 if (s->mode == O_RDONLY) {
363 if (fs->bytes & 0x1) {
364 if (fwrite(&zero, 1, 1, s->f) != 1) {
365 ast_log(LOG_WARNING,
"fwrite() failed: %s\n", strerror(errno));
374 #if __BYTE_ORDER == __BIG_ENDIAN
383 bytes = (fs->hz == 16000 ? (WAV_BUF_SIZE * 2) : WAV_BUF_SIZE);
386 if (fs->maxlen - here < bytes)
387 bytes = fs->maxlen - here;
396 ast_log(LOG_WARNING,
"Short read of %s data (expected %d bytes, read %zu): %s\n",
405 #if __BYTE_ORDER == __BIG_ENDIAN
406 tmp = (
short *)(s->
fr.
data.ptr);
408 for( x = 0; x < samples; x++)
409 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
418 #if __BYTE_ORDER == __BIG_ENDIAN
420 short tmp[16000], *tmpi;
428 #if __BYTE_ORDER == __BIG_ENDIAN
430 if (f->
datalen >
sizeof(tmp)) {
431 ast_log(LOG_WARNING,
"Data length is too long\n");
435 for (x=0; x < f->
datalen/2; x++)
436 tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8);
443 ast_log(LOG_WARNING,
"Bad write (%d): %s\n", res, strerror(errno));
453 static int wav_seek(
struct ast_filestream *fs, off_t sample_offset,
int whence)
455 off_t min = WAV_HEADER_SIZE, max, cur, offset = 0, samples;
457 samples = sample_offset * 2;
459 if ((cur = ftello(fs->f)) < 0) {
460 ast_log(AST_LOG_WARNING,
"Unable to determine current position in wav filestream %p: %s\n", fs, strerror(errno));
464 if (fseeko(fs->f, 0, SEEK_END) < 0) {
465 ast_log(AST_LOG_WARNING,
"Unable to seek to end of wav filestream %p: %s\n", fs, strerror(errno));
469 if ((max = ftello(fs->f)) < 0) {
470 ast_log(AST_LOG_WARNING,
"Unable to determine max position in wav filestream %p: %s\n", fs, strerror(errno));
474 if (whence == SEEK_SET) {
475 offset = samples + min;
476 }
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
477 offset = samples + cur;
478 }
else if (whence == SEEK_END) {
479 offset = max - samples;
481 if (whence != SEEK_FORCECUR) {
482 offset = (offset > max)?max:offset;
485 offset = (offset < min)?min:offset;
486 return fseeko(fs->f, offset, SEEK_SET);
494 if ((fd = fileno(fs->f)) < 0) {
495 ast_log(AST_LOG_WARNING,
"Unable to determine file descriptor for wav filestream %p: %s\n", fs, strerror(errno));
498 if ((cur = ftello(fs->f)) < 0) {
499 ast_log(AST_LOG_WARNING,
"Unable to determine current position in wav filestream %p: %s\n", fs, strerror(errno));
503 if (ftruncate(fd, cur)) {
506 return update_header(fs->f);
512 offset = ftello(fs->f);
514 return (offset - 44)/2;
520 .mime_types =
"audio/x-wav;codec=pcm;bit=16;rate=16000",
522 .rewrite = wav_rewrite,
536 .mime_types =
"audio/wav|audio/x-wav|audio/x-wav;codec=pcm;bit=16;rate=8000",
538 .rewrite = wav_rewrite,
546 .desc_size =
sizeof(
struct wav_desc),
549 static int unload_module(
void)
555 static int load_module(
void)
559 if (ast_format_def_register(&wav_f)
560 || ast_format_def_register(&wav16_f)) {
567 AST_MODULE_INFO(
ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER,
"Microsoft WAV/WAV16 format (8kHz/16kHz Signed Linear)",
568 .support_level = AST_MODULE_SUPPORT_CORE,
570 .unload = unload_module,
Asterisk main include file. File version handling, generic pbx functions.
struct ast_frame_subclass subclass
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
Asterisk architecture endianess compatibility definitions.
struct ast_format_def * fmt
struct ast_frame fr
frame produced by read, typically
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
union ast_frame::@224 data
Module has failed to load, may be in an inconsistent state.
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Data structure associated with a single frame of data.
struct ast_format * format
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.