]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name-engraver.cc
patch::: 1.1.13.uu1
[lilypond.git] / lily / chord-name-engraver.cc
1 /*
2   chord-name-engraver.cc -- implement Chord_name_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "chord-name-engraver.hh"
10 #include "musical-request.hh"
11 #include "text-item.hh"
12 #include "paper-def.hh"
13 #include "lookup.hh"
14 #include "paper-def.hh"
15 #include "main.hh"
16 #include "dimensions.hh"
17
18 ADD_THIS_TRANSLATOR (Chord_name_engraver);
19
20 Chord_name_engraver::Chord_name_engraver ()
21 {
22 }
23
24 void
25 Chord_name_engraver::acknowledge_element (Score_element_info i)
26 {
27   if (Note_req* n = dynamic_cast<Note_req*> (i.req_l_))
28     pitch_arr_.push (n->pitch_);
29 }
30
31 bool
32 Chord_name_engraver::do_try_music (Music* m)
33 {
34   if (Note_req* n = dynamic_cast<Note_req*> (m))
35     {
36       pitch_arr_.push (n->pitch_);
37       return true;
38     }
39   return false;
40 }
41
42 void
43 Chord_name_engraver::do_process_requests ()
44 {
45   if (text_p_arr_.size ())
46     return;
47   if (!pitch_arr_.size ())
48     return;
49
50   /*
51    Banter style chord names (almost).
52    TODO:
53      - don't print inclusive scale (i.e. no "9" in c 9/11)
54      - handle c7 / cmaj7
55      - use #,b iso -es -is on tonica
56      - switch on property, add american (?) chordNameStyle
57
58   Scalar chordNameStyle = get_property ("chordNameStyle");
59   if (chordNameStyle == "Banner")
60      chord = pitches_to_banner (pitch_arr_.size ());
61
62    */
63
64   /*
65    Banter style chord names (almost).
66    TODO:
67      - don't print inclusive scale (i.e. no "9" in c 9/11)
68      - handle c7 / cmaj7
69      - use #,b iso -es -is on tonica
70      - switch on property, add american (?) chordNameStyle
71
72   Scalar chordNameStyle = get_property ("chordNameStyle");
73   if (chordNameStyle == "Banner")
74      chord = pitches_to_banner (pitch_arr_.size ());
75
76    */
77
78   Scalar style = get_property ("textstyle");
79   Scalar alignment = get_property ("textalignment");
80   Text_def* text_p = new Text_def;
81   text_p->align_dir_ = LEFT;
82   if (style.length_i ())
83     text_p->style_str_ = style;
84   if (alignment.isnum_b())
85     text_p->align_dir_= (Direction)(int)alignment;
86
87   Musical_pitch tonic = pitch_arr_[0];
88
89   Array<Musical_pitch> scale;
90   scale.push (Musical_pitch (0)); // c
91   scale.push (Musical_pitch (1)); // d
92   scale.push (Musical_pitch (2)); // e
93   scale.push (Musical_pitch (3)); // f
94   scale.push (Musical_pitch (4)); // g
95   scale.push (Musical_pitch (5)); // a
96   // 7 always means 7-...
97   scale.push (Musical_pitch (6, -1)); // b
98
99   for (int i = 0; i < scale.size (); i++)
100     scale[i].transpose (tonic);
101
102   //urg, should do translation in scheme.
103   char const *acc[] = {"\\textflat\\textflat", "\\textflat", "", "\\textsharp" , "\\textsharp\\textsharp"};
104   String tonic_str = tonic.str ();
105   tonic_str = tonic_str.left_str (1).upper_str ()
106     + acc[tonic.accidental_i_ + 2];
107
108   String add_str;
109   String sep_str;
110   for (int i=1; i < pitch_arr_.size (); i++)
111     {
112       Musical_pitch p = pitch_arr_[i];
113       int trap = p.notename_i_ - tonic.notename_i_ 
114         + (p.octave_i_ - tonic.octave_i_) * 7 + 1;
115       int accidental = p.accidental_i_ - scale[(trap - 1) % 7].accidental_i_;
116       if ((trap == 3) && (accidental == -1))
117         tonic_str += "m"; // hmm
118       else if (accidental || (!(trap % 2) || ((i + 1 == pitch_arr_.size ()) && (trap > 5))))
119         {
120           add_str += sep_str;
121           if ((trap == 7) && (accidental == 1))
122             add_str += "maj7";
123           else
124             {
125               add_str += to_str (trap);
126               if (accidental)
127                 add_str += accidental < 0 ? "-" : "+";
128             }
129           sep_str = "/";
130         }
131     }
132
133   text_p->text_str_ = tonic_str + "$^{" + add_str + "}$";
134   Text_item* item_p =  new Text_item (text_p);
135   item_p->dir_ = DOWN;
136   item_p->fat_b_ = true;
137   text_p_arr_.push (item_p);
138   announce_element (Score_element_info (item_p, 0));
139 }
140
141 void
142 Chord_name_engraver::do_pre_move_processing ()
143 {
144   for (int i=0; i < text_p_arr_.size (); i++)
145     {
146       typeset_element (text_p_arr_[i]);
147     }
148   text_p_arr_.clear ();
149   pitch_arr_.clear ();
150 }
151