| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include "asterisk.h" |
| 35 |
|
| 36 |
#include <sys/stat.h> |
| 37 |
|
| 38 |
#include "asterisk/paths.h" |
| 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" |
| 44 |
#include "asterisk/format_cache.h" |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static const char app[] = "Dictate"; |
| 65 |
|
| 66 |
typedef enum { |
| 67 |
DFLAG_RECORD = (1 << 0), |
| 68 |
DFLAG_PLAY = (1 << 1), |
| 69 |
DFLAG_TRUNC = (1 << 2), |
| 70 |
DFLAG_PAUSE = (1 << 3), |
| 71 |
} dflags; |
| 72 |
|
| 73 |
typedef enum { |
| 74 |
DMODE_INIT, |
| 75 |
DMODE_RECORD, |
| 76 |
DMODE_PLAY |
| 77 |
} dmodes; |
| 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 |
|
| 81 |
static 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 |
|
| 90 |
static int dictate_exec(struct ast_channel *chan, const char *data) |
| 91 |
{ |
| 92 |
char *path = NULL, filein[256], *filename = ""; |
| 93 |
char *parse; |
| 94 |
AST_DECLARE_APP_ARGS(args, |
| 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); |
| 118 |
AST_STANDARD_APP_ARGS(args, parse); |
| 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': |
| 186 |
ast_set_flag(&flags, DFLAG_PAUSE); |
| 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': |
| 215 |
ast_set_flag(&flags, DFLAG_PAUSE); |
| 216 |
mode = DMODE_PLAY; |
| 217 |
break; |
| 218 |
case '8': |
| 219 |
ast_toggle_flag(&flags, DFLAG_TRUNC); |
| 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 '*': |
| 236 |
ast_toggle_flag(&flags, DFLAG_PAUSE); |
| 237 |
if (ast_test_flag(&flags, DFLAG_PAUSE)) { |
| 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': |
| 244 |
ast_set_flag(&flags, DFLAG_PAUSE); |
| 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) { |
| 270 |
if (ast_test_flag(&flags, DFLAG_PAUSE)) { |
| 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); |
| 284 |
ast_channel_stream_set(chan, NULL); |
| 285 |
} |
| 286 |
lastop = DMODE_PLAY; |
| 287 |
} |
| 288 |
|
| 289 |
if (!ast_test_flag(&flags, DFLAG_PAUSE)) { |
| 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; |
| 306 |
if (ast_test_flag(&flags, DFLAG_PAUSE)) { |
| 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); |
| 316 |
if ( ast_test_flag(&flags, DFLAG_TRUNC)) { |
| 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); |
| 323 |
if (ast_test_flag(&flags, DFLAG_TRUNC)) { |
| 324 |
ast_seekstream(fs, 0, SEEK_SET); |
| 325 |
ast_clear_flag(&flags, DFLAG_TRUNC); |
| 326 |
} else { |
| 327 |
ast_seekstream(fs, 0, SEEK_END); |
| 328 |
} |
| 329 |
} |
| 330 |
if (!ast_test_flag(&flags, DFLAG_PAUSE)) { |
| 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 |
|
| 349 |
static int unload_module(void) |
| 350 |
{ |
| 351 |
int res; |
| 352 |
res = ast_unregister_application(app); |
| 353 |
return res; |
| 354 |
} |
| 355 |
|
| 356 |
static int load_module(void) |
| 357 |
{ |
| 358 |
return ast_register_application_xml(app, dictate_exec); |
| 359 |
} |
| 360 |
|
| 361 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Virtual Dictation Machine"); |