00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "jdkmidi/world.h"
00036
00037 #include "jdkmidi/keysig.h"
00038
00039
00040 #ifndef DEBUG_MDKEYSIG
00041 # define DEBUG_MDKEYSIG 0
00042 #endif
00043
00044 #if DEBUG_MDKEYSIG
00045 # undef DBG
00046 # define DBG(a) a
00047 #endif
00048
00049 namespace jdkmidi
00050 {
00051
00052
00053 int MIDIKeySignature::sharp_list[7] = { 3, 0, 4, 1, 5, 2, 6 };
00054 int MIDIKeySignature::flat_list[7] = { 6, 2, 5, 1, 4, 0, 3 };
00055
00056
00057 MIDIKeySignature::MIDIKeySignature()
00058 {
00059 ENTER( "MIDIKeySignature::MIDIKeySignature()" );
00060 use_sharps=true;
00061 sharp_flat=0;
00062 major=true;
00063 Reset();
00064 }
00065
00066 MIDIKeySignature::MIDIKeySignature( const MIDIKeySignature &k )
00067 {
00068 ENTER( "MIDIKeySignature::MIDIKeySignature()" );
00069 use_sharps=k.use_sharps;
00070 sharp_flat=k.sharp_flat;
00071 major=k.major;
00072
00073 Reset();
00074 }
00075
00076
00077
00078
00079
00080
00081
00082 void MIDIKeySignature::Reset()
00083 {
00084 ENTER( "MIDIKeySignature::Reset()" );
00085 if( sharp_flat < -7 )
00086 sharp_flat=-7;
00087 if( sharp_flat >7 )
00088 sharp_flat=7;
00089
00090 for( int note=0; note<7; ++note )
00091 state[note]=ACCNatural;
00092
00093 if( sharp_flat==0 )
00094 {
00095
00096
00097
00098 use_sharps=true;
00099 }
00100 else if( sharp_flat>0 )
00101 {
00102
00103
00104
00105
00106 use_sharps=true;
00107
00108 for( short i=0; i<sharp_flat; ++i )
00109 {
00110 state[sharp_list[i]]=ACCSharp;
00111 }
00112 }
00113 else if( sharp_flat<0 )
00114 {
00115
00116
00117
00118
00119
00120 int flats= -sharp_flat;
00121
00122 use_sharps=false;
00123
00124 for( int i=0; i<flats; ++i )
00125 {
00126 state[ flat_list[i] ]=ACCFlat;
00127 }
00128
00129 }
00130
00131 }
00132
00133
00134
00135
00136 bool MIDIKeySignature::ProcessWhiteNote( int in_note, int *out_note )
00137 {
00138 ENTER( "MIDIKeySignature::ProcessWhiteNote()" );
00139
00140
00141
00142
00143
00144 if( state[in_note]==ACCNatural )
00145 {
00146
00147
00148
00149
00150 *out_note=in_note;
00151
00152
00153
00154
00155
00156
00157 return false;
00158 }
00159 else
00160 {
00161
00162
00163
00164
00165
00166 *out_note=in_note;
00167
00168
00169
00170
00171
00172 state[in_note]=ACCNatural;
00173
00174
00175
00176
00177
00178 return true;
00179 }
00180 }
00181
00182
00183 bool MIDIKeySignature::ProcessBlackNote( int in_note, int *out_note )
00184 {
00185 ENTER( "MIDIKeySignature::ProcessBlackNote()" );
00186
00187
00188
00189
00190
00191 if( state[in_note]==ACCSharp )
00192 {
00193 *out_note=in_note;
00194 return false;
00195 }
00196
00197
00198
00199
00200
00201
00202 if( state[in_note+1]==ACCFlat )
00203 {
00204 *out_note=in_note+1;
00205 return false;
00206 }
00207
00208
00209
00210
00211
00212 if( use_sharps )
00213 {
00214
00215
00216
00217
00218 state[in_note]=ACCSharp;
00219 *out_note=in_note;
00220
00221
00222
00223
00224 return true;
00225 }
00226 else
00227 {
00228
00229
00230
00231
00232 state[in_note+1]=ACCFlat;
00233 *out_note=in_note+1;
00234
00235
00236
00237
00238
00239 return true;
00240 }
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250 bool MIDIKeySignature::ConvertMIDINote(
00251 int in_note,
00252 int *out_note
00253 )
00254 {
00255 ENTER( "MIDIKeySignature::ConvertMIDINote()" );
00256
00257 int octave=in_note/12;
00258 int midi_note=in_note%12;
00259 int actual_note=0;
00260 bool changed=false;
00261
00262 switch( midi_note )
00263 {
00264 case 0:
00265 changed=ProcessWhiteNote(0,&actual_note);
00266 break;
00267 case 2:
00268 changed=ProcessWhiteNote(1,&actual_note);
00269 break;
00270 case 4:
00271 changed=ProcessWhiteNote(2,&actual_note);
00272 break;
00273 case 5:
00274 changed=ProcessWhiteNote(3,&actual_note);
00275 break;
00276 case 7:
00277 changed=ProcessWhiteNote(4,&actual_note);
00278 break;
00279 case 9:
00280 changed=ProcessWhiteNote(5,&actual_note);
00281 break;
00282 case 11:
00283 changed=ProcessWhiteNote(6,&actual_note);
00284 break;
00285
00286 case 1:
00287 changed=ProcessBlackNote(0,&actual_note);
00288 break;
00289 case 3:
00290 changed=ProcessBlackNote(1,&actual_note);
00291 break;
00292 case 6:
00293 changed=ProcessBlackNote(3,&actual_note);
00294 break;
00295 case 8:
00296 changed=ProcessBlackNote(4,&actual_note);
00297 break;
00298 case 10:
00299 changed=ProcessBlackNote(5,&actual_note);
00300 break;
00301 };
00302
00303 *out_note = (octave*7) + actual_note;
00304 return changed;
00305
00306 }
00307
00308 }