FreeSWITCH API Documentation  1.7.0
Macros | Functions
tone2wav.c File Reference
#include <switch.h>
+ Include dependency graph for tone2wav.c:

Go to the source code of this file.

Macros

#define _XOPEN_SOURCE   600
 
#define fail()   r = 255; goto end
 

Functions

static int teletone_handler (teletone_generation_session_t *ts, teletone_tone_map_t *map)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

#define _XOPEN_SOURCE   600

Definition at line 37 of file tone2wav.c.

#define fail ( )    r = 255; goto end

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 73 of file tone2wav.c.

References teletone_generation_session::channels, teletone_generation_session::debug, teletone_generation_session::debug_stream, teletone_generation_session::duration, fail, switch_directories::mod_dir, teletone_generation_session::rate, SCF_MINIMAL, switch_core_destroy(), switch_core_file_close(), switch_core_file_open, switch_core_init(), SWITCH_FALSE, SWITCH_FILE_DATA_SHORT, SWITCH_FILE_FLAG_WRITE, SWITCH_GLOBAL_dirs, switch_loadable_module_init(), switch_loadable_module_load_module(), SWITCH_STATUS_SUCCESS, SWITCH_TRUE, teletone_destroy_session(), teletone_handler(), teletone_init_session(), teletone_run(), and teletone_generation_session::wait.

74 {
77  int r = 0;
78  int rate = 8000;
79  char *file = NULL, *script = NULL;
80  switch_file_handle_t fh = { 0 };
81  const char *err = NULL;
82  switch_bool_t verbose = SWITCH_FALSE;
83  int i = 0, c = 1, help = 0;
84 
85  for(i = 1; i < argc; i++) {
86  if (!strstr(argv[i], "-")) break;
87 
88  if (!strcmp(argv[i], "-v")) {
89  verbose = SWITCH_TRUE;
90  }
91 
92  if (!strcmp(argv[i], "-s")) {
93  c = 2;
94  }
95 
96  if (!strcmp(argv[i], "-h")) {
97  help = 1;
98  }
99 
100  if (!strncmp(argv[i], "-R", 2)) {
101  char *p = argv[i] + 2;
102 
103  if (p) {
104  int tmp = atoi(p);
105  if (tmp > 0) {
106  rate = tmp;
107  }
108  }
109  }
110  }
111 
112  if (argc - i != 2 || help) {
113  char *app = NULL;
114 
115  if (!help) printf("Invalid input!\n");
116 
117  if ((app = strrchr(argv[0], '/'))) {
118  app++;
119  } else {
120  app = argv[0];
121  }
122 
123 
124  printf("USAGE: %s [options] <file> <tones>\n", app);
125  printf("================================================================================\n");
126  printf("Options:\n"
127  "-s\t\tStereo\n"
128  "-h\t\tHelp\n"
129  "-R<rate>\tSet Rate (8000,16000,32000,48000) etc.\n"
130  "-v\t\tVerbose Logging\n"
131  "<file>\t\tAny file supported by libsndfile\n"
132  "<tones>\t\tA valid teletone script http://wiki.freeswitch.org/wiki/TGML"
133  "\n\n\n"
134  );
135  return 255;
136  }
137 
138  file = argv[i];
139  script = argv[i+1];
140 
141  if (switch_core_init(SCF_MINIMAL, verbose, &err) != SWITCH_STATUS_SUCCESS) {
142  printf("Cannot init core [%s]\n", err);
143  fail();
144  }
145 
147 
148  if (switch_loadable_module_load_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) "mod_sndfile", SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) {
149  printf("Cannot init mod_sndfile [%s]\n", err);
150  fail();
151  }
152 
153  if (switch_core_file_open(&fh, file, c, rate, file_flags, NULL) != SWITCH_STATUS_SUCCESS) {
154  printf("Cannot open file %s\n", file);
155  fail();
156  }
157 
159  ts.rate = rate;
160  ts.channels = c;
161  ts.duration = 250 * (rate / 1000 / c);
162  ts.wait = 50 * (rate / 1000 / c);
163  if (verbose) {
164  ts.debug = 10;
165  ts.debug_stream = stdout;
166  }
167  teletone_run(&ts, script);
170 
171  printf("File: %s generated...\n\nPlease support:\nFreeSWITCH http://www.freeswitch.org\nClueCon http://www.cluecon.com\n", file);
172 
173  end:
174 
176 
177  return r;
178 
179 }
static int teletone_handler(teletone_generation_session_t *ts, teletone_tone_map_t *map)
Definition: tone2wav.c:54
switch_status_t switch_loadable_module_load_module(char *dir, char *fname, switch_bool_t runtime, const char **err)
Load a module.
#define switch_core_file_open(_fh, _file_path, _channels, _rate, _flags, _pool)
Open a media file using file format modules.
Definition: switch_core.h:1865
switch_status_t switch_core_init(_In_ switch_core_flag_t flags, _In_ switch_bool_t console, _Out_ const char **err)
Initilize the core.
switch_bool_t
Definition: switch_types.h:405
switch_status_t switch_core_file_close(_In_ switch_file_handle_t *fh)
Close an open file handle.
#define fail()
Definition: tone2wav.c:70
switch_status_t switch_core_destroy(void)
Destroy the core.
Definition: switch_core.c:2877
int teletone_destroy_session(teletone_generation_session_t *ts)
Free the buffer allocated by a tone generation session.
int teletone_run(teletone_generation_session_t *ts, const char *cmd)
Execute a tone generation script and call callbacks after each instruction.
int teletone_init_session(teletone_generation_session_t *ts, int buflen, tone_handler handler, void *user_data)
Initilize a tone generation session.
switch_directories SWITCH_GLOBAL_dirs
Definition: switch_core.c:60
An abstraction to store a tone generation session.
switch_status_t switch_loadable_module_init(switch_bool_t autoload)
Initilize the module backend and load all the modules.
static int teletone_handler ( teletone_generation_session_t ts,
teletone_tone_map_t map 
)
static

Definition at line 54 of file tone2wav.c.

References teletone_generation_session::buffer, switch_core_file_write(), SWITCH_STATUS_SUCCESS, teletone_mux_tones(), and teletone_generation_session::user_data.

Referenced by main().

55 {
57  int wrote;
58  switch_size_t len;
59 
60  wrote = teletone_mux_tones(ts, map);
61 
62  len = wrote;
63  if (switch_core_file_write(fh, ts->buffer, &len) != SWITCH_STATUS_SUCCESS) {
64  return -1;
65  }
66 
67  return 0;
68 }
int teletone_mux_tones(teletone_generation_session_t *ts, teletone_tone_map_t *map)
Execute a single tone generation instruction.
uintptr_t switch_size_t
switch_status_t switch_core_file_write(_In_ switch_file_handle_t *fh, void *data, switch_size_t *len)
Write media to a file handle.