Java pcm格式的音頻實現8位和16位互轉

  //將16位pcm數據轉換成8位有符號的pcm

                byte[] readBuffer = new byte[4096];
                byte[] sendBuffer = new byte[readBuffer.length / 2];
                for (int i = 0; i<readBuffer.length; i += 2) {
                    if ((readBuffer[i + 1] & 0x80) == 0x80) {
                        sendBuffer[i / 2] = (byte) (readBuffer[i + 1] & 0x7f);
                    } else {
                        sendBuffer[i / 2] = (byte) (readBuffer[i + 1] + 0x80);
                    }

                }

 

                //將8位有符號的的pcm數據轉換成16位

                byte[] readBuffer = new byte[4096];
                int audioDataLen = readBuffer.length * 2;
                byte[] audioBuffer = new byte[readBuffer.length * 2];
                
                for (int i = 0; i<readBuffer.length; i++) {
                    /*if (readBuffer[i] == 63 && i != 0 && i != readBuffer.length - 1) {
                        readBuffer[i] = (byte) ((readBuffer[i + 1] + readBuffer[i - 1]) / 2);
                    }*/
                    if ((readBuffer[i] & 0x80) == 0x80) {
                        audioBuffer[2 * i] = 0x00;
                        audioBuffer[2 * i + 1] = (byte) (readBuffer[i] - 0x80);
                    } else {
                        audioBuffer[2 * i] = (byte) 0xff;
                        audioBuffer[2 * i + 1] = (byte) (readBuffer[i] - 0x80);
                    }
                }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章