EasyMidi is a simple and reliable library for working with standard midi file (SMF).
A step by step series of examples that tell you have to get a development env running
Get repository
go get github.com/algoGuy/EasyMIDI/...
package main
import (
"bufio"
"fmt"
"os"
"github.com/algoGuy/EasyMIDI/smfio"
)
func main() {
// Open test midi file
file, _ := os.Open("./Test_-_test1.mid")
defer file.Close()
// Read and save midi to smf.MIDIFile struct
midi, err := smfio.Read(bufio.NewReader(file))
if err != nil {
fmt.Println(err)
}
// Get zero track
track := midi.GetTrack(0)
// Print number of midi tracks
fmt.Println(midi.GetTracksNum())
// Get all midi events via iterator
iter := track.GetIterator()
for iter.MoveNext() {
fmt.Println(iter.GetValue())
}
}
Created midi file from scratch. In current example output midi must contains one note.
package main
import (
"bufio"
"log"
"os"
"github.com/algoGuy/EasyMIDI/smf"
"github.com/algoGuy/EasyMIDI/smfio"
)
func main() {
// Create division
division, err := smf.NewDivision(960, smf.NOSMTPE)
checkErr(err)
// Create new midi struct
midi, err := smf.NewSMF(smf.Format0, *division)
checkErr(err)
// Create track struct
track := &smf.Track{}
// Add track to new midi struct
err = midi.AddTrack(track)
checkErr(err)
// Create some midi and meta events
midiEventOne, err := smf.NewMIDIEvent(0, smf.NoteOnStatus, 0x00, 0x30, 0x50)
checkErr(err)
midiEventTwo, err := smf.NewMIDIEvent(10000, smf.NoteOnStatus, 0x00, 0x30, 0x00)
checkErr(err)
metaEventOne, err := smf.NewMetaEvent(0, smf.MetaEndOfTrack, []byte{})
checkErr(err)
// Add created events to track
err = track.AddEvent(midiEventOne)
checkErr(err)
err = track.AddEvent(midiEventTwo)
checkErr(err)
err = track.AddEvent(metaEventOne)
checkErr(err)
// Save to new midi source file
outputMidi, err := os.Create("outputMidi.mid")
checkErr(err)
defer outputMidi.Close()
// Create buffering stream
writer := bufio.NewWriter(outputMidi)
smfio.Write(writer, midi)
writer.Flush()
}
func checkErr(err error) {
if err != nil {
log.Fatalln(err)
}
}
- Go - The Go Programming Language
This project is licensed under the MIT License - see the LICENSE file for details