Search code examples
javaaudiopcm

I am not able to output 32 bit float or 24 bit signed PCM data to the audio system


I am trying to write a program that outputs sound from an array to the speakers. I would like it to support the following formats if possible.

I am using Windows as my OS.

  • Unsigned 8-bit PCM
  • Signed 16-bit PCM
  • Signed 24-bit PCM
  • Signed 32-bit Float PCM
  • Signed 32-bit PCM and
  • Signed 64-bit Float PCM

I can output the first two, but there is an exception with all of the rest

I have been using the majority of this code for outputting wave files for some time, but I have just started outputting directly to the audio system.

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

}


Solution

  • I've had trouble finding documentation on just what audio formats are supported. As far as I know, Java doesn't support 24 or 32 bit formats. Maybe JavaFX does?

    OK, I found this Java 8 document, Java Sound Technology that says only 8-bit and 16-bit is supported.

    I haven't a clue as to how to go about rolling your own. It might be possible if you knew how to write the correct wav headers and such. The last time I looked at the wav spec it gave me a headache. Not going there again.