midi-reader-writer 0.1.3

Facilitate reading and writing midi files.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//     Facilitate reading and writing midi files.
//
//     Copyright (C) 2021 Pieter Penninckx
//
//     `midi-reader-writer` is licensed under the Apache License, Version 2.0
//     or the MIT license, at your option.
//
//     For the application of the MIT license, the examples included in the doc comments are not
//     considered "substantial portions of this Software".
//
//     License texts can be found:
//     * for the Apache License, Version 2.0: <LICENSE-APACHE.txt> or
//         <http://www.apache.org/licenses/LICENSE-2.0>
//     * for the MIT license: <LICENSE-MIT.txt> or
//         <http://opensource.org/licenses/MIT>.
//
//

//! Everything specific to using this crate with the [`midly`](https://crates.io/crates/midly) crate,
//! version 0.5.x, behind the `engine-midly-0-5` feature.
//!
//! This module also adds some extra functionality to be used with the `midly` crate:
//! * [`merge_tracks`]: Create an iterator over all tracks, merged (behind the `read` feature)
//! * [`TrackSeparator`]: Separate the tracks again.

/// Re-exports from the [`midly`](https://crates.io/crates/midly) crate, version 0.5.x.
pub mod exports {
    pub use midly_0_5::*;
}

use self::exports::{num::u28, MetaMessage, Track, TrackEvent, TrackEventKind};
#[cfg(all(test, feature = "convert-time"))]
use self::exports::{
    num::{u15, u24},
    Format,
};
#[cfg(all(test, any(feature = "read", feature = "convert-time")))]
use self::exports::{
    num::{u4, u7},
    MidiMessage,
};
#[cfg(feature = "convert-time")]
use self::exports::{Fps, Header, Timing};
#[cfg(feature = "convert-time")]
use crate::{
    ConvertMicroSecondsToTicks, ConvertTicksToMicroseconds, MidiEvent, TimeConversionError,
};
#[cfg(feature = "read")]
use itertools::Itertools; // TODO: remove if not needed.
use std::iter::FromIterator;
#[cfg(feature = "convert-time")]
use std::num::NonZeroU64;
use std::num::TryFromIntError;
use std::{
    convert::TryFrom,
    error::Error,
    fmt::{Display, Formatter},
};
#[cfg(feature = "convert-time")]
use timestamp_stretcher::TimestampStretcher;

#[cfg(feature = "convert-time")]
const MICROSECONDS_PER_SECOND: u64 = 1_000_000;
#[cfg(feature = "convert-time")]
const SECONDS_PER_MINUTE: u64 = 60;
#[cfg(feature = "convert-time")]
const MICROSECONDS_PER_MINUTE: u64 = SECONDS_PER_MINUTE * MICROSECONDS_PER_SECOND;
#[cfg(feature = "convert-time")]
const DEFAULT_BEATS_PER_MINUTE: u64 = 120;

