Example #1
0
//---------------------------------------------------------------------------------------
NoteTypeAndDots duration_to_note_type_and_dots(TimeUnits duration)
{
    //determine type
    int type = k_256th;
    if (is_lower_time(duration, 2.0))
        type = k_256th;
    else if (is_lower_time(duration, 4.0))
        type = k_128th;
    else if (is_lower_time(duration, 8.0))
        type = k_64th;
    else if (is_lower_time(duration, 16.0))
        type = k_32nd;
    else if (is_lower_time(duration, 32.0))
        type = k_16th;
    else if (is_lower_time(duration, 64.0))
        type = k_eighth;
    else if (is_lower_time(duration, 128.0))
        type = k_quarter;
    else if (is_lower_time(duration, 256.0))
        type = k_half;
    else if (is_lower_time(duration, 512.0))
        type = k_whole;
    else if (is_lower_time(duration, 1024.0))
        type = k_breve;
    else
        type = k_longa;

    //compute dots
    TimeUnits base = to_duration(type, 0);
    duration -= to_duration(type, 0);
    double factor = 1.0 + duration / base;
    if (factor < 1.25)
        return NoteTypeAndDots(type, 0);
    if (factor < 1.625)
        return NoteTypeAndDots(type, 1);
    if (factor < 1.8125)
        return NoteTypeAndDots(type, 2);
    if (factor < 1.90625)
        return NoteTypeAndDots(type, 3);
    if (factor < 1.953125)
        return NoteTypeAndDots(type, 4);
    if (factor < 1.9765625)
        return NoteTypeAndDots(type, 5);
    if (factor < 1.98828125)
        return NoteTypeAndDots(type, 6);
    if (factor < 1.99414063)
        return NoteTypeAndDots(type, 7);
    if (factor < 1.99707031)
        return NoteTypeAndDots(type, 8);
    else
        return NoteTypeAndDots(type, 9);
}
Example #2
0
//---------------------------------------------------------------------------------------
int get_beat_position(TimeUnits timePos, ImoTimeSignature* pTS)
{
    // Some times it is necessary to know the type of beat (strong, medium, weak,
    // off-beat) at which a note or rest is positioned.
    // This function receives the time for a note/rest and the current time signature
    // and returns the type of beat: either an integer positive value 0..n, meaning
    // 'on-beat', where n is the beat number, or -1 meaning 'off-beat'

    int beatType = pTS->get_bottom_number();

    // coumpute beat duration
    int beatDuration;
    switch (beatType)
    {
        case 1: beatDuration = int( to_duration(k_whole, 0) ); break;
        case 2: beatDuration = int( to_duration(k_half, 0) ); break;
        case 4: beatDuration = int( to_duration(k_quarter, 0) ); break;
        case 8: beatDuration = 3 * int( to_duration(k_eighth, 0) ); break;
        case 16: beatDuration = int( to_duration(k_eighth, 0) ); break;
        default:
        {
            string msg = str( boost::format("[get_beat_position] BeatType %d unknown.")
                              % beatType );
            LOMSE_LOG_ERROR(msg);
            throw runtime_error(msg);
        }
    }

    // compute relative position of this note/rest with reference to the beat
    int beatNum = int(timePos) / beatDuration;               //number of beat
    TimeUnits beatShift = fabs(timePos - TimeUnits(beatDuration * beatNum));

    if (beatShift < 1.0)
        //on-beat
        return beatNum;
    else
        // off-beat
        return k_off_beat;

}
Example #3
0
void thread::sleep(const xtime& xt)
{
    for (int foo=0; foo < 5; ++foo)
    {
#if defined(BOOST_HAS_WINTHREADS)
        int milliseconds;
        to_duration(xt, milliseconds);
        Sleep(milliseconds);
#elif defined(BOOST_HAS_PTHREADS)
#   if defined(BOOST_HAS_PTHREAD_DELAY_NP)
        timespec ts;
        to_timespec_duration(xt, ts);
        int res = 0;
        res = pthread_delay_np(&ts);
        assert(res == 0);
#   elif defined(BOOST_HAS_NANOSLEEP)
        timespec ts;
        to_timespec_duration(xt, ts);

        //  nanosleep takes a timespec that is an offset, not
        //  an absolute time.
        nanosleep(&ts, 0);
#   else
        mutex mx;
        mutex::scoped_lock lock(mx);
        condition cond;
        cond.timed_wait(lock, xt);
#   endif
#elif defined(BOOST_HAS_MPTASKS)
        int microseconds;
        to_microduration(xt, microseconds);
        Duration lMicroseconds(kDurationMicrosecond * microseconds);
        AbsoluteTime sWakeTime(DurationToAbsolute(lMicroseconds));
        threads::mac::detail::safe_delay_until(&sWakeTime);
#endif
        xtime cur;
        xtime_get(&cur, TIME_UTC);
        if (xtime_cmp(xt, cur) <= 0)
            return;
    }
}