Continuous Sine Wave From AKMIDISampler when AKMicrophone is Present

匆匆过客 提交于 2019-12-11 11:01:39

问题


I’m having a problem using AKMIDISampler in my project when there’s an initialized AKMicrophone. Along with correctly playing the woodblock sample when “play” is called on the sampler, the first time “play” is called a constant sine wave starts playing - it never stops.

I’ve replicated the problem in the smallest amount of code below. Issue happens when the class is initialized then playTestSample() is called.

Note that if the AKMicrophone related code is all muted the AKMIDISampler plays fine and the sine wave that currently haunts my dreams doesn’t happen.

(I’ve tried switching to use the AKSampler() just to see if the problem would exist there but I haven’t been able to get any sound out of that).

Fyi: I have “App plays audio or streams audio/video using AirPlay” in the “Required background modes” in info.plist - which is know to fix another sine wave issue.

Thank you very much for any assistance.

Btw: AudioKit rocks and has been a massive help on this project! :^)

AK 4.5.4 Xcode 10.1

import Foundation
import AudioKit

class AudioKitTESTManager {

    var mixer = AKMixer()

    var sampler = AKMIDISampler()

    var mic = AKMicrophone()
    var micMixer = AKMixer()
    var micBooster = AKBooster()

    init() {

        mixer = AKMixer(sampler, micBooster)

        do {
            let woodblock = try AKAudioFile(readFileName: RhythmGameConfig.woodblockSoundName)

            try sampler.loadAudioFiles([woodblock])

        } catch {
            print("Error loading audio files into sampler")
        }


        micMixer = AKMixer(mic)

        micBooster = AKBooster(micMixer)
        micBooster.gain = 0.0


        AudioKit.output = mixer

        AKSettings.playbackWhileMuted = true
        AKSettings.defaultToSpeaker = true
        AKSettings.sampleRate = 44100

        do {
            print("Attempting to start AudioKit")
            try AudioKit.start()
        } catch {
            AKLog("AudioKit did not start!")
        }
    }

    func playTestSample() {
        // You hear the sample and a continuous sine wave starts playing through the samplerMixer
        try? sampler.play(noteNumber: 60, velocity: 90, channel: 1)
    }
}

回答1:


Wheeew. I believe I've found a solution. Maybe it will help out someone else?

It seems that loading the files into the sampler AFTER AudioKit.start() fixes the Sine Wave of Terror!

//..
    do {
        print("Attempting to start AudioKit")
        try AudioKit.start()
    } catch {
        AKLog("AudioKit did not start!")
    }


    do {
        let woodblock = try AKAudioFile(readFileName: RhythmGameConfig.woodblockSoundName)

        try sampler.loadAudioFiles([woodblock])

    } catch {
        print("Error loading audio files into sampler")
    }


来源:https://stackoverflow.com/questions/53563255/continuous-sine-wave-from-akmidisampler-when-akmicrophone-is-present

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!