Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
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 <since>
49 <version>1.2.0</version>
50 </since>
51 <synopsis>
52 Virtual Dictation Machine.
53 </synopsis>
54 <syntax>
55 <parameter name="base_dir" />
56 <parameter name="filename" />
57 </syntax>
58 <description>
59 <para>Start dictation machine using optional <replaceable>base_dir</replaceable> for files.</para>
60 </description>
61 </application>
62 ***/
63
64static const char app[] = "Dictate";
65
66typedef enum {
67 DFLAG_RECORD = (1 << 0),
68 DFLAG_PLAY = (1 << 1),
69 DFLAG_TRUNC = (1 << 2),
70 DFLAG_PAUSE = (1 << 3),
71} dflags;
72
73typedef enum {
78
79#define ast_toggle_flag(it,flag) if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
80
81static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
82{
83 int res = -1;
84 if (!ast_streamfile(chan, file, ast_channel_language(chan))) {
85 res = ast_waitstream(chan, digits);
86 }
87 return res;
88}
89
90static int dictate_exec(struct ast_channel *chan, const char *data)
91{
92 char *path = NULL, filein[256], *filename = "";
93 char *parse;
95 AST_APP_ARG(base);
96 AST_APP_ARG(filename);
97 );
98 char dftbase[256];
99 char *base;
100 struct ast_flags flags = {0};
101 struct ast_filestream *fs;
102 struct ast_frame *f = NULL;
103 int ffactor = 320 * 80,
104 res = 0,
105 done = 0,
106 lastop = 0,
107 samples = 0,
108 speed = 1,
109 digit = 0,
110 len = 0,
111 maxlen = 0,
112 mode = 0;
113 struct ast_format *oldr;
114
115 snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
116 if (!ast_strlen_zero(data)) {
117 parse = ast_strdupa(data);
119 } else
120 args.argc = 0;
121
122 if (args.argc && !ast_strlen_zero(args.base)) {
123 base = args.base;
124 } else {
125 base = dftbase;
126 }
127 if (args.argc > 1 && args.filename) {
128 filename = args.filename;
129 }
130 oldr = ao2_bump(ast_channel_readformat(chan));
131 if ((res = ast_set_read_format(chan, ast_format_slin)) < 0) {
132 ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
133 ao2_cleanup(oldr);
134 return -1;
135 }
136
137 if (ast_channel_state(chan) != AST_STATE_UP) {
138 ast_answer(chan);
139 }
140 ast_safe_sleep(chan, 200);
141 for (res = 0; !res;) {
142 if (ast_strlen_zero(filename)) {
143 if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) ||
144 ast_strlen_zero(filein)) {
145 res = -1;
146 break;
147 }
148 } else {
149 ast_copy_string(filein, filename, sizeof(filein));
150 filename = "";
151 }
152 ast_mkdir(base, 0755);
153 len = strlen(base) + strlen(filein) + 2;
154 if (!path || len > maxlen) {
155 ast_free(path);
156 path = ast_malloc(len);
157 memset(path, 0, len);
158 maxlen = len;
159 } else {
160 memset(path, 0, maxlen);
161 }
162
163 snprintf(path, len, "%s/%s", base, filein);
164 fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, AST_FILE_MODE);
165 mode = DMODE_PLAY;
166 memset(&flags, 0, sizeof(flags));
167 ast_set_flag(&flags, DFLAG_PAUSE);
168 digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
169 done = 0;
170 speed = 1;
171 res = 0;
172 lastop = 0;
173 samples = 0;
174 while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
175 if (digit) {
176 struct ast_frame fr = {AST_FRAME_DTMF, { .integer = digit } };
177 ast_queue_frame(chan, &fr);
178 digit = 0;
179 }
180 if (f->frametype == AST_FRAME_DTMF) {
181 int got = 1;
182 switch(mode) {
183 case DMODE_PLAY:
184 switch (f->subclass.integer) {
185 case '1':
187 mode = DMODE_RECORD;
188 break;
189 case '2':
190 speed++;
191 if (speed > 4) {
192 speed = 1;
193 }
194 res = ast_say_number(chan, speed, AST_DIGIT_ANY, ast_channel_language(chan), NULL);
195 break;
196 case '7':
197 samples -= ffactor;
198 if(samples < 0) {
199 samples = 0;
200 }
201 ast_seekstream(fs, samples, SEEK_SET);
202 break;
203 case '8':
204 samples += ffactor;
205 ast_seekstream(fs, samples, SEEK_SET);
206 break;
207
208 default:
209 got = 0;
210 }
211 break;
212 case DMODE_RECORD:
213 switch (f->subclass.integer) {
214 case '1':
216 mode = DMODE_PLAY;
217 break;
218 case '8':
220 lastop = 0;
221 break;
222 default:
223 got = 0;
224 }
225 break;
226 default:
227 got = 0;
228 }
229 if (!got) {
230 switch (f->subclass.integer) {
231 case '#':
232 done = 1;
233 continue;
234 break;
235 case '*':
238 digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
239 } else {
240 digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
241 }
242 break;
243 case '0':
245 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
246 switch(mode) {
247 case DMODE_PLAY:
248 digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
249 break;
250 case DMODE_RECORD:
251 digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
252 break;
253 }
254 if (digit == 0) {
255 digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
256 } else if (digit < 0) {
257 done = 1;
258 break;
259 }
260 break;
261 }
262 }
263
264 } else if (f->frametype == AST_FRAME_VOICE) {
265 switch(mode) {
266 struct ast_frame *fr;
267 int x;
268 case DMODE_PLAY:
269 if (lastop != DMODE_PLAY) {
271 digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
272 if (digit == 0) {
273 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
274 } else if (digit < 0) {
275 break;
276 }
277 }
278 if (lastop != DFLAG_PLAY) {
279 lastop = DFLAG_PLAY;
280 ast_closestream(fs);
281 if (!(fs = ast_openstream(chan, path, ast_channel_language(chan))))
282 break;
283 ast_seekstream(fs, samples, SEEK_SET);
285 }
286 lastop = DMODE_PLAY;
287 }
288
290 for (x = 0; x < speed; x++) {
291 if ((fr = ast_readframe(fs))) {
292 ast_write(chan, fr);
293 samples += fr->samples;
294 ast_frfree(fr);
295 fr = NULL;
296 } else {
297 samples = 0;
298 ast_seekstream(fs, 0, SEEK_SET);
299 }
300 }
301 }
302 break;
303 case DMODE_RECORD:
304 if (lastop != DMODE_RECORD) {
305 int oflags = O_CREAT | O_WRONLY;
307 digit = play_and_wait(chan, "dictate/record_mode", AST_DIGIT_ANY);
308 if (digit == 0) {
309 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
310 } else if (digit < 0) {
311 break;
312 }
313 }
314 lastop = DMODE_RECORD;
315 ast_closestream(fs);
317 oflags |= O_TRUNC;
318 digit = play_and_wait(chan, "dictate/truncating_audio", AST_DIGIT_ANY);
319 } else {
320 oflags |= O_APPEND;
321 }
322 fs = ast_writefile(path, "raw", NULL, oflags, 0, AST_FILE_MODE);
324 ast_seekstream(fs, 0, SEEK_SET);
326 } else {
327 ast_seekstream(fs, 0, SEEK_END);
328 }
329 }
331 res = ast_writestream(fs, f);
332 }
333 break;
334 }
335
336 }
337
338 ast_frfree(f);
339 }
340 }
341 ast_free(path);
342 if (oldr) {
343 ast_set_read_format(chan, oldr);
344 ao2_ref(oldr, -1);
345 }
346 return 0;
347}
348
349static int unload_module(void)
350{
351 int res;
353 return res;
354}
355
356static int load_module(void)
357{
359}
360
char digit
dmodes
Definition: app_dictate.c:73
@ DMODE_PLAY
Definition: app_dictate.c:76
@ DMODE_RECORD
Definition: app_dictate.c:75
@ DMODE_INIT
Definition: app_dictate.c:74
static const char app[]
Definition: app_dictate.c:64
static int dictate_exec(struct ast_channel *chan, const char *data)
Definition: app_dictate.c:90
dflags
Definition: app_dictate.c:66
@ DFLAG_RECORD
Definition: app_dictate.c:67
@ DFLAG_PAUSE
Definition: app_dictate.c:70
@ DFLAG_TRUNC
Definition: app_dictate.c:69
@ DFLAG_PLAY
Definition: app_dictate.c:68
static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
Definition: app_dictate.c:81
#define ast_toggle_flag(it, flag)
Definition: app_dictate.c:79
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Virtual Dictation Machine")
static int load_module(void)
Definition: app_dictate.c:356
static int unload_module(void)
Definition: app_dictate.c:349
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:3190
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:5161
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4274
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5779
const char * ast_channel_language(const struct ast_channel *chan)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2834
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1601
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:845
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:944
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:1083
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1301
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:1431
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1119
#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:1848
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:8261
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