Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / apps / app_dictate.c
blob86ccad8756fc1d591ba68f36fa90e8c2cdbface9
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, Anthony Minessale II
6 * Anthony Minessale II <anthmct@yahoo.com>
8 * Donated by Sangoma Technologies <http://www.samgoma.com>
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
21 /*! \file
23 * \brief Virtual Dictation Machine Application For Asterisk
25 * \author Anthony Minessale II <anthmct@yahoo.com>
27 * \ingroup applications
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <sys/stat.h>
36 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
37 #include "asterisk/file.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/say.h"
41 #include "asterisk/app.h"
43 static char *app = "Dictate";
44 static char *synopsis = "Virtual Dictation Machine";
45 static char *desc = " Dictate([<base_dir>[,<filename>]])\n"
46 "Start dictation machine using optional base dir for files.\n";
49 typedef enum {
50 DFLAG_RECORD = (1 << 0),
51 DFLAG_PLAY = (1 << 1),
52 DFLAG_TRUNC = (1 << 2),
53 DFLAG_PAUSE = (1 << 3),
54 } dflags;
56 typedef enum {
57 DMODE_INIT,
58 DMODE_RECORD,
59 DMODE_PLAY
60 } dmodes;
62 #define ast_toggle_flag(it,flag) if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
64 static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
66 int res = -1;
67 if (!ast_streamfile(chan, file, chan->language)) {
68 res = ast_waitstream(chan, digits);
70 return res;
73 static int dictate_exec(struct ast_channel *chan, void *data)
75 char *path = NULL, filein[256], *filename = "";
76 char *parse;
77 AST_DECLARE_APP_ARGS(args,
78 AST_APP_ARG(base);
79 AST_APP_ARG(filename);
81 char dftbase[256];
82 char *base;
83 struct ast_flags flags = {0};
84 struct ast_filestream *fs;
85 struct ast_frame *f = NULL;
86 int ffactor = 320 * 80,
87 res = 0,
88 done = 0,
89 oldr = 0,
90 lastop = 0,
91 samples = 0,
92 speed = 1,
93 digit = 0,
94 len = 0,
95 maxlen = 0,
96 mode = 0;
98 snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
99 if (!ast_strlen_zero(data)) {
100 parse = ast_strdupa(data);
101 AST_STANDARD_APP_ARGS(args, parse);
102 } else
103 args.argc = 0;
105 if (args.argc && !ast_strlen_zero(args.base)) {
106 base = args.base;
107 } else {
108 base = dftbase;
110 if (args.argc > 1 && args.filename) {
111 filename = args.filename;
113 oldr = chan->readformat;
114 if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
115 ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
116 return -1;
119 ast_answer(chan);
120 ast_safe_sleep(chan, 200);
121 for (res = 0; !res;) {
122 if (ast_strlen_zero(filename)) {
123 if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) ||
124 ast_strlen_zero(filein)) {
125 res = -1;
126 break;
128 } else {
129 ast_copy_string(filein, filename, sizeof(filein));
130 filename = "";
132 ast_mkdir(base, 0755);
133 len = strlen(base) + strlen(filein) + 2;
134 if (!path || len > maxlen) {
135 path = alloca(len);
136 memset(path, 0, len);
137 maxlen = len;
138 } else {
139 memset(path, 0, maxlen);
142 snprintf(path, len, "%s/%s", base, filein);
143 fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, AST_FILE_MODE);
144 mode = DMODE_PLAY;
145 memset(&flags, 0, sizeof(flags));
146 ast_set_flag(&flags, DFLAG_PAUSE);
147 digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
148 done = 0;
149 speed = 1;
150 res = 0;
151 lastop = 0;
152 samples = 0;
153 while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
154 if (digit) {
155 struct ast_frame fr = {AST_FRAME_DTMF, digit};
156 ast_queue_frame(chan, &fr);
157 digit = 0;
159 if ((f->frametype == AST_FRAME_DTMF)) {
160 int got = 1;
161 switch(mode) {
162 case DMODE_PLAY:
163 switch(f->subclass) {
164 case '1':
165 ast_set_flag(&flags, DFLAG_PAUSE);
166 mode = DMODE_RECORD;
167 break;
168 case '2':
169 speed++;
170 if (speed > 4) {
171 speed = 1;
173 res = ast_say_number(chan, speed, AST_DIGIT_ANY, chan->language, NULL);
174 break;
175 case '7':
176 samples -= ffactor;
177 if(samples < 0) {
178 samples = 0;
180 ast_seekstream(fs, samples, SEEK_SET);
181 break;
182 case '8':
183 samples += ffactor;
184 ast_seekstream(fs, samples, SEEK_SET);
185 break;
187 default:
188 got = 0;
190 break;
191 case DMODE_RECORD:
192 switch(f->subclass) {
193 case '1':
194 ast_set_flag(&flags, DFLAG_PAUSE);
195 mode = DMODE_PLAY;
196 break;
197 case '8':
198 ast_toggle_flag(&flags, DFLAG_TRUNC);
199 lastop = 0;
200 break;
201 default:
202 got = 0;
204 break;
205 default:
206 got = 0;
208 if (!got) {
209 switch(f->subclass) {
210 case '#':
211 done = 1;
212 continue;
213 break;
214 case '*':
215 ast_toggle_flag(&flags, DFLAG_PAUSE);
216 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
217 digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
218 } else {
219 digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
221 break;
222 case '0':
223 ast_set_flag(&flags, DFLAG_PAUSE);
224 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
225 switch(mode) {
226 case DMODE_PLAY:
227 digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
228 break;
229 case DMODE_RECORD:
230 digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
231 break;
233 if (digit == 0) {
234 digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
235 } else if (digit < 0) {
236 done = 1;
237 break;
239 break;
243 } else if (f->frametype == AST_FRAME_VOICE) {
244 switch(mode) {
245 struct ast_frame *fr;
246 int x;
247 case DMODE_PLAY:
248 if (lastop != DMODE_PLAY) {
249 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
250 digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
251 if (digit == 0) {
252 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
253 } else if (digit < 0) {
254 break;
257 if (lastop != DFLAG_PLAY) {
258 lastop = DFLAG_PLAY;
259 ast_closestream(fs);
260 if (!(fs = ast_openstream(chan, path, chan->language)))
261 break;
262 ast_seekstream(fs, samples, SEEK_SET);
263 chan->stream = NULL;
265 lastop = DMODE_PLAY;
268 if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
269 for (x = 0; x < speed; x++) {
270 if ((fr = ast_readframe(fs))) {
271 ast_write(chan, fr);
272 samples += fr->samples;
273 ast_frfree(fr);
274 fr = NULL;
275 } else {
276 samples = 0;
277 ast_seekstream(fs, 0, SEEK_SET);
281 break;
282 case DMODE_RECORD:
283 if (lastop != DMODE_RECORD) {
284 int oflags = O_CREAT | O_WRONLY;
285 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
286 digit = play_and_wait(chan, "dictate/record_mode", AST_DIGIT_ANY);
287 if (digit == 0) {
288 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
289 } else if (digit < 0) {
290 break;
293 lastop = DMODE_RECORD;
294 ast_closestream(fs);
295 if ( ast_test_flag(&flags, DFLAG_TRUNC)) {
296 oflags |= O_TRUNC;
297 digit = play_and_wait(chan, "dictate/truncating_audio", AST_DIGIT_ANY);
298 } else {
299 oflags |= O_APPEND;
301 fs = ast_writefile(path, "raw", NULL, oflags, 0, AST_FILE_MODE);
302 if (ast_test_flag(&flags, DFLAG_TRUNC)) {
303 ast_seekstream(fs, 0, SEEK_SET);
304 ast_clear_flag(&flags, DFLAG_TRUNC);
305 } else {
306 ast_seekstream(fs, 0, SEEK_END);
309 if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
310 res = ast_writestream(fs, f);
312 break;
317 ast_frfree(f);
320 if (oldr) {
321 ast_set_read_format(chan, oldr);
323 return 0;
326 static int unload_module(void)
328 int res;
329 res = ast_unregister_application(app);
330 return res;
333 static int load_module(void)
335 return ast_register_application(app, dictate_exec, synopsis, desc);
338 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Virtual Dictation Machine");