2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \brief Flat, binary, ADPCM vox file format.
22 * \arg File name extensions: vox
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
40 #include "asterisk/lock.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/sched.h"
45 #include "asterisk/module.h"
46 #include "asterisk/endian.h"
48 #define BUF_SIZE 80 /* 80 bytes, 160 samples */
49 #define VOX_SAMPLES 160
51 static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
55 /* Send a frame from the file to the appropriate channel */
56 s->fr.frametype = AST_FRAME_VOICE;
57 s->fr.subclass = AST_FORMAT_ADPCM;
59 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
60 if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) {
62 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
65 *whennext = s->fr.samples = res * 2;
70 static int vox_write(struct ast_filestream *s, struct ast_frame *f)
73 if (f->frametype != AST_FRAME_VOICE) {
74 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
77 if (f->subclass != AST_FORMAT_ADPCM) {
78 ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
81 if ((res = fwrite(f->data, 1, f->datalen, s->f)) != f->datalen) {
82 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
88 static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
90 off_t offset=0,min,cur,max,distance;
94 fseeko(fs->f, 0, SEEK_END);
97 /* have to fudge to frame here, so not fully to sample */
98 distance = sample_offset/2;
99 if(whence == SEEK_SET)
101 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
102 offset = distance + cur;
103 else if(whence == SEEK_END)
104 offset = max - distance;
105 if (whence != SEEK_FORCECUR) {
106 offset = (offset > max)?max:offset;
107 offset = (offset < min)?min:offset;
109 return fseeko(fs->f, offset, SEEK_SET);
112 static int vox_trunc(struct ast_filestream *fs)
114 return ftruncate(fileno(fs->f), ftello(fs->f));
117 static off_t vox_tell(struct ast_filestream *fs)
120 offset = ftello(fs->f) << 1;
124 static const struct ast_format vox_f = {
127 .format = AST_FORMAT_ADPCM,
133 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
136 static int load_module(void)
138 return ast_format_register(&vox_f);
141 static int unload_module(void)
143 return ast_format_unregister(vox_f.name);
146 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialogic VOX (ADPCM) File Format");