#[cfg(feature = "read")]
/// Create an iterator over all the tracks, merged.
/// The item has type `(u64, usize, TrackEventKind)`,
/// where the first element of the triple is the timing of the event relative to the beginning
/// of the tracks, in ticks,
/// the second item of the triple is the track index and the last item is the event itself.
///
/// # Example
/// ```
/// use midi_reader_writer::midly_0_5::{
///     merge_tracks,
///     exports::{
///         MidiMessage,
///         TrackEvent,
///         TrackEventKind,
///         num::{u15, u4, u7, u28}
///     }
/// };
///
/// // Create a note on event with the given channel
/// fn note_on_with_channel(channel: u8) -> TrackEventKind<'static> {
///     // ...
/// #     TrackEventKind::Midi {
/// #         channel: u4::new(channel),
/// #         message: MidiMessage::NoteOn {
/// #             key: u7::new(1),
/// #             vel: u7::new(1),
/// #         },
/// #     }
/// }
///
/// // Create a note on event with the given delta and channel.
/// fn note_on_with_delta_and_channel(delta: u32, channel: u8) -> TrackEvent<'static> {
///     // ...
/// #     TrackEvent {
/// #        delta: u28::new(delta),
/// #        kind: note_on_with_channel(channel),
/// #    }
/// }
///
/// let tracks = vec![
///     vec![
///         note_on_with_delta_and_channel(2, 0),
///         note_on_with_delta_and_channel(100, 1),
///     ],
///     vec![note_on_with_delta_and_channel(30, 2)],
/// ];
/// let result: Vec<_> = merge_tracks(&tracks[..]).collect();
/// assert_eq!(
///     result,
///     vec![
///         (2, 0, note_on_with_channel(0)),
///         (30, 1, note_on_with_channel(2)),
///         (102, 0, note_on_with_channel(1)),
///     ]
/// )
/// ```
pub fn merge_tracks<'a, 'b>(
    tracks: &'b [Track<'a>],
) -> impl Iterator<Item = (u64, usize, TrackEventKind<'a>)> + 'b {
    let mut track_index = 0;
    tracks
        .iter()
        .map(|t| {
            let mut offset = 0;
            let result = t.iter().map(move |e| {
                offset += e.delta.as_int() as u64;
                (offset, track_index, e.kind)
            });
            track_index += 1;
            result
        })
        .kmerge_by(|(t1, _, _), (t2, _, _)| t1 < t2)
}

#[cfg(feature = "read")]
#[test]
fn merge_tracks_has_sufficiently_flexible_lifetime_annotation() {
    fn wrap(bytes: &[u8]) -> Track {
        let event = TrackEvent {
            delta: u28::new(123),
            kind: TrackEventKind::SysEx(bytes),
        };
        merge_tracks(&[vec![event]])
            .map(|(_, _, kind)| TrackEvent {
                delta: u28::new(0),
                kind,
            })
            .collect()
    }

    let bytes = "abc".to_string(); // Force a non-static lifetime.
    wrap(bytes.as_bytes());
}

#[cfg(feature = "read")]
#[test]
fn merge_tracks_works() {
    fn kind(channel: u8) -> TrackEventKind<'static> {
        TrackEventKind::Midi {
            channel: u4::new(channel),
            message: MidiMessage::NoteOn {
                key: u7::new(1),
                vel: u7::new(1),
            },
        }
    }
    fn track_event(delta: u32, channel: u8) -> TrackEvent<'static> {
        TrackEvent {
            delta: u28::new(delta),
            kind: kind(channel),
        }
    }

    let tracks = vec![
        vec![track_event(1, 0), track_event(2, 1), track_event(4, 2)],
        vec![track_event(2, 3), track_event(2, 4), track_event(5, 5)],
    ];
    let result: Vec<_> = merge_tracks(&tracks[..]).collect();
    assert_eq!(
        result,
        vec![
            (1, 0, kind(0)),
            (2, 1, kind(3)),
            (3, 0, kind(1)),
            (4, 1, kind(4)),
            (7, 0, kind(2)),
            (9, 1, kind(5))
        ]
    )
}

#[cfg(feature = "convert-time")]
impl TryFrom<Header> for ConvertTicksToMicroseconds {
    type Error = TimeConversionError;

