format_wav.c 14 KB

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