-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathtone2wav.c
191 lines (157 loc) · 4.46 KB
/
tone2wav.c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthm@freeswitch.org>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthm@freeswitch.org>
* Michael Jerris <mike@jerris.com>
* Pawel Pierscionek <pawel@voiceworks.pl>
* Bret McDanel <trixter AT 0xdecafbad.com>
*
*
* tone2wav.c -- Generate a .wav from teletone spec
*
*/
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
#ifndef WIN32
#ifdef HAVE_SETRLIMIT
#include <sys/resource.h>
#endif
#endif
#include <switch.h>
/* Picky compiler */
#ifdef __ICC
#pragma warning (disable:167)
#endif
static int teletone_handler(teletone_generation_session_t *ts, teletone_tone_map_t *map)
{
switch_file_handle_t *fh = (switch_file_handle_t *) ts->user_data;
int wrote;
switch_size_t len;
wrote = teletone_mux_tones(ts, map);
len = wrote;
if (switch_core_file_write(fh, ts->buffer, &len) != SWITCH_STATUS_SUCCESS) {
return -1;
}
return 0;
}
#define fail() r = 255; goto end
/* the main application entry point */
int main(int argc, char *argv[])
{
teletone_generation_session_t ts;
int file_flags = SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_DATA_SHORT;
int r = 0;
int rate = 8000;
char *file = NULL, *script = NULL;
switch_file_handle_t fh = { 0 };
const char *err = NULL;
switch_bool_t verbose = SWITCH_FALSE;
int i = 0, c = 1, help = 0;
for(i = 1; i < argc; i++) {
if (!strstr(argv[i], "-")) break;
if (!strcmp(argv[i], "-v")) {
verbose = SWITCH_TRUE;
}
if (!strcmp(argv[i], "-s")) {
c = 2;
}
if (!strcmp(argv[i], "-h")) {
help = 1;
}
if (!strncmp(argv[i], "-R", 2)) {
char *p = argv[i] + 2;
if (p) {
int tmp = atoi(p);
if (tmp > 0) {
rate = tmp;
}
}
}
}
if (argc - i != 2 || help) {
char *app = NULL;
if (!help) printf("Invalid input!\n");
if ((app = strrchr(argv[0], '/'))) {
app++;
} else {
app = argv[0];
}
printf("USAGE: %s [options] <file> <tones>\n", app);
printf("================================================================================\n");
printf("Options:\n"
"-s\t\tStereo\n"
"-h\t\tHelp\n"
"-R<rate>\tSet Rate (8000,16000,32000,48000) etc.\n"
"-v\t\tVerbose Logging\n"
"<file>\t\tAny file supported by libsndfile\n"
"<tones>\t\tA valid teletone script http://wiki.freeswitch.org/wiki/TGML"
"\n\n\n"
);
return 255;
}
file = argv[i];
script = argv[i+1];
if (switch_core_init(SCF_MINIMAL, verbose, &err) != SWITCH_STATUS_SUCCESS) {
printf("Cannot init core [%s]\n", err);
fail();
}
switch_loadable_module_init(SWITCH_FALSE);
if (switch_loadable_module_load_module((char *) SWITCH_GLOBAL_dirs.mod_dir, (char *) "mod_sndfile", SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) {
printf("Cannot init mod_sndfile [%s]\n", err);
fail();
}
if (switch_core_file_open(&fh, file, c, rate, file_flags, NULL) != SWITCH_STATUS_SUCCESS) {
printf("Cannot open file %s\n", file);
fail();
}
teletone_init_session(&ts, 0, teletone_handler, &fh);
ts.rate = rate;
ts.channels = c;
ts.duration = 250 * (rate / 1000 / c);
ts.wait = 50 * (rate / 1000 / c);
if (verbose) {
ts.debug = 10;
ts.debug_stream = stdout;
}
teletone_run(&ts, script);
teletone_destroy_session(&ts);
switch_core_file_close(&fh);
printf("File: %s generated...\n\nPlease support:\nFreeSWITCH http://www.freeswitch.org\nClueCon http://www.cluecon.com\n", file);
end:
switch_safe_free(SWITCH_GLOBAL_dirs.mod_dir);
//switch_core_destroy();
return r;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/