2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, Anthony Minessale and Digium, Inc.
5 * Anthony Minessale (anthmct@yahoo.com)
6 * Kevin P. Fleming <kpfleming@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 RAW SLINEAR 16 Format
22 * \arg File name extensions: sln16
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/mod_format.h"
31 #include "asterisk/module.h"
32 #include "asterisk/endian.h"
34 #define BUF_SIZE 640 /* 640 bytes, 320 samples */
35 #define SLIN_SAMPLES 320
37 static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext)
40 /* Send a frame from the file to the appropriate channel */
42 s->fr.frametype = AST_FRAME_VOICE;
43 s->fr.subclass = AST_FORMAT_SLINEAR16;
45 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
46 if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) {
48 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
51 *whennext = s->fr.samples = res/2;
56 static int slinear_write(struct ast_filestream *fs, struct ast_frame *f)
60 if (f->frametype != AST_FRAME_VOICE) {
61 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
64 if (f->subclass != AST_FORMAT_SLINEAR16) {
65 ast_log(LOG_WARNING, "Asked to write non-slinear16 frame (%d)!\n", f->subclass);
68 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
69 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
75 static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
77 off_t offset=0, min, cur, max;
82 fseeko(fs->f, 0, SEEK_END);
84 if (whence == SEEK_SET)
85 offset = sample_offset;
86 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
87 offset = sample_offset + cur;
88 else if (whence == SEEK_END)
89 offset = max - sample_offset;
90 if (whence != SEEK_FORCECUR) {
91 offset = (offset > max)?max:offset;
93 /* always protect against seeking past begining. */
94 offset = (offset < min)?min:offset;
96 return fseeko(fs->f, offset, SEEK_SET);
99 static int slinear_trunc(struct ast_filestream *fs)
101 return ftruncate(fileno(fs->f), ftello(fs->f));
104 static off_t slinear_tell(struct ast_filestream *fs)
106 return ftello(fs->f) / 2;
109 static const struct ast_format slin_f = {
112 .format = AST_FORMAT_SLINEAR16,
113 .write = slinear_write,
114 .seek = slinear_seek,
115 .trunc = slinear_trunc,
116 .tell = slinear_tell,
117 .read = slinear_read,
118 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
121 static int load_module(void)
123 if (ast_format_register(&slin_f))
124 return AST_MODULE_LOAD_FAILURE;
126 return AST_MODULE_LOAD_SUCCESS;
129 static int unload_module(void)
131 return ast_format_unregister(slin_f.name);
134 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw Signed Linear 16KHz Audio support (SLN16)");