Converting between NWC clip text and MIDI values 2005-06-28 09:13 am I've worked out the algorithm in case it's useful to other programmers. This could be useful for finding ranges where clef and enharmonics do matter, or for help in transposing. If this sort of thing is not your cup of tea, please ignore it!-------------------------------------------------------As you process a clip file, you need to capture each clef change, as it provides input into the algorithm. Then each note should be processed individually. Andrew Purdam's transpose script does this note processing very elegantly. One day real-soon now I'm gonna learn PHP so I can write the tools I need rather than just being envious!First, you need a look-up/conversion of some sort for clefs, so that:bass = 6, tenor = 2, alto = 0, treble = -6. Other clefs can be added later if NWC includes them. The value is the position of Middle C in the clef.Second you need a look-up for octave-shift, so that:8va = 7, (none) = 0, 8vb = -7.Next you need another look-up for accidentals, so that:double flat = -2, flat = -1, natural (or nothing) = 0, sharp = 1, double sharp = 2Instead of any of the look-up tables, you could use "if" statements.NWC 2 MIDICALCULATE:alto_clef_position = note_position - clef_lookup + shift_lookupoctave_offset = truncate(alto_clef_position / 7) * 12semitone_offset = truncate(mod(alto_clef_position,7) * 7 / 4 + 0.5 )MIDI_value = 60 + octave_offset + semitone_offset + accidental_lookupThis is the standard MIDI value for the note, with Middle C = 60.MIDI 2 NWCCALCULATE:note_position = truncate((MIDI_value - 60) * 7 / 12 + 0.1) + clef_lookup - shift_lookupsemitone_offset = mod(MIDI_value,12)accidental_offset = mod(mod(semitone_offset,5),2)-(semitone_offset*2-21)*truncate(semitone_offset/10) [this will be 0 or 1]if (accidental_offset = 1) then accidental is "#" else accidental is nothingI've tested for all MIDI values 0 to 127. I'm not happy with the formula for accidental_offset in MIDI2NWC, and will try to find something more succinct. It can always be replaced by something like:accidental is nothing: if semitone_offset in {1, 3, 6, 8, 10} then accidental is "#". Quote Selected
Re: Converting between NWC clip text and MIDI values Reply #1 – 2005-06-28 02:58 pm natural or nothingKey Signature might have some influence here! And in fact you have to keep track of earlier accidentals in the bar, (and at different octaves since that is the way NW works ;-( ), and accidentals tied over from the previous bar.And the other way, you really need to decide whether the note is best sharpened or flattened (which depends on the key) Quote Selected
Re: Converting between NWC clip text and MIDI values Reply #2 – 2005-06-28 09:26 pm Yes - I realised the effect of key signature and accidentals overnight! (it's about 7:30 AM where I am)I'll have a bit more of a think and adjust the algorithm.Cheers, Ewanin Canberra Quote Selected
Re: Converting between NWC clip text and MIDI values Reply #3 – 2005-07-01 01:19 am The obvious thing is to Force Accidentals before you start - it should all work fine then! Quote Selected