Re: [frogs] chord_name_engraver - help with c++ ...

[ Thread Index | Date Index | More lilynet.net/frogs Archives ]


On 05/07/11 21:29, Jan Warchoł wrote:
> 2011/7/5 Wols Lists <antlists@xxxxxxxxxxxxxxx>:
>> This frog is back ... I said I'd wait until 2.14 which has finally
>> arrived :-) But I've revamped my code a bit and am having problems...
> 
> Could you send me a link to the previous discussion on this topic? A
> quick search didn't return anything.

Look for frog emails with a subject containing "chord-name-engraver"
from august last year ... yes I know it's a long time ... but 2.14 was
supposed to be out rsn back then :-) and life happened to me :-)

Just to give a quick overview of the purpose of the changes - let's say
you've got a piano sheet with guitar chords. Very often, it'll be in a
naff key for the guitar and you want to display the guitar chords
transposed by a capo.

So if the actual chord is C, and you want capo 3, you want the chord to
actually print as "C (A)", ie the original chord followed by the
transposed chord in brackets.
> 
> 2011/7/5 Carl Sorensen <c_sorensen@xxxxxxx>:
>>> Next step - that code compiles okay. But it doesn't work when run ...
>>>
>>> I'm getting the following errors:
>>>
>>> Interpreting music...
>>> warning: cannot find property type-check for `capoVertical'
>>> (translation-type?).  perhaps a typing error?
>>> warning: doing assignment anyway
>>> warning: cannot find property type-check for `capoPitch'
>>> (translation-type?).  perhaps a typing error?
>>> warning: doing assignment anyway
>>>
>>> But I've added capoVertical and capoPitch to the ADD_TRANSLATOR section,
>>> and I've added them to define-context-properties.scm. So why can't it
>>> find it? Or is it that I've installed 2.15 locally, and it's finding the
>>> scheme code for 2.14 or 2.12? Unlikely, I would have thought, wouldn't
>>> that cause a crash?
>>
>> Actually, it's not likely that the scheme code would cause a crash.
>>
>> If I were wanting to check this out, I'd put a Scheme display statement at
>> the top of scm/context-properties.scm.
> 
> Interesting.  IIRC i had similar problems with my ambitus patch.  I'll
> look at this when i return to it.
> 
>> If you could post a patch set on Rietveld, we might be able to give you
>> better advice.
> 
> If you have any problem with Rietveld, i'll gladly help you; I can
> upload your patch if you want.
> 
> cheers,
> Janek
> 
Patch attached. I've looked at the web page for rietveld, and I run
gentoo so things do "just build" for me (gentoo bugs excepted :-) Do I
need the git-cl stuff? I don't know what LilyDev is, but I guess I
haven't got it ...

If you look at the patch, the idea is that all of my changes (apart from
the initial setting of bool capo) should be set inside an "if capo"
conditional, so if the user doesn't set capoPitch, lily doesn't go
anywhere near my code.

Cheers,
Wol
From 971ab62c7ec78a09b732d9d4a16a3b46a0d5a0b2 Mon Sep 17 00:00:00 2001
From: Wol <anthony@xxxxxxxxxxxxxxx>
Date: Tue, 5 Jul 2011 21:38:03 +0100
Subject: [PATCH] Modify chord-name-engraver to print transposed guitar chords on piano
 sheets
 Add associated properties capoPitch and capoVertical to
 define-context-properties

---
 lily/chord-name-engraver.cc       |   81 +++++++++++++++++++++++++++++++++++-
 scm/define-context-properties.scm |    2 +
 2 files changed, 80 insertions(+), 3 deletions(-)

diff --git a/lily/chord-name-engraver.cc b/lily/chord-name-engraver.cc
index d0ced5a..5037825 100644
--- a/lily/chord-name-engraver.cc
+++ b/lily/chord-name-engraver.cc
@@ -2,6 +2,7 @@
   This file is part of LilyPond, the GNU music typesetter.
 
   Copyright (C) 1998--2011 Jan Nieuwenhuizen <janneke@xxxxxxx>
+  Copyright (C) 2011 Anthony Youngman <anthony@xxxxxxxxxxxxxxx>
 
   LilyPond is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -43,11 +44,14 @@ protected:
   DECLARE_TRANSLATOR_LISTENER (note);
   DECLARE_TRANSLATOR_LISTENER (rest);
 private:
+  SCM capo_transpose( SCM, SCM) const;
+  
   Item *chord_name_;
   vector<Stream_event*> notes_;
  
   SCM last_chord_;
   Stream_event *rest_event_;
+  
 };
 
 void
@@ -76,6 +80,13 @@ Chord_name_engraver::process_music ()
   SCM inversion = SCM_EOL;
   SCM pitches = SCM_EOL;
 
