在尝试从麦克风获取数据时,每个样本都输出平坦的零

时间:2012-05-26 15:58:50

标签: java javasound synthesizer

我正在基于Dobb博士的教程内置到合成器中的java中制作声码器,每当我尝试从麦克风获取样本时,即使有麦克风输入,它也会为样本返回零。 这是AudioFormat的设置:

// AudioFormat parameters
public  static final int     SAMPLE_RATE = 22050;
private static final int     SAMPLE_SIZE = 16;
private static final int     CHANNELS = 1;
private static final boolean SIGNED = true;
private static final boolean BIG_ENDIAN = true;
// Chunk of audio processed at one time
public static final int BUFFER_SIZE = 1000;
public static final int SAMPLES_PER_BUFFER = BUFFER_SIZE / 2;    

VoiceProvider(麦克风样本提供课程):

public class VoiceProvider implements ISynthProvider{
    private TargetDataLine line;
    public VoiceProvider(){
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, 
            Player.format); // format is an AudioFormat object
        if (!AudioSystem.isLineSupported(info)) {
            System.out.println("o_0 line not supported!");

        }

        // Obtain and open the line.
        try {
            line = (TargetDataLine) AudioSystem.getLine(info);
            line.start();
            line.open(Player.format);

        } catch (LineUnavailableException ex) {
            // Handle the error ... 
        }

    }
    @Override
    public double getSample() {
        byte[] a = {0,0}; 
        if (line.isOpen()){
            //System.out.println("0_0");
            line.read(a, 0, 2);
        }else{
            //System.out.println("o_0");
        }
        System.out.println(Arrays.toString(a));
        return readShort(a,0);

    }
    public static short readShort(byte[] data, int offset) {
    return (short) (((data[offset] << 8)) | ((data[offset + 1] & 0xff)));
    }

1 个答案:

答案 0 :(得分:1)

try {
        line = (TargetDataLine) AudioSystem.getLine(info);
        line.start();
        line.open(Player.format);

    } catch (LineUnavailableException ex) {
        // Handle the error ... 
    }

应该是:

try {
        line = (TargetDataLine) AudioSystem.getLine(info);
        line.open(Player.format);
        line.start();

    } catch (LineUnavailableException ex) {
        // Handle the error ... 
    }