Asterisk - The Open Source Telephony Project GIT-master-f36a736
app_dictate.c
Go to the documentation of this file.
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.sangoma.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#include <sys/stat.h>
37
38#include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
39#include "asterisk/file.h"
40#include "asterisk/pbx.h"
41#include "asterisk/module.h"
42#include "asterisk/say.h"
43#include "asterisk/app.h"
45
46/*** DOCUMENTATION
47 <application name="Dictate" language="en_US">
48 <synopsis>
49 Virtual Dictation Machine.
50 </synopsis>
51 <syntax>
52 <parameter name="base_dir" />
53 <parameter name="filename" />
54 </syntax>
55 <description>
56 <para>Start dictation machine using optional <replaceable>base_dir</replaceable> for files.</para>
57 </description>
58 </application>
59 ***/
60
61static const char app[] = "Dictate";
62
63typedef enum {
64 DFLAG_RECORD = (1 << 0),
65 DFLAG_PLAY = (1 << 1),
66 DFLAG_TRUNC = (1 << 2),
67 DFLAG_PAUSE = (1 << 3),
68} dflags;
69
70typedef enum {
75
76#define ast_toggle_flag(it,flag) if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
77
78static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
79{
80 int res = -1;
81 if (!ast_streamfile(chan, file, ast_channel_language(chan))) {
82 res = ast_waitstream(chan, digits);
83 }
84 return res;
85}
86
87static int dictate_exec(struct ast_channel *chan, const char *data)
88{
89 char *path = NULL, filein[256], *filename = "";
90 char *parse;
92 AST_APP_ARG(base);
93 AST_APP_ARG(filename);
94 );
95 char dftbase[256];
96 char *base;
97 struct ast_flags flags = {0};
98 struct ast_filestream *fs;
99 struct ast_frame *f = NULL;
100 int ffactor = 320 * 80,
101 res = 0,
102 done = 0,
103 lastop = 0,
104 samples = 0,
105 speed = 1,
106 digit = 0,
107 len = 0,
108 maxlen = 0,
109 mode = 0;
110 struct ast_format *oldr;
111
112 snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
113 if (!ast_strlen_zero(data)) {
114 parse = ast_strdupa(data);
116 } else
117 args.argc = 0;
118
119 if (args.argc && !ast_strlen_zero(args.base)) {
120 base = args.base;
121 } else {
122 base = dftbase;
123 }
124 if (args.argc > 1 && args.filename) {
125 filename = args.filename;
126 }
127 oldr = ao2_bump(ast_channel_readformat(chan));
128 if ((res = ast_set_read_format(chan, ast_format_slin)) < 0) {
129 ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
130 ao2_cleanup(oldr);
131 return -1;
132 }
133
134 if (ast_channel_state(chan) != AST_STATE_UP) {
135 ast_answer(chan);
136 }
137 ast_safe_sleep(chan, 200);
138 for (res = 0; !res;) {
139 if (ast_strlen_zero(filename)) {
140 if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) ||
141 ast_strlen_zero(filein)) {
142 res = -1;
143 break;
144 }
145 } else {
146 ast_copy_string(filein, filename, sizeof(filein));
147 filename = "";
148 }
149 ast_mkdir(base, 0755);
150 len = strlen(base) + strlen(filein) + 2;
151 if (!path || len > maxlen) {
152 ast_free(path);
153 path = ast_malloc(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':
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':
213 mode = DMODE_PLAY;
214 break;
215 case '8':
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 '*':
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':
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) {
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);
282 }
283 lastop = DMODE_PLAY;
284 }
285
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;
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);
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);
321 ast_seekstream(fs, 0, SEEK_SET);
323 } else {
324 ast_seekstream(fs, 0, SEEK_END);
325 }
326 }
328 res = ast_writestream(fs, f);
329 }
330 break;
331 }
332
333 }
334
335 ast_frfree(f);
336 }
337 }
338 ast_free(path);
339 if (oldr) {
340 ast_set_read_format(chan, oldr);
341 ao2_ref(oldr, -1);
342 }
343 return 0;
344}
345
346static int unload_module(void)
347{
348 int res;
350 return res;
351}
352
353static int load_module(void)
354{
356}
357
char digit
dmodes
Definition: app_dictate.c:70
@ DMODE_PLAY
Definition: app_dictate.c:73
@ DMODE_RECORD
Definition: app_dictate.c:72
@ DMODE_INIT
Definition: app_dictate.c:71
static const char app[]
Definition: app_dictate.c:61
static int dictate_exec(struct ast_channel *chan, const char *data)
Definition: app_dictate.c:87
dflags
Definition: app_dictate.c:63
@ DFLAG_RECORD
Definition: app_dictate.c:64
@ DFLAG_PAUSE
Definition: app_dictate.c:67
@ DFLAG_TRUNC
Definition: app_dictate.c:66
@ DFLAG_PLAY
Definition: app_dictate.c:65
static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
Definition: app_dictate.c:78
#define ast_toggle_flag(it, flag)
Definition: app_dictate.c:76
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Virtual Dictation Machine")
static int load_module(void)
Definition: app_dictate.c:353
static int unload_module(void)
Definition: app_dictate.c:346
Asterisk main include file. File version handling, generic pbx functions.
#define AST_FILE_MODE
Definition: asterisk.h:32
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
#define ast_log
Definition: astobj2.c:42
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3181
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to a channel's frame queue.
Definition: channel.c:1158
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:5163
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4276
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5781
const char * ast_channel_language(const struct ast_channel *chan)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2824
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1593
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_UP
Definition: channelstate.h:42
Generic File Format Support. Should be included by clients of the file handling routines....
struct ast_filestream * ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
Opens stream for use in seeking, playing.
Definition: file.c:790
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:936
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:244
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1075
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1293
struct ast_filestream * ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
Starts writing a file.
Definition: file.c:1423
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1111
#define AST_DIGIT_ANY
Definition: file.h:48
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1840
Media Format Cache API.
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
enum ast_getdata_result ast_app_getdata(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout)
Plays a stream and gets DTMF data from a channel.
Definition: main/app.c:188
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define AST_FRAME_DTMF
#define ast_frfree(fr)
@ AST_FRAME_VOICE
#define LOG_WARNING
Asterisk module definitions.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
Asterisk file paths, configured in asterisk.conf.
const char * ast_config_AST_SPOOL_DIR
Definition: options.c:154
Core PBX routines and definitions.
#define NULL
Definition: resample.c:96
Say numbers and dates (maybe words one day too)
int ast_say_number(struct ast_channel *chan, int num, const char *ints, const char *lang, const char *options)
says a number
Definition: channel.c:8257
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Main Channel structure associated with a channel.
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:101
Structure used to handle boolean flags.
Definition: utils.h:199
unsigned int flags
Definition: utils.h:200
Definition of a media format.
Definition: format.c:43
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
enum ast_frame_type frametype
unsigned int flags
int done
Definition: test_amihooks.c:48
const char * args
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define ast_clear_flag(p, flag)
Definition: utils.h:77
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
Definition: utils.c:2479
#define ast_set_flag(p, flag)
Definition: utils.h:70