1 Star 0 Fork 0

该用户已趴倒/MidiParser

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
Loading...
README
MIT

MidiParser

MidiParser is a wrapper library of AudioToolbox.framework about SMF(Standard Midi Files) that makes it easy to read/write midi files.

AudioToolbox framework provides many useful APIs for MIDI, but they are not Swifty styles (bridging with C pointers, explicit deallocation, etc). MidiParser wraps them and make more Swifty to access properties.

  • no need to care about deallocating
  • no need UnsafePointers, CFtypes
  • use Enum for constants

Usage

Read

Import *.mid file


import MidiParser

let midi = MidiData()
let data: Data = ... // load .mid file as `Data` type
midi.load(data: data)

print(midi.noteTracks.count) // 5
print(midi.noteTracks[0].trackName) // "Bass"
print(midi.noteTracks[1].trackName) // "Piano"
print(midi.noteTracks[2].trackName) // "Hi-hat only"
print(midi.noteTracks[3].trackName) // "Drums"
print(midi.noteTracks[4].trackName) // "Jazz Guitar"

print(midi.tempoTrack.timeSignatures) 
// [MidiParser.MidiTimeSignature(timeStamp: 0.0, numerator: 4, denominator: 2, cc: 24, bb: 8)]
print(midi.tempoTrack.extendedTempos) 
// [MidiParser.MidiExtendedTempo(timeStamp: 0.0, bpm: 120.0)]

print(midi.infoDictionary[.tempo] as! Int) // 120
print(midi.infoDictionary[.timeSignature] as! String) // 4/4

Midi Events

let track = midi.noteTracks[0]
print(track.count) // number of note events in the track
print(track[0]) // get note event from index with subscript
// MidiNote(timeStamp: 0.0, duration: 0.533333361, note: 45, velocity: 78, channel: 0, releaseVelocity: 64)

Write

// init empty midi
let midi = MidiData()
// add track
let track1 = midi.addTrack()

// add note events to track1
track1.add(note: MidiNote(timeStamp: 0, duration: 10, note: 50, velocity: 100, channel: 0))
track1.add(notes: [
    MidiNote(timeStamp: 5, duration: 10, note: 40, velocity: 100, channel: 0),
    MidiNote(timeStamp: 10, duration: 10, note: 40, velocity: 100, channel: 0),
    MidiNote(timeStamp: 20, duration: 10, note: 40, velocity: 100, channel: 0)
])

// set meta events
track1.keySignatures = [MidiKeySignature(timeStamp: 0, key: .minor(.A))]
track1.patch = MidiPatch(channel: 0, patch: .cello)
track1.trackName = "track1"

// write data to url
try! midi.writeData(to: tmp)
// generate midi file as Data
let output = midi.createData()

open with GarageBand... screen shot 2018-07-17 at 10 50 44

Installation

Carthage

To install using Carthage, add the following line to Cartfile:
github "matsune/MidiParser"

MIT License Copyright (c) 2018 Yuma Matsune Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
Swift
MIT
取消

发行版

暂无发行版

贡献者 (3)

全部

近期动态

3个月前创建了仓库
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Swift
1
https://gitee.com/zyf/MidiParser.git
git@gitee.com:zyf/MidiParser.git
zyf
MidiParser
MidiParser
master

搜索帮助