Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with multiple tracks #152

Closed
grantbrown opened this issue Dec 11, 2021 · 5 comments
Closed

Help with multiple tracks #152

grantbrown opened this issue Dec 11, 2021 · 5 comments
Labels
bug Bug report, or fixes a bug (if Pull Request) help wanted

Comments

@grantbrown
Copy link
Contributor

I think I must be missing something obvious, but I can't get multiple tracks to work. Based on the example here, I put together the following minimal example:

using MIDI
C = Note(60, 96, 0, 192)
E = Note(64, 96, 48, 144)
G = Note(67, 96, 96, 96)

inc = 192
file = MIDIFile()
track1 = MIDITrack()
track2 = MIDITrack()
notes1 = Notes() # tpq automatically = 960
notes2 = Notes() # tpq automatically = 960

push!(notes1, C)
push!(notes1, E)
push!(notes1, G)

push!(notes2, C)
push!(notes2, E)
push!(notes2, G)

addnotes!(track1, notes1)
addnotes!(track2, notes2)
addtrackname!(track1, "simple track")
addtrackname!(track2, "simple track2")
push!(file.tracks, track1)
push!(file.tracks, track2)
writeMIDIFile("test.mid", file)

This results in a file that won't play (Internal data stream error), but no errors are raised in Julia. Thoughts?

@Datseris Datseris added bug Bug report, or fixes a bug (if Pull Request) help wanted labels Dec 11, 2021
@Datseris
Copy link
Member

Datseris commented Dec 22, 2021

https://github.com/JuliaMusic/MIDI.jl/blob/master/src/midifile.jl#L31 the default MIDIFile has type 0. Can you try with file = MIDIFile(1,960,MIDITrack[]) instead, which is the type for multitrack???

@Datseris
Copy link
Member

I think that's the problem. From wikipedia:

image

@grantbrown
Copy link
Contributor Author

That did the trick, thanks! Would it be worth adding something like the following to the fileio_save methods, or do you want to leave it up to the users? I'm happy to test and put together a pull request if that would be useful.

if (data.format == 0 && length(tracks.tracks) > 1)
    @warn("MIDI file-type 0 only supports a single track")
end

Alternatively, is there more work to do merging the tracks into a single track?

@Datseris
Copy link
Member

The warning code you posted seems like a really good idea, yes please do a PR that adds this to the save function. I would not add more info to the documentation string though.

@Datseris
Copy link
Member

In the same PR I think it is worth to make the default MIDIFile() make a type 1 format instead of 0. This is not a breaking change because the default type is not documented to begin with.

grantbrown added a commit to grantbrown/MIDI.jl that referenced this issue Dec 31, 2021
Datseris pushed a commit that referenced this issue Jan 3, 2022
* Change default MIDIFile type to 1 as discussed in #152

* Add a warning for format 0 files with n-tracks > 1

* Bump minor version and update CHANGELOG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug report, or fixes a bug (if Pull Request) help wanted
Projects
None yet
Development

No branches or pull requests

2 participants