Pregunta

I'm trying to write a custom synthesizer in Java on a Ubuntu 11.10 laptop. Here is my code:

package com.sibbo.audio;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;

public class Audio {
    public static void main(String[] args) throws LineUnavailableException {
        byte[] data = new byte[16000 * 2];

        sinus(data);

        AudioFormat format = new AudioFormat(16000, 8, 1, true, true);
        Clip c = javax.sound.sampled.AudioSystem.getClip();
        c.open(format, data, 0, data.length); // throws IllegalArgumentException
    }

    private static void sinus(byte[] data) {
        for (int i = 0; i < data.length; i++) {
            data[i] = (byte) (Math.sin(i / 200.0) * 127);
        }
    }
}

At the marked line, it throws an Exception, saying: "Invalid format". How can I figure out which AudioFormats are legal? What I already tried is switching the boolean values for signed/unsigned and little/big endian.

¿Fue útil?

Solución

I think the error message is meant the other way around. It is not complaining about the AudioFormat not being supported by the system, but instead it cannot parse your data into this format. From the Javadoc:

Throws:
IllegalArgumentException - if the buffer size does not represent an integral number of sample frames, or if format is not fully specified or invalid

Otros consejos

I'm having the same problem, a year later. I figured out something, though; under the assumption that c.getInfo() returns a DataLine.Info, the following gives the supported formats:

((DataLine.Info)clip.getLineInfo()).getFormats()

For some reason, mine only has one supported format, and it's kinda weird. Also, I followed the code, and using an AudioFormat not in the supported list does give you the error "Invalid format."

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top