@@ -192,6 +192,29 @@ impl Line {
192
192
}
193
193
json ! ( notes)
194
194
}
195
+
196
+ fn duration ( & self , sample_rate : u32 , run_size : usize ) -> f32 {
197
+ let mut line = Line {
198
+ deltamsgs : self . deltamsgs . clone ( ) ,
199
+ ticks_per_quarter : self . ticks_per_quarter ,
200
+ ..Default :: default ( )
201
+ } ;
202
+ line. reset ( ) ;
203
+ let mut samples: u32 = 0 ;
204
+ loop {
205
+ samples += run_size as u32 ;
206
+ if line. advance (
207
+ samples,
208
+ run_size,
209
+ sample_rate,
210
+ None ,
211
+ None ,
212
+ ) {
213
+ break ;
214
+ }
215
+ }
216
+ samples as f32 / sample_rate as f32
217
+ }
195
218
}
196
219
197
220
struct Queue {
@@ -261,6 +284,7 @@ component!(
261
284
"advance" : { "args" : [ "seconds" ] } ,
262
285
"skip_line" : { "args" : [ { "desc" : "number of lines" , "default" : 1 } ] } ,
263
286
"multiply_deltas" : { "args" : [ "multiplier" ] } ,
287
+ "duration" : { } ,
264
288
} ,
265
289
) ;
266
290
@@ -296,6 +320,9 @@ impl ComponentTrait for Component {
296
320
while let Ok ( mut line) = self . queue . recv . try_recv ( ) {
297
321
let line_index = ( * line) . index ;
298
322
( * line) . index = ( * self . lines [ line_index] ) . index ;
323
+ while line_index >= self . lines . len ( ) {
324
+ self . lines . push ( Box :: new ( Line :: default ( ) ) ) ;
325
+ }
299
326
self . lines [ line_index] = line;
300
327
}
301
328
self . samples += self . run_size as u32 ;
@@ -368,11 +395,11 @@ impl Component {
368
395
let line_index: usize = body. arg ( 0 ) ?;
369
396
let ticks_per_quarter: u32 = body. arg ( 1 ) ?;
370
397
let deltamsgs = body. arg ( 2 ) ?;
371
- while line_index >= self . lines . len ( ) {
372
- self . lines . push ( Box :: new ( Line :: default ( ) ) ) ;
373
- }
374
398
if let Ok ( immediate) = body. kwarg ( "immediate" ) {
375
399
if immediate {
400
+ while line_index >= self . lines . len ( ) {
401
+ self . lines . push ( Box :: new ( Line :: default ( ) ) ) ;
402
+ }
376
403
self . lines [ line_index] = Box :: new ( Line :: new (
377
404
& deltamsgs,
378
405
ticks_per_quarter,
@@ -441,4 +468,18 @@ impl Component {
441
468
}
442
469
Ok ( None )
443
470
}
471
+
472
+ fn duration_cmd ( & mut self , _body : serde_json:: Value ) -> CmdResult {
473
+ if self . run_size == 0 {
474
+ return Err ( err ! ( "run size is 0" ) . into ( ) ) ;
475
+ }
476
+ if self . sample_rate == 0 {
477
+ return Err ( err ! ( "sample rate is 0" ) . into ( ) ) ;
478
+ }
479
+ let mut duration: f32 = 0.0 ;
480
+ for line in & self . lines {
481
+ duration = line. duration ( self . sample_rate , self . run_size ) . max ( duration) ;
482
+ }
483
+ Ok ( Some ( json ! ( duration) ) )
484
+ }
444
485
}
0 commit comments