userForStackExchange
userForStackExchange

Reputation: 51

what samples will buffer contain when I use installTap(onBus for multichannel audio?

what samples will buffer contain when I use installTap(onBus for multichannel audio?

If channel count > 1 then it will contain left microphone ? or it will contain samples from left microphone and right microphone?

when I use iphone simulator then Format = pcmFormatFloat32, channelCount = 2, sampleRate = 44100.0, not Interleaved

I use this code

let bus = 0
inputNode.installTap(onBus: bus, bufferSize: myTapOnBusBufferSize, format: theAudioFormat) {
            (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in
            self.onNewBuffer(buffer)
        }

func onNewBuffer(_ inputBuffer:AVAudioPCMBuffer!)
{
    var samplesAsDoubles:[Double] = []

    for i in 0 ..< Int(inputBuffer.frameLength)
    {
        let theSample = Double((inputBuffer.floatChannelData?.pointee[i])!)
        samplesAsDoubles.append( theSample )
    }
}

print("number of input busses = \(inputNode.numberOfInputs)")

it print number of input buses = 1

for each sample in my samplesAsDoubles array from buffer that I have inside of block from what channel it will be? at what time this sample was recorded?

Upvotes: 1

Views: 718

Answers (1)

dave234
dave234

Reputation: 4955

From the header comments for floatChannelData:

The returned pointer is to format.channelCount pointers to float. Each of these pointers is to "frameLength" valid samples, which are spaced by "stride" samples.

If format.interleaved is false (as with the standard deinterleaved float format), then the pointers will be to separate chunks of memory. "stride" is 1.

FloatChannelData gives you a 2D float array. For a non-interleaved 2 channel buffer, you would access the individual samples like this:

let channelCount = Int(buffer.format.channelCount)
let frameCount = Int(buffer.frameLength)

if let channels = buffer.floatChannelData {  //channels is 2D float array
    for channelIndex in 0..<channelCount {
        let channel = channels[channelIndex] //1D float array
        print(channelIndex == 0 ? "left" : "right")
        for frameIndex in 0..<frameCount {
            let sample = channel[frameIndex]
            print("    \(sample)")
        }
    }
}

Upvotes: 3

Related Questions