如何将字节数组从wav文件更改为音频数据的短数组

时间:2017-07-20 10:33:34

标签: java android

此代码的目的是从wav文件中提取字节数组,并将该字节数组更改为音频数据,然后我可以使用该数据进行信号处理。代码似乎存在问题,将字节解析为短路。谁能告诉我我做错了什么?

以下是代码:

buttonDecode.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        buttonStart.setEnabled(false);
        buttonDecode.setEnabled(false);
        buttonPlay.setEnabled(false);

        GraphView thegraph = (GraphView) findViewById(R.id.thegraph);
        series = new LineGraphSeries<DataPoint>();
        double x,y;
        x = 0;

        try {
            //old wav byte array code

            ByteArrayOutputStream outt = new ByteArrayOutputStream();
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(wavfile));

            int read;
            byte[] buff = new byte[RECORDER_SAMPLERATE]; //was 1024
            in.skip(44);
            while ((read = in.read(buff)) > 0)
            {
                outt.write(buff, 0, read);
            }
            outt.flush();
            byte[] audioBytes = outt.toByteArray();
            int[] theRealData = null;

            //parsing the vector
            for(int i = 0; i < ((audioBytes.length)/2) ; i++) {
                byte highByte = audioBytes[(2*i)+1];
                byte lowByte = audioBytes[(2*i)];
                short value = twoBytesToShort(highByte,lowByte);
                theRealData[(i+1)/2] = value;
            }

            for(int i = 0; i < theRealData.length;i++){
                int curByte = theRealData[i];
                y = (curByte);
                series.appendData(new DataPoint(x,y), true, theRealData.length);
                x = x + 1;
            }
            thegraph.addSeries(series);

        } catch (IOException e) {
            e.printStackTrace();
        }

        buttonStart.setEnabled(true);
        buttonDecode.setEnabled(true);
        buttonPlay.setEnabled(true);

    }
});

功能:

public static short twoBytesToShort(byte b1, byte b2) {
        return (short) ((b1 << 8) | (b2 & 0xFF));
    }

0 个答案:

没有答案