Audio file get left/right channels

2019-02-28 09:52发布

After a lot of research, I can't find a clear answer. What I want to achieve is: take an mp3/wav file and save its left and right channels to 2 byte[]. Then I can manipulate each channel and then add them up again and create a new wav file. (preferably the solution to be in Java)

Related question is split two channels of AudioRecord of CHANNEL_IN_STEREO. However this is using audiorecord, how to apply it to reading from audio file?

Thanks

1条回答
Evening l夕情丶
2楼-- · 2019-02-28 10:20

Well here is the program to read a 16 bit wave file

Sample Audio: http://freewavesamples.com/korg-triton-slow-choir-st-c4

Data Packing for 16-Bit Stereo PCM: Sample 1


Channel 0 | Channel 0 | Channel 1 | Channel 1

(left) | (left) | (right) | (right)

low-order | high-order | low-order | high-order

byte | byte | byte | byte


And printing it in CSV format. I will not put the CSV writer code here. Its already available here.

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();
        }
    }
}
查看更多
登录 后发表回答