Comments (15)
Hi,
Thanks for using the library!
I took Bar.Beat.Ticks notation from Cubase (another DAW like Reaper). Also it allows to set time and length as accurately as possible. double
can lead to rounding errors on conversion to ticks (which is required since all timestamps stored as integer numbers inside a MIDI file).
Your code is good. I already have plans to expose utilities to get bar/beat length in MIDI ticks so users can easily perform such conversions. It seems I should increase priority of this task :) I'll implement this utilities within this issue so please don't close it.
from drywetmidi.
I've added BarBeatTimeSpanUtilities
with GetBarLength
and GetBeatLength
methods which allow to rewrite your method:
long Convert(int bar, double beats, TempoMap tempoMap)
{
var barPart = TimeConverter.ConvertFrom(new BarBeatTimeSpan(bar), tempoMap);
var beatsPart = BarBeatTimeSpanUtilities.GetBeatLength(bar, tempoMap) * beats;
return barPart + (long)Math.Round(beatsPart);
}
New API is in develop branch so if you want to try it right now you can build the library from sources.
Also I'm thinking on providing customizable beat division so it can be ticks or cents.
from drywetmidi.
This logic seems to work for me.
static readonly BarBeatTimeSpan OneBeat = new BarBeatTimeSpan(0, 1);
long Convert(int bar, double beats, TempoMap tempoMap)
{
var wholeBeatTimeSpan = new BarBeatTimeSpan(bar, (long)beats);
var timeOfWholeBeat = TimeConverter.ConvertFrom(wholeBeatTimeSpan, tempoMap);
var wholeBeats = (long)beats;
if (beats == wholeBeats)
// not fractional
return timeOfWholeBeat;
var beatLength = LengthConverter.ConvertFrom(
OneBeat, timeOfWholeBeat, tempoMap);
var partialBeats = beats - wholeBeats;
var partialTicks = partialBeats * beatLength;
return timeOfWholeBeat + (long)partialTicks;
}
If there's a more straightforward way, please let me know. Thanks, again!
from drywetmidi.
Awesome!
For my purposes I needed to expand my original Convert
method to calculate both time and length of an event:
static readonly BarBeatTimeSpan WdOneBeat = new BarBeatTimeSpan(0, 1);
(long time, long len) ConvertWetDry((long bar, double beat, double len) e, TempoMap tempoMap)
{
// my bars/beats are 1-based
var bar = e.bar - 1;
var beat = e.beat - 1;
var wdTime = TimeConverter.ConvertFrom(
new BarBeatTimeSpan(bar, (long)beat), tempoMap);
var lzOneBeatTicks = new Lazy<long>(Get1BeatTicks);
var fraction = e.beat - Math.Truncate(e.beat);
if (fraction != 0)
{
// there is a fraction of a beat to account for
// how manyTicks is OneBeat at this time?
var extraTicks = fraction * lzOneBeatTicks.Value;
wdTime += (long)extraTicks;
}
// now the length
var wdLen = LengthConverter.ConvertFrom(
new BarBeatTimeSpan(bar, (long)e.len), wdTime, tempoMap);
fraction = e.len - Math.Truncate(e.len);
if (fraction != 0)
{
var extraTicks = fraction * lzOneBeatTicks.Value;
wdLen += (long)extraTicks;
}
return (wdTime, wdLen);
long Get1BeatTicks() =>
LengthConverter.ConvertFrom(WdOneBeat, wdTime, tempoMap);
}
I will rewrite this method using your new utilities on the develop branch the next chance I get.
Thanks!
from drywetmidi.
@ronnieoverby I've added new time span class – BarBeatCentsTimeSpan
. Cents is a double
number from 0
to 100
. So you can throw away your conversion and work with this new class :)
BarBeatCentsTimeSpan
is in develop branch so if you want to try it right now you can build the library from sources.
from drywetmidi.
@ronnieoverby Did you try new time span class?
from drywetmidi.
from drywetmidi.
@ronnieoverby Any news? :)
from drywetmidi.
Seems to work, but I did run into an argument range exception with cents
.
I would prefer if BarBeatCentsTimeSpan
didn't have a Cents
property, but instead had a double Beats
property, that could represent both the beat and the cents with one value. That way, any value for the cents part would be valid.
In any case, it's nice to avoid having to do the math myself. Thanks for the improvement.
from drywetmidi.
OK, I'll think on combining beats and cents together. The problem is how to name the time span class :)
from drywetmidi.
from drywetmidi.
Current name implies there is a cents part of the time span. But for double beat there is no this part. It's just bar and fractional beat. Cent is a hundredth part (like percent). But fractional part of a beat is not.
There is a classical programming joke: There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors. :)
Maybe BarBeatFractionTimeSpan
is a good name?
from drywetmidi.
from drywetmidi.
I've changed BarBeatCentsTimeSpan
to BarBeatFractionTimeSpan
with Bars
and Beats
properties where Beats
are double
.
@ronnieoverby Please check this new API and close the issue if you're happy with it.
from drywetmidi.
That change suits my purposes perfectly. I'm very grateful that you were so considerate of what I was trying to accomplish.
from drywetmidi.
Related Issues (20)
- 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
- Skipping notes HOT 3
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.