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