Asterisk - The Open Source Telephony Project  18.5.0
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 76 of file app_dictate.c.

Referenced by dictate_exec().

Enumeration Type Documentation

◆ dflags

enum dflags
Enumerator
DFLAG_RECORD 
DFLAG_PLAY 
DFLAG_TRUNC 
DFLAG_PAUSE 

Definition at line 63 of file app_dictate.c.

63  {
64  DFLAG_RECORD = (1 << 0),
65  DFLAG_PLAY = (1 << 1),
66  DFLAG_TRUNC = (1 << 2),
67  DFLAG_PAUSE = (1 << 3),
68 } dflags;
dflags
Definition: app_dictate.c:63

◆ dmodes

enum dmodes
Enumerator
DMODE_INIT 
DMODE_RECORD 
DMODE_PLAY 

Definition at line 70 of file app_dictate.c.

Function Documentation

◆ AST_MODULE_INFO_STANDARD_EXTENDED()

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Virtual Dictation Machine"   
)

Referenced by load_module().

◆ dictate_exec()

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

Definition at line 87 of file app_dictate.c.

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::frametype, ast_frame_subclass::integer, len(), LOG_WARNING, NULL, parse(), play_and_wait(), ast_frame::samples, and ast_frame::subclass.

Referenced by load_module().

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);
115  AST_STANDARD_APP_ARGS(args, parse);
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':
183  ast_set_flag(&flags, DFLAG_PAUSE);
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':
212  ast_set_flag(&flags, DFLAG_PAUSE);
213  mode = DMODE_PLAY;
214  break;
215  case '8':
216  ast_toggle_flag(&flags, DFLAG_TRUNC);
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 '*':
233  ast_toggle_flag(&flags, DFLAG_PAUSE);
234  if (ast_test_flag(&flags, DFLAG_PAUSE)) {
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':
241  ast_set_flag(&flags, DFLAG_PAUSE);
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) {
267  if (ast_test_flag(&flags, DFLAG_PAUSE)) {
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 
286  if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
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;
303  if (ast_test_flag(&flags, DFLAG_PAUSE)) {
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);
313  if ( ast_test_flag(&flags, DFLAG_TRUNC)) {
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);
320  if (ast_test_flag(&flags, DFLAG_TRUNC)) {
321  ast_seekstream(fs, 0, SEEK_SET);
322  ast_clear_flag(&flags, DFLAG_TRUNC);
323  } else {
324  ast_seekstream(fs, 0, SEEK_END);
325  }
326  }
327  if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
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 }
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1574
char digit
#define AST_DIGIT_ANY
Definition: file.h:48
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define ast_toggle_flag(it, flag)
Definition: app_dictate.c:76
#define ast_set_flag(p, flag)
Definition: utils.h:70
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
unsigned int flags
Definition: utils.h:200
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4302
ast_channel_state
ast_channel states
Definition: channelstate.h:35
Definition of a media format.
Definition: format.c:43
const char * args
#define NULL
Definition: resample.c:96
#define AST_FRAME_DTMF
#define AST_FILE_MODE
Definition: asterisk.h:32
struct ast_frame_subclass subclass
#define ast_strlen_zero(foo)
Definition: strings.h:52
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
int done
Definition: test_amihooks.c:48
#define ao2_bump(obj)
Definition: astobj2.h:491
#define ast_log
Definition: astobj2.c:42
int ast_set_read_format(struct ast_channel *chan, struct ast_format *format)
Sets read format on channel chan.
Definition: channel.c:5849
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to a channel&#39;s frame queue.
Definition: channel.c:1139
static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
Definition: app_dictate.c:78
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:899
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
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:1361
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1038
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define ast_free(a)
Definition: astmm.h:182
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1068
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:5189
const char * ast_config_AST_SPOOL_DIR
Definition: options.c:154
int 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:197
Structure used to handle boolean flags.
Definition: utils.h:199
#define ast_clear_flag(p, flag)
Definition: utils.h:77
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
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3171
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
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:8337
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:209
#define ast_frfree(fr)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2814
Data structure associated with a single frame of data.
const char * ast_channel_language(const struct ast_channel *chan)
enum ast_frame_type frametype
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
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:755
#define AST_APP_ARG(name)
Define an application argument.
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
Definition: main/utils.c:2231

◆ load_module()

static int load_module ( void  )
static

Definition at line 353 of file app_dictate.c.

References app, AST_MODULE_INFO_STANDARD_EXTENDED(), ast_register_application_xml, ASTERISK_GPL_KEY, and dictate_exec().

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

◆ play_and_wait()

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

Definition at line 78 of file app_dictate.c.

References ast_channel_language(), ast_streamfile(), and ast_waitstream().

Referenced by dictate_exec().

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 }
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1250
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1776
const char * ast_channel_language(const struct ast_channel *chan)

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 346 of file app_dictate.c.

References app, and ast_unregister_application().

347 {
348  int res;
350  return res;
351 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static const char app[]
Definition: app_dictate.c:61

Variable Documentation

◆ app

const char app[] = "Dictate"
static

Definition at line 61 of file app_dictate.c.

Referenced by load_module(), and unload_module().