使用AudioKit裁剪MIDI文件 [英] Cropping MIDI file using AudioKit

查看:136
本文介绍了使用AudioKit裁剪MIDI文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AudioKit裁剪和循环MIDI文件的特定部分.

I am trying to crop and loop a certain part of a MIDI file using AudioKit.

我正在使用音序器,发现一些与我需要的东西接近但不完全相同的东西.

I am using a sequencer and found a couple of things that are close to what I need, but not exactly.

我在AKSequencer中找到了一种称为clearRange的方法.通过这种方法,我可以使不需要的MIDI部分静音,但是我还没有找到一种方法来修整音序器并只保留我感兴趣的部分.现在只有我想要的部分才有声音但我仍然得到了沉默的部分.

I found a method in AKSequencer called clearRange. With this method I am able to silence the parts of the MIDI I don't want, but I haven't found a way to trim the sequencer and only keep the part I am interested in. Right now only the part I want has sound but I still get the silent parts.

是否有一种方法可以修整音序器或只用我想保留的音序器来创建新的音序器?

Is there a way to trim a sequencer or to create a new sequencer with only the portion I want to keep from the original one?

谢谢!

推荐答案

Apple的MusicSequence(基于AKSequencer的一个令人沮丧的限制)是,尽管您可以轻松地设置循环部分的右侧" ,左侧将始终循环回到零,并且无法更改.因此,要从左侧进行裁剪,您需要隔离要循环播放的部分并将其移至上方,以使循环的开始位置为零.

One frustrating limitation of Apple's MusicSequence (which AKSequencer is based on) is that although you can easily set the 'right side' of the looping section, the left side will always loop back to zero and cannot be changed. So to crop from the left side, you need to isolate the section that you want to loop and shift it over so that the start of your loop is at zero.

从AudioKit 4.2.4开始,这是可能的.使用AKMusicTrack的.getMIDINoteData()获取一个AKMIDINoteData结构的数组,其内容可以编辑,然后用于替换原始数据.如果您有16节拍的曲目,并且想循环播放最后4个节拍,则可以执行以下操作:

As of AudioKit 4.2.4, this is possible. Use AKMusicTrack's .getMIDINoteData() to get an array of AKMIDINoteData structs whose content can be edited and then used to replace the original data. If you had a 16 beat track and you wanted to loop the last four beats, you could do something ike this:

let loopStart = 12.0
let loopLength = 4.0

// keep track of the original track contents
let originalLength = 16.0
let originalContents = track.getMIDINoteData()

// isolate the segment for looping and shift it to the start of the track
let loopSegment = originalContents.filter { loopStart ..< (loopStart + loopLength) ~= $0.position.beats }
let shiftedSegment = loopSegment.map { AKMIDINoteData(noteNumber: $0.noteNumber,
                                                      velocity: $0.velocity,
                                                      channel: $0.channel,
                                                      duration: $0.duration,
                                                      position: AKDuration(beats: $0.position.beats - loopStart))
}

// replace the track contents with the loop, and assert the looping behaviour
track.replaceMIDINoteData(with: shiftedSegment)
seq.setLength(AKDuration(beats: loopLength))
seq.enableLooping()

// and to get back to the original:
track.replaceMIDINoteData(with: originalContents)
seq.setLength(AKDuration(beats: originalLength))
seq.enableLooping()

如果您想循环播放部分以重复原始序列的长度,则可以使用shiftedSegment作为模板并从中构建一个16节拍序列.

If you wanted to looping section to repeat for the length of the original sequence, then you could use the shiftedSegment as a template and build a 16 beat sequence from it.

这篇关于使用AudioKit裁剪MIDI文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