音频文件获得左/右声道

时间:2016-08-24 19:58:27

标签: java android audio-processing

经过大量的研究,我找不到明确的答案。我想要实现的是:获取一个mp3 / wav文件并将其左右声道保存为2字节[]。然后我可以操作每个通道,然后再次添加它们并创建一个新的wav文件。 (最好用Java解决方案)

相关问题是split two channels of AudioRecord of CHANNEL_IN_STEREO。然而,这是使用audiorecord,如何将其应用于从音频文件中读取?

由于

1 个答案:

答案 0 :(得分:1)

这里是读取16位波形文件的程序

示例音频:http://freewavesamples.com/korg-triton-slow-choir-st-c4

16位立体声PCM的数据打包: 样本1

频道0 |频道0 |第1频道|第1频道

(左)| (左)| (右)| (右)

低阶|高阶|低阶|高阶

byte | byte | byte |字节

以CSV格式打印。我不会在这里放置CSV编写器代码。它已在这里可用。

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;

public class AudioReader {

    class WavHeader {
        public byte[] RIFF_ID = new byte[4]; // "riff"
        public int SIZE; //
        public byte[] WAV_ID = new byte[4]; // "WAVE"
        public byte[] FMT_ID = new byte[4]; // "fmt id"
        public int FMT_SZ; // fmt
        public int FORMAT; //
        public short CHANNELS; // Channels
        public int SAMPLE_PER_SEC; // Sample per second
        public int AVGBYTE_PER_SEC; // Average Byte per second
        public short BLOCK_SZ; // CHANNELS * (BIT>>3)
        public short BIT; // BITS
        public byte[] DATA_ID = new byte[4]; // "data"
        public int DATA_SZ; //
    }

    String m_strFileName;
    WavHeader m_objHeader;

    public AudioReader(String strFileName) {
        m_strFileName = strFileName;
        m_objHeader = new WavHeader();
    }

    private void printHeaderBigEndian(int nSamples) {
        try {
            // 1
            String str1 = new String(m_objHeader.RIFF_ID, "ISO-8859-1");
            System.out.println("Riff Id: " + str1);
            // 2
            System.out.println("Size: " + m_objHeader.SIZE);
            // 3
            String str2 = new String(m_objHeader.WAV_ID, "ISO-8859-1");
            System.out.println("Wav Id: " + str2);
            // 3
            String str3 = new String(m_objHeader.FMT_ID, "ISO-8859-1");
            System.out.println("Fmt Id: " + str3);
            // 4
            System.out.println("Format: " + m_objHeader.FORMAT);
            // 5
            System.out.println("Channels: " + m_objHeader.CHANNELS);
            // 6
            System.out.println("Sample per sec: " + m_objHeader.SAMPLE_PER_SEC);
            // 7
            System.out.println("Avg Byte per sec: "
                    + m_objHeader.AVGBYTE_PER_SEC);
            // 8
            System.out.println("Block Sz: " + m_objHeader.BLOCK_SZ);
            // 9
            System.out.println("Bit: " + m_objHeader.BIT);
            // 10
            String str4 = new String(m_objHeader.DATA_ID, "ISO-8859-1");
            System.out.println("Data Id: " + str4);
            // 11
            System.out.println("Data Sz: " + m_objHeader.DATA_SZ);
            // 12
            System.out.println("Samples: " + nSamples);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // ////////////////////////////////////////////////////////////////////////////////////
    //
    public void ReadUsingDataInputStream() {
        try {
            File in = new File(m_strFileName);
            FileInputStream fis = new FileInputStream(in);
            DataInputStream dis = new DataInputStream(fis);

            dis.read(m_objHeader.RIFF_ID, 0, 4);
            m_objHeader.SIZE = Integer.reverseBytes(dis.readInt());
            dis.read(m_objHeader.WAV_ID, 0, 4);
            dis.read(m_objHeader.FMT_ID, 0, 4);
            m_objHeader.FMT_SZ = Integer.reverseBytes(dis.readInt());
            m_objHeader.FORMAT = Short.reverseBytes(dis.readShort());
            m_objHeader.CHANNELS = Short.reverseBytes(dis.readShort());
            m_objHeader.SAMPLE_PER_SEC = Integer.reverseBytes(dis.readInt());
            m_objHeader.AVGBYTE_PER_SEC = Integer.reverseBytes(dis.readInt());
            m_objHeader.BLOCK_SZ = Short.reverseBytes(dis.readShort());
            m_objHeader.BIT = Short.reverseBytes(dis.readShort());
            dis.read(m_objHeader.DATA_ID, 0, 4);
            m_objHeader.DATA_SZ = Integer.reverseBytes(dis.readInt());

            int nSamples = m_objHeader.DATA_SZ / m_objHeader.BLOCK_SZ;
            printHeaderBigEndian(nSamples);

            String strCsvFileName = in.getParent() + "//" + in.getName()
                    + ".csv";

            String strLine[] = new String[3];

            File out = new File(strCsvFileName);
            out.createNewFile();
            FileWriter fw = new FileWriter(out);
            CSVWriter csvW = new CSVWriter(fw);

            for (int n = 1; n < nSamples; n++) {
                strLine[0] = Integer.toString(n);
                strLine[1] = Short.toString(Short.reverseBytes(dis.readShort()));
                strLine[2] = Short.toString(Short.reverseBytes(dis.readShort()));
                csvW.writeNext(strLine);
            }

            dis.close();
            csvW.Close();

            System.out.println("Done.....");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}