AudioKit: change sound based upon gyro data / swing phone around?

99封情书 提交于 2019-12-08 13:29:14

问题


This is an AudioKit question:

I am really new to AudioKit and audio in general.

My question is: How could I use AudioKit to create a sound that changes as I move my phone around? I already know how to get the gyro information so lets say I can take the gyro values between 0-10, zero being no movement and 10 being a lot of movement of the phone. I want to translate that into sounds that corresponds to how hard/quickly the phone is being moved. To start just move the sound higher in pitch as the speed increase, low pitch down at zero. Sounds easy yes?

I'm just not experienced enough to know which AudioKit class to use or how to use it to achieve my results.

Thank you! Michael


回答1:


You have to write your own AKOperationGenerator.

enum PitchEnvVCOSynthParameter: Int {
    case frequency, gate
}

struct PitchEnvVCO {
    static var frequency: AKOperation {
        return AKOperation.parameters[PitchEnvVCOSynthParameter.frequency.rawValue]
    }
    static var gate: AKOperation {
        return AKOperation.parameters[PitchEnvVCOSynthParameter.gate.rawValue]
    }
}

extension AKOperationGenerator {
    var frequency: Double {
        get { return self.parameters[PitchEnvVCOSynthParameter.frequency.rawValue] }
        set(newValue) { self.parameters[PitchEnvVCOSynthParameter.frequency.rawValue] = newValue }
    }
    var gate: Double {
        get { return self.parameters[PitchEnvVCOSynthParameter.gate.rawValue] }
        set(newValue) { self.parameters[PitchEnvVCOSynthParameter.gate.rawValue] = newValue }
    }
}

and

let generator = AKOperationGenerator { parameters in
    let oscillator = AKOperation.squareWave(
        frequency: PitchEnvVCO.frequency
    )
    return oscillator
}

and then make your variable control the frequency

var vco1Freq: Double = 440.0
{
    didSet {
        generator.parameters[PitchEnvVCOSynthParameter.frequency.rawValue] = vco1Freq
    }
}

Fetch the gyro data and make it control your variable like describes here



来源:https://stackoverflow.com/questions/46647642/audiokit-change-sound-based-upon-gyro-data-swing-phone-around

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