Giter Club home page Giter Club logo

Comments (15)

melanchall avatar melanchall commented on July 19, 2024 1

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.

melanchall avatar melanchall commented on July 19, 2024 1

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.

ronnieoverby avatar ronnieoverby commented on July 19, 2024

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.

ronnieoverby avatar ronnieoverby commented on July 19, 2024

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.

melanchall avatar melanchall commented on July 19, 2024

@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.

melanchall avatar melanchall commented on July 19, 2024

@ronnieoverby Did you try new time span class?

from drywetmidi.

ronnieoverby avatar ronnieoverby commented on July 19, 2024

from drywetmidi.

melanchall avatar melanchall commented on July 19, 2024

@ronnieoverby Any news? :)

from drywetmidi.

ronnieoverby avatar ronnieoverby commented on July 19, 2024

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.

melanchall avatar melanchall commented on July 19, 2024

OK, I'll think on combining beats and cents together. The problem is how to name the time span class :)

from drywetmidi.

ronnieoverby avatar ronnieoverby commented on July 19, 2024

from drywetmidi.

melanchall avatar melanchall commented on July 19, 2024

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.

ronnieoverby avatar ronnieoverby commented on July 19, 2024

from drywetmidi.

melanchall avatar melanchall commented on July 19, 2024

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.

ronnieoverby avatar ronnieoverby commented on July 19, 2024

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)

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.