切割波形文件 [英] cutting a wave file

查看:203
本文介绍了切割波形文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何能我用java切 .wave 文件?

How can i cut a .wave file using java ?

我要的是:

剪切它应该削减从previous音频标记(纳秒级),以在纳秒的当前位置。的(标记定位到当前位置在纳秒的声音被切断后)的后,我拿到这块音频,我要救那一块的音频文件。

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 ?

推荐答案

这原是借马丁道琼斯回答

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); }
 }
}

}

最初回答<一个href=\"http://stackoverflow.com/questions/7546010/obtaining-an-audioinputstream-upto-some-x-bytes-from-the-original-cutting-an-aud/7547123#7547123\">HERE

这篇关于切割波形文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