1. 程式人生 > >alsa音訊播放過程中的基本概念

alsa音訊播放過程中的基本概念

以下為 ALSA-Project/FramesPeriods[1] 學習筆記

1, sample_rate: 即每秒進行多少次取樣,常見的比如 8000、16000、44100和48000等
2, sample_bits: 即每次取樣多少個bit,多是 16bit。其他常見有 24bits、32bits等
3, channels: 通道數,即單聲道mono(1), 立體聲stero(2), 1.2(3), 或是1.5(6)等
4, sample_format: 每次取樣結果的表示格式, 主要區分bit位數和大小端, 如 S8、S16LE、S16BE、S24LE、S24BE、S32LE、S32BE等
5, period_size: 每次相鄰中斷之間處理的sample個數,處理完一個period,音效卡硬體產生一箇中斷
period_count: 每次最大可提交的period的個數。
因為alsa內部使用ringbuffer,period_size * period_count 決定了ring_buffer的大小。

以上引數由音訊輸出硬體決定,同一個音效卡可能掛接多個音訊輸出裝置(比如HDMI、3.5寸耳機輸出),其輸出的音訊引數規格各不相同。比如樹莓配一代支援的音訊輸出規格如下

[email protected]:~ $ tinypcminfo -D 0 -d 0
Info for card 0, device 0:

PCM out:
      Access:   0x000009
   Format[0]:   0x000006
   Format[1]:   00000000
 Format Name:   U8, S16_LE
   Subformat:   0x000001
        Rate:   min=8000Hz  max=48000Hz
    Channels:   min=1       max=2
 Sample bits:   min=8       max=16
 Period size:   min=256     max=65536
Period count:   min=1       max=128

PCM in:
cannot open device '/dev/snd/pcmC0D0c'
Device does not exist.
[email protected]
:~ $ tinypcminfo -D 0 -d 1 Info for card 0, device 1: PCM out: Access: 0x000009 Format[0]: 0x000004 Format[1]: 00000000 Format Name: S16_LE Subformat: 0x000001 Rate: min=44100Hz max=48000Hz Channels: min=2 max=2 Sample bits: min=16 max=16 Period size: min=256 max=16384 Period count: min=1 max=64 PCM in: cannot open device '/dev/snd/pcmC0D1c' Device does not exist.
[email protected]
:~ $

因而應用再實現音訊播放時,需要根據實際情況選擇那個輸出通道,並且根據其實際輸出規格要求,提供對應的PCM資料。若原始直接的音訊資料(比如解碼輸出),和所選的音訊輸出通道的規格要求不一致,則需要做轉換處理

[1] https://www.alsa-project.org/main/index.php/FramesPeriods