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