Giter Club home page Giter Club logo

Comments (10)

melanchall avatar melanchall commented on July 19, 2024

Hi,

I want to split a midi file into windows of 8 bars, with a step size of 1 bar

So what the length of each new MIDI file? 1 bar or 8 bars?

from drywetmidi.

cristianmtr avatar cristianmtr commented on July 19, 2024

Thanks for the quick reply!

8 bars. Then move 1 bar forward in time, and generate the next 8 bars from the file. And so on, until I reach the end of the file (the last 8 bar sequence)

Does that make sense?

from drywetmidi.

melanchall avatar melanchall commented on July 19, 2024

Thanks for clarification. Please try this code:

using Melanchall.DryWetMidi.Smf;
using Melanchall.DryWetMidi.Smf.Interaction;
using Melanchall.DryWetMidi.Tools;

// ...

var newFiles = midiFile.SplitByGrid(
    new SteppedGrid(new BarBeatTimeSpan(8, 0), new BarBeatTimeSpan(1, 0)));

This code splits midiFile into new files with length of 8 bars and 1 bar:

|--------|-|--------|-|--------|-|--------|-|--------|

Just remove files with length of 1 bar (every second) and you'll get desired result. If it's supposed that 1-bar files will be empty you can try to set RemoveEmptyFiles to true on SplittingMidiFileByGridSettings passed to SplitByGrid:

var newFiles = midiFile.SplitByGrid(
    new SteppedGrid(new BarBeatTimeSpan(8, 0), new BarBeatTimeSpan(1, 0)),
    new SplittingMidiFileByGridSettings
    {
        RemoveEmptyFiles = true
    });

You can find more information about MIDI file splitting on the library Wiki: MIDI file splitter. Please note that SplittingMidiFileByGridSettings has some useful properties that can help you to get desired result.

Thanks for your case. It seems it will be useful to add kind of Margin property to implementations of IGrid to support scenarios like yours.

from drywetmidi.

cristianmtr avatar cristianmtr commented on July 19, 2024

Wow, thanks so much for this!

But actually, it's not what I meant:

Let's take this line as being a musical piece, each - being a bar.

|--------|----------...

In the above I have extracted an 8-bar segment.

I then want to extract the next 8-bar segment, but with a step size of 1

-|--------|---------...

Does that make sense?

The code you provided extracts an 8-bar segments, then moves 9 bars as a step, and then extracts the next 8bar segment

from drywetmidi.

melanchall avatar melanchall commented on July 19, 2024

No, it extracts 8-bar file, then extracts 1-bar file, then extracts 8-bar file and so on :) You just need to ignore those files with length of 1 bar.

from drywetmidi.

cristianmtr avatar cristianmtr commented on July 19, 2024

Yes, I understand that. But that's now what I want.

Given a piece like this and an initial 8 bar segment.

|--------|----------...

I don't want to move 1 bar starting after the segment, but from the beginning

-|--------|---------...

As compared with what you are doing, where the next segment would be this:

---------|--------|-...

It's a sliding window.

from drywetmidi.

melanchall avatar melanchall commented on July 19, 2024

Oh, sorry, finally got it :) I'll try to provide you a solution a bit later.

from drywetmidi.

cristianmtr avatar cristianmtr commented on July 19, 2024

Thanks a lot !

I am trying myself to figure it out, but my C# skills aren't as sharp as they used to be...

from drywetmidi.

melanchall avatar melanchall commented on July 19, 2024

Please try this code:

var result = new List<MidiFile>();
var windowSize = 8;

for (var i = 0; ; i++)
{
    var startTime = new BarBeatTimeSpan(i, 0);
    var endTime = new BarBeatTimeSpan(i + windowSize, 0);

    var grid = new ArbitraryGrid(new[] { startTime, endTime });
    var newFiles = midiFile.SplitByGrid(grid).ToList();

    if (!newFiles.Any())
        break;
    else if (newFiles.Count == 1)
        result.Add(newFiles[0]);
    else
    {
        var secondNewFile = newFiles[1];
        var tempoMap = secondNewFile.GetTempoMap();
        var length = secondNewFile.GetTimedEvents().LastOrDefault()?.TimeAs<BarBeatTimeSpan>(tempoMap);

        if (length.Bars == windowSize || (length.Bars == windowSize - 1 && length.Beats > 0 && length.Ticks > 0))
            result.Add(secondNewFile);
        else
            break;
    }
}

length.Bars == windowSize || (length.Bars == windowSize - 1 && length.Beats > 0 && length.Ticks > 0) means

  • length of new file is exactly of window size; or
  • length of new file is windowSize - 1 but it close to window size, so 7.3.99 will be considered as 8 bars; this condition is placed for cases of rounding errors during time/length conversions.

from drywetmidi.

cristianmtr avatar cristianmtr commented on July 19, 2024

Thanks a lot, that worked!

from drywetmidi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.