MTAudioProcessingTap with kMTAudioProcessingTapCreationFlag_PostEffects does not reflect the volume of AVAudioMix

I am trying to build a level measurement for AVPlayer. I do this with MTAudioProcessingTap, which is passed in AVAudioMix, which in turn goes to AVPlayerItem. MTAudioProcessingTapcreated using the flag kMTAudioProcessingTapCreationFlag_PostEffects.
Technical Q & A QA1783 has the following meanings: PreEffectsand PostEffectsflags:

When you create the pre-effects audio input using the kMTAudioProcessingTapCreationFlag_PreEffects flag, the tap will be called before any effects specified in the AVAudioMixInputParameters parameters are applied; when you create a "post-effects" response using the kMTAudioProcessingTapCreationFlag_PostEffects flag, the faucet will be called after applying these effects. Currently, the only "effect" supported by AVAudioMixInputParameters is a linear volume jump.

Problem:
When creating with help, kMTAudioProcessingTapCreationFlag_PostEffectsI would expect that the samples obtained with help MTAudioProcessingTapwould reflect the volume or sound ramps set to AVAudioMixInputParameters. For example, if I set the volume to 0, I would expect to get all 0 samples. However, the samples I received seem completely unaffected by volume or volume ramps.

Am I doing something wrong?

Here is a quick dirty pad that illustrates the problem. The example sets the volume directly, but I observed the same problem when using sound ramps. Tested for both macOS and iOS:

import Foundation
import XCPlayground
import PlaygroundSupport
import AVFoundation
import Accelerate

PlaygroundPage.current.needsIndefiniteExecution = true;

let assetURL = Bundle.main.url(forResource: "sample", withExtension: "mp3")!

let asset = AVAsset(url: assetURL)
let playerItem = AVPlayerItem(asset: asset)
var audioMix = AVMutableAudioMix()

// The volume. Set to > 0 to hear something.
let kVolume: Float = 0.0

var parameterArray: [AVAudioMixInputParameters] = []

for assetTrack in asset.tracks(withMediaType: .audio) {

    let parameters = AVMutableAudioMixInputParameters(track: assetTrack);
    parameters.setVolume(kVolume, at: kCMTimeZero)
    parameterArray.append(parameters)

    // Omitting most callbacks to keep sample short:
    var callbacks = MTAudioProcessingTapCallbacks(
        version: kMTAudioProcessingTapCallbacksVersion_0,
        clientInfo: nil,
        init: nil,
        finalize: nil,
        prepare: nil,
        unprepare: nil,
        process: { (tap, numberFrames, flags, bufferListInOut, numberFramesOut, flagsOut) in

            guard MTAudioProcessingTapGetSourceAudio(tap, numberFrames, bufferListInOut, flagsOut, nil, numberFramesOut) == noErr else {
                preconditionFailure()
            }

            // Assume 32bit float format, native endian:

            for i in 0..<bufferListInOut.pointee.mNumberBuffers {

                let buffer = bufferListInOut.pointee.mBuffers
                let stride: vDSP_Stride = vDSP_Stride(buffer.mNumberChannels)
                let numElements: vDSP_Length = vDSP_Length(buffer.mDataByteSize / UInt32(MemoryLayout<Float>.stride))

                for j in 0..<Int(buffer.mNumberChannels) {

                    // Use vDSP_maxmgv tof ind the maximum amplitude
                    var start = buffer.mData!.bindMemory(to: Float.self, capacity: Int(numElements))
                    start += Int(j * MemoryLayout<Float>.stride)
                    var magnitude: Float = 0
                    vDSP_maxmgv(start, stride, &magnitude, numElements - vDSP_Length(j))

                    DispatchQueue.main.async {
                        print("buff: \(i), chan: \(j), max: \(magnitude)")
                    }
                }
            }
        }
    )

    var tap: Unmanaged<MTAudioProcessingTap>?

    guard MTAudioProcessingTapCreate(kCFAllocatorDefault, &callbacks, kMTAudioProcessingTapCreationFlag_PostEffects, &tap) == noErr else {
        preconditionFailure()
    }

    parameters.audioTapProcessor = tap?.takeUnretainedValue()

}

audioMix.inputParameters = parameterArray

playerItem.audioMix = audioMix

let player = AVPlayer(playerItem: playerItem)
player.rate = 1.0
+4
source share

Source: https://habr.com/ru/post/1688358/


All Articles