Merge changes from team/group/appdocsxml
[asterisk/asterisk.git] / apps / app_dictate.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, Anthony Minessale II
5  *
6  * Anthony Minessale II <anthmct@yahoo.com>
7  *
8  * Donated by Sangoma Technologies <http://www.samgoma.com>
9  *
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.
15  *
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.
19  */
20
21 /*! \file
22  *
23  * \brief Virtual Dictation Machine Application For Asterisk
24  *
25  * \author Anthony Minessale II <anthmct@yahoo.com>
26  *
27  * \ingroup applications
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <sys/stat.h>
35
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"
42
43 /*** DOCUMENTATION
44         <application name="Dictate" language="en_US">
45                 <synopsis>
46                         Virtual Dictation Machine.
47                 </synopsis>
48                 <syntax>
49                         <parameter name="base_dir" />
50                         <parameter name="filename" />
51                 </syntax>
52                 <description>
53                         <para>Start dictation machine using optional <replaceable>base_dir</replaceable> for files.</para>
54                 </description>
55         </application>
56  ***/
57
58 static char *app = "Dictate";
59
60 typedef enum {
61         DFLAG_RECORD = (1 << 0),
62         DFLAG_PLAY = (1 << 1),
63         DFLAG_TRUNC = (1 << 2),
64         DFLAG_PAUSE = (1 << 3),
65 } dflags;
66
67 typedef enum {
68         DMODE_INIT,
69         DMODE_RECORD,
70         DMODE_PLAY
71 } dmodes;
72
73 #define ast_toggle_flag(it,flag) if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
74
75 static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
76 {
77         int res = -1;
78         if (!ast_streamfile(chan, file, chan->language)) {
79                 res = ast_waitstream(chan, digits);
80         }
81         return res;
82 }
83
84 static int dictate_exec(struct ast_channel *chan, void *data)
85 {
86         char *path = NULL, filein[256], *filename = "";
87         char *parse;
88         AST_DECLARE_APP_ARGS(args,
89                 AST_APP_ARG(base);
90                 AST_APP_ARG(filename);
91         );
92         char dftbase[256];
93         char *base;
94         struct ast_flags flags = {0};
95         struct ast_filestream *fs;
96         struct ast_frame *f = NULL;
97         int ffactor = 320 * 80,
98                 res = 0,
99                 done = 0,
100                 oldr = 0,
101                 lastop = 0,
102                 samples = 0,
103                 speed = 1,
104                 digit = 0,
105                 len = 0,
106                 maxlen = 0,
107                 mode = 0;
108
109         snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
110         if (!ast_strlen_zero(data)) {
111                 parse = ast_strdupa(data);
112                 AST_STANDARD_APP_ARGS(args, parse);
113         } else
114                 args.argc = 0;
115
116         if (args.argc && !ast_strlen_zero(args.base)) {
117                 base = args.base;
118         } else {
119                 base = dftbase;
120         }
121         if (args.argc > 1 && args.filename) {
122                 filename = args.filename;
123         }
124         oldr = chan->readformat;
125         if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
126                 ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
127                 return -1;
128         }
129
130         ast_answer(chan);
131         ast_safe_sleep(chan, 200);
132         for (res = 0; !res;) {
133                 if (ast_strlen_zero(filename)) {
134                         if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) ||
135                                 ast_strlen_zero(filein)) {
136                                 res = -1;
137                                 break;
138                         }
139                 } else {
140                         ast_copy_string(filein, filename, sizeof(filein));
141                         filename = "";
142                 }
143                 ast_mkdir(base, 0755);
144                 len = strlen(base) + strlen(filein) + 2;
145                 if (!path || len > maxlen) {
146                         path = alloca(len);
147                         memset(path, 0, len);
148                         maxlen = len;
149                 } else {
150                         memset(path, 0, maxlen);
151                 }
152
153                 snprintf(path, len, "%s/%s", base, filein);
154                 fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, AST_FILE_MODE);
155                 mode = DMODE_PLAY;
156                 memset(&flags, 0, sizeof(flags));
157                 ast_set_flag(&flags, DFLAG_PAUSE);
158                 digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
159                 done = 0;
160                 speed = 1;
161                 res = 0;
162                 lastop = 0;
163                 samples = 0;
164                 while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
165                         if (digit) {
166                                 struct ast_frame fr = {AST_FRAME_DTMF, digit};
167                                 ast_queue_frame(chan, &fr);
168                                 digit = 0;
169                         }
170                         if ((f->frametype == AST_FRAME_DTMF)) {
171                                 int got = 1;
172                                 switch(mode) {
173                                 case DMODE_PLAY:
174                                         switch(f->subclass) {
175                                         case '1':
176                                                 ast_set_flag(&flags, DFLAG_PAUSE);
177                                                 mode = DMODE_RECORD;
178                                                 break;
179                                         case '2':
180                                                 speed++;
181                                                 if (speed > 4) {
182                                                         speed = 1;
183                                                 }
184                                                 res = ast_say_number(chan, speed, AST_DIGIT_ANY, chan->language, NULL);
185                                                 break;
186                                         case '7':
187                                                 samples -= ffactor;
188                                                 if(samples < 0) {
189                                                         samples = 0;
190                                                 }
191                                                 ast_seekstream(fs, samples, SEEK_SET);
192                                                 break;
193                                         case '8':
194                                                 samples += ffactor;
195                                                 ast_seekstream(fs, samples, SEEK_SET);
196                                                 break;
197                                                 
198                                         default:
199                                                 got = 0;
200                                         }
201                                         break;
202                                 case DMODE_RECORD:
203                                         switch(f->subclass) {
204                                         case '1':
205                                                 ast_set_flag(&flags, DFLAG_PAUSE);
206                                                 mode = DMODE_PLAY;
207                                                 break;
208                                         case '8':
209                                                 ast_toggle_flag(&flags, DFLAG_TRUNC);
210                                                 lastop = 0;
211                                                 break;
212                                         default:
213                                                 got = 0;
214                                         }
215                                         break;
216                                 default:
217                                         got = 0;
218                                 }
219                                 if (!got) {
220                                         switch(f->subclass) {
221                                         case '#':
222                                                 done = 1;
223                                                 continue;
224                                                 break;
225                                         case '*':
226                                                 ast_toggle_flag(&flags, DFLAG_PAUSE);
227                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
228                                                         digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
229                                                 } else {
230                                                         digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
231                                                 }
232                                                 break;
233                                         case '0':
234                                                 ast_set_flag(&flags, DFLAG_PAUSE);
235                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
236                                                 switch(mode) {
237                                                 case DMODE_PLAY:
238                                                         digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
239                                                         break;
240                                                 case DMODE_RECORD:
241                                                         digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
242                                                         break;
243                                                 }
244                                                 if (digit == 0) {
245                                                         digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
246                                                 } else if (digit < 0) {
247                                                         done = 1;
248                                                         break;
249                                                 }
250                                                 break;
251                                         }
252                                 }
253                                 
254                         } else if (f->frametype == AST_FRAME_VOICE) {
255                                 switch(mode) {
256                                         struct ast_frame *fr;
257                                         int x;
258                                 case DMODE_PLAY:
259                                         if (lastop != DMODE_PLAY) {
260                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
261                                                         digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
262                                                         if (digit == 0) {
263                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
264                                                         } else if (digit < 0) {
265                                                                 break;
266                                                         }
267                                                 }
268                                                 if (lastop != DFLAG_PLAY) {
269                                                         lastop = DFLAG_PLAY;
270                                                         ast_closestream(fs);
271                                                         if (!(fs = ast_openstream(chan, path, chan->language)))
272                                                                 break;
273                                                         ast_seekstream(fs, samples, SEEK_SET);
274                                                         chan->stream = NULL;
275                                                 }
276                                                 lastop = DMODE_PLAY;
277                                         }
278
279                                         if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
280                                                 for (x = 0; x < speed; x++) {
281                                                         if ((fr = ast_readframe(fs))) {
282                                                                 ast_write(chan, fr);
283                                                                 samples += fr->samples;
284                                                                 ast_frfree(fr);
285                                                                 fr = NULL;
286                                                         } else {
287                                                                 samples = 0;
288                                                                 ast_seekstream(fs, 0, SEEK_SET);
289                                                         }
290                                                 }
291                                         }
292                                         break;
293                                 case DMODE_RECORD:
294                                         if (lastop != DMODE_RECORD) {
295                                                 int oflags = O_CREAT | O_WRONLY;
296                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
297                                                         digit = play_and_wait(chan, "dictate/record_mode", AST_DIGIT_ANY);
298                                                         if (digit == 0) {
299                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
300                                                         } else if (digit < 0) {
301                                                                 break;
302                                                         }
303                                                 }
304                                                 lastop = DMODE_RECORD;
305                                                 ast_closestream(fs);
306                                                 if ( ast_test_flag(&flags, DFLAG_TRUNC)) {
307                                                         oflags |= O_TRUNC;
308                                                         digit = play_and_wait(chan, "dictate/truncating_audio", AST_DIGIT_ANY);
309                                                 } else {
310                                                         oflags |= O_APPEND;
311                                                 }
312                                                 fs = ast_writefile(path, "raw", NULL, oflags, 0, AST_FILE_MODE);
313                                                 if (ast_test_flag(&flags, DFLAG_TRUNC)) {
314                                                         ast_seekstream(fs, 0, SEEK_SET);
315                                                         ast_clear_flag(&flags, DFLAG_TRUNC);
316                                                 } else {
317                                                         ast_seekstream(fs, 0, SEEK_END);
318                                                 }
319                                         }
320                                         if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
321                                                 res = ast_writestream(fs, f);
322                                         }
323                                         break;
324                                 }
325                                 
326                         }
327
328                         ast_frfree(f);
329                 }
330         }
331         if (oldr) {
332                 ast_set_read_format(chan, oldr);
333         }
334         return 0;
335 }
336
337 static int unload_module(void)
338 {
339         int res;
340         res = ast_unregister_application(app);
341         return res;
342 }
343
344 static int load_module(void)
345 {
346         return ast_register_application_xml(app, dictate_exec);
347 }
348
349 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Virtual Dictation Machine");