怎么玩.WAV倒退?

时间:2016-03-06 19:30:19

标签: android

我有.WAV文件。我需要向后玩。我怎么能这样做?我从文件中读取InputStream数据,然后将其转换为字节数组。反转此数组,然后将此数组写入另一个文件?错误的逻辑在哪里?

      public void copyWaveFile(String inFilename,String outFilename){
                    FileInputStream in = null;
                    FileOutputStream out = null;
                    long totalAudioLen = 0;`enter code here`
                    long totalDataLen = totalAudioLen + 36;
                    long longSampleRate = frequency;
                    int channels = 1;
                    long byteRate = RECORDER_BPP * frequency * channels/8;
                    byte[] data = new byte[recBufSize];

                    try {
                        in = new FileInputStream(inFilename);
                        out = new FileOutputStream(outFilename);
                        totalAudioLen = in.getChannel().size();
                        totalDataLen = totalAudioLen + 36;

                       // Why this is don't work?
                        data = convertStreamToByteArray(in,recBufSize);
                        byte[] reverseData = reverseByteArray(data);
                        out.write(reverseData);

                        WriteWaveFileHeader(out, totalAudioLen, totalDataLen, longSampleRate, channels, byteRate);

                        in.close();
                        out.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

         // convert InputStream to byte array   
         public static byte[] convertStreamToByteArray(InputStream is,int size) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[size];
                byte[] result = null;
                int b;
                try {
                    while ((b = is.read(buffer, 0, buffer.length)) != -1) {
                        baos.write(buffer, 0, b);
                    }
                    result = baos.toByteArray();

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        baos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return result;
            }

            // reverse byte array
            private byte[] reverseByteArray(byte[] arr) {
                int i = 0, j = arr.length-1;
                byte tmp;
                while(i<j) {
                    tmp    = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tmp;
                    i++;
                    j--;
                }
                return arr;
            }

        // header for wav file`enter code here
        private void WriteWaveFileHeader(
                FileOutputStream out, long totalAudioLen,
                long totalDataLen, long longSampleRate, int channels,
                long byteRate) throws IOException {

            byte[] header = new byte[44];

            header[0] = 'R';  // RIFF/WAVE header
            header[1] = 'I';
            header[2] = 'F';
            header[3] = 'F';
            header[4] = (byte) (totalDataLen & 0xff);
            header[5] = (byte) ((totalDataLen >> 8) & 0xff);
            header[6] = (byte) ((totalDataLen >> 16) & 0xff);
            header[7] = (byte) ((totalDataLen >> 24) & 0xff);
            header[8] = 'W';
            header[9] = 'A';
            header[10] = 'V';
            header[11] = 'E';
            header[12] = 'f';  // 'fmt ' chunk
            header[13] = 'm';
            header[14] = 't';
            header[15] = ' ';
            header[16] = 16;  // 4 bytes: size of 'fmt ' chunk
            header[17] = 0;
            header[18] = 0;
            header[19] = 0;
            header[20] = 1;  // format = 1
            header[21] = 0;`enter code here`
            header[22] = (byte) channels;
            header[23] = 0;
            header[24] = (byte) (longSampleRate & 0xff);
            header[25] = (byte) ((longSampleRate >> 8) & 0xff);
            header[26] = (byte) ((longSampleRate >> 16) & 0xff);
            header[27] = (byte) ((longSampleRate >> 24) & 0xff);
            header[28] = (byte) (byteRate & 0xff);
            header[29] = (byte) ((byteRate >> 8) & 0xff);
            header[30] = (byte) ((byteRate >> 16) & 0xff);
            header[31] = (byte) ((byteRate >> 24) & 0xff);
            header[32] = (byte) (1 * 16 / 8);  // block align
            header[33] = 0;
            header[34] = RECORDER_BPP;  // bits per sample
            header[35] = 0;
            header[36] = 'd';
            header[37] = 'a';
            header[38] = 't';
            header[39] = 'a';
            header[40] = (byte) (totalAudioLen & 0xff);
            header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
            header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
            header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
            out.write(header, 0, 44);
        }

1 个答案:

答案 0 :(得分:0)

这取决于比特率。但一般来说,你不能只重新排序字节。 wave文件具有逻辑格式,必须保留该格式才能再次播放该文件。例如,对于具有16位的PCM编码波形文件,我试图反转16位块。所以最后的16位块首先没有重新排序字节。想一想,我怀疑这可行。必须有一种标题,必须在开头保留或重写,也许有一些字节表示音频数据的结束。

这应该有所帮助,但它并不像你想象的那么简单。 RIFF WAVE

也许有一个Java库?