@@ -152,22 +152,20 @@ def to_next_multiple(sample_rate: ExpressionLike, quantum: int,
152152 #double negative for ceil division.
153153 return lambda duration : - (- (duration * sample_rate )// quantum ) * (quantum / sample_rate )
154154 else :
155- qI = sp .Integer (quantum )
156- k = qI / sample_rate # factor to go from #quanta -> duration
157- mqI = sp .Integer (min_quanta )
158-
159- def _build_sym (d ):
160- u = d * sample_rate / qI # "duration in quanta" (real)
161- ce = sp .ceiling (u ) # number of quanta after rounding up
162-
163- # Enforce: 0 if d <= 0; else at least mqI quanta.
164- # max(mqI, ceil(u)) <=> mqI if u <= mqI, else ceil(u)
165- # do not evaluate right now because parameters could still be variable,
166- # then it's just overhead.
167- return sp .Piecewise (
168- (0 , sp .Le (d , 0 )),
169- (k * mqI , sp .Le (u , mqI )),
170- (k * ce , True )
171- , evaluate = False )
172-
173- return lambda duration : ExpressionScalar (_build_sym (duration ))
155+ # work with sympy
156+ sample_rate = sample_rate .sympified_expression
157+ duration_per_quantum = sp .Integer (quantum ) / sample_rate
158+ minimal_duration = duration_per_quantum * min_quanta
159+
160+ def build_next_multiple (duration : ExpressionLike ) -> ExpressionScalar :
161+ duration = sp .sympify (duration )
162+ rounded_up_duration = sp .ceiling (duration / duration_per_quantum ) * duration_per_quantum
163+
164+ next_multiple_sp = sp .Piecewise (
165+ (0 , sp .Le (duration , 0 )),
166+ (minimal_duration , sp .Le (duration , minimal_duration )),
167+ (rounded_up_duration , True )
168+ )
169+ return ExpressionScalar (next_multiple_sp )
170+
171+ return build_next_multiple
0 commit comments