]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/key-performer.cc
2003 -> 2004
[lilypond.git] / lily / key-performer.cc
index 0ed1f33afab5c2f24f05df44563cffb80bc8b5c8..246cad751c54801b27325a48247c85231aaac2b8 100644 (file)
@@ -3,63 +3,99 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 1997 Jan Nieuwenhuizen <jan@digicash.com>
+  (c) 1997--2004 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
-#include "key-performer.hh"
-#include "command-request.hh"
-#include "midi-item.hh"
+#include "lily-guile.hh"
 
+#include "audio-item.hh"
+#include "performer.hh"
+#include "warn.hh"
 
-IMPLEMENT_STATIC_NAME(Key_performer);
-IMPLEMENT_IS_TYPE_B1(Key_performer,Performer);
-ADD_THIS_PERFORMER(Key_performer);
 
-Key_performer::Key_performer()
+class Key_performer : public Performer
 {
-    key_req_l_ = 0;
+public:
+  TRANSLATOR_DECLARATIONS(Key_performer);
+  ~Key_performer ();
+
+protected:
+  virtual bool try_music (Music* req);
+  virtual void create_audio_elements ();
+  virtual void stop_translation_timestep ();
+
+private:
+  Key_change_ev* key_req_;
+  Audio_key* audio_;
+};
+
+Key_performer::Key_performer ()
+{
+  key_req_ = 0;
+  audio_ = 0;
 }
 
-Key_performer::~Key_performer()
+Key_performer::~Key_performer ()
 {
 }
 
-void 
-Key_performer::do_print() const
+void
+Key_performer::create_audio_elements ()
 {
-#ifndef NPRINT
-    if ( key_req_l_ )
-       key_req_l_->print();
-#endif
+  if (key_req_) 
+    {
+      SCM pitchlist = key_req_->get_mus_property ("pitch-alist");
+      SCM proc = scm_primitive_eval (ly_symbol2scm ("accidentals-in-key")); 
+      SCM acc = gh_call1 (proc, pitchlist);
+      
+      Pitch key_do (0, 
+                   gh_scm2int (ly_caar (pitchlist)),
+                   gh_scm2int (ly_cdar (pitchlist)));
+
+      Pitch c_do (0, 0, 0);
+                 
+      SCM c_pitchlist
+       = ly_transpose_key_alist (pitchlist,
+                                 interval (key_do, c_do).smobbed_copy ());
+
+      /* MIDI keys are too limited for lilypond scales.
+        We check for minor scale and assume major otherwise.  */
+      SCM minor = scm_primitive_eval (ly_symbol2scm ("minor"));
+      audio_ = new Audio_key (gh_scm2int (acc),
+                             SCM_BOOL_T != scm_equal_p (minor, c_pitchlist));
+
+      Audio_element_info info (audio_, key_req_);
+      announce_element (info);
+      key_req_ = 0;
+    }
 }
 
 void
-Key_performer::process_requests()
+Key_performer::stop_translation_timestep ()
 {
-    if ( key_req_l_ ) {
-       int sharps_i = key_req_l_->sharps_i();
-       int flats_i = key_req_l_->flats_i();
-       // midi cannot handle non-conventional keys
-       if ( !( flats_i && sharps_i ) ) {
-           Midi_key k( sharps_i - flats_i, key_req_l_->minor_b() );
-           play_event( &k );
-       }
-       key_req_l_ = 0;
+  if (audio_)
+    {
+      play_element (audio_);
+      audio_ = 0;
     }
 }
 
 bool
-Key_performer::try_request( Request* req_l )
+Key_performer::try_music (Music* req)
 {
-    if ( key_req_l_ )
-       return false;
+  if (Key_change_ev *kc = dynamic_cast <Key_change_ev *> (req))
+    {
+      if (key_req_)
+       warning (_ ("FIXME: key change merge"));
 
-    if ( req_l->command() )
-       key_req_l_ = req_l->command()->keychange();
-
-    if ( key_req_l_ )
-       return true;
+      key_req_ = kc;
+      return true;
+    }
 
-    return false;
+  return false;
 }
 
+ENTER_DESCRIPTION(Key_performer,
+                 "","",
+                 "key-change-event",
+                 "","","");