FreeSWITCH API Documentation  1.7.0
tone2wav.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * Michael Jerris <mike@jerris.com>
28  * Pawel Pierscionek <pawel@voiceworks.pl>
29  * Bret McDanel <trixter AT 0xdecafbad.com>
30  *
31  *
32  * tone2wav.c -- Generate a .wav from teletone spec
33  *
34  */
35 
36 #ifndef _XOPEN_SOURCE
37 #define _XOPEN_SOURCE 600
38 #endif
39 
40 #ifndef WIN32
41 #ifdef HAVE_SETRLIMIT
42 #include <sys/resource.h>
43 #endif
44 #endif
45 
46 #include <switch.h>
47 
48 /* Picky compiler */
49 #ifdef __ICC
50 #pragma warning (disable:167)
51 #endif
52 
53 
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 }
69 
70 #define fail() r = 255; goto end
71 
72 /* the main application entry point */
73 int main(int argc, char *argv[])
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 }
180 
181 /* For Emacs:
182  * Local Variables:
183  * mode:c
184  * indent-tabs-mode:t
185  * tab-width:4
186  * c-basic-offset:4
187  * End:
188  * For VIM:
189  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
190  */
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
An abstraction to store a tone mapping.
Definition: libteletone.h:93
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
int teletone_mux_tones(teletone_generation_session_t *ts, teletone_tone_map_t *map)
Execute a single tone generation instruction.
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.
uintptr_t switch_size_t
switch_directories SWITCH_GLOBAL_dirs
Definition: switch_core.c:60
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.
Main Library Header.
An abstraction to store a tone generation session.
int main(int argc, char *argv[])
Definition: tone2wav.c:73
switch_status_t switch_loadable_module_init(switch_bool_t autoload)
Initilize the module backend and load all the modules.