2010-09-18 122 views
1

我試圖從文件中獲取原始音頻數據(我習慣於看到-1和1之間的浮點值)。Core Audio AudioFIleReadPackets ...尋找原始音頻

我試圖從緩衝區實時拉出這些數據,以便我可以爲應用程序提供某種類型的測量。

我基本上是用AudioFileReadPackets將整個文件讀入內存。我創建了一個RemoteIO音頻單元來進行回放,並且在playbackCallback內部,我將mData提供給AudioBuffer,以便它可以發送到硬件。

我遇到的最大問題是從我的數據數組(從AudioFileReadPackets)發送到緩衝區的數據是UInt32 ...我真的很困惑。它看起來像是32位,我已經設置每個數據包/幀爲4bytes。我怎麼得到我的原始音頻數據(從-1到1)?

這是我的格式描述

// Describe format 
audioFormat.mSampleRate   = 44100.00; 
audioFormat.mFormatID   = kAudioFormatLinearPCM; 
audioFormat.mFormatFlags  = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked; 
audioFormat.mFramesPerPacket = 1; 
audioFormat.mChannelsPerFrame = 2; 
audioFormat.mBitsPerChannel  = 16; 
audioFormat.mBytesPerPacket  = 4; 
audioFormat.mBytesPerFrame  = 4; 

我讀波形文件當前。

謝謝!

回答

0

看看這個功能... 數據是SInt16。

static void recordingCallback (
    void        *inUserData, 
    AudioQueueRef      inAudioQueue, 
    AudioQueueBufferRef     inBuffer, 
    const AudioTimeStamp    *inStartTime, 
    UInt32        inNumPackets, 
    const AudioStreamPacketDescription *inPacketDesc 
) { 


    // This callback, being outside the implementation block, needs a reference to the AudioRecorder object 
    AudioRecorder *recorder = (AudioRecorder *) inUserData; 

    // if there is audio data, write it to the file 
    if (inNumPackets > 0) { 

     SInt16 *frameBuffer = inBuffer->mAudioData; 
     //NSLog(@"byte size %i, number of packets %i, starging packetnumber %i", inBuffer->mAudioDataByteSize, inNumPackets,recorder.startingPacketNumber); 

     //int totalSlices = 1; 
     //int framesPerSlice = inNumPackets/totalSlices; 
     float total = 0; 
     for (UInt32 frame=0; frame<inNumPackets; frame+=20) { 
      total += (float)abs((SInt16)frameBuffer[frame]) ; 
     } 
1

我不知道到底爲什麼你得到UInt32的數據,從該行回調,但我懷疑它實際上是兩個交錯UINT16包,每個通道的一個。無論如何,如果你需要文件中的浮點數據,它需要被轉換,我不相信@John Ballinger推薦的方式是正確的。我的建議是:

// Get buffer in render/read callback 
SInt16 *frames = inBuffer->mAudioData; 
for(int i = 0; i < inNumPackets; i++) { 
    Float32 currentFrame = frames[i]/32768.0f; 
    // Do stuff with currentFrame (add it to your buffer, etc.) 
} 

你不能簡單地將幀轉換爲你想要的格式。如果您需要浮點數據,則需要除以32768,這是16位採樣的最大可能值。這將產生{-1.0 .. 1.0}範圍內的正確的浮點數據。

+0

所以我拿了UInt32,把低位比特分成了1個SInt16,另一個比特分成了另一個SInt16,看起來每個比特都很好地代表了2個獨立的通道。我正在使用Michael Bolton的示例代碼編寫的示例代碼,該代碼示範瞭如何在iPhone上使用RemoteIO音頻單元,它看起來像Aran只是將兩個交叉通道作爲UInt32傳遞到remoteIO單元,因爲他沒有進行任何類型的測量/波形圖。在將UInt32分解爲雙重SInt16之後,它看起來應該是一切。 – Corey 2010-09-20 16:37:14

+0

同樣,數據處於浮點狀態也不是必須的。我創建了一個波形繪製類,它希望數據處於浮動狀態,實際上是單獨打開音頻文件並使用AudioFileRead()函數將數據拉出(浮點數)。 因此,我的應用程序正在執行以下操作: 完成錄製後,通過打開錄製的文件來繪製波形,讀取每個第x個樣本並將其添加到數組中,循環遍歷所得數組,並使用核心圖形繪製波形。然後當用戶按下游戲時,它初始化AU。 – Corey 2010-09-20 16:42:49

+0

啊,好的。那麼你是否設法解決問題呢? – 2010-09-21 08:48:45