简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM