AudioKit - 使用音序器在特定位置播放声音文件

时间:2018-01-21 12:16:48

标签: ios swift audio audiokit

我想使用AudioKit框架生成一些高音和低音的小音序。

所以我首先要看的是这样的信息:" 1100011010"

- >每个列应循环播放,如果它的值为"1",则AudioKit应播放(短)high frequency声音,如果不是,则应播放(短)lower frequency声音。

因为简单的定时器循环触发每0.15秒 .play() - 运行 0.1s声音(高/低)的功能不会#39 ; t似乎非常准确我决定使用 *AudioKit Sequencer*

(o)audiokit:

enum Sequence: Int {
    case snareDrum
}
var snareDrum = AKSynthSnare()
var sequencer = AKSequencer()
var pumper: AKCompressor?
var mixer = AKMixer()

public init() {
    snareDrum >>> mixer
    pumper = AKCompressor(mixer)

    AudioKit.output = pumper
    AudioKit.start()
}

func setupTracks() {
    _ = sequencer.newTrack()
    sequencer.tracks[Sequence.snareDrum.rawValue].setMIDIOutput(snareDrum.midiIn)
    generateMessageSequence()

    sequencer.enableLooping()
    sequencer.setTempo(2000)
    sequencer.play()
}    

(o)玩:     var message =" 1100011010"     var counter = 0

for i in message {
  counter+=0.15
  if (i=="1") {
   // play high sound at specific position 
  }
  else {
   // play low sound at specific position
  } 
}

(o)在特定位置播放低音:

 sequencer.tracks[Sequence.snareDrum.rawValue].add(noteNumber: 20,
                                                   velocity: 10,
                                                   position: AKDuration(beats: counter),
                                                   duration: AKDuration(beats: 1))

我的问题:如何使用(position: AKDuration(beats: counter)) //the code from above在特定位置播放本地声音文件,而不是使用默认乐器AKSynthSnare()

1 个答案:

答案 0 :(得分:4)

您可以创建两个轨道,每个轨道都有一个AKMIDISampler。一个人玩“低”'样本,另一个扮演一个高的'样品。将高音符分配给高音轨,将低音符分配到低音轨。

let sequencer = AKSequencer()
let lowTrack = sequencer.newTrack()
let lowSampler = AKMIDISampler()
try! lowSampler.loadWav("myLowSoundFile")
lowTrack?.setMIDIOutput(lowSampler.midiIn)

let highTrack = sequencer.newTrack()
let highSampler = AKMIDISampler()
try! highSampler.loadWav("myHighSoundFile")
highTrack?.setMIDIOutput(highSampler.midiIn)

sequencer.setLength(AKDuration(beats: 4.0))
sequencer.enableLooping()

然后将高音和低音分配给每个音轨

let message = "1100011010"
let dur = 4.0 / Double(message.count)
var position: Double = 0
for i in message {
  position += dur

  if (i == "1") {
   highTrack?.add(noteNumber: 60, velocity: 100, position: AKDuration(beats: position), duration: AKDuration(beats: dur * (2/3)))
  } else {
   lowTrack?.add(noteNumber: 60, velocity: 100, position: AKDuration(beats: position), duration: AKDuration(beats: dur * (2/34)))
  } 
}

(我没有运行代码,但这样的事情应该有效)