平臺
RK3288 + Android 7.1
開發
在開始之前需要先了解下當前的音頻設備情況:
- 查看當前支持的聲卡設備(找到card[0, N])
rk3288:/proc/asound # ll
total 0
lrwxrwxrwx 1 root root 5 2020-04-15 13:51 Camera -> card1
dr-xr-xr-x 5 root root 0 2020-04-15 13:51 card0
dr-xr-xr-x 3 root root 0 2020-04-15 13:51 card1
-r--r--r-- 1 root root 0 2020-04-15 13:51 cards
-r--r--r-- 1 root root 0 2020-04-15 13:51 devices
-r--r--r-- 1 root root 0 2020-04-15 13:51 hwdep
-r--r--r-- 1 root root 0 2020-04-15 13:51 pcm
lrwxrwxrwx 1 root root 5 2020-04-15 13:51 rockchipes8316c -> card0
-r--r--r-- 1 root root 0 2020-04-15 13:51 timers
-r--r--r-- 1 root root 0 2020-04-15 13:51 version
- 查看cards中的內容:
rk3288:/proc/asound # cat cards
0 [rockchipes8316c]: rockchip_es8316 - rockchip,es8316-codec
rockchip,es8316-codec
1 [Camera ]: USB-Audio - USB Camera
Generic USB Camera at usb-ff540000.usb-1.2, high speed
- 當前使用的聲卡信息
rk3288:/proc/asound # tinypcminfo -D 0
Info for card 0, device 0:
PCM out:
Access: 0x000009
Format[0]: 0x000044
Format[1]: 00000000
Format Name: S16_LE, S24_LE
Subformat: 0x000001
Rate: min=8000Hz max=96000Hz
Channels: min=2 max=2
Sample bits: min=16 max=32
Period size: min=32 max=65536
Period count: min=2 max=4096
PCM in:
Access: 0x000009
Format[0]: 0x000044
Format[1]: 00000000
Format Name: S16_LE, S24_LE
Subformat: 0x000001
Rate: min=8000Hz max=96000Hz
Channels: min=2 max=2
Sample bits: min=16 max=32
Period size: min=32 max=65536
Period count: min=2 max=4096
- 採集API:AudioRecord
//audioSource: MediaRecorder.AudioSource.CAMCORDER, MediaRecorder.AudioSource.MIC 等
// 一般情況下會用到這兩個
//sampleRateInHz: 常用的有: 8000,11025,16000,22050,44100,96000;
//channelConfig: AudioFormat.CHANNEL_CONFIGURATION_DEFAULT, 單聲道和立聲
//audioFormat: AudioFormat.ENCODING_PCM_16BIT, 測試中要捕獲PCM數據, 其它參數未研究.
//bufferSizeInBytes: 由AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat)獲得
public AudioRecord(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat,
int bufferSizeInBytes)
- 播放API: AudioTrack
//streamType: AudioManager.STREAM_MUSIC, 輸出通道, 也可以選擇ALARM等
//sampleRateInHz: 常用的有: 8000,11025,16000,22050,44100,96000;
//channelConfig: AudioFormat.CHANNEL_CONFIGURATION_DEFAULT, 單聲道和立聲
//audioFormat: AudioFormat.ENCODING_PCM_16BIT, 測試中要捕獲PCM數據, 其它參數未研究.
//bufferSizeInBytes:
//mode: AudioTrack中有MODE_STATIC和MODE_STREAM兩種分類。
// STREAM的意思是由用戶在應用程序通過write方式把數據一次一次得寫到audiotrack中。
// 這個和我們在socket中發送數據一樣,應用層從某個地方獲取數據,例如通過編解碼得到PCM數據,然後write到audiotrack。
// 這種方式的壞處就是總是在JAVA層和Native層交互,效率損失較大。
// 而STATIC的意思是一開始創建的時候,就把音頻數據放到一個固定的buffer,然後直接傳給audiotrack,
// 後續就不用一次次得write了。AudioTrack會自己播放這個buffer中的數據。
// 這種方法對於鈴聲等內存佔用較小,延時要求較高的聲音來說很適用。
public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat,
int bufferSizeInBytes, int mode)
- 邊採集邊播放PCM代碼(僅核心代碼, 不完整)
//Audio Parameter
static class AudioParam{
static final int FREQ_8000 = 8000;
static final int FREQ_11025 = 11025;
static final int FREQ_16000 = 16000;
static final int FREQ_22050 = 22050;
static final int FREQ_44100 = 44100;
static final int FREQ_96000 = 96000;
int device;
int channel;
int bitFormat;
int buffSize;
//int doubleBuffSize;
int buffSizeOut;
int rate;
public AudioParam(int device, int rate, int channel, int bitFormat){
this.device = device;
this.rate = rate;
this.channel = channel;
this.bitFormat = bitFormat;
initBufferSize();
}
private void initBufferSize() {
buffSize = AudioRecord.getMinBufferSize(rate, channel, bitFormat);
buffSizeOut = AudioTrack.getMinBufferSize(rate, channel, bitFormat);
Logger.d("AudioPCM", "buffSize(" + buffSize + "), buffSizeOut(" + buffSizeOut + ")");
if(buffSizeOut < 0)buffSizeOut = buffSize;
//doubleBuffSize = buffSize * 2;
}
static AudioParam getDefaultInParam(){
return new AudioParam(MediaRecorder.AudioSource.MIC,
FREQ_44100,
AudioFormat.CHANNEL_CONFIGURATION_DEFAULT,
AudioFormat.ENCODING_PCM_16BIT);
}
static AudioParam getDefaultOutParam(){
return new AudioParam(AudioManager.STREAM_MUSIC,
FREQ_44100,
AudioFormat.CHANNEL_CONFIGURATION_DEFAULT,
AudioFormat.ENCODING_PCM_16BIT);
}
}
//Thread for capture audio
class CaptureThread extends Thread{
@Override
public void run() {
AudioRecord mic = new AudioRecord(audioParamIn.device, audioParamIn.rate, audioParamIn.channel,
audioParamIn.bitFormat, audioParamIn.buffSize);
mic.startRecording();
byte[] pcmBuffer = new byte[2048];
while (!Thread.interrupted()) {
int size = mic.read(pcmBuffer, 0, pcmBuffer.length);
Logger.d(TAG, "read " + size + " bytes");
if (size <= 0) {
break;
}else{
if(playThd != null){
playThd.write(pcmBuffer, size);
}
}
}
mic.stop();
mic.release();
}
}
//Thread for play
class PlayThread{
private AudioTrack mAudioTrack;
PlayThread(){
try {
audioParamOut = new AudioParam(AudioManager.STREAM_MUSIC,
AudioParam.FREQ_44100,
AudioFormat.CHANNEL_CONFIGURATION_DEFAULT,
AudioFormat.ENCODING_PCM_16BIT);
createAudioTrack();
mAudioTrack.play();
} catch (Exception e) {
e.printStackTrace();
}
}
void write(byte[] bs, int size){
Logger.d(TAG, "write " + size + " bytes");
if(mAudioTrack != null){
mAudioTrack.write(bs, 0, size);
}
}
void stop(){
if(mAudioTrack != null){
mAudioTrack.stop();
}
}
private void createAudioTrack() throws Exception{
// STREAM_ALARM:警告聲
// STREAM_MUSCI:音樂聲,例如music等
// STREAM_RING:鈴聲
// STREAM_SYSTEM:系統聲音
// STREAM_VOCIE_CALL:電話聲音
mAudioTrack = new AudioTrack(audioParamOut.device,
audioParamOut.rate,
audioParamOut.channel,
audioParamOut.bitFormat,
audioParamOut.buffSizeOut,
AudioTrack.MODE_STREAM);
}
}
擴展
- 需要確定能正常的識別到USB 攝像頭的MIC輸入設備(這個過程走了許多彎路)
- 在測試過程中發現 MediaRecorder.AudioSource.CAMCORDER, MediaRecorder.AudioSource.MIC對應的設備並不是固定的
- 當未接USB攝像頭的時候 MediaRecorder.AudioSource.MIC 對應的是 主板的MIC
- 當接上USB攝像頭後, MediaRecorder.AudioSource.MIC 對應的是USB攝像頭上的MIC.