melanchall / drywetmidi

.NET library to read, write, process MIDI files and to work with MIDI devices
https://melanchall.github.io/drywetmidi
MIT License
545 stars 75 forks source link

How to use on Unity 2017? #3

Closed FabioZumbi12 closed 6 years ago

FabioZumbi12 commented 6 years ago

Hi, Sorry but this is not an issue, but i want to use this library on a Unity project but i dont know how. Do you know if is possible, and how to use?

I found other midi readers but what i want its only a midi file reader, i dont want to play.

Thanks!

melanchall commented 6 years ago

Hi,

You can try to download the project sources, copy them to your Unity solution and build it. There is the one file in the library code that, I think, will not be compiled – DryWetMidi/Smf/Utilities/IOUtilities.cs – since it uses WinAPI functions. To compile this file you need to eliminate WinAPI calls. Or I can prepare "good" file for you.

Let me know how integration the library into your project is going.

Max

FabioZumbi12 commented 6 years ago

Great, thanks to fast response!!

I have downloaded the source and imported the folder into my project. With this i can use some classes like MidiFile, but what i want its only get all notes on/off and the timesample of each note to i iterate bteween notes and sinc using the note time to sync with an audio file with same timesamples. You know a good way to this? And seems some options from your examples is not available for me. Its because i using C#?

Something like this:

midiFile.GetTempoMap();
midiFile.GetTimedEvents();

(i still lookin into your wiki) Edit: I working in a game like GuitarHero/RockBand xD Edit 2: Basically is working, now i only need to get all of this output as is, like tracknames and notes: http://prntscr.com/hnct0n This is part of the debug logs using this code:

        MidiFile midi = MidiFile.Read(midiFilePath);        
        foreach (TrackChunk track in midi.GetTrackChunks())
        {
            Debug.Log("Track: " + track.ToString());
            foreach (MidiEvent ev in track.Events)
            {
                Debug.Log("MidiEvent: " + ev.ToString());
            }
        }
melanchall commented 6 years ago

Please clarify what do you need. You want to print track names and events?

There is no such thing like name of track in a MIDI file. But track chunk can contain Sequence/Track Name event. To get it you can use this code:

var trackName = trackChunk.Events
                          .OfType<SequenceTrackNameEvent>()
                          .FirstOrDefault()
                          ?.Text;

All events override ToString so output should be nice.

You said that you want to get notes with timestamps. You can use this code:

IEnumerable<Note> notes = midiFile.GetNotes();

Note class has Time property which is what you need.

FabioZumbi12 commented 6 years ago

Ohh man, it worked!

I needed to import classes "System.Linq" and "Melanchall.DryWetMidi.Smf.Interaction" to allow me to use GetNotes() and OfType.

Thaaank you for help for now.

Oh, just to know, to i use this on my game, i just neet to put your credits and copyright right?

melanchall commented 6 years ago

You are welcome :)

Feel free to open new issues if you have any questions about the library.

If you will put link to the library along with its name to your game I'll be very happy :) But it is completely up to you.

FabioZumbi12 commented 6 years ago

Hi, Me again. Just a little question. I did not found the note time equivalent to PCM timesamples to sync with Unity AudioSources. I can use seconds, but is not precise and Unity source only give me pcm samples and seconds. Can you give me a tip about this?

melanchall commented 6 years ago

Hi,

You can try to operate by seconds. To get note's time in this format use this code:

var tempoMap = midiFile.GetTempoMap();
var metricTime = note.TimeAs<MetricTimeSpan>(tempoMap);

Now you have note's time as an instance of the MetricTimeSpan class. Underlying measure of this class is number of microseconds. To access this value use the TotalMicroseconds property of the MetricTimeSpan.

You can also convert PCM sample number to microseconds too knowing sample rate of your audio. I think number of microseconds is the most accurate measure in your case.

FabioZumbi12 commented 6 years ago

Hi, Thanks and i using this way you suggested but after some time breaking my head i see this metric time only give me a limited time. As example:

var metricTime = note.TimeAs<MetricTimeSpan>(tempoMap);

Using metricTime.Seconds only get a number 0 - 59, and not minutes and hours represented as seconds, only seconds until one minute, then get back to 0 where the correct should be 60, 61, 62...

Is this intentional?

The same with milliseconds, when we have on second in milliseconds, back to 0.

EDIT: Using note.TimedNoteOnEvent.TimeAs<MetricTimeSpan>(tempo).TotalMicroseconds and converting my song time to microseconds i done the sync.

But get as suggestion add a TotalMillis to tempo options too ;)

melanchall commented 6 years ago

Hi,

It is done intentionally that Hours returns number 0-23, Minutes and Seconds return number 0-59. Good suggestion to add properties like TotalSeconds, TotalMinutes and so on. Thank you :)

note.TimedNoteOnEvent.TimeAs<MetricTimeSpan>(tempo).TotalMicroseconds is totally equivalent to note.TimeAs<MetricTimeSpan>(tempo).TotalMicroseconds and I recommend to use the last one.

mherbold commented 4 years ago

Hello - Google search led me here to answer a similar question of mine, but GetNotes is apparently no longer a method of MidiFile. What is the current way to do this?

melanchall commented 4 years ago

Hi,

GetNotes is an extension method for MidiFile. You need to add

using Melanchall.DryWetMidi.Interaction;

at the top of a class file in order to be able to use the method.

mherbold commented 4 years ago

Thanks for the quick reply! facepalm I should have been able to figure that one out without bugging you - sorry!

I'm now actually using trackChunk.GetNotes() instead, inside my track chunks loop. Mentioning it here so others can find this as well. Thank you!

melanchall commented 2 years ago

:rocket: Official DryWetMIDI asset has been released in Unity Asset Store so now you can use standard Unity ways to integrate the library. More info in the Using in Unity article.