Java-调整WAV文件的播放速度 [英] Java - Adjust playback speed of a WAV file

查看:504
本文介绍了Java-调整WAV文件的播放速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能很稠密,但似乎无法找到解决问题的方法

I'm likely dense but I cannot seem to find a solution to my issue

(注意::我可以找到很多人报告此问题,似乎是由于Java更新(可能是1.5?)导致的.也许不再支持SAMPLE_RATE?无法找到任何解决方案).

(NOTE: I CAN find lots of people reporting this issue, seems like it happened as a result of newer Java (possible 1.5?). Perhaps SAMPLE_RATE is no longer supported? I am unable to find any solution).

我正在尝试调整SAMPLE_RATE以加快/减慢歌曲速度.我可以成功播放一个.wav文件,没有问题,所以我研究了FloatControl来调节音量:

I'm trying to adjust the SAMPLE_RATE to speed up/slow down song. I can successfully play a .wav file without issue, so I looked into FloatControl which worked for adjusting volume:

public void adjustVolume(String audioType, float gain) {
        FloatControl gainControl = null;

        gainControl = (FloatControl) clipSFX.getControl(FloatControl.Type.MASTER_GAIN);
                if(gain > MAX_VOLUME)
                    gain = MAX_VOLUME;
                if(gain < MIN_VOLUME)
                    gain = MIN_VOLUME;

            //set volume
            gainControl.setValue(gain);
    }

但是当尝试将此原理转换为SAMPLE_RATE时,我在此阶段很早就收到了一个错误:

But when trying to translate this principle to SAMPLE_RATE, I get an error very early on at this stage:

    public void adjustVolume(String audioType, float gain) {
        FloatControl gainControl = null;

        gainControl = (FloatControl) clipSFX.getControl(FloatControl.Type.SAMPLE_RATE);
        //ERROR: Exception in thread "Thread-3" java.lang.IllegalArgumentException: Unsupported control type: Sample Rate

        //I haven't gotten this far yet since the above breaks, but in theory will then set value?
            gainControl.setValue(gain);
}

我在网上找到的所有内容似乎都与从麦克风或某些外部线路上获取输入有关,并且似乎没有转换为使用音频文件,因此我不确定我所缺少的内容.任何帮助,将不胜感激!谢谢!

Everything I've found online seems to be related to taking input from a mic or some external line and doesn't seem to translate to using an audio file, so I'm unsure what I'm missing. Any help would be appreciated! Thanks!

推荐答案

在这里,我们有一种方法可以改变速度-通过使采样率加倍来改变速度.基本上,步骤如下:

Here we have a method that changes the speed - by doubling the sample rate. Basically the steps are as follows:

  • 打开文件的音频流
  • 获取格式
  • 更改采样率后创建新格式
  • 打开具有该格式的数据行
  • 从文件/音频流中读取并播放到行上

这里的概念是SourceDataLine,AudioFormat和AudioInputStream.如果您查看javax.sound教程,将会发现它们,甚至是类的页面.现在,您可以创建自己的方法(例如Adjust(factor)),该方法只会获取新格式,而其他所有格式都保持不变.

The concepts here are SourceDataLine, AudioFormat and AudioInputStream. If you look at the javax.sound tutorial you will find them, or even the pages of the classes. You can now create your own method (like adjust(factor)) that just gets the new format and all else stay the same.

  public void play() {
    try {
      File fileIn = new File(" ....);
      AudioInputStream audioInputStream=AudioSystem.getAudioInputStream(fileIn);
      AudioFormat formatIn=audioInputStream.getFormat();
      AudioFormat format=new AudioFormat(formatIn.getSampleRate()*2, formatIn.getSampleSizeInBits(), formatIn.getChannels(), true, formatIn.isBigEndian());
          System.out.println(formatIn.toString());
          System.out.println(format.toString());
      byte[] data=new byte[1024];
      DataLine.Info dinfo=new DataLine.Info(SourceDataLine.class, format);
      SourceDataLine line=(SourceDataLine)AudioSystem.getLine(dinfo);
      if(line!=null) {
        line.open(format);
        line.start();
        while(true) {
          int k=audioInputStream.read(data, 0, data.length);
          if(k<0) break;
          line.write(data, 0, k);
        }
        line.stop();
        line.close();
      }
    }
    catch(Exception ex) { ex.printStackTrace(); }
  }

这篇关于Java-调整WAV文件的播放速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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