Trimming a MIDI File Using AudioKit

I am trying to trim and contact a specific part of a MIDI file with AudioKit.

I use a sequencer and found a couple of things that are close to what I need, but not really.

I found a method in AKSequencercalled clearRange. With this method, I can silence the MIDI parts that I don’t want, but I have not found a way to trim the sequencer and only keep the part I am interested in. Right now, only the part I want has a sound but I still get the silent parts.

Is there a way to crop a sequencer or create a new sequencer with only the part that I want to keep from the original?

Thank!

+4
source share
1 answer

Apple MusicSequence ( AKSequencer) , , " " , . , , , , , .

AudioKit 4.2.4 . AKMusicTrack .getMIDINoteData() AKMIDINoteData, , . 16- , , - :

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 .

+1

Source: https://habr.com/ru/post/1688237/


All Articles