Skip to content

Commit

Permalink
MIDIFile Track.eventsAtBeatPositions(ppq:) now returns `MIDIFileE…
Browse files Browse the repository at this point in the history
…vent`
  • Loading branch information
orchetect committed Jun 20, 2023
1 parent 3b38881 commit 603acba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
16 changes: 7 additions & 9 deletions Sources/MIDIKitSMF/MIDIFile/Chunk/Track.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,15 @@ extension MIDIFile.Chunk.Track {
/// Returns ``events`` mapped to their beat position in the sequence.
/// This is computed so avoid frequent calls to this method.
/// Ensure the `ppq` supplied is the same as used in the MIDI file.
public func eventsAtBeatPositions(ppq: UInt16) -> [(beat: Double, event: MIDIFileEventPayload)] {
public func eventsAtBeatPositions(ppq: UInt16) -> [(beat: Double, event: MIDIFileEvent)] {
var position: Double = 0.0
return events.lazy
.map { $0.smfUnwrappedEvent }
.map { (delta, payload) in
let deltaTicks = delta.ticksValue(using: .musical(ticksPerQuarterNote: ppq))
if deltaTicks != 0 {
position += Double(deltaTicks) / Double(ppq)
}
return (beat: position, event: payload)
return events.map {
let deltaTicks = $0.delta.ticksValue(using: .musical(ticksPerQuarterNote: ppq))
if deltaTicks != 0 {
position += Double(deltaTicks) / Double(ppq)
}
return (beat: position, event: $0)
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/MIDIKitSMF/MIDIFile/Types/TimeBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import MIDIKitCore
extension MIDIFile {
/// MIDI file timebase as described in the MIDI file header.
public enum TimeBase: Equatable, Hashable {
/// Musical: Delta-time ticks per quarter note (PPQN / PPQ / PPQBase).
/// Musical: Delta-time ticks per quarter note (PPQN / PPQ / PPQBase / TPQN).
///
/// Common values: 96, 120, 480, 960. Cubase exports at 480 by default.
/// Can also be a larger value if needed.
Expand Down Expand Up @@ -41,8 +41,8 @@ extension MIDIFile.TimeBase {
extension MIDIFile.TimeBase {
var rawData: Data {
switch self {
case let .musical(TicksPerQuarterNote):
return (TicksPerQuarterNote & 0b01111111_11111111)
case let .musical(ticksPerQuarterNote):
return (ticksPerQuarterNote & 0b01111111_11111111)
.toData(.bigEndian)

case let .timecode(smpteFormat, ticksPerFrame):
Expand Down

0 comments on commit 603acba

Please sign in to comment.