@@ -106,28 +106,33 @@ impl MidiChannel {
106106 } ;
107107 let mut velocity = self . velocity . into ( ) ;
108108 if !bypass {
109- self . track . as_ref ( ) . map ( |x| {
110- match x. kind {
111- TrackKind :: Vocal => {
112- velocity = if settings. vocal_mute { 0 } else { settings. vocal_velocity } ;
113- } ,
114- TrackKind :: Guitar => {
115- velocity = if settings. guitar_mute { 0 } else { settings. guitar_velocity } ;
116- } ,
117- TrackKind :: Piano => {
118- velocity = if settings. piano_mute { 0 } else { settings. piano_velocity } ;
119- } ,
120- _ => ( ) ,
109+ match & self . track {
110+ Some ( track) => {
111+ match track. kind {
112+ TrackKind :: Vocal => {
113+ velocity = if settings. vocal_mute { 0 } else { settings. vocal_velocity } ;
114+ } ,
115+ TrackKind :: Guitar => {
116+ velocity = if settings. guitar_mute { 0 } else { settings. guitar_velocity } ;
117+ } ,
118+ TrackKind :: Piano => {
119+ velocity = if settings. piano_mute { 0 } else { settings. piano_velocity } ;
120+ } ,
121+ _ => ( ) ,
122+ }
123+ } ,
124+ None => {
125+ velocity = if settings. click_mute { 0 } else { settings. click_velocity } ;
121126 }
122- } ) ;
127+ }
123128 }
124129 let mut count = 0 ;
125130 loop {
126131 if let Some ( next) = self . messages . get ( self . next_index ) {
127- if play_control. is_bar_in_range ( next. entry . bar_props ( ) . bar_ordinal )
132+ if play_control. is_bar_in_range ( next. bar_ordinal ( ) )
128133 && play_control
129134 . position
130- . is_passed ( next. entry . pass_mode ( ) , & next. bar_position ( ) )
135+ . is_passed ( next. pass_mode , & next. bar_position ( ) )
131136 {
132137 self . next_index += 1 ;
133138 count += 1 ;
@@ -156,7 +161,7 @@ impl MidiChannel {
156161 hub. send (
157162 settings,
158163 speed,
159- & MidiMessage :: new ( & first_msg. entry , None , msg) ,
164+ & MidiMessage :: new ( first_msg. pass_mode , first_msg . bar_position ( ) , None , msg) ,
160165 self . velocity . into ( ) ,
161166 ) ;
162167 let msg = StructuredShortMessage :: ControlChange {
@@ -167,7 +172,7 @@ impl MidiChannel {
167172 hub. send (
168173 settings,
169174 speed,
170- & MidiMessage :: new ( & first_msg. entry , None , msg) ,
175+ & MidiMessage :: new ( first_msg. pass_mode , first_msg . bar_position ( ) , None , msg) ,
171176 self . velocity . into ( ) ,
172177 ) ;
173178 }
@@ -183,6 +188,16 @@ impl MidiChannel {
183188 self . program = U7 :: new ( params. 0 ) ;
184189 self . velocity = U7 :: new ( params. 1 ) ;
185190 }
191+ pub fn setup_no_track (
192+ & mut self ,
193+ _settings : & MidiSettings ,
194+ _hub : & mut MidiHub ,
195+ params : ( u8 , u8 ) ,
196+ ) {
197+ self . track = None ;
198+ self . program = U7 :: new ( params. 0 ) ;
199+ self . velocity = U7 :: new ( params. 1 ) ;
200+ }
186201}
187202
188203pub struct MidiState {
@@ -241,10 +256,37 @@ impl MidiState {
241256 }
242257 None
243258 }
259+ fn create_click_channel ( & mut self , settings : & MidiSettings , hub : & mut MidiHub , tab : & Tab , index : & mut usize ) {
260+ let params = settings. get_click_channel_params ( ) ;
261+ if let Some ( channel) = self . channels . get_mut ( * index) {
262+ channel. setup_no_track ( settings, hub, params) ;
263+ println ! ( "switch_tab(), setup click channel: [{}] -> {}, {}" , index, params. 0 , params. 1 ) ;
264+ * index += 1 ;
265+ let scale_root = tab. meta . scale . calc_root_syllable ( ) ;
266+ let signature = tab. signature ( ) ;
267+ let bar_units = tab. bar_units ( ) ;
268+ let beat_delay = Some ( Units :: from ( signature. beat_unit ) ) ;
269+ for bar in tab. bars . iter ( ) {
270+ for beat in 0 ..signature. bar_beats {
271+ let in_bar_pos = Units ( beat as f32 * Units :: from ( signature. beat_unit ) . 0 ) ;
272+ let root = bar. get_chord ( Some ( in_bar_pos) ) . map ( |x| x. root ) . unwrap_or ( scale_root) ;
273+ let note = tab. meta . scale . calc_click_note ( & tab. meta . key , & settings. click_octave , & root) ;
274+ let pos = BarPosition :: new ( bar_units, bar. props . bar_ordinal , in_bar_pos) ;
275+ if let Some ( midi_msg) = MidiUtil :: note_midi_on_msg ( & note, channel. channel , channel. velocity ) {
276+ channel. add_message ( MidiMessage :: new ( EntryPassMode :: Immediate , pos, None , midi_msg) ) ;
277+ }
278+ if let Some ( midi_msg) = MidiUtil :: note_midi_off_msg ( & note, channel. channel , channel. velocity ) {
279+ channel. add_message ( MidiMessage :: new ( EntryPassMode :: Immediate , pos, beat_delay, midi_msg) ) ;
280+ }
281+ }
282+ }
283+ }
284+ }
244285 pub fn switch_tab ( & mut self , settings : & MidiSettings , hub : & mut MidiHub , tab : Arc < Tab > ) {
245286 self . tab = Some ( tab. clone ( ) ) ;
246287 self . reset_channels ( ) ;
247288 let mut index: usize = 0 ;
289+ self . create_click_channel ( settings, hub, & tab, & mut index) ;
248290 for track in tab. tracks . iter ( ) {
249291 if index >= self . channels . len ( ) {
250292 return ;
@@ -263,7 +305,7 @@ impl MidiState {
263305 for entry in lane. entries . iter ( ) {
264306 if let Some ( msgs) = MidiUtil :: get_midi_msgs ( channel, bar, & entry) {
265307 for msg in msgs {
266- channel. add_message ( MidiMessage :: new ( entry, msg. 0 , msg. 1 ) ) ;
308+ channel. add_message ( MidiMessage :: of_entry ( entry, msg. 0 , msg. 1 ) ) ;
267309 }
268310 }
269311 }
0 commit comments