question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't sync midi file notes with audio file

See original GitHub issue

Hi, first of all, good job for this library and the support you always give to the users.

I’m new to unity and I’m having fun trying to make a guitar hero style game where there are notes spawned in lines and you have to press the correct button at the correct moment. To do it, I’m using midi files to have the map of the notes and an audio file to play the real song above the notes. The audio file is a simple mp3 file so it doesn’t contains any information of the notes.

Following the issue #79 I managed to spawn and visualize in sync notes in my game, but when I tried to add the real audio file to the game, I notice that after a few seconds, the visualized notes and the audio get out of sync and I can’t understand why.

With my little knowledge, I would expect that the midi file notes would map perfectly the audio file notes, but this isn’t the case… So I tried to fix this issue by matching the midi file position to the audio source file position but I still can’t get my game in sync.

This is the piece of code I wrote:

    # To delay the start of the music by X seconds
    [SerializeField] private float startAudioSourceDelayInSeconds = 4;

    # To delay the start of the playback by X seconds (it can be different from the audio delay)
    [SerializeField] private float startPlaybackDelayInSeconds = 5;

    private float dspSongTime;

    # I'm basing the sync with the unity audio system to be more precise
    void Awake() {
        dspSongTime = (float) AudioSettings.dspTime + startAudioSourceDelayInSeconds;
        audioSource.PlayScheduled(dspSongTime);
    }

    # I'm using to "MoveToTime" function to move the midi file to the audio source position each frame
    private void Update() {
        long positionInMicroseconds = (long)((AudioSettings.dspTime - dspSongTime + startPlaybackDelayInSeconds) * 1000000); // Convert seconds to microseconds
        var positionInTicks = new MetricTimeSpan(positionInMicroseconds);
        _playback.MoveToTime(positionInTicks);
    }

I would also share how I’m transposing the notes from top to bottom:

    # The initialPositionY is where the note is spawned
    private void Update() {
        float yPosition = initialPositionY - _playback.GetCurrentTime<MetricTimeSpan>().TotalMicroseconds / 100000.0f;
        transform.position = new Vector3(transform.position.x, yPosition, transform.position.z);
    }

Have you any idea on how I could reach my goal?

Issue Analytics

  • State:closed
  • Created 5 months ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
DanieleCalicommented, May 4, 2023

Just to update this question. I’ve downloaded waveform free as DAW and manually changed the tempo of both midi and audio file for each bar in order to get them in sync. Now it is also working on my game. Thanks again for the support, have a good day 😄

1reaction
DanieleCalicommented, May 1, 2023

So, you suggest to work on a DAW to sync midi and song file ok. I just have to study how to do it 😄 Thanks again with the support!

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to sync a midi file with an audio file
I want to take a classical music piece in .mp3 (or other audio file if necessary) file and take the same music piece...
Read more >
How can I sync midi with audio tracks? - Cockos Forums
So I got the idea to add an accompaniment to a song I have as a . flac file. I want to add...
Read more >
Audio and MIDI not recording in sync - Bugs & Workarounds
Audio signal and MIDI messages arriving in sync into Logic are not recorded in sync. ... Top track shows where the MIDI note...
Read more >
Synchronization problems and solutions in Logic Pro for Mac
MIDI and audio are not synchronized ... Open the File > Project Settings > Synchronization > Audio pane and choose ... MIDI and...
Read more >
Syncing MIDI file recorded on external instrument to audio ...
First I'd try this: In the MIDI ONLY project that you've imported change the MIDI track(s) time base to linear. Copy and paste...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found