ALSA lib基本概念


1.channel

通道,即我們熟知的聲道數。左/右聲道,5.1channel等等

2.sample

A sample is a single value that describes the amplitude of the audio signal at a single point in time, on a single channel.

sample即一次采樣,通常的sample bit指的是一個channnel上,一次采樣的bit數(常見的sample bit 8/16/24/32bits)

3.frame

When we talk about working with digital audio, we often want to talk about the data that represents all channels at a single point in time. This is a collection of samples, one per channel, and is generally called a "frame". When we talk about the passage of time in terms of frames, its roughly equivalent to what people when they measure in terms of samples, but is more accurate; more importantly, when we're talking about the amount of data needed to represent all the channels at a point in time, its the only unit that makes sense.

一個frame是一次采樣時所有channel上的sample bit.即frame = channels * (sample bit).

4.peroid

A period is the number of frames in between each hardware interrupt.

每當hardware buffer 中有peroid size個frame的空間時,硬件就產生中斷,來通知alsa driver來往硬件寫數據。

5.buffer size

This determines how large the hardware buffer is.

hardware buffer size 是由多個peroid組成。buffer size = peroid size * peroids。

 

6.Data access and layout

interleaveda: data layout arrangement where the samples of each channel that will be played at the same time follow each other sequentially. See "non-interleaved"

non-interleaveda: data layout where the samples for a single channel follow each other sequentially; samples for another channel are either in another buffer or another part of this buffer. Contrast with "interleaved"

在一個period以內(interleaved和non-interleaved是在一個period里面排),數據是按照channel1排完了再排channel2呢,還是一個frame一個frame的來排(frame在alsa里指的是一次采樣時間內,兩個channel的數據放一塊兒就是一個frame)上圖是interleaved。

 7.Hardware parameter

These are parameters that directly affect the hardware of the audio interface.

 Hardware parameter是作用於聲卡硬件的,包括sample rate, sample format, interupt intervals, data access and layout, buffer size.

8.Software parameter

These are parameters that control the operation of the device driver rather than the hardware itself. Most programs that use the ALSA Audio API will not need to set any of these; a few will need set a subset of them.

software parameter 是作用於alsa core的。通常用來控制When to start the device, what to do about xruns, Available minimum space/data for wakeup,Transfer chunk size.

8.1 When to start the device

When you open the audio interface, ALSA ensures that it is not active - no data is being moved to or from its external connectors. Presumably, at some point you want this data transfer to begin. There are several options for how to make this happen.

 The control point here the start threshold, which defines the number of frames of space/data necessary to start the device automatically. If set to some value other than zero for playback, it is necessary to prefill the playback buffer before the device will start. If set to zero, the first data written to the device (or first attempt to read from a capture stream) will start the device.

 You can also start the device explicitly using snd_pcm_start, but this requires buffer prefilling in the case of the playback stream. If you attempt to start the stream without doing this, you will get -EPIPE as a return code, indicating that there is no data waiting to deliver to the playback hardware buffer.

我們可以通過API snd_pcm_sw_params_set_start_threshold來設置什么時候開始啟動聲卡。對於playback,假設第設置start threshold 為320,那么就是說,當用戶調用writei,寫入的數據,將暫時存在alsa驅動空間里,當這個數據量達到 320幀時,alsa驅動才開始將數據寫入hardware buffer,並啟動DA轉換。

 

8.2What to do about xruns

If an xrun occurs, the device driver may, if requested, take certain steps to handle it. Options include stopping the device, or silencing all or part of the hardware buffer used for playback.

 the stop threshold

if the number of frames of data/space available meets or exceeds this value, the driver will stop the interface.

the silence threshold

if the number of frames of space available for a playback stream meets or exceeds this value, the driver will fill part of the playback hardware buffer with silence.

silence size

when the silence threshold level is met, this determines how many frames of silence are written into the playback hardware buffer

 xrun指的是,聲卡period一到,引發一個中斷,告訴alsa驅動,要填入數據,或讀走數據,但是,問題在於alsa的讀取和寫入操作必須用戶調用writei和readi才會發生的,它不會去緩存數據。如果上層沒有用戶調用writei和readi,那么就會產生 overrun(錄制時,數據都滿了,還沒被alsa驅動讀走)和underrun(需要數據來播放,alsa驅動卻不寫入數據),統稱為xrun。

當xrun發生時,可以在空余空間超過stop threshold時,stop audio interface.

也可以通過設置silence threshold,當空余空間超過silence threshold時,就hardware buffer 寫入silence.

 

8.3 Available minimum space/data for wakeup

Programs that use poll(2) or select(2) to determine when audio data may be transferred to/from the interface may set this to control at what point relative to the state of the hardware buffer, they wish to be woken up.

 這個software parameters僅用在interrupt-driven模式。這個模式是alsa驅動層的,不是硬件interrupt。它的意思是,用戶使用 snd_pcm_wait()時,這個實際封裝的是系統的poll調用,表示用戶在等待,那么在等待什么呢?對於playback來講,就是等待下面的聲卡的hardware buffer里有一定數量的空間,可以放入新的數據了,對於record來講,就是等待下面聲卡新采集的數據達到了一定數量了。這個一定數量,就是用 snd_pcm_sw_params_set_avail_min來設置,單位是frame。

8.4 Transfer chunk size

this determines the number of frames used when transferring data to/from the device hardware buffer.

 

 

ALSA lib cross compile:

http://blog.sina.com.cn/s/blog_7d7e9d0f0101lqlp.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM