麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

iOS音頻系列(三)--AudioQueue

2019-11-14 11:13:09
字體:
來源:轉載
供稿:網友

本篇是AudioQueue的官方文檔的筆記。Audio Queue Services可以play和record以下三類任何audio data:

Linear PCM.Any comPRessed format supported natively on the Apple platform you are developing for.Any other format for which a user has an installed codec.

對于最后一種類型,我們可以在使用AudioQueue同時自己將自己需要的format轉化成LPCM。AudioQueue是對mic和speaker的高度抽象,同時可以非常簡單的時間音頻codecs。與此同時,它也有一些高級功能,例如多個音頻的同步播放,回放等等。


About Audio Queues

這章會了解到audio queue的功能,結構體,以及內部運行的機理。具體的內容包括audio queues,audio queue buffers,audio queue會使用到的callback等。還有就是audio queue的狀態以及參數。

What Is an Audio Queue?

An audio queue 是iOS中play和record audio的對象.底層是AudioQueueRef。Audio queue可以完成以下工作:

Connecting to audio hardwareManaging memoryEmploying codecs, as needed, for compressed audio formatsMediating recording or playback

Audio Queue Architecture

Audio queue的具體結構有以下幾個部分構成:

A set of audio queue buffers, each of which is a temporary repository for some audio dataA buffer queue, an ordered list for the audio queue buffersAn audio queue callback function, that you write

根據我們使用audio queue的用途(record or play),具體的結構略有不同,僅僅只是callback函數函數的內容不同。

Audio Queues for Recording

一個用于record 的audio queue,需要使用AudioQueueNewInput方法創建,它的具體結構如圖:

A recording audio queue

Audio Queues for Playback

一個用于play的audio queue,需要使用AudioQueueNewOutput函數創建,

A playback audio queue

Audio Queue Buffers

audio queue buffer的數據結構如下:

1234567
typedef struct AudioQueueBuffer {    const UInt32   mAudioDataBytesCapacity;    void *const    mAudioData;    UInt32         mAudioDataByteSize;    void           *mUserData;} AudioQueueBuffer;typedef AudioQueueBuffer *AudioQueueBufferRef;

其中mAudioData字段表示這個buffer中的有用數據的地址,其他的字段用來輔助audio queue來管理使用這個buffer。一個audio queue可以使用任何數目的buffers。但是我們一般選擇3個,比較好管理。

Audio queue通過下面的方式管理它們內部的buffers:

An audio queue allocates a buffer when you call the AudioQueueAllocateBuffer function.When you release an audio queue by calling the AudioQueueDispose function, the queue releases its buffers.

The Buffer Queue and Enqueuing

buffer queue是由audio buffers組成的,是audio queue中的buffers。我們前面介紹了audio queue是如何使用callback管理內部的buffers。不論當前是用于record或者是pleyback,將buffer放到audio queue都是需要我們在callback函數中去手動調用的。

The Recording Process

The recording process

In step 1 , recording begins. The audio queue fills a buffer with acquired data.In step 2, the first buffer has been filled. The audio queue invokes the callback, handing it the full buffer (buffer 1). The callback (step 3) writes the contents of the buffer to an audio file. At the same time, the audio queue fills another buffer (buffer 2) with freshly acquired data.In step 4, the callback enqueues the buffer (buffer 1) that it has just written to disk, putting it in line to be filled again. The audio queue again invokes the callback (step 5), handing it the next full buffer (buffer 2). The callback (step 6) writes the contents of this buffer to the audio file. This looping steady state continues until the user stops the recording.

The Playback Process

The playback process

Controlling the Playback Process

Audio queue buffers在queue是順序播放的,我們可以通theAudioQueueEnqueueBufferWithParameters方法來進行控制

The Audio Queue Callback Function

Audio queue在運行過程中會不斷的調用callback函數,通常間隔時間和audio queue buffer的大小相關,一般是幾秒一次。

audio queue callback主要任務是將audio queue buffer歸還給audio queue。callback中通過AudioQueueEnqueueBuffer方法將buffer加載到audio queue的最后。在playback中,可以使用AudioQueueEnqueueBufferWithParameters在enqueue的過程中進行更多的控制。

The Recording Audio Queue Callback Function

如果你僅僅使用audio queue去將record的audio data寫入file system,callback的方法實現的原型如下:

12345678
AudioQueueInputCallback (    void                               *inUserData,    AudioQueueRef                      inAQ,    AudioQueueBufferRef                inBuffer,    const AudioTimeStamp               *inStartTime,    UInt32                             inNumberPacketDescriptions,    const AudioStreamPacketDescription *inPacketDescs);

一個recording audio queue會觸發我們注冊的callback,會在callback的參數中傳入所有需要的關于audio data的相關信息:

inUserData 是一個自定義的結構體,用來存儲audio queueu以及audio queue buffer的狀態信息,也包括AudioFileID,audio data format等。inAQ 表示哪個audio queue觸發這個callback。inBuffer 是一個audio queue buffer,它的內容是由audio queue填充的,內部包括最新的audio data。并且這些audio data已經根據初始化時候傳遞的格式參數格式化好的數據。inStartTime 表示這個buffer中的第一個采樣的采樣時間點,一般app中不太需要這個參數。inNumberPacketDescriptions 表示inPacketDescs參數中的packet descriptions的個數。如果你是錄入VBR format,audio queue就會在callback中提供這個參數,如果是CBR,audio queue就不會使用packet descriptions參數,這個參數會是NULL。inPacketDescs 表示buffer中samples相關的一系列的packet descriptions。是否設置同上一個參數。

The Playback Audio Queue Callback Function

