Asterisk - The Open Source Telephony Project GIT-master-8f1982c
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
Macros | Enumerations | Functions | Variables
app_dictate.c File Reference

Virtual Dictation Machine Application For Asterisk. More...

#include "asterisk.h"
#include <sys/stat.h>
#include "asterisk/paths.h"
#include "asterisk/file.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/say.h"
#include "asterisk/app.h"
#include "asterisk/format_cache.h"
Include dependency graph for app_dictate.c:

Go to the source code of this file.

Macros

#define ast_toggle_flag(it, flag)   if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
 

Enumerations

enum  dflags { DFLAG_RECORD = (1 << 0) , DFLAG_PLAY = (1 << 1) , DFLAG_TRUNC = (1 << 2) , DFLAG_PAUSE = (1 << 3) }
 
enum  dmodes { DMODE_INIT , DMODE_RECORD , DMODE_PLAY }
 

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY, "Virtual Dictation Machine")
 
static int dictate_exec (struct ast_channel *chan, const char *data)
 
static int load_module (void)
 
static int play_and_wait (struct ast_channel *chan, char *file, char *digits)
 
static int unload_module (void)
 

Variables

static const char app [] = "Dictate"
 

Detailed Description

Virtual Dictation Machine Application For Asterisk.

Author
Anthony Minessale II anthm.nosp@m.ct@y.nosp@m.ahoo..nosp@m.com

Definition in file app_dictate.c.

Macro Definition Documentation

◆ ast_toggle_flag

#define ast_toggle_flag (   it,
  flag 
)    if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)

Definition at line 79 of file app_dictate.c.

Enumeration Type Documentation

◆ dflags

enum dflags
Enumerator
DFLAG_RECORD 
DFLAG_PLAY 
DFLAG_TRUNC 
DFLAG_PAUSE 

Definition at line 66 of file app_dictate.c.

66 {
67 DFLAG_RECORD = (1 << 0),
68 DFLAG_PLAY = (1 << 1),
69 DFLAG_TRUNC = (1 << 2),
70 DFLAG_PAUSE = (1 << 3),
71} dflags;
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

◆ dmodes

enum dmodes
Enumerator
DMODE_INIT 
DMODE_RECORD 
DMODE_PLAY 

Definition at line 73 of file app_dictate.c.

73 {
77} dmodes;
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

Function Documentation

◆ AST_MODULE_INFO_STANDARD_EXTENDED()

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Virtual Dictation Machine"   
)

◆ dictate_exec()

static int dictate_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 90 of file app_dictate.c.

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}
char digit
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
#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:3130
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:1169
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:5103
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4214
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5721
const char * ast_channel_language(const struct ast_channel *chan)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2774
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1541
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
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
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
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)
#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
const char * ast_config_AST_SPOOL_DIR
Definition: options.c:155
#define NULL
Definition: resample.c:96
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:8240
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
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

References ao2_bump, ao2_cleanup, ao2_ref, args, ast_answer(), AST_APP_ARG, ast_app_getdata(), ast_channel_language(), ast_channel_readformat(), ast_channel_stream_set(), ast_clear_flag, ast_closestream(), ast_config_AST_SPOOL_DIR, ast_copy_string(), AST_DECLARE_APP_ARGS, AST_DIGIT_ANY, AST_FILE_MODE, ast_format_slin, AST_FRAME_DTMF, AST_FRAME_VOICE, ast_free, ast_frfree, ast_log, ast_malloc, ast_mkdir(), ast_openstream(), ast_queue_frame(), ast_read(), ast_readframe(), ast_safe_sleep(), ast_say_number(), ast_seekstream(), ast_set_flag, ast_set_read_format(), AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_strdupa, ast_strlen_zero(), ast_test_flag, ast_toggle_flag, ast_waitfor(), ast_write(), ast_writefile(), ast_writestream(), DFLAG_PAUSE, DFLAG_PLAY, DFLAG_TRUNC, digit, DMODE_PLAY, DMODE_RECORD, done, ast_frame::flags, ast_flags::flags, ast_frame::frametype, ast_frame_subclass::integer, len(), LOG_WARNING, NULL, play_and_wait(), ast_frame::samples, and ast_frame::subclass.

Referenced by load_module().

◆ load_module()

static int load_module ( void  )
static

Definition at line 356 of file app_dictate.c.

357{
359}
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
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640

References app, ast_register_application_xml, and dictate_exec().

◆ play_and_wait()

static int play_and_wait ( struct ast_channel chan,
char *  file,
char *  digits 
)
static

Definition at line 81 of file app_dictate.c.

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}
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1301
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

References ast_channel_language(), ast_streamfile(), ast_waitstream(), and make_ari_stubs::file.

Referenced by dictate_exec().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 349 of file app_dictate.c.

350{
351 int res;
353 return res;
354}
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392

References app, and ast_unregister_application().

Variable Documentation

◆ app

const char app[] = "Dictate"
static

Definition at line 64 of file app_dictate.c.

Referenced by load_module(), and unload_module().