2

我正在尝试使用 AudioKit 过滤掉低于指定阈值的频率。我AKHighPassFilter用来完成这个。

func playSound(duration: Int, frequencyRange: Range<Double>) {

    generator = AKOperationGenerator() { parameters in

        let volume = AKOperation.sineWave(frequency: parameters[0]).scale(minimum: 0, maximum: 0.1)
        let jitter = AKOperation.jitter(amplitude: parameters[1], minimumFrequency: lowerFrequency, maximumFrequency: upperFrequency)

        return AKOperation.sineWave(frequency: jitter, amplitude: volume)
    }

    generator!.parameters = [baseFrequency, amplitude]

    // Exception thrown at this line after method called 1 time
    var highPassFilter = AKHighPassFilter(generator)
    highPassFilter.cutoffFrequency = frequencyRange.lowerBound
    highPassFilter.resonance = 0

    AudioKit.output = highPassFilter
    AudioKit.start()

    generator?.play()
}

我第一次打电话playSound(duration:, frequencyRange:),它工作正常。一旦我停止 AudioKit 并再次尝试播放声音,就会在此行引发异常:

var highPassFilter = AKHighPassFilter(generator)

这是崩溃的控制台输出:

2017-05-01 14:32:01.169 MyApp[18123:3459824] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: [_nodes containsObject: node]'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000109ea7b0b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010990c141 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000109eabcf2 +[NSException raise:format:arguments:] + 98
    3   AVFAudio                            0x000000010cdc3a9e _Z19AVAE_RaiseExceptionP8NSStringz + 158
    4   AVFAudio                            0x000000010cdc9cbb _ZN17AVAudioEngineImpl10DetachNodeEP11AVAudioNodeb + 421
    5   AVFAudio                            0x000000010cdc9ad0 -[AVAudioEngine detachNode:] + 67
    6   AudioKit                            0x0000000108d3bee4 _TToFC8AudioKit6AKNodeD + 84
    7   libobjc.A.dylib                     0x0000000109920b8e _ZN11objc_object17sidetable_releaseEb + 202
    8   AudioKit                            0x0000000108d39a99 _TToFC8AudioKit16AKHighPassFilterE + 25
    9   libobjc.A.dylib                     0x000000010990a9bc _ZL27object_cxxDestructFromClassP11objc_objectP10objc_class + 127
    10  libobjc.A.dylib                     0x0000000109916d34 objc_destructInstance + 129
    11  libobjc.A.dylib                     0x0000000109916d66 object_dispose + 22
    12  AudioKit                            0x0000000108d3bf1b _TToFC8AudioKit6AKNodeD + 139
    13  libobjc.A.dylib                     0x0000000109920b8e _ZN11objc_object17sidetable_releaseEb + 202
    14  MyApp                             0x00000001087301ec _TFC7MyApp11SoundPlayerClass9playSoundfT8durationSi14frequencyRangeGVs5RangeSd__T_ + 988
    15  MyApp                             0x000000010873a1a4 _TFC7MyApp18MainViewController13playFrequencyfCS_12FrostyButtonT_ + 2276
    16  MyApp                             0x000000010873a4fa _TToFC7MyApp18MainViewController13playFrequencyfCS_12FrostyButtonT_ + 58
    17  UIKit                               0x000000010ab63d22 -[UIApplication sendAction:to:from:forEvent:] + 83
    18  UIKit                               0x000000010ace825c -[UIControl sendAction:to:forEvent:] + 67
    19  UIKit                               0x000000010ace8577 -[UIControl _sendActionsForEvents:withEvent:] + 450
    20  UIKit                               0x000000010ace74b2 -[UIControl touchesEnded:withEvent:] + 618
    21  UIKit                               0x000000010abd149a -[UIWindow _sendTouchesForEvent:] + 2707
    22  UIKit                               0x000000010abd2bb0 -[UIWindow sendEvent:] + 4114
    23  UIKit                               0x000000010ab7f7b0 -[UIApplication sendEvent:] + 352
    24  UIKit                               0x000000011e3c075c -[UIApplicationAccessibility sendEvent:] + 85
    25  UIKit                               0x000000010b362adc __dispatchPreprocessedEventFromEventQueue + 2926
    26  UIKit                               0x000000010b35aa3a __handleEventQueue + 1122
    27  UIKit                               0x000000010b35bd08 __handleEventQueue + 5936
    28  CoreFoundation                      0x0000000109e4dc01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    29  CoreFoundation                      0x0000000109e330cf __CFRunLoopDoSources0 + 527
    30  CoreFoundation                      0x0000000109e325ff __CFRunLoopRun + 911
    31  CoreFoundation                      0x0000000109e32016 CFRunLoopRunSpecific + 406
    32  GraphicsServices                    0x000000010ecd4a24 GSEventRunModal + 62
    33  UIKit                               0x000000010ab620d4 UIApplicationMain + 159
    34  MyApp                             0x000000010872cb77 main + 55
    35  libdyld.dylib                       0x000000010fbaf65d start + 1
    36  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

在添加之前AKHighPassFilter,该方法运行良好。我欢迎提出建议:如何解决此崩溃。感谢您的阅读。

4

1 回答 1

1

我认为这里的主要问题是您在每次 playSound 调用时重新创建所有对象,并且每次调用时都在 play 方法中设置 AudioKit.output。您要做的是在初始化例程中创建对象链,然后根据需要更改参数。所有的连接只需要建立一次。一种技术是使用 AKMixer 作为输入或输出,并从中添加和删除节点。例如,高通滤波器可以过滤 AKMixer 的输出。这个混合器可以连接任意数量的节点。AVAudioEngine 在运行时可能对删除或添加的节点有些敏感。

于 2017-11-16T17:24:12.787 回答