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

How can I append one midi-file to another? #42

Closed
FischertBilligheim opened this issue Oct 18, 2019 · 5 comments
Closed

How can I append one midi-file to another? #42

FischertBilligheim opened this issue Oct 18, 2019 · 5 comments
Labels
feature request API changes required

Comments

@FischertBilligheim
Copy link
Contributor

I would like to append one midi file to another.
I did the following:
I opened the first file (multiple tracks)
I opened the second file (multiple tracks) - and changed the beginning of all tracks to the end of the first midi file.
I added then all tracks of file2 to file1.

That seems not be sufficiant. Do I have to recalculate all delta-times of file2 with the „ticks-per-count-value“ of file1?

Thanks for your support!
Regards
Thomas

@melanchall
Copy link
Owner

Hi Thomas,

Yes, you need to reduce time divisions of both files to common value. You can take either ticks per quarter note of first file or second one. Or you can set new value that will cause minimum rounding error on delta-times processing.

I have plans on MIDI file merge tool, so thanks for your question. I'll increase priority of this task.

Max

@FischertBilligheim
Copy link
Contributor Author

Hi Max,
I realized the functionality this evening in my application- and it works.
Maybe - as you mentioned - you can realize it in the library in the future…..

@melanchall melanchall added the feature request API changes required label Feb 2, 2020
@unitybru
Copy link

+1 for the ability to stitch MIDI files more easily.

For those who find this thread, here is how I did it. I had a list of MIDI files that I needed to append one after the other. The trick is to shift things before adding the chunks.

var fileNameOut = "myoutput.mid";
var midiFileOut = new MidiFile()
{
	TimeDivision = midiFile.TimeDivision // copied from master file
};

// Add all parts after shifting them
long addedSoFarMicroseconds = 0;
foreach (var midiPart in lsToWrite) // lsToWrite is a list of MidiFile objects
{
	var currentDuration = midiPart.GetDuration<MetricTimeSpan>();
	midiPart.ShiftEvents(new MetricTimeSpan(addedSoFarMicroseconds));
	midiFileOut.Chunks.AddRange(midiPart.Chunks);
	addedSoFarMicroseconds += currentDuration.TotalMicroseconds;
}
midiFileOut.Write(fileNameOut);

@melanchall
Copy link
Owner

@unitybru Thanks for the code snippet!

Please note that this code will work only for files with the same time division and tempo. I still have plans on MIDI files merger tool, but currently I'm busy with a big task about supporting macOS in Devices API of the library.

Merging MIDI files requires more work in general case than it can be supposed :)

@melanchall
Copy link
Owner

Starting with the 7.0.0 version you can use built-in Merger tool to merge MIDI files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request API changes required
Projects
None yet
Development

No branches or pull requests

3 participants