    /// Create a new `ConvertTicksToMicrosecond` with the given header.
    ///
    /// The parameter `header` is used to determine the meaning of "tick", since this is stored
    /// in the header in a midi file.
    fn try_from(header: Header) -> Result<Self, Self::Error> {
        let time_stretcher;
        let ticks_per_beat;
        use TimeConversionError::*;
        match header.timing {
            Timing::Metrical(t) => {
                let tpb = NonZeroU64::new(t.as_int() as u64).ok_or(ZeroTicksPerBeatNotSupported)?;
                time_stretcher = TimestampStretcher::new(
                    MICROSECONDS_PER_MINUTE / DEFAULT_BEATS_PER_MINUTE,
                    tpb,
                );
                ticks_per_beat = Some(tpb);
            }
            Timing::Timecode(Fps::Fps29, ticks_per_frame) => {
                // Frames per second = 30 / 1.001 = 30000 / 1001
                // microseconds = ticks * microseconds_per_second / (ticks_per_frame * frames_per_second) ;
                time_stretcher = TimestampStretcher::new(
                    MICROSECONDS_PER_SECOND * 1001,
                    NonZeroU64::new((ticks_per_frame as u64) * 30000)
                        .ok_or(ZeroTicksPerFrameNotSupported)?,
                );
                ticks_per_beat = None;
            }
            Timing::Timecode(fps, ticks_per_frame) => {
                // microseconds = ticks * microseconds_per_second / (ticks_per_frame * frames_per_second) ;
                time_stretcher = TimestampStretcher::new(
                    MICROSECONDS_PER_SECOND,
                    NonZeroU64::new((fps.as_int() as u64) * (ticks_per_frame as u64))
                        .ok_or(ZeroTicksPerFrameNotSupported)?,
                );
                ticks_per_beat = None;
            }
        }
        Ok(Self {
            time_stretcher,
            ticks_per_beat,
        })
    }
}

#[cfg(feature = "convert-time")]
impl<'a> MidiEvent for TrackEventKind<'a> {
    fn tempo(&self) -> Option<u32> {
        if let TrackEventKind::Meta(MetaMessage::Tempo(tempo)) = self {
            Some(tempo.as_int())
        } else {
            None
        }
    }
}

#[cfg(feature = "convert-time")]
#[test]
pub fn convert_ticks_to_microseconds_works_with_one_event() {
    // 120 beats per minute
    // = 120 beats per 60 seconds
    // = 120 beats per 60 000 000 microseconds
    // so the tempo is
    //   60 000 000 / 120 microseconds per beat
    //   = 10 000 000 / 20 microseconds per beat
    //   =    500 000 microseconds per beat
    let tempo_in_microseconds_per_beat = 500000;
    let ticks_per_beat = 32;
    // One event after 1 second.
    // One second corresponds to two beats, so to 64 ticks.
    let event_time_in_ticks: u64 = 64;
    let header = Header {
        timing: Timing::Metrical(u15::from(ticks_per_beat)),
        format: Format::SingleTrack,
    };
    let mut converter =
        ConvertTicksToMicroseconds::try_from(header).expect("No error expected at this point.");
    let microseconds = converter.convert(
        0,
        &TrackEventKind::Meta(MetaMessage::Tempo(u24::from(
            tempo_in_microseconds_per_beat,
        ))),
    );
    assert_eq!(microseconds, 0);
    let microseconds = converter.convert(
        event_time_in_ticks,
        &TrackEventKind::Midi {
            channel: u4::from(0),
            message: MidiMessage::NoteOn {
                key: u7::from(60),
                vel: u7::from(90),
            },
        },
    );
    assert_eq!(microseconds, 1000000);
}

#[cfg(feature = "convert-time")]
impl From<Header> for ConvertMicroSecondsToTicks {
    /// Create a new `ConvertMicroSecondsToTicks` with the given header.
    ///
    /// The parameter `header` is used to determine the meaning of "tick", since this is stored
    /// in the header in a midi file.
    fn from(header: Header) -> Self {
        let time_stretcher;
        let ticks_per_beat;
        match header.timing {
            Timing::Metrical(t) => {
                let tpb = t.as_int() as u64;
                time_stretcher = TimestampStretcher::new(
                    tpb,
                    NonZeroU64::new(MICROSECONDS_PER_MINUTE / DEFAULT_BEATS_PER_MINUTE)
                        .expect("Bug: MICROSECONDS_PER_MINUTE / DEFAULT_BEATS_PER_MINUTE should not be zero."),
                );
                ticks_per_beat = Some(tpb);
            }
            Timing::Timecode(Fps::Fps29, ticks_per_frame) => {
                // Frames per second = 30 / 1.001 = 30000 / 1001
                // ticks = microseconds * ticks_per_frame * frames_per_second / microseconds_per_second
                time_stretcher = TimestampStretcher::new(
                    (ticks_per_frame as u64) * 30000,
                    NonZeroU64::new(MICROSECONDS_PER_SECOND * 1001)
                        .expect("Bug: MICROSECONDS_PER_SECOND * 1001 should not be zero."),
                );
                ticks_per_beat = None;
            }
            Timing::Timecode(fps, ticks_per_frame) => {
                // ticks = microseconds * ticks_per_frame * frames_per_second / microseconds_per_second
                time_stretcher = TimestampStretcher::new(
                    (fps.as_int() as u64) * (ticks_per_frame as u64),
                    NonZeroU64::new(MICROSECONDS_PER_SECOND)
                        .expect("Bug: MICROSECONDS_PER_SECOND should not be zero."),
                );
                ticks_per_beat = None;
            }
        }
        Self {
            time_stretcher,
            ticks_per_beat,
        }
    }
}

