如何在Java中将播放音频保存为WAV格式

时间:2019-06-09 09:14:00

标签: java audio wav javasound

我正在使用waywardgeek开发的Sonic算法来加快和减慢音频文件的速度。一切正常。但是我需要的是将播放音频保存到wav文件中。我尝试使用ByteArrayInputStreamTargetDataLink保存。但是我做不到。因此,有人可以帮助我将播放音频保存到wav文件中吗?而且我还是Java Audio的新手。

这是代码,

Main.java

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Main {

    private static void runSonic(
            AudioInputStream audioStream,
            SourceDataLine line,
            float speed,
            float pitch,
            float rate,
            float volume,
            boolean emulateChordPitch,
            int quality,
            int sampleRate,
            int numChannels) throws IOException {
        Sonic sonic = new Sonic(sampleRate, numChannels);
        int bufferSize = line.getBufferSize();
        byte inBuffer[] = new byte[bufferSize];
        byte outBuffer[] = new byte[bufferSize];
        int numRead, numWritten;

        sonic.setSpeed(speed);
        sonic.setPitch(pitch);
        sonic.setRate(rate);
        sonic.setVolume(volume);
        sonic.setChordPitch(emulateChordPitch);
        sonic.setQuality(quality);
        do {
            numRead = audioStream.read(inBuffer, 0, bufferSize);
            if (numRead <= 0) {
                sonic.flushStream();
            } else {
                sonic.writeBytesToStream(inBuffer, numRead);
            }
            do {
                numWritten = sonic.readBytesFromStream(outBuffer, bufferSize);
                if (numWritten > 0) {
                    line.write(outBuffer, 0, numWritten);
                }
            } while (numWritten > 0);
        } while (numRead > 0);
    }

    public static void main(String[] argv) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
        float speed = 1.0f;
        float pitch = 1.0f;
        float rate = 1.5f;
        float volume = 1.0f;
        boolean emulateChordPitch = false;
        int quality = 0;

        AudioInputStream stream = AudioSystem.getAudioInputStream(new File("C:\\audio1.wav"));
        AudioFormat format = stream.getFormat();
        int sampleRate = (int) format.getSampleRate();
        int numChannels = format.getChannels();
        SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, format,
                ((int) stream.getFrameLength() * format.getFrameSize()));
        SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(stream.getFormat());
        line.start();
        runSonic(stream, line, speed, pitch, rate, volume, emulateChordPitch, quality,
                sampleRate, numChannels);
        line.drain();
        line.stop();
    }
}

我尝试使用以下代码,并将其保存到wav文件中,但在播放时具有一词音频。而且,对于TargetDataLink,也没有任何作用。

byteArrayOutputStream = new ByteArrayInputStream(outBuffer);
audioStreams = new AudioInputStream(byteArrayOutputStream, line.getFormat(), bufferSize);
AudioSystem.write(audioStreams, AudioFileFormat.Type.WAVE, new File("C:\\audio2.wav"));

Sonic.java

由于允许的字符数有限,因此无法在此处发布完整的代码。代码是here

PS::该代码由Bill Cox开发。感谢您Bill Cox的出色代码。

2 个答案:

答案 0 :(得分:2)

我已经尝试过,现在我想出了使用/[a-zA-Z]+/glet letter = ' <section class="contact" id="contact">'; let words = letter.match (/[a-zA-Z]+/g); // Match all alphabet let firstWord = words.length > 0 ? words[0] : ''; console.log(firstWord);的解决方案。首先,我使用ByteArrayInputStreamByteArrayOutputStream outBuffer一样写ByteArrayOutputStream。然后将其转换为SourceDataLine并使用line.write()保存。 byteArray的{​​{3}}帮助我做到了。它正在工作,但我不知道这是否是最佳解决方案。因此,欢迎提出任何建议。谢谢。

AudioInputStream

答案 1 :(得分:1)

我很确定在过去的问题中都涵盖了两个问题,即如何更改播放速率以及如何保存波形。例如Java - Adjust playback speed of a WAV file参见线性插值的答案。这并不难实现。如果只希望将速度提高一倍,则选择的答案很好,但是插值算法允许指定许多其他速度。

关于保存到wav,我建议从Oracle Sound教程开始。 https://docs.oracle.com/javase/tutorial/sound/converters.html

就将其作为流而不是将完整的文件保存在内存中而言,这需要一些管理,但这是一个非常常见的任务,应该在任何程序员的技巧之内。在输入和处理数据时,将其保存到输出缓冲区阵列,并在输出缓冲区上跟踪进度。仅在此缓冲区就绪时才写入输出流。

步骤概述:读取字节,转换为PCM,进行音高转换,如果准备输出完整的缓冲区:将PCM转换回字节,写入字节。

相关问题