Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

Commit c1983d2

Browse files
committed
sox: complete trimAudio, add fadeAudio
1 parent f1dddf5 commit c1983d2

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

src/net/sourceforge/sox/SoxController.java

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import java.io.IOException;
77
import java.io.InputStream;
88
import java.io.InputStreamReader;
9+
import java.text.DecimalFormat;
910
import java.util.ArrayList;
11+
import java.util.Arrays;
1012
import java.util.List;
1113

1214
import org.ffmpeg.android.BinaryInstaller;
@@ -104,16 +106,17 @@ public double getLength(String path) {
104106
}
105107

106108
/**
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)
110113
* @return path to trimmed audio
111114
*/
112-
public String trimAudio(String path, double start) {
115+
public String trimAudio(String path, String start, String length) {
113116
ArrayList<String> cmd = new ArrayList<String>();
114117

115118
File file = new File(path);
116-
String outFile = file.getAbsolutePath() + "_fadeout.wav";
119+
String outFile = file.getAbsolutePath() + "_trimmed.wav";
117120
cmd.add(soxBin);
118121
cmd.add(path);
119122
cmd.add("-e");
@@ -122,10 +125,16 @@ public String trimAudio(String path, double start) {
122125
cmd.add("16");
123126
cmd.add(outFile);
124127
cmd.add("trim");
125-
cmd.add(Double.toString(start));
128+
cmd.add(start);
129+
if( length != null )
130+
cmd.add(length);
126131

127132
try {
128133
int rc = execSox(cmd, new LoggingCallback());
134+
if( rc != 0 ) {
135+
Log.e(TAG, "trimAudio receieved non-zero return code!");
136+
outFile = null;
137+
}
129138
} catch (IOException e) {
130139
// TODO Auto-generated catch block
131140
e.printStackTrace();
@@ -137,6 +146,66 @@ public String trimAudio(String path, double start) {
137146
return outFile;
138147
}
139148

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+
140209
private int execSox(List<String> cmd, ShellCallback sc) throws IOException,
141210
InterruptedException {
142211

0 commit comments

Comments
 (0)