音频文件获取左/右声道
Posted
技术标签:
【中文标题】音频文件获取左/右声道【英文标题】:Audio file get left/right channels 【发布时间】:2017-01-01 02:32:09 【问题描述】:经过大量研究,我找不到明确的答案。我想要实现的是:取一个 mp3/wav 文件并将其左右声道保存为 2 byte[]。然后我可以操作每个通道,然后将它们再次添加并创建一个新的 wav 文件。 (最好是Java的解决方案)
相关问题是split two channels of AudioRecord of CHANNEL_IN_STEREO。但是这是使用音频记录,如何将其应用于从音频文件中读取?
谢谢
【问题讨论】:
【参考方案1】:这里是读取 16 位波形文件的程序
示例音频:http://freewavesamples.com/korg-triton-slow-choir-st-c4
16 位立体声 PCM 的数据打包: 示例 1
频道 0 |频道 0 |频道 1 |频道 1
(左)| (左)| (右) | (右)
低阶 |高阶|低阶 |高阶
字节 |字节 |字节 |字节
并以 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();
【讨论】:
以上是关于音频文件获取左/右声道的主要内容,如果未能解决你的问题,请参考以下文章