Problem #18

Code Wars II
Danger Level: Double Black uu




}Musical Transposition~

Point Value: 16
 

Problem Statement

Some musical instruments, such as the flute and the clarinet, require written music to be transposed for that instrument's tuning.  For instance, music for a Bb clarinet is written 2 half steps above the actual notes.  Write a program to transpose music according to the rules in the appendix.
 

Program Input

The input file PROB18.IN begins with a line indicating the tuning of the target instrument.  For this problem you may assume the only instrument tunings will be Bb, D and Eb.  The next line specifies the key signature of the source music.  The notes from the source music begin on the next line separated by white space.  The program may assume that no line will be longer than 80 characters, and that the source music is written for an instrument tuned to C.

TARGET: Bb CLARINET
KEY SIGNATURE: F
A5 B5 C5 D5 E5 A6 G#5 E5 B5 D5
C#5 A5 G5 F#5 E5 F#5 D5
F5 E5 D5 C5 Bb5 A5 G4 F4 E4
F4 A6 E4 G5 D4 G5 F5 E5 D5 C5 Bb5 A5
 

Program Output

The program should write the transposed music to the output file PROB18.OUT.  The first line should be identical to the first line of the input file, except the program should change the key word TARGET to INSTRUMENT.  The second line should indicate the transposed key signature, and the notes should follow thereafter.  The program should maintain the same line breaks between notes as in the input file.

INSTRUMENT: Bb CLARINET
KEY SIGNATURE: G
B5 C#5 D5 E5 F#5 B6 A#6 F#5 C#5 E5
D#5 B5 A6 G#5 F#5 G#5 E5
G5 F#5 E5 D5 C5 B5 A5 G4 F#4
G#4 B6 F#4 A6 E4 A5 G5 F#5 E5 D5 C5 B5
 
 
 
 

Appendix A: Basic Music Theory

Tranditional western music, like math, has a sophisticated written symbolic language and vocabulary.  We will present a small subset here for use in solving this problem.  The solution to this problem is quite simple once you understand what is being asked.

There are seven basic named notes: A, B, C, D, E, F, and G, which occur in a rotating sequence called an octave.  In written music, the octave of a particular note is indicated by its position on the staff.  In our shorthand notation we'll write a number after each note to indicate its octave, such as E4 or C5.  The octave number is incremented each time the cycle restarts at A, so the sequence looks like this: A1, B1, C1, D1, E1, F1, G1, A2, B2, C2, D2, etc.

There are 12 half-step intervals in each octave.  The half-steps are shown in figure 1.  A whole-step interval is two half-steps, i.e., A to B or D to E.  The notes in between the basic named notes are called sharp (#) or flat (b).  The general term for sharps and flats is accidental.  When one of the basic notes appears without an accidental, it is called a natural. (The symbol for a natural isn't included in most standard fonts, so we won't show it here.  This symbol is rarely drawn, so for this problem we'll assume any note without a # or b is a natural.)  Notice that there is no half-step inteval between B and C, nor between E and F.   In our notation we will write the accidental before the octave number, i.e., G#6 means "the G sharp in octave six".
 

1/2 steps sharps flats
0
A
A
1
A#
Bb
2
B
B
3
C
C
4
C#
Db
5
D
D
6
D#
Eb
7
E
E
8
F
F
9
F#
Gb
10
G
G
11
G#
Ab
12
A
A
figure 1
Key Notes in the key (scale)
Bb Bb C D Eb F G A
C C D E F G A B
D D E F# G A B C#
Eb Eb F G Ab Bb C D
F F G A Bb C D E
G G A B C D E F#
A A B.. C# D E F# G#
figure 2

Traditional western music is written around a certain key, which is a grouping of seven of the twelve notes.  The ordered sequence of these notes is called a scale.  The keys which we will use for this problem are shown in figure 2.  Sometimes the intermediate notes (between the basic named notes) are sometimes referenced by the #, sometimes by the b, depending on the key signature.  This means that although G# may be the same frequency (or pitch) as Ab, one notation will be preferred over the other in certain circumstances.  Using these notes by way of example, if the key signature is A, the note should be written as G#, but if the key signature is Eb, that same note should be written as Ab.
 
 

Appendix B: Transposition

trans•pose - to alter a sequence of musical notes by a fixed interval.

As indicated in the problem statement, some musical instruments require written music to be transposed for that instrument's tuning.  The instruments are often named by their tuning.  For instance, a Bb clarinet produces a Bb pitch when fingered for a C.  An Eb clarinet, as you might guess, produces an Eb pitch when fingered for a C.  The music for these instruments must therefore be transposed appropriately.  In the case of the Bb clarinet, each note must be raised by two half steps, Bb to C, D to E, etc.  When music is transposed this way the key signature is also typically transposed.  For the Bb clarinet the key of F would transpose up a whole step to the key of G.  Music for the D clarinet must be transposed down a whole step, i.e., from the key of A to the key of G.  An Eb clarinet requires music to be transposed down three half-steps, such as from the key of F to the key of D.

The transposed notes should be written in such a way as to conform to the transposed key signature.  For all the notes that are in the key this is a simple mapping, but for notes with accidentals outside the key it becomes trickier.  The rule is to transpose a note from its position (or interval) in the original key to the corresponding interval in the transposed key.  Let's stick with the Bb clarinet and examine some of the possibilities:
 

oringinal
key   note
transposed
key    note
comments
Bb Gb C Ab The key of C has no accidentals, so the transposed note retains the accidental of the original note.
G D# A E# The keys of G and A both have sharps, so D# (the 5th note in the scale) is transposed to E# (also the 5th in the scale).  F is not used even though is is the same pitch as E# and may appear simpler.
Eb B F C# Since a B natural is a half step above the 5th note in the scale, we transpose to 1/2 step above the 5th note in F (the transposed key), to a C#.
F Eb G F The Eb is a half step below the 7th note in the scale, so we transpose to 1/2 step below the 7th note in G (the transposed key), to an F.

The program may assume that the note data will not contain double-sharps or double-flats, either in the original or transposed data.  These rare cases do sometimes occur in written music.