6
6
import java .io .IOException ;
7
7
import java .io .InputStream ;
8
8
import java .io .InputStreamReader ;
9
+ import java .text .DecimalFormat ;
9
10
import java .util .ArrayList ;
11
+ import java .util .Arrays ;
10
12
import java .util .List ;
11
13
12
14
import org .ffmpeg .android .BinaryInstaller ;
@@ -104,16 +106,17 @@ public double getLength(String path) {
104
106
}
105
107
106
108
/**
107
- * Discard all audio before start
108
- * sox <path> -e signed-integer -b 16 outFile trim <start>
109
- * @param seconds
109
+ * Discard all audio not between start and length (length = end by default)
110
+ * sox <path> -e signed-integer -b 16 outFile trim <start> <length>
111
+ * @param start
112
+ * @param length (optional)
110
113
* @return path to trimmed audio
111
114
*/
112
- public String trimAudio (String path , double start ) {
115
+ public String trimAudio (String path , String start , String length ) {
113
116
ArrayList <String > cmd = new ArrayList <String >();
114
117
115
118
File file = new File (path );
116
- String outFile = file .getAbsolutePath () + "_fadeout .wav" ;
119
+ String outFile = file .getAbsolutePath () + "_trimmed .wav" ;
117
120
cmd .add (soxBin );
118
121
cmd .add (path );
119
122
cmd .add ("-e" );
@@ -122,10 +125,16 @@ public String trimAudio(String path, double start) {
122
125
cmd .add ("16" );
123
126
cmd .add (outFile );
124
127
cmd .add ("trim" );
125
- cmd .add (Double .toString (start ));
128
+ cmd .add (start );
129
+ if ( length != null )
130
+ cmd .add (length );
126
131
127
132
try {
128
133
int rc = execSox (cmd , new LoggingCallback ());
134
+ if ( rc != 0 ) {
135
+ Log .e (TAG , "trimAudio receieved non-zero return code!" );
136
+ outFile = null ;
137
+ }
129
138
} catch (IOException e ) {
130
139
// TODO Auto-generated catch block
131
140
e .printStackTrace ();
@@ -137,6 +146,66 @@ public String trimAudio(String path, double start) {
137
146
return outFile ;
138
147
}
139
148
149
+ /**
150
+ * Fade audio file
151
+ * sox <path> outFile fade <type> <fadeInLength> <stopTime> <fadeOutLength>
152
+ * @param path
153
+ * @param type
154
+ * @param fadeInLength specify 0 if no fade in is desired
155
+ * @param stopTime (optional)
156
+ * @param fadeOutLength (optional)
157
+ * @return
158
+ */
159
+ public String fadeAudio (String path , String type , String fadeInLength , String stopTime , String fadeOutLength ) {
160
+
161
+ final List <String > curves = Arrays .asList ( new String []{ "q" , "h" , "t" , "l" , "p" } );
162
+
163
+ if (!curves .contains (type )) {
164
+ Log .e (TAG , "fadeAudio: passed invalid type: " + type );
165
+ return null ;
166
+ }
167
+
168
+ File file = new File (path );
169
+ String outFile = file .getAbsolutePath () + "_faded.wav" ;
170
+
171
+ ArrayList <String > cmd = new ArrayList <String >();
172
+ cmd .add (soxBin );
173
+ cmd .add (path );
174
+ cmd .add (outFile );
175
+ cmd .add ("fade" );
176
+ cmd .add (type );
177
+ cmd .add (fadeInLength );
178
+ if (stopTime != null )
179
+ cmd .add (stopTime );
180
+ if (fadeOutLength != null )
181
+ cmd .add (fadeOutLength );
182
+
183
+ try {
184
+ int rc = execSox (cmd , new LoggingCallback ());
185
+ if (rc != 0 ) {
186
+ Log .e (TAG , "fadeAudio receieved non-zero return code!" );
187
+ outFile = null ;
188
+ }
189
+ } catch (IOException e ) {
190
+ // TODO Auto-generated catch block
191
+ e .printStackTrace ();
192
+ } catch (InterruptedException e ) {
193
+ // TODO Auto-generated catch block
194
+ e .printStackTrace ();
195
+ }
196
+ return outFile ;
197
+ }
198
+
199
+ /**
200
+ * Takes a seconds.frac value and formats it into:
201
+ * hh:mm:ss:ss.frac
202
+ * @param seconds
203
+ */
204
+ public String formatTimePeriod (double seconds ) {
205
+ String seconds_frac = new DecimalFormat ("#.##" ).format (seconds );
206
+ return String .format ("0:0:%s" , seconds_frac );
207
+ }
208
+
140
209
private int execSox (List <String > cmd , ShellCallback sc ) throws IOException ,
141
210
InterruptedException {
142
211
0 commit comments