+  SCM capo_markup;
+  SCM capo_bass = SCM_EOL;
+  SCM capo_inversion = SCM_EOL;
+  SCM capo_pitches = SCM_EOL;
+  
+  bool capo = false;
+
   if (rest_event_)
     {
       SCM no_chord_markup = get_property ("noChordSymbol");
@@ -88,6 +99,17 @@ Chord_name_engraver::process_music ()
       if (!notes_.size ())
         return;
 
+      // This is set by "\set ChordNames.CapoPitch = #(ly:make-pitch 0 1 1)" in the lily source file
+      // declare properties in define-context-properties.scm
+
+      SCM capo_pitch = get_property ( "capoPitch" );
+      bool capo = false;
+      if ( !(capo_pitch == SCM_EOL) )
+      {
+        Pitch *cp = unsmob_pitch (capo_pitch);
+        capo = (Pitch::compare ( *cp, Pitch() ) != 0);
+      }
+      
       Stream_event *inversion_event = 0;
       for (vsize i = 0; i < notes_.size (); i++)
       {
@@ -100,11 +122,27 @@ Chord_name_engraver::process_music ()
         {
           inversion_event = n;
           inversion = p;
+          if (capo)
+          {
+            capo_inversion = capo_transpose (p, capo_pitch);
+          }
         }
         else if (n->get_property ("bass") == SCM_BOOL_T)
+        {
           bass = p;
+          if (capo)
+          {
+            capo_bass = capo_transpose (p, capo_pitch);
+          }
+        }
         else
+        {
           pitches = scm_cons (p, pitches);
+          if (capo)
+          {
+            capo_pitches = scm_cons (capo_transpose (p, capo_pitch), capo_pitches);
+          }
+        }
       }
 
       if (inversion_event)
@@ -123,10 +161,19 @@ Chord_name_engraver::process_music ()
       }
 
       pitches = scm_sort_list (pitches, Pitch::less_p_proc);
-
+      if (capo)
+      {
+        capo_pitches = scm_sort_list (capo_pitches, Pitch::less_p_proc);
+      }
+      
       SCM name_proc = get_property ("chordNameFunction");
       markup = scm_call_4 (name_proc, pitches, bass, inversion,
           context ()->self_scm ());
+      if (capo)
+      {
+        capo_markup = scm_call_4 ( name_proc, capo_pitches, capo_bass, capo_inversion,
+                                   context ()->self_scm ());
+      }
     }
   /*
     Ugh.
@@ -135,8 +182,25 @@ Chord_name_engraver::process_music ()
 
   chord_name_ = make_item ("ChordName", 
       rest_event_ ? rest_event_->self_scm () : notes_[0]->self_scm ());
-  chord_name_->set_property ("text", markup);
-
+  if (!capo) {
+    chord_name_->set_property ("text", markup);
+  } else {
+    SCM capovertical = get_property ("capoVertical");
+    SCM paren_proc = ly_lily_module_constant ("parenthesize-markup");
+    SCM line_proc = ly_lily_module_constant ("line_markup");
+    SCM hspace_proc = ly_lily_module_constant ("hspace_markup");
+    
+    SCM final_markup = scm_list_n (line_proc,
+                                   scm_list_3 (markup,
+                                               scm_list_2 (hspace_proc,
+                                                           scm_from_int(1)),
+                                               scm_list_2 (paren_proc, capo_markup)),
+                                   SCM_UNDEFINED);
+    
+    chord_name_->set_property ("text", final_markup);
+  }
+  
+  
   SCM chord_changes = get_property("chordChanges");
   if (to_boolean (chord_changes) && scm_is_pair (last_chord_)
       && ly_is_equal (chord_as_scm, last_chord_))
@@ -145,6 +209,15 @@ Chord_name_engraver::process_music ()
   last_chord_ = chord_as_scm;
 }
 
+SCM
+Chord_name_engraver::capo_transpose (SCM scm_pitch, SCM capo_pitch) const
+{
+  Pitch *s_p = unsmob_pitch (scm_pitch);
+  Pitch *c_p = unsmob_pitch (capo_pitch);
+  Pitch orig = s_p->transposed ( *c_p);
+  return orig.smobbed_copy ();
+}
+
 IMPLEMENT_TRANSLATOR_LISTENER (Chord_name_engraver, note);
 void
 Chord_name_engraver::listen_note (Stream_event *ev)
@@ -179,6 +252,8 @@ ADD_TRANSLATOR (Chord_name_engraver,
 		"ChordName ",
 
 		/* read */
+                "capoPitch "
+                "capoVertical "
 		"chordChanges "
 		"chordNameExceptions "
 		"chordNameFunction "
diff --git a/scm/define-context-properties.scm b/scm/define-context-properties.scm
index dab5211..d17f72f 100644
--- a/scm/define-context-properties.scm
+++ b/scm/define-context-properties.scm
@@ -135,6 +135,8 @@ that normally end on beats.")
      (beatStructure ,list? "List of @code{baseMoment}s that are combined
 to make beats.")
 
+     (capoPitch ,ly:pitch? "The pitch to transpose chords down by when using the capo.")
+     (capoVertical ,boolean? "Whether to display actual and transposed pitches above each other or not.")
      (chordChanges ,boolean? "Only show changes in chords scheme?")
      (chordNameExceptions ,list? "An alist of chord exceptions.
 Contains @code{(@var{chord} . @var{markup})} entries.")
-- 
1.7.3.4



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/