@@ -408,21 +408,41 @@ impl ArpState {
408408 }
409409 }
410410
411- fn insert_note ( & mut self , note : Note ) {
411+ fn insert_note ( & mut self , note : Note , shape : & ArpShape ) {
412412 if let Some ( index) = self . store . insert ( note) {
413- if self . index > index {
414- self . index += 1 ;
413+ match shape {
414+ ArpShape :: Indices ( _) => {
415+ // If the shape is a list of indices then the `index` field refers to and index
416+ // into _that_ list rather than the store, so don't touch it.
417+ }
418+ _ => {
419+ // If the note referred to by the current index has moved in the store, adjust
420+ // the index to point to that note again.
421+ if self . index > index {
422+ self . index += 1 ;
423+ }
424+ }
415425 }
416426 }
417427 }
418- fn remove_note ( & mut self , note : Note ) {
428+ fn remove_note ( & mut self , note : Note , shape : & ArpShape ) {
419429 if let Some ( index) = self . store . remove ( note) {
420- if self . index > index {
421- self . index -= 1 ;
430+ match shape {
431+ ArpShape :: Indices ( _) => {
432+ // If the shape is a list of indices then the `index` field refers to and index
433+ // into _that_ list rather than the store, so don't touch it.
434+ }
435+ _ => {
436+ // If the note referred to by the current index has moved in the store, adjust
437+ // the index to point to that note again.
438+ if self . index > index {
439+ self . index -= 1 ;
440+ }
441+ }
422442 }
423443 }
424444 }
425- fn reset ( & mut self , shape : ArpShape ) {
445+ fn reset ( & mut self , shape : & ArpShape ) {
426446 self . index = 0 ;
427447 use ArpShape :: * ;
428448 match shape {
@@ -450,7 +470,7 @@ impl ArpState {
450470 self . index -= 1 ;
451471 self . store . entries [ self . index ] . note
452472 }
453- fn tick ( & mut self , shape : ArpShape ) {
473+ fn tick ( & mut self , shape : & ArpShape ) {
454474 self . current_note = if self . store . entries . is_empty ( ) {
455475 self . reset ( shape) ;
456476 None
@@ -649,35 +669,35 @@ where
649669 let mut events = KeyEvents :: empty ( ) ;
650670 for event in key_events {
651671 if event. pressed {
652- state. insert_note ( event. note ) ;
672+ state. insert_note ( event. note , & shape ) ;
653673 for i in 0 ..extend_octaves_high {
654674 if let Some ( note) =
655675 event. note . add_octaves_checked ( i as i8 + 1 )
656676 {
657- state. insert_note ( note) ;
677+ state. insert_note ( note, & shape ) ;
658678 }
659679 }
660680 for i in 0 ..extend_octaves_low {
661681 if let Some ( note) =
662682 event. note . add_octaves_checked ( -( i as i8 + 1 ) )
663683 {
664- state. insert_note ( note) ;
684+ state. insert_note ( note, & shape ) ;
665685 }
666686 }
667687 } else {
668- state. remove_note ( event. note ) ;
688+ state. remove_note ( event. note , & shape ) ;
669689 for i in 0 ..extend_octaves_high {
670690 if let Some ( note) =
671691 event. note . add_octaves_checked ( i as i8 + 1 )
672692 {
673- state. remove_note ( note) ;
693+ state. remove_note ( note, & shape ) ;
674694 }
675695 }
676696 for i in 0 ..extend_octaves_low {
677697 if let Some ( note) =
678698 event. note . add_octaves_checked ( -( i as i8 + 1 ) )
679699 {
680- state. remove_note ( note) ;
700+ state. remove_note ( note, & shape ) ;
681701 }
682702 }
683703 }
@@ -690,7 +710,7 @@ where
690710 velocity_01 : 0.0 ,
691711 } ) ;
692712 }
693- state. tick ( shape) ;
713+ state. tick ( & shape) ;
694714 if let Some ( note) = state. current_note {
695715 events. push ( KeyEvent {
696716 note,
0 commit comments