add a file-format driver for 16KHz signed linear... which may or may not work
[asterisk/asterisk.git] / formats / format_sln16.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Anthony Minessale and Digium, Inc.
5  * Anthony Minessale (anthmct@yahoo.com)
6  * Kevin P. Fleming <kpfleming@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
19 /*! \file
20  *
21  * \brief RAW SLINEAR 16 Format
22  * \arg File name extensions: sln16
23  * \ingroup formats
24  */
25  
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/mod_format.h"
31 #include "asterisk/module.h"
32 #include "asterisk/endian.h"
33
34 #define BUF_SIZE        640             /* 640 bytes, 320 samples */
35 #define SLIN_SAMPLES    320
36
37 static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext)
38 {
39         int res;
40         /* Send a frame from the file to the appropriate channel */
41
42         s->fr.frametype = AST_FRAME_VOICE;
43         s->fr.subclass = AST_FORMAT_SLINEAR16;
44         s->fr.mallocd = 0;
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) {
47                 if (res)
48                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
49                 return NULL;
50         }
51         *whennext = s->fr.samples = res/2;
52         s->fr.datalen = res;
53         return &s->fr;
54 }
55
56 static int slinear_write(struct ast_filestream *fs, struct ast_frame *f)
57 {
58         int res;
59
60         if (f->frametype != AST_FRAME_VOICE) {
61                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
62                 return -1;
63         }
64         if (f->subclass != AST_FORMAT_SLINEAR16) {
65                 ast_log(LOG_WARNING, "Asked to write non-slinear16 frame (%d)!\n", f->subclass);
66                 return -1;
67         }
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));
70                         return -1;
71         }
72         return 0;
73 }
74
75 static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
76 {
77         off_t offset=0, min, cur, max;
78
79         min = 0;
80         sample_offset <<= 1;
81         cur = ftello(fs->f);
82         fseeko(fs->f, 0, SEEK_END);
83         max = ftello(fs->f);
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;
92         }
93         /* always protect against seeking past begining. */
94         offset = (offset < min)?min:offset;
95
96         return fseeko(fs->f, offset, SEEK_SET);
97 }
98
99 static int slinear_trunc(struct ast_filestream *fs)
100 {
101         return ftruncate(fileno(fs->f), ftello(fs->f));
102 }
103
104 static off_t slinear_tell(struct ast_filestream *fs)
105 {
106         return ftello(fs->f) / 2;
107 }
108
109 static const struct ast_format slin_f = {
110         .name = "sln16",
111         .exts = "sln16",
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,
119 };
120
121 static int load_module(void)
122 {
123         if (ast_format_register(&slin_f))
124                 return AST_MODULE_LOAD_FAILURE;
125
126         return AST_MODULE_LOAD_SUCCESS;
127 }
128
129 static int unload_module(void)
130 {
131         return ast_format_unregister(slin_f.name);
132 }       
133
134 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw Signed Linear 16KHz Audio support (SLN16)");