Comments (10)
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.
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.
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.
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.
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.
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.
Oh, sorry, finally got it :) I'll try to provide you a solution a bit later.
from drywetmidi.
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.
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, so7.3.99
will be considered as8
bars; this condition is placed for cases of rounding errors during time/length conversions.
from drywetmidi.
Thanks a lot, that worked!
from drywetmidi.
Related Issues (20)
- Can't sync midi file notes with audio file HOT 12
- HELP !!! Notes in panel to midi file?? HOT 9
- please help a noob HOT 6
- Remove all notes example from the README not working HOT 9
- Using wetdrymidi in C# project causes error CS0009
- InputDevice event listening crash HOT 2
- Crash when running in Unity on M2 MacBook HOT 3
- MidiDeviceException Internal error HOT 9
- Android Support
- Unity cannot exit after use HOT 10
- MidiClock sync problem HOT 6
- MIDI Tempo changes count returns 1 but the song has 6 tempo changes. HOT 1
- add il2cpp mode HOT 5
- CustomChunk no mididata throw error HOT 4
- CSharp Code Error HOT 2
- MIDI File returns error: InvalidMetaEventParameterValueException HOT 1
- Unable to send Note-On event to device HOT 8
- Cannot get track name to change HOT 10
- How to use DryWetMidi in Visual Basic (VS 2022) ? HOT 20
- Plans for MIDI 2.0 Support? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drywetmidi.