diff --git a/libs/pbd/pbd/integer_division.h b/libs/pbd/pbd/integer_division.h index 323fbf03c8..1eee8c58b3 100644 --- a/libs/pbd/pbd/integer_division.h +++ b/libs/pbd/pbd/integer_division.h @@ -32,6 +32,10 @@ template T int_div_round (T x, T y) { + if (y == 0) { + /* usually `y' is sample-rate, or beats/bar */ + return (x == 0) ? 0 : 1; + } /* essentially ((x + (y/2)) / y) but handles signed/negative values correcvtly */ return (x + PBD_IDIV_ROUNDING(x,y)) / y ; } diff --git a/libs/temporal/temporal/superclock.h b/libs/temporal/temporal/superclock.h index 4eb2c02bf3..7891d0096b 100644 --- a/libs/temporal/temporal/superclock.h +++ b/libs/temporal/temporal/superclock.h @@ -35,7 +35,7 @@ typedef int64_t superclock_t; static superclock_t superclock_ticks_per_second = 508032000; // 2^10 * 3^4 * 5^3 * 7^2 #endif -static inline superclock_t superclock_to_samples (superclock_t s, int sr) { return int_div_round (s * sr, superclock_ticks_per_second); } +static inline superclock_t superclock_to_samples (superclock_t s, int sr) { return sr == 0 ? 0 : int_div_round (s * sr, superclock_ticks_per_second); } static inline superclock_t samples_to_superclock (int64_t samples, int sr) { return int_div_round (samples * superclock_ticks_per_second, superclock_t (sr)); } extern int (*sample_rate_callback)();