35 #include "asterisk/mod_format.h"
43 #define WAV_BUF_SIZE 320
45 #define WAV_HEADER_SIZE 44
57 #if __BYTE_ORDER == __LITTLE_ENDIAN
63 #if __BYTE_ORDER == __BIG_ENDIAN
65 (((((b) ) & 0xFF) << 24) | \
66 ((((b) >> 8) & 0xFF) << 16) | \
67 ((((b) >> 16) & 0xFF) << 8) | \
68 ((((b) >> 24) & 0xFF) ))
70 (((((b) ) & 0xFF) << 8) | \
71 ((((b) >> 8) & 0xFF) ))
72 #define ltohl(b) htoll(b)
73 #define ltohs(b) htols(b)
75 #error "Endianess not defined"
82 short format, chans, bysam, bisam;
89 if (fread(&format, 1, 2, f) != 2) {
93 if (ltohs(format) != 1) {
94 ast_log(
LOG_WARNING,
"Not a supported wav file format (%d). Only PCM encoded, 16 bit, mono, 8kHz files are supported with a lowercase '.wav' extension.\n", ltohs(format));
97 if (fread(&chans, 1, 2, f) != 2) {
101 if (ltohs(chans) != 1) {
105 if (fread(&freq, 1, 4, f) != 4) {
109 if (((ltohl(freq) != 8000) && (ltohl(freq) != 16000)) ||
110 ((ltohl(freq) == 8000) && (hz != 8000)) ||
111 ((ltohl(freq) == 16000) && (hz != 16000))) {
112 ast_log(
LOG_WARNING,
"Unexpected frequency mismatch %d (expecting %d)\n", ltohl(freq),hz);
116 if (fread(&bysec, 1, 4, f) != 4) {
121 if (fread(&bysam, 1, 2, f) != 2) {
125 if (ltohs(bysam) != 2) {
129 if (fread(&bisam, 1, 2, f) != 2) {
134 if (fseek(f,hsize-16,SEEK_CUR) == -1 ) {
143 int type, size, formtype;
145 if (fread(&type, 1, 4, f) != 4) {
149 if (fread(&size, 1, 4, f) != 4) {
154 if (fread(&formtype, 1, 4, f) != 4) {
158 if (memcmp(&type,
"RIFF", 4)) {
162 if (memcmp(&formtype,
"WAVE", 4)) {
172 if (fread(&buf, 1, 4, f) != 4) {
177 if (fread(&data, 1, 4, f) != 4) {
182 if (memcmp(&buf,
"fmt ", 4) == 0) {
187 if(memcmp(buf,
"data", 4) == 0 )
190 if (fseek(f,data,SEEK_CUR) == -1 ) {
196 curpos = lseek(fd, 0, SEEK_CUR);
197 truelength = lseek(fd, 0, SEEK_END);
198 lseek(fd, curpos, SEEK_SET);
199 truelength -= curpos;
207 int datalen,filelen,bytes;
210 fseek(f, 0, SEEK_END);
214 datalen = htoll(bytes);
216 filelen = htoll(36 + bytes);
222 if (fseek(f, 4, SEEK_SET)) {
226 if (fwrite(&filelen, 1, 4, f) != 4) {
230 if (fseek(f, 40, SEEK_SET)) {
234 if (fwrite(&datalen, 1, 4, f) != 4) {
238 if (fseeko(f, cur, SEEK_SET)) {
249 unsigned int hs = htoll(16);
250 unsigned short fmt = htols(1);
251 unsigned short chans = htols(1);
252 unsigned short bysam = htols(2);
253 unsigned short bisam = htols(16);
254 unsigned int size = htoll(0);
256 if (writehz == 16000) {
265 if (fwrite(
"RIFF", 1, 4, f) != 4) {
269 if (fwrite(&size, 1, 4, f) != 4) {
273 if (fwrite(
"WAVEfmt ", 1, 8, f) != 8) {
277 if (fwrite(&hs, 1, 4, f) != 4) {
281 if (fwrite(&fmt, 1, 2, f) != 2) {
285 if (fwrite(&chans, 1, 2, f) != 2) {
289 if (fwrite(&hz, 1, 4, f) != 4) {
293 if (fwrite(&bhz, 1, 4, f) != 4) {
297 if (fwrite(&bysam, 1, 2, f) != 2) {
301 if (fwrite(&bisam, 1, 2, f) != 2) {
305 if (fwrite(
"data", 1, 4, f) != 4) {
309 if (fwrite(&size, 1, 4, f) != 4) {
345 if (s->
mode == O_RDONLY) {
354 if (fs->
bytes & 0x1) {
355 if (!fwrite(&zero, 1, 1, s->
f)) {
365 #if __BYTE_ORDER == __BIG_ENDIAN
377 if (fs->
maxlen - here < bytes)
378 bytes = fs->
maxlen - here;
395 #if __BYTE_ORDER == __BIG_ENDIAN
398 for( x = 0; x < samples; x++)
399 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
408 #if __BYTE_ORDER == __BIG_ENDIAN
410 short tmp[16000], *tmpi;
430 #if __BYTE_ORDER == __BIG_ENDIAN
432 if (f->
datalen >
sizeof(tmp)) {
437 for (x=0; x < f->
datalen/2; x++)
438 tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8);
459 samples = sample_offset * 2;
461 if ((cur = ftello(fs->
f)) < 0) {
466 if (fseeko(fs->
f, 0, SEEK_END) < 0) {
471 if ((max = ftello(fs->
f)) < 0) {
476 if (whence == SEEK_SET)
477 offset = samples + min;
479 offset = samples + cur;
480 else if (whence == SEEK_END)
481 offset = max - samples;
483 offset = (offset > max)?max:offset;
486 offset = (offset < min)?min:offset;
487 return fseeko(fs->
f, offset, SEEK_SET);
495 if ((fd = fileno(fs->
f)) < 0) {
499 if ((cur = ftello(fs->
f)) < 0) {
504 if (ftruncate(fd, cur)) {
513 offset = ftello(fs->
f);
515 return (offset - 44)/2;
547 .desc_size =
sizeof(
struct wav_desc),
union ast_frame_subclass subclass
Asterisk main include file. File version handling, generic pbx functions.
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
Asterisk architecture endianess compatibility definitions.
#define AST_FORMAT_SLINEAR16
char * ast_getformatname(format_t format)
Get the name of a format.
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
if(yyss+yystacksize-1<=yyssp)
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
#define AST_FORMAT_SLINEAR
Data structure associated with a single frame of data.
enum ast_frame_type frametype
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
static snd_pcm_format_t format
union ast_frame::@172 data
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.