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