Skip to content

Running through headphones causing out of sync frames #466

Description

@petar-dambovaliev

Hi, i am making an application that would allow people to compose music via a script.
I am in early development stage and i am having some issues.
When i increase and decrease the song tempo, all instruments slow down and speed up in sync.
However, when i plug in my headphones and i change the tempo, all instruments go out of sync.
I have debugged the way i calculate note duration and seems to be correct.
So i don't know what else to do but post my problem here.

const SEC_PER_MIN: f32 = 60.0;

pub struct Rates<'a> {
    pub(crate) sample_rate: Hz,
    pub(crate) beat_per_min: f32,
    pub(crate) duration: &'a Duration,
}

pub fn calc_duration(rates: Rates) -> Hz {
    // example rate per beat =  (44100 samples per second * 60) / 60 beats per minute
    let rate_per_beat: f32 = (rates.sample_rate * SEC_PER_MIN) / rates.beat_per_min;
    rate_per_beat * rates.duration.to_beats()
}

#[derive(Debug, Clone)]
pub enum Duration {
    Whole,
    HalfDotted,
    Half,
    QuarterDotted,
    Quarter,
    EightDotted,
    Eight,
    SixteenthDotted,
    Sixteenth,
    ThirtySecondDotted,
    ThirtySecond,
}

impl Duration {
    pub fn to_beats(&self) -> f32 {
        match self {
            Self::Whole => 4.0,
            Self::HalfDotted => 3.0,
            Self::Half => 2.0,
            Self::QuarterDotted => 1.5,
            Self::Quarter => 1.0,
            Self::EightDotted => 0.75,
            Self::Eight => 0.5,
            Self::SixteenthDotted => 0.375,
            Self::Sixteenth => 0.25,
            Self::ThirtySecondDotted => 0.1875,
            Self::ThirtySecond => 0.125,
        }
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions