I've been trying to figure out for the life of me how to cut up a certain amount of milliseconds out of a sample. My first problem is when I insert this code into eclipse. It tells me AudioInputStream, AudioFileFormat and FileFormat are not available. If I can't use FileFormat then i can't pass methods into other methods to get the correct calculations to save the correct amount of music. I can't even import them. But more importantly, how can I change the parameter "secondstocopy" into "millisecondstocopy" without ruining the integrity of the algorithm? Still new to java, thank you for your help!
//edit// FileFormat fileFormat = AudioSystem.getAudioFileFormat(file); It seems I can't use this bit of code for android. The getAudioFileFormat doesn't work. Nor can I calculate the fram rate because of.. int bytesPerSecond = format. * (int)format.getFrameRate(); How do I properly get the format and frame rate with media player so that I can calculate the bytesPerSecond and secondsToCopy? AudioSystem is part of JavaSound, and JavaSound is part of the desktop JVM/SDK. JavaSound is NOT present in the Android JVM/SDK, so your old code will not compile on any current Android SDK. I get an exception using java on android (java.lang.NoClassDefFoundError), why?
How can I calculate the file format and fram rate and stuff without the javasound jvm/sdk. I can tell you now that the sounds are 44100 and 16 bit something. In the get info pane of the recording file, it says total bit rate: 128,000 with two audio channels. I'm not good with encoding stuff but I'm recording the audio with a program on mac called wiretap pro. It tells me the the parameters.
import java.io.*;
import javax.sound.sampled.*;
class AudioFileProcessor {
public static void main(String[] args) {
copyAudio("/tmp/uke.wav", "/tmp/uke-shortened.wav", ƒ, 1);
}
public static void copyAudio(String sourceFileName, String destinationFileName, int startSecond, int secondsToCopy) {
AudioInputStream inputStream = null;
AudioInputStream shortenedStream = null;
try {
File file = new File(sourceFileName);
AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
AudioFormat format = fileFormat.getFormat();
inputStream = AudioSystem.getAudioInputStream(file);
int bytesPerSecond = format.getFrameSize() * (int)format.getFrameRate();
inputStream.skip(startSecond * bytesPerSecond);
long framesOfAudioToCopy = secondsToCopy * (int)format.getFrameRate();
shortenedStream = new AudioInputStream(inputStream, format, framesOfAudioToCopy);
File destinationFile = new File(destinationFileName);
AudioSystem.write(shortenedStream, fileFormat.getType(), destinationFile);
} catch (Exception e) {
println(e);
} finally {
if (inputStream != null) try { inputStream.close(); } catch (Exception e) { println(e); }
if (shortenedStream != null) try { shortenedStream.close(); } catch (Exception e) { println(e); }
}
}
}
this code was taken from: cutting a wave file