|
| 1 | +package net.sourceforge.sox; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.ArrayList; |
| 5 | + |
| 6 | +import android.util.Log; |
| 7 | + |
| 8 | +/** |
| 9 | + * Concatenates two files together with a crossfade of user |
| 10 | + * defined length. |
| 11 | + * |
| 12 | + * It is a Java port of the scripts/crossfade_cat.sh script |
| 13 | + * in the sox source tree. |
| 14 | + * |
| 15 | + * Original script by Kester Clegg, with modifications by Chris |
| 16 | + * Bagwell. |
| 17 | + * |
| 18 | + * @author Abel Luck |
| 19 | + * |
| 20 | + */ |
| 21 | +// TODO make runnable? |
| 22 | +public class CrossfadeCat { |
| 23 | + private final static String TAG = "SOX-XFADE"; |
| 24 | + private SoxController mController; |
| 25 | + private String mFirstFile; |
| 26 | + private String mSecondFile; |
| 27 | + private double mFadeLength; |
| 28 | + private String mFinalMix; |
| 29 | + private ArrayList<String> mTemporaryFiles = new ArrayList<String>();; |
| 30 | + |
| 31 | + public CrossfadeCat(SoxController controller, String firstFile, String secondFile, double fadeLength, String outFile) { |
| 32 | + mController = controller; |
| 33 | + mFirstFile = firstFile; |
| 34 | + mSecondFile = secondFile; |
| 35 | + mFadeLength = fadeLength; |
| 36 | + mFinalMix = outFile; |
| 37 | + } |
| 38 | + |
| 39 | + public boolean start() { |
| 40 | + // find length of first file |
| 41 | + double length = mController.getLength(mFirstFile); |
| 42 | + |
| 43 | + double trimLength = length - mFadeLength; |
| 44 | + String trimLengthStr = mController.formatTimePeriod(trimLength); |
| 45 | + String fadeLengthStr = mController.formatTimePeriod(mFadeLength); |
| 46 | + |
| 47 | + // Obtain trimLength seconds of fade out position from the first File |
| 48 | + String trimmedOne = mController.trimAudio(mFirstFile, trimLengthStr, null); |
| 49 | + if( trimmedOne == null ) |
| 50 | + return abort(); |
| 51 | + mTemporaryFiles.add(trimmedOne); |
| 52 | + |
| 53 | + // We assume a fade out is needed (i.e., firstFile doesn't already fade out) |
| 54 | + |
| 55 | + String fadedOne = mController.fadeAudio(trimmedOne, "t", "0", fadeLengthStr, fadeLengthStr); |
| 56 | + if( fadedOne == null ) |
| 57 | + return abort(); |
| 58 | + mTemporaryFiles.add(fadedOne); |
| 59 | + |
| 60 | + // Get crossfade section from the second file |
| 61 | + String trimmedTwo = mController.trimAudio(mSecondFile, "0", fadeLengthStr); |
| 62 | + if( trimmedTwo == null ) |
| 63 | + return abort(); |
| 64 | + mTemporaryFiles.add(trimmedTwo); |
| 65 | + |
| 66 | + String fadedTwo = mController.fadeAudio(trimmedTwo, "t", fadeLengthStr, null, null); |
| 67 | + if( fadedTwo == null ) |
| 68 | + return abort(); |
| 69 | + mTemporaryFiles.add(fadedTwo); |
| 70 | + |
| 71 | + // Mix crossfaded files together at full volume |
| 72 | + ArrayList<String> files = new ArrayList<String>(); |
| 73 | + files.add(fadedOne); |
| 74 | + files.add(fadedTwo); |
| 75 | + |
| 76 | + String crossfaded = new File(mFirstFile).getAbsolutePath() + "-x-" + new File(mSecondFile).getName() +".wav"; |
| 77 | + crossfaded = mController.combineMix(files, crossfaded); |
| 78 | + if( crossfaded == null ) |
| 79 | + return abort(); |
| 80 | + mTemporaryFiles.add(crossfaded); |
| 81 | + |
| 82 | + // Trim off crossfade sections from originals |
| 83 | + String trimmedThree = mController.trimAudio(mFirstFile, "0", trimLengthStr); |
| 84 | + if( trimmedThree == null ) |
| 85 | + return abort(); |
| 86 | + mTemporaryFiles.add(trimmedThree); |
| 87 | + String trimmedFour = mController.trimAudio(mSecondFile, fadeLengthStr, null); |
| 88 | + if( trimmedFour == null ) |
| 89 | + return abort(); |
| 90 | + mTemporaryFiles.add(trimmedFour); |
| 91 | + |
| 92 | + // Combine into final mix |
| 93 | + files.clear(); |
| 94 | + files.add(trimmedThree); |
| 95 | + files.add(crossfaded); |
| 96 | + files.add(trimmedFour); |
| 97 | + mFinalMix = mController.combine(files, mFinalMix); |
| 98 | + cleanup(); |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + private void cleanup() { |
| 103 | + for(String file : mTemporaryFiles) { |
| 104 | + File f = new File(file); |
| 105 | + boolean result = f.delete(); |
| 106 | + if( !result ) |
| 107 | + Log.e(TAG, "Error, could not delete: " + file); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private boolean abort() { |
| 112 | + cleanup(); |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | +} |
0 commit comments