#[cfg(feature = "convert-time")]
#[test]
pub fn convert_microseconds_to_ticks_works_with_one_event() {
    // 120 beats per minute
    // = 120 beats per 60 seconds
    // = 120 beats per 60 000 000 microseconds
    // so the tempo is
    //   60 000 000 / 120 microseconds per beat
    //   = 10 000 000 / 20 microseconds per beat
    //   =    500 000 microseconds per beat
    let tempo_in_microseconds_per_beat = 500000;
    let ticks_per_beat = 32;
    // One event after 1 second.
    // One second corresponds to two beats, so to 64 ticks.
    let expected_vent_time_in_ticks: u64 = 64;
    let event_time_in_microseconds: u64 = 1000000;
    let header = Header {
        timing: Timing::Metrical(u15::from(ticks_per_beat)),
        format: Format::SingleTrack,
    };
    let mut converter = ConvertMicroSecondsToTicks::from(header);
    let microseconds = converter
        .convert(
            0,
            &TrackEventKind::Meta(MetaMessage::Tempo(u24::from(
                tempo_in_microseconds_per_beat,
            ))),
        )
        .expect("No error expected at this point.");
    assert_eq!(microseconds, 0);
    let observed_event_time_in_ticks = converter
        .convert(
            event_time_in_microseconds,
            &TrackEventKind::Midi {
                channel: u4::from(0),
                message: MidiMessage::NoteOn {
                    key: u7::from(60),
                    vel: u7::from(90),
                },
            },
        )
        .expect("No error expected at this point");
    assert_eq!(expected_vent_time_in_ticks, observed_event_time_in_ticks);
}

/// The error type returned by the [`TrackSeparator::push()`] method.
#[derive(Debug)]
#[non_exhaustive]
pub enum SeparateTracksError {
    /// The specified time overflows what can be specified.
    TimeOverflow,
    /// Time decreased from one event to a next event.
    TimeCanOnlyIncrease,
}

impl From<TryFromIntError> for SeparateTracksError {
    fn from(_: TryFromIntError) -> Self {
        SeparateTracksError::TimeOverflow
    }
}

impl Display for SeparateTracksError {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        match self {
            SeparateTracksError::TimeOverflow => {
                write!(
                    f,
                    "the time overflows what can be represented in a midi file."
                )
            }
            SeparateTracksError::TimeCanOnlyIncrease => {
                write!(f, "subsequent events with decreasing time found.")
            }
        }
    }
}

impl Error for SeparateTracksError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }
}

/// Write to a track, keeping ... well, keeping "track" of the timing relative to the beginning.
#[derive(Clone)]
struct TrackWriter<'a> {
    track: Track<'a>,
    ticks: u64,
}

impl<'a> TrackWriter<'a> {
    fn new() -> Self {
        TrackWriter {
            track: Vec::new(),
            ticks: 0,
        }
    }

    fn push(&mut self, ticks: u64, kind: TrackEventKind<'a>) -> Result<(), SeparateTracksError> {
        use SeparateTracksError::*;
        if self.ticks > ticks {
            return Err(TimeCanOnlyIncrease);
        }
        let delta = ticks - self.ticks;
        let delta = u28::try_from(u32::try_from(delta)?).ok_or(TimeOverflow)?;
        self.ticks = ticks;
        self.track.push(TrackEvent { delta, kind });
        Ok(())
    }
}

/// Separate events into different tracks.
pub struct TrackSeparator<'a> {
    tracks: Vec<TrackWriter<'a>>,
}

impl<'a> TrackSeparator<'a> {
    /// Create a new `TrackSeparator`.
    ///
    /// # Example
    /// ```
    /// use midi_reader_writer::midly_0_5::TrackSeparator;
    /// let track_separator = TrackSeparator::new();
    /// let tracks : Vec<_> = track_separator.collect();
    /// assert!(tracks.is_empty());
    /// ```
    #[inline]
    pub fn new() -> Self {
        TrackSeparator { tracks: Vec::new() }
    }

    /// Push a new event.
    ///
    /// # Parameters
    /// * `ticks`: the time in midi ticks, relative to the beginning
    ///   of the tracks
    /// * `track_index`: the index of the track to which the event belongs
    /// * `event`: the event
    ///
    /// # Example
    /// ```
    /// use midi_reader_writer::midly_0_5::{TrackSeparator, exports::{TrackEventKind, TrackEvent, MetaMessage}};
    /// # use midi_reader_writer::midly_0_5::exports::{
    /// #     MidiMessage,
    /// #     num::{u4, u7, u28}
    /// # };
    ///
    /// // Create a note on event with the given channel
    /// fn note_on_with_channel(channel: u8) -> TrackEventKind<'static> {
    ///     // ...
    /// #     TrackEventKind::Midi {
    /// #         channel: u4::new(channel),
    /// #         message: MidiMessage::NoteOn {
    /// #             key: u7::new(1),
    /// #             vel: u7::new(1),
    /// #         },
    /// #     }
    /// }
    ///
    /// // Create a note on event with the given delta and channel.
    /// fn note_on_with_delta_and_channel(delta: u32, channel: u8) -> TrackEvent<'static> {
    ///     // ...
    /// #     TrackEvent {
    /// #        delta: u28::new(delta),
    /// #        kind: note_on_with_channel(channel),
    /// #    }
    /// }
    ///
    /// // Create an end-of-track event with the given delta.
    /// fn end_of_track(delta: u32) -> TrackEvent<'static> {
    ///     // ...
    /// #     TrackEvent {
    /// #       delta: u28::new(delta),
    /// #       kind: TrackEventKind::Meta(MetaMessage::EndOfTrack)
    /// #    }
    /// }
    ///
    /// let mut track_separator = TrackSeparator::new();
    /// track_separator.push(5, 0, note_on_with_channel(0));
    /// track_separator.push(0, 1, note_on_with_channel(1));
    /// track_separator.push(10, 0, note_on_with_channel(2));
    /// track_separator.push(3, 1, TrackEventKind::Meta(MetaMessage::EndOfTrack));
    /// let tracks : Vec<_> = track_separator.collect();
    /// assert_eq!(tracks.len(), 2);
    /// assert_eq!(
    ///             tracks[0],
    ///             vec![
    ///                 note_on_with_delta_and_channel(5, 0),
    ///                 note_on_with_delta_and_channel(5, 2),
    ///                 end_of_track(0)
    ///             ]
    /// );
    /// assert_eq!(tracks[1], vec![note_on_with_delta_and_channel(0, 1), end_of_track(3)]);
    /// ```

    #[inline]
    pub fn push(
        &mut self,
        ticks: u64,
        track_index: usize,
        event: TrackEventKind<'a>,
    ) -> Result<(), SeparateTracksError> {
        if self.tracks.len() <= track_index {
            self.tracks.resize(track_index + 1, TrackWriter::new());
        }
        self.tracks[track_index].push(ticks, event)
    }

