author | mickeyl <mickeyl> | 2004-01-20 13:07:31 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2004-01-20 13:07:31 (UTC) |
commit | d388324e7b91bdd95553c3849d77cddd5bd0bfc4 (patch) (unidiff) | |
tree | ef660ffd25aa8cabbfa58e6c3b170e7c82511bc6 /noncore/multimedia/powerchord/chordengine.cpp | |
parent | 105007cca23072ee42a1f36625979966eb8a4854 (diff) | |
download | opie-d388324e7b91bdd95553c3849d77cddd5bd0bfc4.zip opie-d388324e7b91bdd95553c3849d77cddd5bd0bfc4.tar.gz opie-d388324e7b91bdd95553c3849d77cddd5bd0bfc4.tar.bz2 |
initial import of powerchord courtesy Camilo Mesias <mailto:camilo@mesias.co.uk>
modified some bits to be compatible with the opie build system
packaging not complete yet
i hope camilo will maintain this here...
Diffstat (limited to 'noncore/multimedia/powerchord/chordengine.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/multimedia/powerchord/chordengine.cpp | 297 |
1 files changed, 297 insertions, 0 deletions
diff --git a/noncore/multimedia/powerchord/chordengine.cpp b/noncore/multimedia/powerchord/chordengine.cpp new file mode 100644 index 0000000..d8c3c4e --- a/dev/null +++ b/noncore/multimedia/powerchord/chordengine.cpp | |||
@@ -0,0 +1,297 @@ | |||
1 | // | ||
2 | // ChordEngine class to calculate chords | ||
3 | // | ||
4 | |||
5 | // Copyright (c) 2001 Camilo Mesias | ||
6 | // camilo@mesias.co.uk | ||
7 | // | ||
8 | // derived from JavaScript code by Jim Cranwell, used with permission | ||
9 | // | ||
10 | // This program is free software; you can redistribute it and/or | ||
11 | // modify it under the terms of the GNU General Public License | ||
12 | // as published by the Free Software Foundation; either version 2 | ||
13 | // of the License, or (at your option) any later version. | ||
14 | // | ||
15 | // This program is distributed in the hope that it will be useful, | ||
16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | // GNU General Public License for more details. | ||
19 | // | ||
20 | // You should have received a copy of the GNU General Public License | ||
21 | // along with this program; if not, write to the Free Software | ||
22 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
23 | |||
24 | |||
25 | #include <stdio.h> | ||
26 | |||
27 | #include <string.h> | ||
28 | |||
29 | #include "chordengine.h" | ||
30 | |||
31 | ChordEngine::ChordEngine(){ | ||
32 | |||
33 | string[0] = OPEN; | ||
34 | string[1] = OPEN; | ||
35 | string[2] = OPEN; | ||
36 | string[3] = OPEN; | ||
37 | string[4] = OPEN; | ||
38 | string[5] = OPEN; | ||
39 | |||
40 | base_note = 0; | ||
41 | chord_type = 0; | ||
42 | fret_pos = OPEN; | ||
43 | span_size = 1; | ||
44 | variation = 7; | ||
45 | tuning = 0; | ||
46 | |||
47 | js_tunit(tuning); | ||
48 | js_whatchord(chord_type); | ||
49 | js_vboy(variation); | ||
50 | |||
51 | label_text[0] = 0; | ||
52 | } | ||
53 | |||
54 | int ChordEngine::finger(int f){ | ||
55 | return string[f]; | ||
56 | } | ||
57 | #define BREAK (401) | ||
58 | void ChordEngine::calculate(){ | ||
59 | |||
60 | sprintf(label_text, "%s %s %s%s", | ||
61 | notes[base_note], | ||
62 | (chord_type)?keys[chord_type]:"", | ||
63 | (fret_pos==OPEN)?"":"fret-", | ||
64 | (fret_pos)?frets[fret_pos]:""); | ||
65 | |||
66 | string[0] = | ||
67 | string[1] = | ||
68 | string[2] = | ||
69 | string[3] = | ||
70 | string[4] = | ||
71 | string[5] = MUTED; | ||
72 | |||
73 | for (int in=0;in<6;in++){ | ||
74 | notename[in]=0; | ||
75 | } | ||
76 | |||
77 | js_tunit(tuning); | ||
78 | // js_MMM[0] = 4; | ||
79 | //js_MMM[1] = 9; | ||
80 | //js_MMM[2] = 2; | ||
81 | //js_MMM[3] = 7; | ||
82 | //js_MMM[4] = 11; | ||
83 | //js_MMM[5] = 4; | ||
84 | |||
85 | int js_D = base_note; | ||
86 | |||
87 | int js_Q = span_size; | ||
88 | |||
89 | js_Q += 2; | ||
90 | |||
91 | int js_V = variation; | ||
92 | |||
93 | int js_FR = fret_pos; | ||
94 | |||
95 | int js_G = 0; | ||
96 | |||
97 | int js_A = 0; | ||
98 | |||
99 | if (js_FR){ | ||
100 | js_G = 8; | ||
101 | } | ||
102 | |||
103 | js_L = 1; | ||
104 | js_Y = 1; | ||
105 | js_Z = 1; | ||
106 | |||
107 | js_vboy(variation); | ||
108 | |||
109 | js_Y += js_Q; | ||
110 | int js_K = 0; | ||
111 | int js_W = js_D - js_Z; | ||
112 | int js_H = -js_Z; | ||
113 | int js_N[6] = {0, 0, 0, 0, 0, 0}; | ||
114 | int js_TCK[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
115 | int js_E = 0; | ||
116 | int js_X = 0; | ||
117 | |||
118 | for (int j=0; j<41; j++){ | ||
119 | js_A = 0; | ||
120 | for (int b=0; b<7; b++){ | ||
121 | if (js_N[b] == 1){ | ||
122 | js_A++; | ||
123 | } | ||
124 | if ((js_A==6)||(j>39)){ | ||
125 | for (int i=0; i<12; i++){ | ||
126 | if (js_TCK[i] != js_T[i]){ | ||
127 | memset((void *)js_N, 0, 6*sizeof(int)); | ||
128 | memset((void *)js_TCK, 0, 12*sizeof(int)); | ||
129 | js_V++; | ||
130 | js_X++; | ||
131 | if (js_V>23){ | ||
132 | js_V = 0; | ||
133 | } | ||
134 | js_vboy(js_V); | ||
135 | j = 0; | ||
136 | i = BREAK; | ||
137 | b = BREAK; | ||
138 | if (js_X > 23){ | ||
139 | js_N[0] = js_N[1] = js_N[2] = js_N[3] = js_N[4] = js_N[5] = 1; | ||
140 | j = BREAK; | ||
141 | } | ||
142 | } | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | int js_S = 0; | ||
147 | js_W = (js_W + js_Z)%12; | ||
148 | js_H = (js_H + js_Z)%12; | ||
149 | |||
150 | while (js_S < js_Y){ | ||
151 | js_S++; | ||
152 | if (js_E > js_Q){ | ||
153 | js_E = 0; | ||
154 | js_K = (js_K + js_L)%6; | ||
155 | } | ||
156 | if (js_E<0){ | ||
157 | js_E = js_Q; | ||
158 | js_K = (js_K + js_L)%6; | ||
159 | } | ||
160 | if (js_T[js_H] == 0){ | ||
161 | for (int c=0;c<13;c++){ | ||
162 | js_W = (js_W+js_Z)%12; | ||
163 | js_H = (js_H+js_Z)%12; | ||
164 | if (js_T[js_H] == 1){ | ||
165 | c=BREAK; | ||
166 | } | ||
167 | } | ||
168 | } | ||
169 | if (js_N[js_K] == 1){ | ||
170 | for(int b=0; b<7; b++){ | ||
171 | js_K = (js_K+js_L)%6; | ||
172 | if(js_N[js_K] == 0){ | ||
173 | b = BREAK; | ||
174 | } | ||
175 | if(b==6){ | ||
176 | js_S = BREAK; | ||
177 | } | ||
178 | } | ||
179 | } | ||
180 | |||
181 | // js_K is the index of the string | ||
182 | // js_MMM is the base note played by that string in the current tuning | ||
183 | // js_FR is the fret position | ||
184 | // js_E | ||
185 | // js_F represents the note made by this string here | ||
186 | // js_N[] records if we have found a finger position for this string | ||
187 | // js_W - represents the note we want???? | ||
188 | |||
189 | int js_F = (js_MMM[js_K]+js_E+js_FR)%12; | ||
190 | |||
191 | if ((js_W==js_F)&&(js_N[js_K]==0)){ | ||
192 | |||
193 | int js_EG = js_E+js_G; | ||
194 | |||
195 | string[js_K] = js_EG; | ||
196 | notename[js_K] = notes[js_W]; | ||
197 | |||
198 | js_N[js_K] = 1; | ||
199 | js_TCK[js_H] = 1; | ||
200 | js_S = BREAK; | ||
201 | } | ||
202 | js_E += js_VM; | ||
203 | |||
204 | } | ||
205 | |||
206 | } | ||
207 | |||
208 | } | ||
209 | |||
210 | |||
211 | const int ChordEngine::alt_tunings[][6] = { | ||
212 | {4, 9,2,7,11,4}, | ||
213 | {4,11,4,8,11,4}, | ||
214 | {4, 9,4,9, 1,4}, | ||
215 | {4, 9,2,6,11,4}, | ||
216 | {4, 9,2,7, 0,5}, | ||
217 | {2, 9,2,7, 9,2}, | ||
218 | {2, 7,0,7, 0,2}, | ||
219 | {2, 9,2,6, 9,2}, | ||
220 | {2, 9,2,7,11,4}, | ||
221 | {2, 7,2,7,11,2}, | ||
222 | {2, 9,2,9, 0,2}, | ||
223 | {0, 7,0,7, 9,4}, | ||
224 | {5, 9,2,7,11,4}, | ||
225 | {7,10,2,7,10,2}, | ||
226 | }; | ||
227 | |||
228 | void ChordEngine::js_tunit(int t){ | ||
229 | int max=0; | ||
230 | int octave=0; | ||
231 | for (int i=0;i<6;i++){ | ||
232 | js_MMM[i] = ChordEngine::alt_tunings[t][i]; | ||
233 | if (js_MMM[i] < max){ | ||
234 | octave += 12; | ||
235 | } | ||
236 | max = js_MMM[i]; | ||
237 | note_indices[i] = octave + js_MMM[i]; | ||
238 | } | ||
239 | } | ||
240 | |||
241 | const int ChordEngine::chordbases[][12] = { | ||
242 | {1,0,0,0,1,0,0,1,0,0,0,0}, // maj | ||
243 | {1,0,0,1,0,0,0,1,0,0,0,0}, // min | ||
244 | {1,0,0,0,1,0,0,1,0,0,1,0}, // 7th | ||
245 | {1,0,0,1,0,0,0,1,0,0,1,0}, // m7 | ||
246 | {1,0,0,0,1,0,0,1,0,0,0,1}, // maj7 | ||
247 | {1,0,0,0,1,0,0,1,0,1,0,0}, // 6th | ||
248 | {1,0,0,1,0,0,0,1,0,1,0,0}, // m6th | ||
249 | {1,0,0,0,1,0,0,0,1,0,0,0}, // aug | ||
250 | {1,0,0,1,0,0,1,0,0,1,0,0}, // dim | ||
251 | {1,0,0,0,0,1,0,1,0,0,0,0}, // sus4 | ||
252 | {1,0,0,0,0,1,0,1,0,0,1,0}, // 7sus4 | ||
253 | {1,0,1,0,1,0,0,1,0,0,1,0}, // 9th | ||
254 | {1,0,1,0,1,0,0,1,0,0,0,0}, // add9 | ||
255 | {1,0,1,1,0,0,0,1,0,0,1,0}, // m9th | ||
256 | {1,1,0,0,1,0,0,1,0,0,0,1}, // maj9 | ||
257 | {1,0,1,0,0,0,0,1,0,0,0,0}, // sus2 | ||
258 | {1,0,1,0,0,0,0,1,0,0,1,0}, // 7sus2 | ||
259 | {1,0,1,0,0,1,0,1,0,0,1,0}, // 11th | ||
260 | {1,0,1,1,0,1,0,1,0,0,1,0}, // m11th | ||
261 | {1,0,0,0,1,0,0,1,0,1,1,0}, // 13th | ||
262 | {1,0,0,1,0,0,0,1,0,1,1,0}, // m13th | ||
263 | {1,0,0,1,0,0,0,1,0,1,0,1}, // maj13 | ||
264 | {1,0,1,0,1,0,0,1,0,1,0,0}, // 6/9 | ||
265 | {1,0,0,0,1,0,1,0,0,0,0,0}, // flat5 | ||
266 | {1,0,0,1,1,0,0,0,0,0,1,0}, // 7#9 | ||
267 | {1,0,0,1,0,0,1,0,0,0,1,0}, // Ø7 | ||
268 | {1,0,0,0,0,0,0,1,0,0,0,0}, // 5 | ||
269 | }; | ||
270 | |||
271 | void ChordEngine::js_whatchord(int c){ | ||
272 | for (int i=0;i<12;i++){ | ||
273 | js_T[i] = ChordEngine::chordbases[c][i]; | ||
274 | } | ||
275 | } | ||
276 | |||
277 | void ChordEngine::js_vboy(int v){ | ||
278 | js_L = ((v/3)%2)?5:1; | ||
279 | js_Y = ((v/6)%2)?45:4; | ||
280 | js_Z = (v%3 == 0)?1:(v%3 == 1)?7:11; | ||
281 | js_VM = ((v/12)%2)?-1:1; | ||
282 | } | ||
283 | |||
284 | const char* ChordEngine::notes[] = {"C", "C#", "D", "Eb", "E", "F", "F#", "G", "G#", "A", "Bb", "B", 0}; | ||
285 | |||
286 | const char* ChordEngine::keys[] = {"maj", "min", "7th", "m7", "maj7", "6th", "m6th", "aug", "dim", "sus4", "7sus4", "9th", "add9", "m9th", "maj9", "sus2", "7sus2", "11th", "m11th", "13th", "m13th", "maj13", "6|9", "flat5", "7#9", "Ø7", "5", 0}; | ||
287 | |||
288 | const char* ChordEngine::frets[] = {"open", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", 0}; | ||
289 | |||
290 | const char* ChordEngine::variations[] = {"V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13", "V14", "V15", "V16", "V17", "V18", "V19", "V20", "V21", "V22", "V23", 0}; | ||
291 | |||
292 | const char* ChordEngine::tunings[] = {"EADGBE", "EBEG#BE", "EAEAC#E", "EADF#BE", "EADGCF", "DADGAD", "DGCGCD", "DADF#AD", "DADGBE", "DGDGBD", "DADACD", "CGCGAE", "FADGBE", "Gminor",0}; | ||
293 | |||
294 | |||
295 | |||
296 | |||
297 | |||