#MIDI #parser #builder #SMF

ghakuf

A Rust library for parsing/building SMF (Standard MIDI File)

10 releases

0.5.3 Feb 2, 2019
0.5.2 Feb 2, 2019
0.5.1 Oct 29, 2017
0.4.0 Sep 15, 2017
0.2.2 Jul 11, 2017

#6 in Audio

Download history 14/week @ 2018-12-13 40/week @ 2018-12-20 2/week @ 2018-12-27 18/week @ 2019-01-03 16/week @ 2019-01-10 7/week @ 2019-01-17 6/week @ 2019-01-24 28/week @ 2019-01-31 4/week @ 2019-02-07 5/week @ 2019-02-14 14/week @ 2019-02-21 33/week @ 2019-02-28 8/week @ 2019-03-07 4/week @ 2019-03-14 57/week @ 2019-03-21

70 downloads per month

MIT/Apache

68KB
1K SLoC

ghakuf

A Rust library for parsing/building SMF (Standard MIDI File).

Build Status Crates.io

Examples

ghakuf has parse module and build module separatory.

Perser

ghakuf's parser is made by Event-driven online algorithm. You must prepare original handler implementing Handler trait to catch SMF messages. Any number of handlers you can add for parser if you want.

use ghakuf::messages::*;
use ghakuf::reader::*;
use std::path;

let path = path::Path::new("test.mid");
let mut handler = HogeHandler{};
let mut reader = Reader::new(
    &mut handler,
    &path,
).unwrap();
let _ = reader.read();

struct HogeHandler {}
impl Handler for HogeHandler {
    fn header(&mut self, format: u16, track: u16, time_base: u16) {
      // Something
    }
    fn meta_event(&mut self, delta_time: u32, event: &MetaEvent, data: &Vec<u8>) {
      // you
    }
    fn midi_event(&mut self, delta_time: u32, event: &MidiEvent) {
      // want
    }
    fn sys_ex_event(&mut self, delta_time: u32, event: &SysExEvent, data: &Vec<u8>) {
      // to
    }
    fn track_change(&mut self) {
      // do
    }
}

Builder

ghakuf build SMF by Message enums. Message enum consists of MetaEvent, MidiEvent, SysExEvent, and TrackChange. You can use running status if you want. At track change, you should use not only MetaEvent::EndOfTrack message, but also TrackChange message.

use ghakuf::messages::*;
use ghakuf::writer::*;
use std::path;

let tempo: u32 = 60 * 1000000 / 102; //bpm:102
let write_messages: Vec<Message> = vec![
    Message::MetaEvent {
        delta_time: 0,
        event: MetaEvent::SetTempo,
        data: [(tempo >> 16) as u8, (tempo >> 8) as u8, tempo as u8].to_vec(),
    },
    Message::MetaEvent {
        delta_time: 0,
        event: MetaEvent::EndOfTrack,
        data: Vec::new(),
    },
    Message::TrackChange,
    Message::MidiEvent {
        delta_time: 0,
        event: MidiEvent::NoteOn {
            ch: 0,
            note: 0x3c,
            velocity: 0x7f,
        },
    },
    Message::MidiEvent {
        delta_time: 192,
        event: MidiEvent::NoteOn {
            ch: 0,
            note: 0x40,
            velocity: 0,
        },
    },
    Message::MetaEvent {
        delta_time: 0,
        event: MetaEvent::EndOfTrack,
        data: Vec::new(),
    }
];
let path = path::Path::new("examples/example.mid");
let mut writer = Writer::new();
writer.running_status(true);
for message in &write_messages {
    writer.push(&message);
}
let _ = writer.write(&path);

Supported SMF Event

You can use three type events. In Message enum, these events have delta time and data.

Meta Event

  • SequenceNumber
  • TextEvent
  • CopyrightNotice
  • SequenceOrTrackName
  • InstrumentName
  • Lyric
  • Marker
  • CuePoint
  • MIDIChannelPrefix
  • EndOfTrack
  • SetTempo
  • SMTPEOffset
  • TimeSignature
  • KeySignature
  • SequencerSpecificMetaEvent

MIDI Event

  • NoteOff { ch: u8, note: u8, velocity: u8 }
  • NoteOn { ch: u8, note: u8, velocity: u8 }
  • PolyphonicKeyPressure { ch: u8, note: u8, velocity: u8 }
  • ControlChange { ch: u8, control: u8, data: u8 }
  • ProgramChange { ch: u8, program: u8 }
  • ChannelPressure { ch: u8, pressure: u8 }
  • PitchBendChange { ch: u8, data: i16 }

System Exclusive Event

  • (F0 event)
  • (F7 event)

License

ghakuf is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.

See LICENSE-APACHE, and LICENSE-MIT for details.

Dependencies

~158KB