简体   繁体   中英

cutting a wave file

How can i cut a .wave file using java ?

What i want is :

when the user presses the button labeled cut it should cut the audio from the previous mark (in nanoseconds) to the current position in nanoseconds. (mark is positioned to the current position in nanoseconds after the sound is cut) After i get that piece of audio,i want to save that piece of audio file.

// obtain an audio stream 
long mark = 0; // initially set to zero
//get the current position in nanoseconds
// after that how to proceed ?
// another method ?

How can i do that ?

This has originally been answered by Martin Dow

import java.io.*;
import javax.sound.sampled.*;

class AudioFileProcessor {

public static void main(String[] args) {
  copyAudio("/tmp/uke.wav", "/tmp/uke-shortened.wav", 2, 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); }
 }
}

}

Originally answered HERE

  • Create an AudioInputStream from the file source (you can use AudioSystem.getAudioInputStream(File) for this).
  • Use AudioFormat from the stream's getFormat() to determine the number of bytes you need to read from the stream and the positions.
    • File position (bytes) = time(seconds) / sample rate * sample size (bits) * 8 * channels for wave files
  • Create a new AudioInputStream based on the original that only reads the data you want from the original. You can do this by skipping the bytes you want in the original stream, create a wrapper that fixes the length for the endpoint and then using AudioSystem.getAudioInputStream(AudioFormat, AudioInputStream). There are other ways to do this as well which might be better.
  • Use AudioSystem.write() method to write out the new file.

You might also want to look at Tritonus and its AudioOutputStream, it might make things easier.

有一个API可以帮助您实现目标http://code.google.com/p/musicg-sound-api/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM