format_wav.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Work with WAV in the proprietary Microsoft format.
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/channel.h>
  15. #include <asterisk/file.h>
  16. #include <asterisk/logger.h>
  17. #include <asterisk/sched.h>
  18. #include <asterisk/module.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <stdlib.h>
  22. #include <sys/time.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #ifdef __linux__
  28. #include <endian.h>
  29. #else
  30. #include <machine/endian.h>
  31. #endif
  32. /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
  33. /* Portions of the conversion code are by guido@sienanet.it */
  34. struct ast_filestream {
  35. void *reserved[AST_RESERVED_POINTERS];
  36. /* This is what a filestream means to us */
  37. int fd; /* Descriptor */
  38. int bytes;
  39. int needsgain;
  40. struct ast_frame fr; /* Frame information */
  41. char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
  42. char empty; /* Empty character */
  43. short buf[160];
  44. int foffset;
  45. int lasttimeout;
  46. int maxlen;
  47. struct timeval last;
  48. };
  49. AST_MUTEX_DEFINE_STATIC(wav_lock);
  50. static int glistcnt = 0;
  51. static char *name = "wav";
  52. static char *desc = "Microsoft WAV format (8000hz Signed Linear)";
  53. static char *exts = "wav";
  54. #define BLOCKSIZE 160
  55. #define GAIN 2 /* 2^GAIN is the multiple to increase the volume by */
  56. #if __BYTE_ORDER == __LITTLE_ENDIAN
  57. #define htoll(b) (b)
  58. #define htols(b) (b)
  59. #define ltohl(b) (b)
  60. #define ltohs(b) (b)
  61. #else
  62. #if __BYTE_ORDER == __BIG_ENDIAN
  63. #define htoll(b) \
  64. (((((b) ) & 0xFF) << 24) | \
  65. ((((b) >> 8) & 0xFF) << 16) | \
  66. ((((b) >> 16) & 0xFF) << 8) | \
  67. ((((b) >> 24) & 0xFF) ))
  68. #define htols(b) \
  69. (((((b) ) & 0xFF) << 8) | \
  70. ((((b) >> 8) & 0xFF) ))
  71. #define ltohl(b) htoll(b)
  72. #define ltohs(b) htols(b)
  73. #else
  74. #error "Endianess not defined"
  75. #endif
  76. #endif
  77. static int check_header(int fd)
  78. {
  79. int type, size, formtype;
  80. int fmt, hsize;
  81. short format, chans, bysam, bisam;
  82. int bysec;
  83. int freq;
  84. int data;
  85. if (read(fd, &type, 4) != 4) {
  86. ast_log(LOG_WARNING, "Read failed (type)\n");
  87. return -1;
  88. }
  89. if (read(fd, &size, 4) != 4) {
  90. ast_log(LOG_WARNING, "Read failed (size)\n");
  91. return -1;
  92. }
  93. size = ltohl(size);
  94. if (read(fd, &formtype, 4) != 4) {
  95. ast_log(LOG_WARNING, "Read failed (formtype)\n");
  96. return -1;
  97. }
  98. if (memcmp(&type, "RIFF", 4)) {
  99. ast_log(LOG_WARNING, "Does not begin with RIFF\n");
  100. return -1;
  101. }
  102. if (memcmp(&formtype, "WAVE", 4)) {
  103. ast_log(LOG_WARNING, "Does not contain WAVE\n");
  104. return -1;
  105. }
  106. if (read(fd, &fmt, 4) != 4) {
  107. ast_log(LOG_WARNING, "Read failed (fmt)\n");
  108. return -1;
  109. }
  110. if (memcmp(&fmt, "fmt ", 4)) {
  111. ast_log(LOG_WARNING, "Does not say fmt\n");
  112. return -1;
  113. }
  114. if (read(fd, &hsize, 4) != 4) {
  115. ast_log(LOG_WARNING, "Read failed (formtype)\n");
  116. return -1;
  117. }
  118. if (ltohl(hsize) < 16) {
  119. ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
  120. return -1;
  121. }
  122. if (read(fd, &format, 2) != 2) {
  123. ast_log(LOG_WARNING, "Read failed (format)\n");
  124. return -1;
  125. }
  126. if (ltohs(format) != 1) {
  127. ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format));
  128. return -1;
  129. }
  130. if (read(fd, &chans, 2) != 2) {
  131. ast_log(LOG_WARNING, "Read failed (format)\n");
  132. return -1;
  133. }
  134. if (ltohs(chans) != 1) {
  135. ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
  136. return -1;
  137. }
  138. if (read(fd, &freq, 4) != 4) {
  139. ast_log(LOG_WARNING, "Read failed (freq)\n");
  140. return -1;
  141. }
  142. if (ltohl(freq) != 8000) {
  143. ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
  144. return -1;
  145. }
  146. /* Ignore the byte frequency */
  147. if (read(fd, &bysec, 4) != 4) {
  148. ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n");
  149. return -1;
  150. }
  151. /* Check bytes per sample */
  152. if (read(fd, &bysam, 2) != 2) {
  153. ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n");
  154. return -1;
  155. }
  156. if (ltohs(bysam) != 2) {
  157. ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
  158. return -1;
  159. }
  160. if (read(fd, &bisam, 2) != 2) {
  161. ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam));
  162. return -1;
  163. }
  164. // Skip any additional header
  165. if ( lseek(fd,ltohl(hsize)-16,SEEK_CUR) == -1 ) {
  166. ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", ltohl(hsize)-16 );
  167. return -1;
  168. }
  169. // Skip any facts and get the first data block
  170. for(;;)
  171. {
  172. char buf[4];
  173. /* Begin data chunk */
  174. if (read(fd, &buf, 4) != 4) {
  175. ast_log(LOG_WARNING, "Read failed (data)\n");
  176. return -1;
  177. }
  178. /* Data has the actual length of data in it */
  179. if (read(fd, &data, 4) != 4) {
  180. ast_log(LOG_WARNING, "Read failed (data)\n");
  181. return -1;
  182. }
  183. data = ltohl(data);
  184. if( memcmp(buf, "data", 4) == 0 ) break;
  185. if( memcmp(buf, "fact", 4) != 0 ) {
  186. ast_log(LOG_WARNING, "Unknown block - not fact or data\n");
  187. return -1;
  188. }
  189. if ( lseek(fd,data,SEEK_CUR) == -1 ) {
  190. ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data );
  191. return -1;
  192. }
  193. }
  194. #if 0
  195. curpos = lseek(fd, 0, SEEK_CUR);
  196. truelength = lseek(fd, 0, SEEK_END);
  197. lseek(fd, curpos, SEEK_SET);
  198. truelength -= curpos;
  199. #endif
  200. return data;
  201. }
  202. static int update_header(int fd)
  203. {
  204. off_t cur,end;
  205. int datalen,filelen,bytes;
  206. cur = lseek(fd, 0, SEEK_CUR);
  207. end = lseek(fd, 0, SEEK_END);
  208. /* data starts 44 bytes in */
  209. bytes = end - 44;
  210. datalen = htoll(bytes);
  211. /* chunk size is bytes of data plus 36 bytes of header */
  212. filelen = htoll(36 + bytes);
  213. if (cur < 0) {
  214. ast_log(LOG_WARNING, "Unable to find our position\n");
  215. return -1;
  216. }
  217. if (lseek(fd, 4, SEEK_SET) != 4) {
  218. ast_log(LOG_WARNING, "Unable to set our position\n");
  219. return -1;
  220. }
  221. if (write(fd, &filelen, 4) != 4) {
  222. ast_log(LOG_WARNING, "Unable to set write file size\n");
  223. return -1;
  224. }
  225. if (lseek(fd, 40, SEEK_SET) != 40) {
  226. ast_log(LOG_WARNING, "Unable to set our position\n");
  227. return -1;
  228. }
  229. if (write(fd, &datalen, 4) != 4) {
  230. ast_log(LOG_WARNING, "Unable to set write datalen\n");
  231. return -1;
  232. }
  233. if (lseek(fd, cur, SEEK_SET) != cur) {
  234. ast_log(LOG_WARNING, "Unable to return to position\n");
  235. return -1;
  236. }
  237. return 0;
  238. }
  239. static int write_header(int fd)
  240. {
  241. unsigned int hz=htoll(8000);
  242. unsigned int bhz = htoll(16000);
  243. unsigned int hs = htoll(16);
  244. unsigned short fmt = htols(1);
  245. unsigned short chans = htols(1);
  246. unsigned short bysam = htols(2);
  247. unsigned short bisam = htols(16);
  248. unsigned int size = htoll(0);
  249. /* Write a wav header, ignoring sizes which will be filled in later */
  250. lseek(fd,0,SEEK_SET);
  251. if (write(fd, "RIFF", 4) != 4) {
  252. ast_log(LOG_WARNING, "Unable to write header\n");
  253. return -1;
  254. }
  255. if (write(fd, &size, 4) != 4) {
  256. ast_log(LOG_WARNING, "Unable to write header\n");
  257. return -1;
  258. }
  259. if (write(fd, "WAVEfmt ", 8) != 8) {
  260. ast_log(LOG_WARNING, "Unable to write header\n");
  261. return -1;
  262. }
  263. if (write(fd, &hs, 4) != 4) {
  264. ast_log(LOG_WARNING, "Unable to write header\n");
  265. return -1;
  266. }
  267. if (write(fd, &fmt, 2) != 2) {
  268. ast_log(LOG_WARNING, "Unable to write header\n");
  269. return -1;
  270. }
  271. if (write(fd, &chans, 2) != 2) {
  272. ast_log(LOG_WARNING, "Unable to write header\n");
  273. return -1;
  274. }
  275. if (write(fd, &hz, 4) != 4) {
  276. ast_log(LOG_WARNING, "Unable to write header\n");
  277. return -1;
  278. }
  279. if (write(fd, &bhz, 4) != 4) {
  280. ast_log(LOG_WARNING, "Unable to write header\n");
  281. return -1;
  282. }
  283. if (write(fd, &bysam, 2) != 2) {
  284. ast_log(LOG_WARNING, "Unable to write header\n");
  285. return -1;
  286. }
  287. if (write(fd, &bisam, 2) != 2) {
  288. ast_log(LOG_WARNING, "Unable to write header\n");
  289. return -1;
  290. }
  291. if (write(fd, "data", 4) != 4) {
  292. ast_log(LOG_WARNING, "Unable to write header\n");
  293. return -1;
  294. }
  295. if (write(fd, &size, 4) != 4) {
  296. ast_log(LOG_WARNING, "Unable to write header\n");
  297. return -1;
  298. }
  299. return 0;
  300. }
  301. static struct ast_filestream *wav_open(int fd)
  302. {
  303. /* We don't have any header to read or anything really, but
  304. if we did, it would go here. We also might want to check
  305. and be sure it's a valid file. */
  306. struct ast_filestream *tmp;
  307. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  308. memset(tmp, 0, sizeof(struct ast_filestream));
  309. if ((tmp->maxlen = check_header(fd)) < 0) {
  310. free(tmp);
  311. return NULL;
  312. }
  313. if (ast_mutex_lock(&wav_lock)) {
  314. ast_log(LOG_WARNING, "Unable to lock wav list\n");
  315. free(tmp);
  316. return NULL;
  317. }
  318. tmp->fd = fd;
  319. tmp->needsgain = 1;
  320. tmp->fr.data = tmp->buf;
  321. tmp->fr.frametype = AST_FRAME_VOICE;
  322. tmp->fr.subclass = AST_FORMAT_SLINEAR;
  323. /* datalen will vary for each frame */
  324. tmp->fr.src = name;
  325. tmp->fr.mallocd = 0;
  326. tmp->bytes = 0;
  327. glistcnt++;
  328. ast_mutex_unlock(&wav_lock);
  329. ast_update_use_count();
  330. }
  331. return tmp;
  332. }
  333. static struct ast_filestream *wav_rewrite(int fd, char *comment)
  334. {
  335. /* We don't have any header to read or anything really, but
  336. if we did, it would go here. We also might want to check
  337. and be sure it's a valid file. */
  338. struct ast_filestream *tmp;
  339. if ((tmp = malloc(sizeof(struct ast_filestream)))) {
  340. memset(tmp, 0, sizeof(struct ast_filestream));
  341. if (write_header(fd)) {
  342. free(tmp);
  343. return NULL;
  344. }
  345. if (ast_mutex_lock(&wav_lock)) {
  346. ast_log(LOG_WARNING, "Unable to lock wav list\n");
  347. free(tmp);
  348. return NULL;
  349. }
  350. tmp->fd = fd;
  351. glistcnt++;
  352. ast_mutex_unlock(&wav_lock);
  353. ast_update_use_count();
  354. } else
  355. ast_log(LOG_WARNING, "Out of memory\n");
  356. return tmp;
  357. }
  358. static void wav_close(struct ast_filestream *s)
  359. {
  360. char zero = 0;
  361. if (ast_mutex_lock(&wav_lock)) {
  362. ast_log(LOG_WARNING, "Unable to lock wav list\n");
  363. return;
  364. }
  365. glistcnt--;
  366. ast_mutex_unlock(&wav_lock);
  367. ast_update_use_count();
  368. /* Pad to even length */
  369. if (s->bytes & 0x1)
  370. write(s->fd, &zero, 1);
  371. close(s->fd);
  372. free(s);
  373. s = NULL;
  374. }
  375. static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
  376. {
  377. int res;
  378. int delay;
  379. int x;
  380. short tmp[sizeof(s->buf) / 2];
  381. int bytes = sizeof(tmp);
  382. off_t here;
  383. /* Send a frame from the file to the appropriate channel */
  384. here = lseek(s->fd, 0, SEEK_CUR);
  385. if ((s->maxlen - here) < bytes)
  386. bytes = s->maxlen - here;
  387. if (bytes < 0)
  388. bytes = 0;
  389. /* ast_log(LOG_DEBUG, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
  390. if ( (res = read(s->fd, tmp, bytes)) <= 0 ) {
  391. if (res) {
  392. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  393. }
  394. return NULL;
  395. }
  396. #if __BYTE_ORDER == __BIG_ENDIAN
  397. for( x = 0; x < sizeof(tmp)/2; x++) tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
  398. #endif
  399. if (s->needsgain) {
  400. for (x=0;x<sizeof(tmp)/2;x++)
  401. if (tmp[x] & ((1 << GAIN) - 1)) {
  402. /* If it has data down low, then it's not something we've artificially increased gain
  403. on, so we don't need to gain adjust it */
  404. s->needsgain = 0;
  405. }
  406. }
  407. if (s->needsgain) {
  408. for (x=0;x<sizeof(tmp)/2;x++) {
  409. s->buf[x] = tmp[x] >> GAIN;
  410. }
  411. } else {
  412. memcpy(s->buf, tmp, sizeof(s->buf));
  413. }
  414. delay = res / 2;
  415. s->fr.frametype = AST_FRAME_VOICE;
  416. s->fr.subclass = AST_FORMAT_SLINEAR;
  417. s->fr.offset = AST_FRIENDLY_OFFSET;
  418. s->fr.datalen = res;
  419. s->fr.data = s->buf;
  420. s->fr.mallocd = 0;
  421. s->fr.samples = delay;
  422. *whennext = delay;
  423. return &s->fr;
  424. }
  425. static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
  426. {
  427. int res = 0;
  428. int x;
  429. short tmp[8000], *tmpi;
  430. float tmpf;
  431. if (f->frametype != AST_FRAME_VOICE) {
  432. ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
  433. return -1;
  434. }
  435. if (f->subclass != AST_FORMAT_SLINEAR) {
  436. ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass);
  437. return -1;
  438. }
  439. if (f->datalen > sizeof(tmp)) {
  440. ast_log(LOG_WARNING, "Data length is too long\n");
  441. return -1;
  442. }
  443. if (!f->datalen)
  444. return -1;
  445. #if 0
  446. printf("Data Length: %d\n", f->datalen);
  447. #endif
  448. if (fs->buf) {
  449. tmpi = f->data;
  450. /* Volume adjust here to accomodate */
  451. for (x=0;x<f->datalen/2;x++) {
  452. tmpf = ((float)tmpi[x]) * ((float)(1 << GAIN));
  453. if (tmpf > 32767.0)
  454. tmpf = 32767.0;
  455. if (tmpf < -32768.0)
  456. tmpf = -32768.0;
  457. tmp[x] = tmpf;
  458. tmp[x] &= ~((1 << GAIN) - 1);
  459. #if __BYTE_ORDER == __BIG_ENDIAN
  460. tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
  461. #endif
  462. }
  463. if ((write (fs->fd, tmp, f->datalen) != f->datalen) ) {
  464. ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
  465. return -1;
  466. }
  467. } else {
  468. ast_log(LOG_WARNING, "Cannot write data to file.\n");
  469. return -1;
  470. }
  471. fs->bytes += f->datalen;
  472. update_header(fs->fd);
  473. return 0;
  474. }
  475. static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
  476. {
  477. off_t min,max,cur;
  478. long offset=0,samples;
  479. samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
  480. min = 44; /* wav header is 44 bytes */
  481. cur = lseek(fs->fd, 0, SEEK_CUR);
  482. max = lseek(fs->fd, 0, SEEK_END);
  483. if (whence == SEEK_SET)
  484. offset = samples + min;
  485. else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
  486. offset = samples + cur;
  487. else if (whence == SEEK_END)
  488. offset = max - samples;
  489. if (whence != SEEK_FORCECUR) {
  490. offset = (offset > max)?max:offset;
  491. }
  492. // always protect the header space.
  493. offset = (offset < min)?min:offset;
  494. return lseek(fs->fd,offset,SEEK_SET);
  495. }
  496. static int wav_trunc(struct ast_filestream *fs)
  497. {
  498. if(ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR)))
  499. return -1;
  500. return update_header(fs->fd);
  501. }
  502. static long wav_tell(struct ast_filestream *fs)
  503. {
  504. off_t offset;
  505. offset = lseek(fs->fd, 0, SEEK_CUR);
  506. /* subtract header size to get samples, then divide by 2 for 16 bit samples */
  507. return (offset - 44)/2;
  508. }
  509. static char *wav_getcomment(struct ast_filestream *s)
  510. {
  511. return NULL;
  512. }
  513. int load_module()
  514. {
  515. return ast_format_register(name, exts, AST_FORMAT_SLINEAR,
  516. wav_open,
  517. wav_rewrite,
  518. wav_write,
  519. wav_seek,
  520. wav_trunc,
  521. wav_tell,
  522. wav_read,
  523. wav_close,
  524. wav_getcomment);
  525. }
  526. int unload_module()
  527. {
  528. return ast_format_unregister(name);
  529. }
  530. int usecount()
  531. {
  532. int res;
  533. if (ast_mutex_lock(&wav_lock)) {
  534. ast_log(LOG_WARNING, "Unable to lock wav list\n");
  535. return -1;
  536. }
  537. res = glistcnt;
  538. ast_mutex_unlock(&wav_lock);
  539. return res;
  540. }
  541. char *description()
  542. {
  543. return desc;
  544. }
  545. char *key()
  546. {
  547. return ASTERISK_GPL_KEY;
  548. }