简体   繁体   English

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

[英]AudioKit - Play sound files at specific position using sequencer

I'd like to use the AudioKit framework to generate a small sound sequence of some high and low sounds. 我想使用AudioKit框架生成一些高音和低音的小音序。

So what I'm starting with is the message that could look like this: " 1100011010 " 所以我开始的是这样的消息:“ 1100011010

--> Every column should be looped through and if it's value is "1" AudioKit should play a (short) high frequency sound and if not it should play a (short) lower frequency sound. - >每列应循环播放,如果值为"1" AudioKit应播放(短) high frequency声音,如果不是,则播放(短) lower frequency声音。

Because a simple timer-loop that triggers every 0.15s the .play() -function for running a 0.1s sound (high/low) doesn't seems to be very accurate I decided to use the *AudioKit Sequencer* : 因为一个简单的定时器循环触发每0.15 运行0.1s声音(高/低).play()函数似乎不是很准确我决定使用*AudioKit Sequencer*

(o) audiokit: (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) play: var message="1100011010" var counter=0 (o)play: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) 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))

My question : How is it possible to play local sound files at specific positions using (position: AKDuration(beats: counter)) //the code from above instead of using default instruments like in this case AKSynthSnare() ? 我的问题 :如何使用(position: AKDuration(beats: counter)) //the code from above而不是使用默认乐器(例如AKSynthSnare()来播放特定位置的本地声音文件?

You could create two tracks, each with an AKMIDISampler. 您可以创建两个轨道,每个轨道都有一个AKMIDISampler。 One plays a 'low' sample, and the other plays a 'high' sample. 一个播放“低”样本,另一个播放“高”样本。 Assign the high notes to the high track, and low notes to the low track. 将高音符分配给高音轨,将低音符分配给低音轨。

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()

then assign the high and low sounds to each track 然后为每个音轨分配高低音

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)))
  } 
}

(I haven't run the code, but something like this should work) (我没有运行代码,但这样的东西应该工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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