Repository navigation
Expand file tree
/
Copy pathSoxController.java
More file actions
327 lines (264 loc) · 7.43 KB
/
Copy pathSoxController.java
File metadata and controls
327 lines (264 loc) · 7.43 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package net.sourceforge.sox;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.ffmpeg.android.ShellUtils.ShellCallback;
public class SoxController {
private final static String TAG = "SOX";
String[] libraryAssets = {"sox"};
private String soxBin;
private File fileBinDir;
private ShellCallback callback;
public SoxController(File fileAppRoot, ShellCallback _callback) throws FileNotFoundException, IOException {
callback = _callback;
fileBinDir = new File(fileAppRoot,"lib");
File fileSox = new File(fileBinDir,"libsox.so");
if (fileSox.exists())
soxBin = fileSox.getCanonicalPath();
else
soxBin = "sox";
}
private class LengthParser implements ShellCallback {
public double length;
public int retValue = -1;
@Override
public void shellOut(String shellLine) {
if( !shellLine.startsWith("Length") )
return;
String[] split = shellLine.split(":");
if(split.length != 2) return;
String lengthStr = split[1].trim();
try {
length = Double.parseDouble( lengthStr );
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
@Override
public void processComplete(int exitValue) {
retValue = exitValue;
}
}
/**
* Retrieve the length of the audio file
* sox file.wav 2>&1 -n stat | grep Length | cut -d : -f 2 | cut -f 1
* @return the length in seconds or null
*/
public double getLength(String path) {
ArrayList<String> cmd = new ArrayList<String>();
cmd.add(soxBin);
cmd.add(path);
cmd.add("-n");
cmd.add("stat");
LengthParser sc = new LengthParser();
try {
execSox(cmd, sc);
} catch (Exception e) {
return -1;
}
return sc.length;
}
/**
* Discard all audio not between start and length (length = end by default)
* sox <path> -e signed-integer -b 16 outFile trim <start> <length>
* @param start
* @param length (optional)
* @return path to trimmed audio
*/
public String trimAudio(String path, double start, double length) throws Exception {
ArrayList<String> cmd = new ArrayList<String>();
File file = new File(path);
String outFile = file.getCanonicalPath() + "_trimmed.wav";
cmd.add(soxBin);
cmd.add(path);
cmd.add("-e");
cmd.add("signed-integer");
cmd.add("-b");
cmd.add("16");
cmd.add(outFile);
cmd.add("trim");
cmd.add(start+"");
if( length != -1 )
cmd.add(length+"");
int rc = execSox(cmd, callback);
if( rc != 0 ) {
outFile = null;
}
if (file.exists())
return outFile;
else
return null;
}
/**
* Fade audio file
* sox <path> outFile fade <type> <fadeInLength> <stopTime> <fadeOutLength>
* @param path
* @param type
* @param fadeInLength specify 0 if no fade in is desired
* @param stopTime (optional)
* @param fadeOutLength (optional)
* @return
*/
public String fadeAudio(String path, String type, double fadeInLength, double stopTime, double fadeOutLength ) throws IOException {
final List<String> curves = Arrays.asList( new String[]{ "q", "h", "t", "l", "p"} );
if(!curves.contains(type)) {
throw new RuntimeException("fadeAudio: passed invalid type: " + type);
}
File file = new File(path);
String outFile = file.getCanonicalPath() + "_faded.wav";
ArrayList<String> cmd = new ArrayList<String>();
cmd.add(soxBin);
cmd.add(path);
cmd.add(outFile);
cmd.add("fade");
cmd.add(type);
cmd.add(fadeInLength+"");
if(stopTime != -1)
cmd.add(stopTime+"");
if(fadeOutLength != -1)
cmd.add(fadeOutLength+"");
try {
int rc = execSox(cmd, callback);
if(rc != 0) {
//Log.e(TAG, "fadeAudio receieved non-zero return code!");
outFile = null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outFile;
}
/**
* Combine and mix audio files
* sox -m -v 1.0 file[0] -v 1.0 file[1] ... -v 1.0 file[n] outFile
* TODO support passing of volume
* @param files
* @return combined and mixed file (null on failure)
*/
public String combineMix(List<String> files, String outFile) {
ArrayList<String> cmd = new ArrayList<String>();
cmd.add(soxBin);
cmd.add("-m");
for(String file : files) {
cmd.add("-v");
cmd.add("1.0");
cmd.add(file);
}
cmd.add(outFile);
try {
int rc = execSox(cmd, callback);
if(rc != 0) {
// Log.e(TAG, "combineMix receieved non-zero return code!");
outFile = null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outFile;
}
/**
* Simple combiner
* sox file[0] file[1] ... file[n] <outFile>
* @param files
* @param outFile
* @return outFile or null on failure
*/
public String combine(List<String> files, String outFile) throws Exception {
ArrayList<String> cmd = new ArrayList<String>();
cmd.add(soxBin);
for(String file : files) {
cmd.add(file);
}
cmd.add(outFile);
int rc = execSox(cmd, callback);
if(rc != 0) {
throw new Exception ("exit code: " + rc);
}
return outFile;
}
/**
* Takes a seconds.frac value and formats it into:
* hh:mm:ss:ss.frac
* @param seconds
*/
/*
public String formatTimePeriod(double seconds) {
long milliTime = (long)(seconds * 100f);
Date dateTime = new Date(milliTime);
return String.format(Locale.US, "%s:%s.%s", dateTime.getHours(),dateTime.getMinutes(),dateTime.getSeconds());
}*/
public int execSox(List<String> cmd, ShellCallback sc) throws IOException,
InterruptedException {
String soxBin = new File(fileBinDir, "sox").getCanonicalPath();
Runtime.getRuntime().exec("chmod 700 " + soxBin);
return execProcess(cmd, sc);
}
private int execProcess(List<String> cmds, ShellCallback sc)
throws IOException, InterruptedException {
//ensure that the arguments are in the correct Locale format
for (String cmd :cmds)
{
cmd = String.format(Locale.US, "%s", cmd);
}
ProcessBuilder pb = new ProcessBuilder(cmds);
pb.directory(fileBinDir);
StringBuffer cmdlog = new StringBuffer();
for (String cmd : cmds) {
cmdlog.append(cmd);
cmdlog.append(' ');
}
sc.shellOut(cmdlog.toString());
// pb.redirectErrorStream(true);
Process process = pb.start();
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(
process.getErrorStream(), "ERROR", sc);
// any output?
StreamGobbler outputGobbler = new StreamGobbler(
process.getInputStream(), "OUTPUT", sc);
// kick them off
errorGobbler.start();
outputGobbler.start();
int exitVal = process.waitFor();
while (outputGobbler.isAlive() || errorGobbler.isAlive());
sc.processComplete(exitVal);
return exitVal;
}
class StreamGobbler extends Thread {
InputStream is;
String type;
ShellCallback sc;
StreamGobbler(InputStream is, String type, ShellCallback sc) {
this.is = is;
this.type = type;
this.sc = sc;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
if (sc != null)
sc.shellOut(line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}