Java中基于静默的wav文件分割
我想把wav文件分成若干个wav文件,每个文件 分裂 通过字里行间的沉默,我试着编一点代码。我设法把文件分开了 但当试图打破沉默时,事情变得复杂了。。有什么帮助吗
谢谢
- 将WAV文件拆分为多个部分。这个类将一个大的WAV文件拆分为
- 多个WAV文件,每个文件的长度固定(分割文件长度)。信息技术 接受
- 它从嵌入式资源输入文件,并写入一系列输出*。wav文件李>
以下是我目前的代码:
public class WaveSplitter {
public static int SPLIT_FILE_LENGTH_MS = 0;
public static final String INPUT_FILE_LOCATION = "resources/AUD-20171027-WA0001.wav";
public static void main(String[] args) {
try {
// Get the wave file from the embedded resources
URL defaultImage =
WaveSplitter.class.getResource(INPUT_FILE_LOCATION);
// GSpeechDuplex duplex = new
// GSpeechDuplex("AIzaSyDHQsnCHDk71x-Dpp05IIK3tYNOEP84z1s");
// duplex.setLanguage("heb");
File audioFile = new File(defaultImage.toURI());
File soundFile = new File(defaultImage.toURI());
FileInputStream fileInputStream = null;
long duration = 0;
try {
fileInputStream = new FileInputStream(soundFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
duration =
Objects.requireNonNull(fileInputStream).getChannel().size() / 128;
} catch (IOException e) {
}
AudioInputStream stream;
WavFile wavFile = WavFile.openWavFile(new File(defaultImage.toURI()));
AudioInputStream audioInputStream =
AudioSystem.getAudioInputStream(soundFile);
AudioFormat format = audioInputStream.getFormat();
long frames = audioInputStream.getFrameLength();
double durationInSeconds = (frames + 0.0) / format.getFrameRate();
System.out.println("Duration " + durationInSeconds + " seconds \n");
durationInSeconds = durationInSeconds * 1000;
SPLIT_FILE_LENGTH_MS = (int) durationInSeconds;
// wavFile.display();
// Get the number of audio channels in the wav file
int numberChannels = wavFile.getNumChannels();
System.out.println("number channels is " + numberChannels);
System.out.println("SPLIT_FILE_LENGTH_MS is " + SPLIT_FILE_LENGTH_MS);
int framesRead;
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
byte[] bytes = FileUtils.readFileToByteArray(soundFile);
// String encoded = Base64.encodeToString(bytes, 0);
// playSoundFile(soundFile);
WavFile inputWavFile = WavFile.openWavFile(soundFile);
// File audioFile = new File("AUD-20171027-WA0001.wav");
// sendToGoogleViaOnvegoServer(myByteData);
/// System.out.println("Seneteces:\n " + bytes);
// Get the number of audio channels in the wav file
int numChannels = inputWavFile.getNumChannels();
// set the maximum number of frames for a target file,
// based on the number of milliseconds assigned for each file
int maxFramesPerFile = (int) inputWavFile.getSampleRate() *
SPLIT_FILE_LENGTH_MS / 1000;
System.out.println("maxFramesPerFile is " + maxFramesPerFile + "\n");
// Create a buffer of maxFramesPerFile frames
double[] buffer = new double[maxFramesPerFile * numChannels];
// sendToGoogleViaOnvegoServer(myByteData);
framesRead = inputWavFile.readFrames(buffer, maxFramesPerFile);
// System.out.print(buffer.length);
int fileCount = 0;
int s = 0;
int j = 0;
int timercount = 0;
int audiorecord = 0;
int count = 0;
// SPLIT_FILE_LENGTH_MS = 0;
double maxlengh = framesRead * numChannels;
System.out.print("Audio Duration " + maxFramesPerFile + "\n");
int arraylengh = framesRead * numChannels;
System.out.print("frame*channels " + framesRead * numChannels);
///loop to find silent places
do {
// Read frames into buffer
framesRead = inputWavFile.readFrames(buffer, maxFramesPerFile);
// System.out.print(framesRead);
for (s = 0; s < arraylengh; s++) {
if (buffer[s] == 0) {
// System.out.print(s + ", " + buffer[s] + "\n");
count++;
timercount++;
}
else {
for (j = s; j < arraylengh; j++) {
if (buffer[j] != 0)
{
} else {
SPLIT_FILE_LENGTH_MS = 1500;
maxFramesPerFile = (int)
inputWavFile.getSampleRate() * SPLIT_FILE_LENGTH_MS /
1000;
framesRead = inputWavFile.readFrames(buffer,
maxFramesPerFile);
// framesRead = inputWavFile.readFrames(buffer,
maxFramesPerFile);
WavFile outputWavFile = WavFile.newWavFile(new
File("out" + (fileCount + 1) + ".wav"),
inputWavFile.getNumChannels(), framesRead,
inputWavFile.getValidBits(),
inputWavFile.getSampleRate());
// Write the buffer
outputWavFile.writeFrames(buffer, framesRead);
outputWavFile.close();
return;
}
}
}
// System.out.print(maxFramesPerFile);
}
// SPLIT_FILE_LENGTH_MS=1000;
maxFramesPerFile = (int) inputWavFile.getSampleRate() *
SPLIT_FILE_LENGTH_MS / 1000;
framesRead = inputWavFile.readFrames(buffer, maxFramesPerFile);
WavFile outputWavFile = WavFile.newWavFile(new File("out" + (
fileCount + 1) + ".wav"),
inputWavFile.getNumChannels(), framesRead,
inputWavFile.getValidBits(),
inputWavFile.getSampleRate());
// Write the buffer
outputWavFile.writeFrames(buffer, framesRead);
outputWavFile.close();
fileCount++;
// System.out.print(fileCount);
} while (framesRead != 0);
System.out.print(count + "\n");
System.out.print(fileCount);
//catch error
} catch (Exception e) {
System.err.println(e);
}
}
// Play sound function
public static void playSoundFile(File soundFile)
throws UnsupportedAudioFileException, IOException, LineUnavailableException {
try {
final Clip clip = (Clip) AudioSystem.getLine(new Info(Clip.class));
clip.removeLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP)
clip.close();
}
});
clip.open(AudioSystem.getAudioInputStream(soundFile));
clip.start();
} catch (Exception exc) {
exc.printStackTrace(System.out);
}
}
}
共 (0) 个答案