format_vox.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Flat, binary, ADPCM vox file format.
  21. * \arg File name extensions: vox
  22. *
  23. * \ingroup formats
  24. */
  25. /*** MODULEINFO
  26. <support_level>extended</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/mod_format.h"
  31. #include "asterisk/module.h"
  32. #include "asterisk/endian.h"
  33. #include "asterisk/format_cache.h"
  34. #define BUF_SIZE 80 /* 80 bytes, 160 samples */
  35. #define VOX_SAMPLES 160
  36. static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
  37. {
  38. int res;
  39. /* Send a frame from the file to the appropriate channel */
  40. AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
  41. if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
  42. if (res)
  43. ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
  44. return NULL;
  45. }
  46. *whennext = s->fr.samples = res * 2;
  47. s->fr.datalen = res;
  48. return &s->fr;
  49. }
  50. static int vox_write(struct ast_filestream *s, struct ast_frame *f)
  51. {
  52. int res;
  53. if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
  54. ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
  55. return -1;
  56. }
  57. return 0;
  58. }
  59. static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
  60. {
  61. off_t offset = 0, min = 0, cur, max, distance;
  62. if ((cur = ftello(fs->f)) < 0) {
  63. ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
  64. return -1;
  65. }
  66. if (fseeko(fs->f, 0, SEEK_END) < 0) {
  67. ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
  68. return -1;
  69. }
  70. if ((max = ftello(fs->f)) < 0) {
  71. ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
  72. return -1;
  73. }
  74. /* have to fudge to frame here, so not fully to sample */
  75. distance = sample_offset/2;
  76. if (whence == SEEK_SET) {
  77. offset = distance;
  78. } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
  79. offset = distance + cur;
  80. } else if (whence == SEEK_END) {
  81. offset = max - distance;
  82. }
  83. if (whence != SEEK_FORCECUR) {
  84. offset = (offset > max)?max:offset;
  85. offset = (offset < min)?min:offset;
  86. }
  87. return fseeko(fs->f, offset, SEEK_SET);
  88. }
  89. static int vox_trunc(struct ast_filestream *fs)
  90. {
  91. int fd;
  92. off_t cur;
  93. if ((fd = fileno(fs->f)) < 0) {
  94. ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for vox filestream %p: %s\n", fs, strerror(errno));
  95. return -1;
  96. }
  97. if ((cur = ftello(fs->f)) < 0) {
  98. ast_log(AST_LOG_WARNING, "Unable to determine current position in vox filestream %p: %s\n", fs, strerror(errno));
  99. return -1;
  100. }
  101. /* Truncate file to current length */
  102. return ftruncate(fd, cur);}
  103. static off_t vox_tell(struct ast_filestream *fs)
  104. {
  105. off_t offset;
  106. offset = ftello(fs->f) << 1;
  107. return offset;
  108. }
  109. static struct ast_format_def vox_f = {
  110. .name = "vox",
  111. .exts = "vox",
  112. .write = vox_write,
  113. .seek = vox_seek,
  114. .trunc = vox_trunc,
  115. .tell = vox_tell,
  116. .read = vox_read,
  117. .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
  118. };
  119. static int load_module(void)
  120. {
  121. vox_f.format = ast_format_adpcm;
  122. if (ast_format_def_register(&vox_f))
  123. return AST_MODULE_LOAD_FAILURE;
  124. return AST_MODULE_LOAD_SUCCESS;
  125. }
  126. static int unload_module(void)
  127. {
  128. return ast_format_def_unregister(vox_f.name);
  129. }
  130. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Dialogic VOX (ADPCM) File Format",
  131. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  132. .load = load_module,
  133. .unload = unload_module,
  134. .load_pri = AST_MODPRI_APP_DEPEND
  135. );