<?xml version="1.0"?>
<Denemo>
  <merge>
    <title>A Denemo Keymap</title>
    <author>AT, JRR, RTS</author>
    <map>
      <row>
        <action>FiguredBassFilterOn</action>
        <scheme>;;; Figured Bass filter

(if (not (defined? 'ToggleFiguredBassMode::Active))
    (define ToggleFiguredBassMode::Active #f))

    
(if (not ToggleFiguredBassMode::Active)
    (let 
	((Figures 0) (AddFigure 0) (GetFigures 0) (loop 0))
	 (begin
	   (set! AddFigure (lambda (note bassnote)
			     (begin
			       (set! Figures (cons  (d-BassFigure bassnote note) Figures)))))
	   (set! GetFigures (lambda ()
			      (begin
				(string-append "figures=" (string-join (reverse Figures))))))
	   (set! loop  (lambda (bassnote)
			 (let* ( 
				(midi (d-GetMidi))
				(velocity (bit-extract midi 16 24))
				(note (bit-extract midi 8 16))
				(command (bit-extract midi 0 8)))
			   ;; body of the let*
			       (begin
				 (if (boolean? (d-GetNoteName))
				     (d-NextNote))
				 (if (or (= command #x90) (= command #x80))
				     (begin
				       (if (and (= command #x90)(= bassnote 0))
					   (begin 
					     (set! bassnote note)
					     (set! Figures '())
					   ;;  (d-PutMidi midi)
					     (if (or (d-GetNonprinting) (= bassnote (d-GetNoteAsMidi)))
					      (begin                  ;; note ok
						;(d-PushPosition)
					     (d-PlayMidiKey midi)
					     ;;;(d-PutMidi midi) ;; this will alter the enharmonic...
					     ;;this will have advanced so go back
					    ;(d-PopPosition)
					     )				   
				    	      (begin                    ;; note wrong
				    	  	 (set! bassnote 0)
				    	  	 (d-PlayMidiKey #xF06001))
				    	  )	    
					     );;; end of  noteon while we have no bass note
			     ;;; not attempted bass note on 
					   (begin
					     (if (and (= command #x80)(= note bassnote))
						 (begin 
						   (set! bassnote 0)
						   (d-EditFiguredBass (GetFigures))
  						   (if (boolean? (d-GetNoteName))
		 					  (d-NextNote))
									);;; end of received bassnote off
				 ;;; not bass note off
						 (begin
						   (if  (= command #x90)
							(begin
							  (AddFigure note bassnote)
							  (d-PlayMidiKey midi)))))))));; end of if noteon or noteoff				  
		;;;;;;;;;; Pedal Down					  
				 (if (and (= command #xB0) (= note #x40) (= velocity #x7F))	   
				     (begin
				       	(display "Pedal down")
					(if (and (&gt; bassnote 0) (null? Figures))
						(begin (d-PlayMidiKey #xF05A01)
							(set! Figures (cons "~"  Figures))
							(d-EditFiguredBass (GetFigures))
							(set! Figures '())
							(set! bassnote 0))
						(begin
				       (d-PlayMidiKey #xF01A01)
				       (set! Figures (cons " | "  Figures)))))
				       
				        (begin
				     (if (and (= command #xB0)   (= note 1))
				     	 (let ((thestep  (round(/ (- velocity 64) 16))))
					   (PlayNote  
					    (number-&gt;string  (+ 60 (* 4 thestep) ))
					    100)
	     
					   (eval-string (string-append "(d-SetEnharmonicPosition " (number-&gt;string  thestep) ")"))))))
				       
				       
				       
		;;;;;;;;;;; End of Pedal Down		       	       
				 ;;(d-PutMidi midi)
				 
				 (if (= midi 0)
				 	(set! ToggleFiguredBassMode::Active #f))
				 
				 (if ToggleFiguredBassMode::Active 
				     (loop bassnote))
				 ));; end of let, the body of the loop proc
			   )) ;;; loop definition
			   
	    (d-InputFilterNames "Figured Bass MIDI Filter")
    	   (set! ToggleFiguredBassMode::Active #t)
	   (d-SetMidiCapture #t)	   
	   (loop 0)
	   
	  ;; (d-SetMidiCapture #f)
					   ));;; end of not active block
						    );;; end of if active/not active if
	   (d-InputFilterNames "No active MIDI Filter")
    	   (set! ToggleFiguredBassMode::Active #f)
    	   (d-PutMidi 0);;; to swallow up the last d-GetMidi??
    	   (d-SetMidiCapture #f)	
	   </scheme>
        <label>Figured Bass Filter On</label>
        <tooltip>Start filtering MIDI events, First note is bass, hold this down while entering notes for figures. Pedal changes harmony on same bass.</tooltip>
      </row>
    </map>
  </merge>
</Denemo>
