Code To Work With Musical Keys (chords)

V

vf007

Hello,
I'm working on a macro to transpose keys in song sheets.

Sub TransposeKeys()
Dim MusicNotesArray(11) As String
MusicNotesArray(0) = Chr(65) 'A
MusicNotesArray(1) = Chr(66) & Chr(98) 'Bb
MusicNotesArray(2) = Chr(66) 'B
MusicNotesArray(3) = Chr(67) 'C
MusicNotesArray(4) = Chr(67) & Chr(35) 'C#
MusicNotesArray(5) = Chr(68) 'D
MusicNotesArray(6) = Chr(69) & Chr(98) 'Eb
MusicNotesArray(7) = Chr(69) 'E
MusicNotesArray(8) = Chr(70) 'F
MusicNotesArray(9) = Chr(70) & Chr(35) 'F#
MusicNotesArray(10) = Chr(71) 'G
MusicNotesArray(11) = Chr(65) & Chr(98) 'Ab

'define actual page as a range
Dim MyRange As Range
Set MyRange = Selection.Range
Set MyRange = MyRange.GoTo(What:=wdGoToBookmark, Name:="\page")

'replace strings only in specified style
MyRange.Find.ClearFormatting
MyRange.Find.Style = ActiveDocument.Styles("Título 2")
MyRange.Find.Replacement.ClearFormatting
With MyRange.Find
.Text = MusicNotesArray(5) 'just an example
.Replacement.Text = MusicNotesArray(9) 'just an example
.MatchCase = True
.Forward = True
.Wrap = wdFindStop
End With
MyRange.Find.Execute Replace:=wdReplaceAll

End Sub

Now i'm working on a code to find and replace all ocuurences of the
musical scale in a song (sorry about my english).
My very hard problem is turn my code inteligent. For example, the code
must to find "C" and replace to "D"; "Am7" to "Bm7", etc. I'm not
considering de variations of the chords (m7, º, 7M, etc), only the #'s
and b's). The problem is if I have:

C Am7 F D#mº
onono onono nonon nonono ono

It will turned to

D Bm7 G Eb#m <- THIS CHORD IS INCORRECT!
onono onono nonon nonono ono

How to identify those chords with # or b and to avoid its incorrect
transposition?

Regards.
VFrar
 
W

ward

-----Original Message-----
Hello,
I'm working on a macro to transpose keys in song sheets.
.........
How to identify those chords with # or b and to avoid its
incorrect transposition?

Maybe you can write a function that will:
- first find a letter(A, B, C, D, E, F, G)
- then determines if the letter is succeeded by an "b" or
a "#", or nothing at all
- and then returns (for example in two ranges) the first
part ("Ab") and the extra part (e.g. "m7")

You can then replace the text in the first part ("Ab")
with the appropriate transpotioned chord.

BTW: i think you will need an extra array that will tell
you program that Ab and G# both should be replaced by (for
example) Ab.

Ward
 
W

Word Heretic

G'day vf007 <[email protected]>,

the .words collection is probably what you need to iterate.


Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: Steve at wordheretic.com


vf007 was spinning this yarn:
Hello,
I'm working on a macro to transpose keys in song sheets.

Sub TransposeKeys()
Dim MusicNotesArray(11) As String
MusicNotesArray(0) = Chr(65) 'A
MusicNotesArray(1) = Chr(66) & Chr(98) 'Bb
MusicNotesArray(2) = Chr(66) 'B
MusicNotesArray(3) = Chr(67) 'C
MusicNotesArray(4) = Chr(67) & Chr(35) 'C#
MusicNotesArray(5) = Chr(68) 'D
MusicNotesArray(6) = Chr(69) & Chr(98) 'Eb
MusicNotesArray(7) = Chr(69) 'E
MusicNotesArray(8) = Chr(70) 'F
MusicNotesArray(9) = Chr(70) & Chr(35) 'F#
MusicNotesArray(10) = Chr(71) 'G
MusicNotesArray(11) = Chr(65) & Chr(98) 'Ab

'define actual page as a range
Dim MyRange As Range
Set MyRange = Selection.Range
Set MyRange = MyRange.GoTo(What:=wdGoToBookmark, Name:="\page")

'replace strings only in specified style
MyRange.Find.ClearFormatting
MyRange.Find.Style = ActiveDocument.Styles("Título 2")
MyRange.Find.Replacement.ClearFormatting
With MyRange.Find
Text = MusicNotesArray(5) 'just an example
Replacement.Text = MusicNotesArray(9) 'just an example
MatchCase = True
Forward = True
Wrap = wdFindStop
End With
MyRange.Find.Execute Replace:=wdReplaceAll

End Sub

Now i'm working on a code to find and replace all ocuurences of the
musical scale in a song (sorry about my english).
My very hard problem is turn my code inteligent. For example, the code
must to find "C" and replace to "D"; "Am7" to "Bm7", etc. I'm not
considering de variations of the chords (m7, º, 7M, etc), only the #'s
and b's). The problem is if I have:

C Am7 F D#mº
onono onono nonon nonono ono

It will turned to

D Bm7 G Eb#m <- THIS CHORD IS INCORRECT!
onono onono nonon nonono ono

How to identify those chords with # or b and to avoid its incorrect
transposition?

Regards.
VFrari

Steve Hudson

Word Heretic, Sydney, Australia

wordheretic.com

If answers r 2 terse, ask again or hassle an MVP,
at least they get recognition for it then.
Lengthy replies offlist require payment.
 
W

Word Heretic

G'day (e-mail address removed) (V),

your 2d range is already accomplished by using the words collection
across the range. Its better than my first for each character idea -
ANYTHING is better than the for each char method ;-)

Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: Steve at wordheretic.com


