13 个版本
使用旧的 Rust 2015
| 0.5.6 | 2020年10月4日 | 
|---|---|
| 0.5.5 | 2020年5月6日 | 
| 0.5.4 | 2020年1月28日 | 
| 0.5.3 | 2019年2月2日 | 
| 0.2.2 | 2017年7月11日 | 
#742 在 解析器实现
80 每月下载量
用于 chord-composer
66KB
 1K  SLoC
ghakuf
用于解析/构建 SMF (标准 MIDI 文件) 的 Rust 库。
示例
ghakuf 有独立的解析模块和构建模块。
解析器
ghakuf 的解析器是通过事件驱动的在线算法制作的。您必须准备原始的处理程序实现 Handler 特性以捕获 SMF 消息。您可以添加任意数量的处理程序到解析器。
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
    }
}
构建器
ghakuf 通过消息枚举构建 SMF。消息枚举包括 MetaEvent、MidiEvent、SysExEvent 和 TrackChange。如果您想使用运行状态,可以使用它。在轨道更改时,您不仅应使用 MetaEvent::EndOfTrack 消息,还应使用 TrackChange 消息。
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);
支持的 SMF 事件
您可以使用三种类型的事件。在消息枚举中,这些事件具有时间戳和数据。
元事件
- 序列号
- 文本事件
- 版权声明
- 序列或轨道名称
- 乐器名称
- 歌词
- 标记
- 提示点
- MIDI 通道前缀
- 轨道结束
- 设置速度
- SMTPE 偏移
- 拍号
- 调号
- 序列器特定元事件
MIDI 事件
- 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 }
系统独占事件
- (F0 事件)
- (F7 事件)
许可证
ghakuf 主要在 MIT 许可证和 Apache 许可证(版本 2.0)的条款下分发,部分受各种类似 BSD 的许可证保护。
请参阅 LICENSE-APACHE 和 LICENSE-MIT 以获取详细信息。
依赖关系
~210KB