    /// Append all events from an iterator of triples of type `(u64, usize, TrackEventKind)`,
    /// where
    /// * the first item of the triple (of type `u64`) is the time in midi ticks, relative to the beginning
    ///   of the tracks
    /// * the second item of the triple (of type `usize`) is the track index.
    /// * the last item of the triple is the event itself.
    /// # Example
    /// ```
    /// use midi_reader_writer::midly_0_5::{TrackSeparator, exports::{TrackEventKind, TrackEvent}};
    /// # use midi_reader_writer::midly_0_5::exports::{MetaMessage, MidiMessage, num::{u4, u7, u28}};
    ///
    /// fn note_on_with_channel(channel: u8) -> TrackEventKind<'static> {
    ///     // ...
    /// #    TrackEventKind::Midi {
    /// #        channel: u4::new(channel),
    /// #        message: MidiMessage::NoteOn {
    /// #            key: u7::new(1),
    /// #            vel: u7::new(1),
    /// #        },
    /// #    }
    /// }
    ///
    /// fn note_on_with_channel_and_delta_time(delta: u32, channel: u8) -> TrackEvent<'static> {
    ///     // ...
    /// #    TrackEvent {
    /// #        delta: u28::new(delta),
    /// #        kind: note_on_with_channel(channel),
    /// #    }
    /// }
    /// // Create an end-of-track event with the given delta.
    /// fn end_of_track(delta: u32) -> TrackEvent<'static> {
    ///     // ...
    /// #     TrackEvent {
    /// #       delta: u28::new(delta),
    /// #       kind: TrackEventKind::Meta(MetaMessage::EndOfTrack)
    /// #    }
    /// }
    ///
    /// let events : Vec<(u64, usize, _)>= vec![
    ///     (1, 0, note_on_with_channel(0)),
    ///     (2, 1, note_on_with_channel(3)),
    ///     (3, 0, note_on_with_channel(1)),
    ///     (4, 1, note_on_with_channel(4)),
    ///     (7, 0, note_on_with_channel(2)),
    ///     (9, 1, note_on_with_channel(5)),
    /// ];
    ///
    /// let mut separator = TrackSeparator::new();
    /// separator.extend(events).expect("No error should occur here.");
    /// let separated : Vec<_> = separator.collect();
    ///
    /// assert_eq!(
    ///     separated,
    ///     vec![
    ///         vec![
    ///             note_on_with_channel_and_delta_time(1, 0),
    ///             note_on_with_channel_and_delta_time(2, 1),
    ///             note_on_with_channel_and_delta_time(4, 2),
    ///             end_of_track(0)
    ///         ],
    ///         vec![
    ///             note_on_with_channel_and_delta_time(2, 3),
    ///             note_on_with_channel_and_delta_time(2, 4),
    ///             note_on_with_channel_and_delta_time(5, 5),
    ///             end_of_track(0)
    ///         ],
    ///     ]
    /// );
    /// ```
    #[inline]
    pub fn extend<I>(&mut self, iter: I) -> Result<(), SeparateTracksError>
    where
        I: IntoIterator<Item = (u64, usize, TrackEventKind<'a>)>,
    {
        for (ticks, track_index, event) in iter {
            self.push(ticks, track_index, event)?
        }
        Ok(())
    }

    /// Create a collection containing all the tracks.
    ///
    /// The number of tracks is determined by the highest track index.
    ///
    /// An [`EndOfTrack`](exports::MetaMessage::EndOfTrack) event is added to the tracks that do not end with an `EndOfTrack` event.
    pub fn collect<B>(self) -> B
    where
        B: FromIterator<Track<'a>>,
    {
        self.tracks
            .into_iter()
            .map(|t| {
                let mut track = t.track;
                let end_of_track_is_marked_with_event = if let Some(last_event) = track.last() {
                    last_event.kind == TrackEventKind::Meta(MetaMessage::EndOfTrack)
                } else {
                    false
                };
                if !end_of_track_is_marked_with_event {
                    track.push(TrackEvent {
                        kind: TrackEventKind::Meta(MetaMessage::EndOfTrack),
                        delta: u28::new(0),
                    });
                }
                track
            })
            .collect()
    }
}