這個片段會介紹如果使用playing audio queue,那么callback應該的信息:

12345
AudioQueueOutputCallback (    void                  *inUserData,    AudioQueueRef         inAQ,    AudioQueueBufferRef   inBuffer);

一個playback audio queue會觸發這個callback,提供一些關于audio data的有用信息:

inUserData 見上inAQ 表示哪個audio queue觸發這個callback。inBuffer 表示被audio queue設置為空的audio queue buffer,你需要在callback中將其內部信息填滿,填充內容是你從AudioFile中讀取的audio data。

Using Codecs and Audio Data Formats

我們日常使用Audio Queue Services時,都會使用codecs(audio data coding/decoding componets)用來在不同audio format之間進行轉化。

每個audio queue都有一個audio data format,可以在AudioStreamBasicDescription結構體中得到。當我們在ASBD中指定了mFormatID以后,audio queue在向buffer中填充數據時候就會使用相應的codec。同樣如果指定sample rate和channel count,audio queue也會同樣。具體的過程見下圖:

Audio format conversion during recording

第一步中,app會告知audio queue開始record,同時告訴它使用的的data format。第二步中,audio queue將獲取到的new data使用codec轉化成目標format。然后audio queue會調用callback函數,傳入格式化以后的audio data。第三步中,callback函數會將格式化以后的audio data寫入file中。

整個過程中,callback函數壓根就不需要知道data fromat是什么。

Audio format conversion during playback

在播放過程中,正好和錄音過程相反,只需要在創建audio queue時候將data format告知即可。


Audio Queue Control and State

audio queue在創建和銷毀的過程有一個聲明周期。app需要管理它的聲明周期,控制它的狀態,具體控制狀態的方法如下:

Start (AudioQueueStart).初始化audio queue用來record或者playback。Prime (AudioQueuePrime).對于playback,在調用AudioQueueStart摯愛去哪調用確保數據可用,這個方法和record沒有關系。Stop (AudioQueueStop). 調用以后會重置audio queue,然后會停止record或者playback。在playback應用中,一般在沒有audio data可以播放時候調用。Pause (AudioQueuePause). 在record或者playback中調用這個方法不會影響到buffers。如果需要恢復,調用AudioQueueStart。Flush (AudioQueueFlush). 在enqueue最后一個audio queue buffer以后調用這個方法,確保所有的數據被record或者play(主要是在midst processing的數據)。Reset (AudioQueueReset). 調用以后立即停止audio queue,然后將所有的buffers移除,重置所有的DSP狀態等到。

在調用AudioQueueStop方法時候有兩種模式:同步和異步。

Synchronous stopping happens immediately, without regard for previously buffered audio data.Asynchronous stopping happens after all queued buffers have been played or recorded.

Recording Audio

當我們的record使用Audio Queue Services,存儲的路徑可以是磁盤上的任何地方,或者網絡,或者內存中。這部分內容記錄大多數的使用場景,存儲在磁盤中。

具體的步驟如下:

定義一個結構體去存儲狀態,format,文件路徑等信息。完成audio queue callback函數,其中將record以后的數據進行存儲。為audio queue buffers計算出合適的大小,并且在file中寫入magic cookies。初始化自定義的結構體創建recording audio queue,然后給它創建3個audio queue buffers,然后創建一個file用來存儲record以后的audio data。啟動audio queue當audio queue停止以后,dispose它以及buffers

具體的實現內容可以參考Apple官方文檔:Recording Audio。

Playing Audio

當我們使用Audio Queue Service去play audio時,音頻源文件可以是任何在disk file或者memory中,這部分內容是如何用Audio Queue Service播放存儲在disk上的audio file。

具體的步驟如下:

定義一個結構體管理Audio queue的狀態,format,file path等完成audio queue callback函數去進行實際的播放創建一個函數用來計算最適合的audio queue buffer的大小打開audio file,確定它的audio data format創建audio queue,對它進行配置為audio queue創建buffers,然后啟動audio queue,當播放結束,callback讓audio queue停止播放銷毀audio queue

具體的實現內容可以參考Apple官方文檔:Playing Audio。

可運行的Demo

請參考我的github: https://github.com/brownfeng/AudioQueueServiceDemo


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美一级二级毛片视频 | 天天夜夜操操 | 久久久久久久久久久国产精品 | 羞羞的视频免费观看 | 欧美福利视频一区二区 | 亚洲第一视频在线 | 久久久噜噜噜久久熟有声小说 | 黄色视频一级毛片 | 9797色| 免费网站看v片在线a | 久草网在线 | 亚洲国产资源 | 国产91porn | 91精品国产日韩91久久久久久360 | 国产一级淫片免费看 | 欧美激情猛片xxxⅹ大3 | 在线观看中文字幕国产 | 国产超碰人人爽人人做人人爱 | 欧美国产日韩在线观看成人 | 中文字幕欧美亚洲 | 伊人午夜 | 日本黄色免费观看视频 | 国产精品久久久久久久久久久久久久久久 | 欧美一级特黄特色大片免费 | 国产午夜亚洲精品午夜鲁丝片 | 国产手机av在线 | 成人一级黄色 | 中文字幕激情视频 | 污片视频网站 | 国产精品久久国产精麻豆96堂 | 免费男女视频 | 国产成人精品区 | 久久久成人999亚洲区美女 | 黄色网络免费看 | 久草在线资源福利站 | 成人毛片网站 | 亚洲精品tv久久久久久久久久 | 欧美一区2区三区4区公司二百 | 国产伦精品一区二区三区在线 | 黑人一级片视频 | 久久久久久久久久91 |