首页 > 解决方案 > 我无法将 32 位浮点数或 24 位带符号的 PCM 数据输出到音频系统

问题描述

我正在尝试编写一个程序,将声音从阵列输出到扬声器。如果可能的话,我希望它支持以下格式。

我使用 Windows 作为我的操作系统。

我可以输出前两个,但其余的都有一个例外

一段时间以来,我一直在使用大部分代码来输出波形文件,但我刚刚开始直接输出到音频系统。

package sound.streamer;

import effects.AdsrEnvelope;
import effects.AmpLimiter;
import effects.Tremulant;
import effects.Vibrato;
import java.nio.ByteBuffer;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import music.note.NoteRange;
import sound.generator.waveforms.*;

/**
 *
 * @author Edward Jenkins
 */
public class SoundOutputter {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        NoteRange noteRange = new NoteRange("C-1", "C9");
        double frequency = noteRange.getFrequency("C4");
        int sampleRate = 44100;
        int bitRate = 32;
        boolean signed;
        boolean isFloat = false;
        Encoding encoding;
        int bytesPerSample = bitRate / 8;
        SourceDataLine sdl;
        byte[] outputBytes;

        if (bitRate == 8) {
            signed = false;
            encoding = new Encoding("PCM_UNSIGNED");
        } else {
            if (isFloat) {
                encoding = new Encoding("PCM_FLOAT");
            } else {
                signed = true;
                encoding = new Encoding("PCM_SIGNED");
            }
        }

        // harmoncis
        double[] harmonicVolumes = {1, 1, 0, 1};

        PwmPulseGenerator asg = new PwmPulseGenerator(frequency, 5);
        AdsrEnvelope ae = new AdsrEnvelope(0.5, 0.3, 0.5, true, 0.8, 1, sampleRate);
        Vibrato v = new Vibrato(4, 0.5, frequency, 1, sampleRate);
        Tremulant t = new Tremulant(7, 0.25, 0.5, sampleRate, false);
        int sampleLength = asg.getSampleLength();
        AmpLimiter al = new AmpLimiter(bitRate, isFloat);
        AudioFormat af = new AudioFormat(encoding, (float) sampleRate, bitRate,
                1, bytesPerSample, sampleRate, true);

        try {
            sdl = AudioSystem.getSourceDataLine(af);

            double point;
            double[] points = new double[sampleLength];
            double pointVolume;

            sdl.open(af, 44100);
            sdl.start();
            
            for (int exportIndex = 0; exportIndex < sampleLength; exportIndex++) {
                if (exportIndex == sampleRate * 4) {
                    ae.setSustain(false);
                }
                //asg.setFrequency(v.getVibratoFrequency(exportIndex));
                point = asg.generateWaveformPoint();
                pointVolume = ae.getADSRvolume(exportIndex);
                //pointVolume *= t.drawTremulantPoint(pointVolume, ae.getSustainStartPoint());
                asg.setAmplitude(pointVolume);
                points[exportIndex] = point;
                points[exportIndex] = al.calculateAmpLimit(points[exportIndex]);
                outputBytes = convertToBytes(bitRate, isFloat, points[exportIndex]);
                sdl.write(outputBytes, 0, outputBytes.length);
            }

            sdl.drain();
            sdl.stop();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }
    
    public static byte[] convertToBytes(int bitRate, boolean isFloat, double point) {

        byte[] output = {64};

        ByteBuffer buffer;

        // switch on bitrate
        switch (bitRate) {
            case 64:
                buffer = ByteBuffer.allocate(8);
                buffer.putDouble(point);
                output = buffer.array();
                break;
            case 32:
                if (isFloat) {
                    buffer = ByteBuffer.allocate(4);
                    buffer.putFloat((float)point);
                    output = buffer.array();
                } else {
                    buffer = ByteBuffer.allocate(4);
                    buffer.putInt((int)point);
                    output = buffer.array();
                }
                break;
            case 16:
                buffer = ByteBuffer.allocate(2);
                    buffer.putShort((short)point);
                    output = buffer.array();
                break;
            case 8:
                point = (int)point + 128;
                output = new byte[1];
                output[0] = (byte)point;
                break;
        }
        return output;
    }

}

标签: javaaudiopcm

解决方案


我很难找到有关支持哪些音频格式的文档。据我所知,Java 不支持 24 位或 32 位格式。也许JavaFX可以?

好的,我找到了这个 Java 8 文档,Java Sound Technology,它说只支持 8 位和 16 位。

我不知道如何自己动手。如果您知道如何编写正确的 wav 标头等,这可能是可能的。上次我查看 wav 规范时,它让我头疼。不会再去那里了。


推荐阅读