PCM
PCM(Pulse Code Modulation),脉冲编码调制。人耳听到的是模拟信号,PCM是把声音从模拟信号转化为数字信号的技术
采样
模拟信号的波形是无限光滑的,可以看成由无数个点组成,由于存储空间是相对有限的,数字编码过程中,必须要对波形的点进行采样。采样(Sampling):每隔一段时间采集一次模拟信号的样本,是一个在时间上将模拟信号离散化(把连续信号转换成离散信号)的过程。
采样率
每秒采集的样本数量,称为采样率(采样频率,采样速率,Sampling Rate)。比如,采样率44.1kHz表示1秒钟采集44100个样本。
量化
量化(Quantization):将每一个采样点的样本值数字化。
位深度
位深度(采样精度,采样大小,Bit Depth):使用多少个二进制位来存储一个采样点的样本值。位深度越高,表示的振幅越精确。常见的CD采用16bit的位深度,能表示65536(216)个不同的值。
编码
编码:将采样和量化后的数字数据转成二进制码流。
其他概念
声道(Channel)
单声道产生一组声波数据,双声道(立体声)产生两组声波数据。
采样率44.1kHZ、位深度16bit的1分钟立体声PCM数据有多大?
- 采样率 * 位深度 * 声道数 * 时间
- 44100 * 16 * 2 * 60 / 8 ≈ 10.34MB
比特率
比特率(Bit Rate),指单位时间内传输或处理的比特数量,单位是:比特每秒(bit/s或bps),还有:千比特每秒(Kbit/s或Kbps)、兆比特每秒(Mbit/s或Mbps)、吉比特每秒(Gbit/s或Gbps)、太比特每秒(Tbit/s或Tbps)。
采样率44.1kHZ、位深度16bit的立体声PCM数据的比特率是多少?
- 采样率 * 位深度 * 声道数
- 44100 * 16 * 2 = 1411.2Kbps
通常,采样率、位深度越高,数字化音频的质量就越好。从比特率的计算公式可以看得出来:比特率越高,数字化音频的质量就越好。
AudioRecord
https://developer.android.com/reference/android/media/AudioRecord?hl=en
public class MicRecord extends Thread{
AudioRecord audioRecord;
volatile boolean canPlay = true;
@Override
public void run() {
final int recordbuffsize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_DEFAULT,
AudioFormat.ENCODING_PCM_16BIT);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, recordbuffsize);
audioRecord.startRecording();
byte[] recordData = new byte[recordbuffsize];
while(canPlay){
int readSize = audioRecord.read(recordData, 0, recordbuffsize);
}
audioRecord.stop();
audioRecord.release();
}
public void stopRecord(){
canPlay = false;
}
}
WAV
WAV(Waveform Audio File Format),是由IBM和Microsoft开发的音频文件格式,扩展名是.wav,通常采用PCM编码,常用于Windows系统中。
WAV的文件格式如下图所示,前面有44个字节的文件头,紧跟在后面的就是音频数据(比如PCM数据)。
根据Wav文件格式,PcmToWav工具类,写44个字节文件头。(注意为小端字节序)
import android.media.AudioFormat;
import android.media.AudioRecord;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class PcmToWavUtil {
private int mBufferSize; //缓存的音频大小
private int mSampleRate = 8000;// 8000|16000
private int mChannelConfig = AudioFormat.CHANNEL_IN_STEREO; //立体声
private int mChannelCount = 2;
private int mEncoding = AudioFormat.ENCODING_PCM_16BIT;
public PcmToWavUtil() {
this.mBufferSize = AudioRecord.getMinBufferSize(mSampleRate, mChannelConfig, mEncoding);
}
/**
* @param sampleRate sample rate、采样率
* @param channelConfig channel、声道
* @param encoding Audio data format、音频格式
*/
public PcmToWavUtil(int sampleRate, int channelConfig, int channelCount, int encoding) {
this.mSampleRate = sampleRate;
this.mChannelConfig = channelConfig;
this.mChannelCount = channelCount;
this.mEncoding = encoding;
this.mBufferSize = AudioRecord.getMinBufferSize(mSampleRate, mChannelConfig, mEncoding);
}
/**
* pcm文件转wav文件
*
* @param inFilename 源文件路径
* @param outFilename 目标文件路径
*/
public void pcmToWav(String inFilename, String outFilename) {
FileInputStream in;
FileOutputStream out;
long totalAudioLen;
long totalDataLen;
long longSampleRate = mSampleRate;
int channels = mChannelCount;
long byteRate = 16 * mSampleRate * channels / 8;
byte[] data = new byte[mBufferSize];
try {
in = new FileInputStream(inFilename);
out = new FileOutputStream(outFilename);
totalAudioLen = in.getChannel().size();
totalDataLen = totalAudioLen + 36;//44-8(RIFF+dadasize(4个字节))
writeWaveFileHeader(out, totalAudioLen, totalDataLen,
longSampleRate, channels, byteRate);
while (in.read(data) != -1) {
out.write(data);
}
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 加入wav文件头
*/
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'; //WAVE
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;
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) (2 * 16 / 8); // block align
header[33] = 0;
header[34] = 16; // bits per sample
header[35] = 0;
header[36] = 'd'; //data
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);
}
}
命令行
通过下面的命令可以将PCM转成WAV。
$ ffmpeg -ar 44100 -ac 2 -f s16le -i in.pcm out.wav
需要注意的是:上面命令生成的WAV文件头有78字节。对比44字节的文件头,它多增加了一个34字节大小的LIST chunk。
关于LIST chunk的参考资料:
加上一个输出文件参数-bitexact可以去掉LIST Chunk。
$ ffmpeg -ar 44100 -ac 2 -f s16le -i in.pcm -bitexact out2.wav
Android Demo 代码
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.HandlerThread;
import android.util.Log;
import com.lecture.av.audiorecordwav.util.PcmToWavUtil;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AudioChannel extends Thread {
private int sampleRate;
private int channelConfig;
private int minBufferSize;
private byte[] buffer;
private HandlerThread handlerThread;
private AudioRecord audioRecord;
private boolean isRecoding;
private SimpleDateFormat sdf;
public AudioChannel(int sampleRate, int channels) {
this.sampleRate = sampleRate;
//双通道应该传的值
channelConfig = channels == 2 ? AudioFormat.CHANNEL_IN_STEREO :
AudioFormat.CHANNEL_IN_MONO;
minBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig,
AudioFormat.ENCODING_PCM_16BIT);
Log.i("AudioChannel", "minBufferSize: " + minBufferSize);
buffer = new byte[minBufferSize];
sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd-HH:mm:ss");
}
@Override
public void run() {
//读取麦克风的数据
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleRate, channelConfig,
AudioFormat.ENCODING_PCM_16BIT, minBufferSize);
//开始录音
audioRecord.startRecording();
FileOutputStream writer = null;
Date current = new Date();
String time = sdf.format(current);
try {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
writer = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + time + ".pcm", true);
while (!Thread.currentThread().isInterrupted() && isRecoding) {
if (audioRecord.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
//len实际长度len 打印下这个值
int len = audioRecord.read(buffer, 0, buffer.length);
Log.i("AudioChannel", "len: " + len);
writer.write(buffer);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
audioRecord.stop();
audioRecord.release();
audioRecord = null;
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
new PcmToWavUtil(44100, AudioFormat.CHANNEL_IN_STEREO,
2, AudioFormat.ENCODING_PCM_16BIT).pcmToWav(
Environment.getExternalStorageDirectory() + "/" + time + ".pcm"
, Environment.getExternalStorageDirectory() + "/" + time + ".wav");
Log.i("AudioChannel", "AudioChannel run finish ");
}
public void startLive() {
isRecoding = true;
this.start();
}
public void stopLive() {
isRecoding = false;
try {
join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}