]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name-engraver.cc
patch::: 1.1.15.jcn1: pats
[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      - move this stuff to new Item class Chord_name
54      - switch on property, add american (?) chordNameStyle
55
56   Scalar chordNameStyle = get_property ("chordNameStyle");
57   if (chordNameStyle == "Banter")
58      chord = pitches_to_banter (pitch_arr_));
59
60    */
61
62   Scalar style = get_property ("textstyle");
63   Scalar alignment = get_property ("textalignment");
64   Text_def* text_p = new Text_def;
65   text_p->align_dir_ = LEFT;
66   if (style.length_i ())
67     text_p->style_str_ = style;
68   if (alignment.isnum_b())
69     text_p->align_dir_= (Direction)(int)alignment;
70
71
72   /*
73     find tonic: after longest line of triads
74    */
75
76   int tonic_i = 0;
77   Scalar chord_inversions = get_property ("chordInversion");
78   if (chord_inversions.to_bool ())
79     {
80       int longest_i = 0;
81       for (int i = 0; i < pitch_arr_.size (); i++)
82         for (int j = 0; j < pitch_arr_.size (); j++)
83           {
84             int gap = pitch_arr_[(i + j + 1) % pitch_arr_.size ()].notename_i_
85               - pitch_arr_[(i + j) % pitch_arr_.size ()].notename_i_;
86             while (gap < 0)
87               gap += 7;
88             gap %= 7;
89             if (gap != 2)
90               {
91                 if (j > longest_i)
92                   {
93                     longest_i = j;
94                     tonic_i = i;
95                   }
96                 break;
97               }
98           }
99
100       int biggest_i = 0;
101       if (!longest_i)
102         for (int i = 0; i < pitch_arr_.size (); i++)
103           {
104             int gap = pitch_arr_[i].notename_i_
105               - pitch_arr_[(i - 1 + pitch_arr_.size ()) 
106               % pitch_arr_.size ()].notename_i_;
107             while (gap < 0)
108               gap += 7;
109             gap %= 7;
110             if (gap > biggest_i)
111               {
112                 biggest_i = gap;
113                 tonic_i = i;
114               }
115           }
116     }
117
118   Musical_pitch inversion = pitch_arr_[0];
119   if (tonic_i)
120     {
121       Musical_pitch last (0, 0, -5);
122       Array<Musical_pitch> pitches;
123       for (int i = 0; i < pitch_arr_.size (); i++)
124         {
125           Musical_pitch p = pitch_arr_[(tonic_i + i) % pitch_arr_.size ()];
126           if (p < last)
127             {
128               p.octave_i_ = last.octave_i_;
129               if (p < last)
130                 p.octave_i_++;
131             }
132           pitches.push (p);
133           last = p;
134         }
135       pitch_arr_ = pitches;
136     }
137
138   Musical_pitch tonic = pitch_arr_[0];
139
140   Array<Musical_pitch> scale;
141   scale.push (Musical_pitch (0)); // c
142   scale.push (Musical_pitch (1)); // d
143   scale.push (Musical_pitch (2)); // e
144   scale.push (Musical_pitch (3)); // f
145   scale.push (Musical_pitch (4)); // g
146   scale.push (Musical_pitch (5)); // a
147   // 7 always means 7-...
148   scale.push (Musical_pitch (6, -1)); // b
149
150
151   for (int i = 0; i < scale.size (); i++)
152     scale[i].transpose (tonic);
153
154   //urg, should do translation in scheme.
155   char const *acc[] = {"\\textflat\\textflat ", "\\textflat ", "", "\\textsharp " , "\\textsharp\\textsharp "};
156   String tonic_str = tonic.str ();
157   tonic_str = tonic_str.left_str (1).upper_str ()
158     + acc[tonic.accidental_i_ + 2];
159
160   String add_str;
161   String sep_str;
162   for (int i=1; i < pitch_arr_.size (); i++)
163     {
164       Musical_pitch p = pitch_arr_[i];
165       int trap = p.notename_i_ - tonic.notename_i_ 
166         + (p.octave_i_ - tonic.octave_i_) * 7 + 1;
167       int accidental = p.accidental_i_ - scale[(trap - 1) % 7].accidental_i_;
168       if ((trap == 3) && (accidental == -1))
169         tonic_str += "m"; // hmm
170       else if (accidental || (!(trap % 2) || ((i + 1 == pitch_arr_.size ()) && (trap > 5))))
171         {
172           add_str += sep_str;
173           if ((trap == 7) && (accidental == 1))
174             add_str += "maj7";
175           else
176             {
177               add_str += to_str (trap);
178               if (accidental)
179                 add_str += accidental < 0 ? "-" : "+";
180             }
181           sep_str = "/";
182         }
183     }
184
185   String inversion_str;
186   if (tonic_i)
187     {
188       inversion_str = inversion.str ();
189       inversion_str = "/" + inversion_str.left_str (1).upper_str ()
190         + acc[tonic.accidental_i_ + 2];
191
192     }
193   text_p->text_str_ = tonic_str + "$^{" + add_str + "}$" + inversion_str;
194   Text_item* item_p =  new Text_item (text_p);
195   item_p->dir_ = DOWN;
196   item_p->fat_b_ = true;
197   text_p_arr_.push (item_p);
198   announce_element (Score_element_info (item_p, 0));
199 }
200
201 void
202 Chord_name_engraver::do_pre_move_processing ()
203 {
204   for (int i=0; i < text_p_arr_.size (); i++)
205     {
206       typeset_element (text_p_arr_[i]);
207     }
208   text_p_arr_.clear ();
209   pitch_arr_.clear ();
210 }
211