V was spinning this yarn:
What happened with your last ideia?
I was thinking about a 2d range...
VFRARI

Steve Hudson

Word Heretic, Sydney, Australia

wordheretic.com

If answers r 2 terse, ask again or hassle an MVP,
at least they get recognition for it then.
Lengthy replies offlist require payment.
 
W

Word Heretic

G'day (e-mail address removed) (V),

Your problems are twofold. I am a registered music composer with
several published works. I know just enough to know when I go
screaming for any one of my mates who has proper fifth year
musicianship or higher. My ear is good enough to say "this note is
wrong, and so is this one, and I've done something strange with this
chord progression" and they wave their hand over the few offenders and
all is sweet again. Your first problem is you need to eyeball such a
person and have a good yack to understand keys, tones, semi-tones and
transposition.

Once you have done this, the VBA is fairly trivial. You populate a 2d
array with your key scales. You then iterate the .words collection of
the range, which are all notes entire, eg F# including the trailing
space is a word. Use Trim$() to trim off the spurious spacing.

Find the offset of that note in your chosen key, apply any
transpositioning changes required by the scale types, and you have the
offset into the new key's scale for the note. You will need to use
manual modulo arithmetic, err 12 semitones per key? - see, I need all
my references as well to keep this stuff straight - to reduce to an
octave. If you blow your upper or lower limits (<1 or >12) you need to
indicate the octave change.

If any of these VBA concepts is beyond your current comprehension, ask
for clarification to ensure mental scarification.

Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: Steve at wordheretic.com


V was spinning this yarn:
 
V

V

Thanks Steve! I see you're very interested to help me. Some concepts
are strange to me. Maybe you could to put something in vba code.
Sorry. I'm a beginning user in VB & VBA.
VFrari

Word Heretic said:
G'day (e-mail address removed) (V),

Your problems are twofold. I am a registered music composer with
several published works. I know just enough to know when I go
screaming for any one of my mates who has proper fifth year
musicianship or higher. My ear is good enough to say "this note is
wrong, and so is this one, and I've done something strange with this
chord progression" and they wave their hand over the few offenders and
all is sweet again. Your first problem is you need to eyeball such a
person and have a good yack to understand keys, tones, semi-tones and
transposition.

Once you have done this, the VBA is fairly trivial. You populate a 2d
array with your key scales. You then iterate the .words collection of
the range, which are all notes entire, eg F# including the trailing
space is a word. Use Trim$() to trim off the spurious spacing.

Find the offset of that note in your chosen key, apply any
transpositioning changes required by the scale types, and you have the
offset into the new key's scale for the note. You will need to use
manual modulo arithmetic, err 12 semitones per key? - see, I need all
my references as well to keep this stuff straight - to reduce to an
octave. If you blow your upper or lower limits (<1 or >12) you need to
indicate the octave change.

If any of these VBA concepts is beyond your current comprehension, ask
for clarification to ensure mental scarification.

Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: Steve at wordheretic.com


V was spinning this yarn:
What happened with your last ideia?
I was thinking about a 2d range...
VFRARI

Word Heretic said:
G'day vf007 <[email protected]>,

the .words collection is probably what you need to iterate.


Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: Steve at wordheretic.com


vf007 was spinning this yarn:


Hello,
I'm working on a macro to transpose keys in song sheets.

Sub TransposeKeys()
Dim MusicNotesArray(11) As String
MusicNotesArray(0) = Chr(65) 'A
MusicNotesArray(1) = Chr(66) & Chr(98) 'Bb
MusicNotesArray(2) = Chr(66) 'B
MusicNotesArray(3) = Chr(67) 'C
MusicNotesArray(4) = Chr(67) & Chr(35) 'C#
MusicNotesArray(5) = Chr(68) 'D
MusicNotesArray(6) = Chr(69) & Chr(98) 'Eb
MusicNotesArray(7) = Chr(69) 'E
MusicNotesArray(8) = Chr(70) 'F
MusicNotesArray(9) = Chr(70) & Chr(35) 'F#
MusicNotesArray(10) = Chr(71) 'G
MusicNotesArray(11) = Chr(65) & Chr(98) 'Ab

'define actual page as a range
Dim MyRange As Range
Set MyRange = Selection.Range
Set MyRange = MyRange.GoTo(What:=wdGoToBookmark, Name:="\page")

'replace strings only in specified style
MyRange.Find.ClearFormatting
MyRange.Find.Style = ActiveDocument.Styles("Título 2")
MyRange.Find.Replacement.ClearFormatting
With MyRange.Find
Text = MusicNotesArray(5) 'just an example
Replacement.Text = MusicNotesArray(9) 'just an example
MatchCase = True
Forward = True
Wrap = wdFindStop
End With
MyRange.Find.Execute Replace:=wdReplaceAll

End Sub

Now i'm working on a code to find and replace all ocuurences of the
musical scale in a song (sorry about my english).
My very hard problem is turn my code inteligent. For example, the code
must to find "C" and replace to "D"; "Am7" to "Bm7", etc. I'm not
considering de variations of the chords (m7, º, 7M, etc), only the #'s
and b's). The problem is if I have:

C Am7 F D#mº
onono onono nonon nonono ono

It will turned to

D Bm7 G Eb#m <- THIS CHORD IS INCORRECT!
onono onono nonon nonono ono

How to identify those chords with # or b and to avoid its incorrect
transposition?

Regards.
VFrari


------------------------------------------------
[/url]


Steve Hudson

Word Heretic, Sydney, Australia

wordheretic.com

If answers r 2 terse, ask again or hassle an MVP,
at least they get recognition for it then.
Lengthy replies offlist require payment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top