Musical Substitution Cipher
Description:
This Python program takes a text input from the user and generates an XML file where each letter in the input text is associated with a specific musical pitch. The pitches start at C3 for the letter 'A' and proceed chromatically through the musical scale.
Luis = B3 G#4 G#3 F#4
Run the code
Python Code
import xml.etree.ElementTree as ET
# Mapping letters to pitches
def letter_to_pitch(letter):
pitch_dict = {
'A': 'C3', 'B': 'C#3', 'C': 'D3', 'D': 'D#3', 'E': 'E3', 'F': 'F3', 'G': 'F#3',
'H': 'G3', 'I': 'G#3', 'J': 'A3', 'K': 'A#3', 'L': 'B3', 'M': 'C4', 'N': 'C#4',
'O': 'D4', 'P': 'D#4', 'Q': 'E4', 'R': 'F4', 'S': 'F#4', 'T': 'G4', 'U': 'G#4',
'V': 'A4', 'W': 'A#4', 'X': 'B4', 'Y': 'C5', 'Z': 'C#5'
}
return pitch_dict.get(letter.upper(), 'rest')
# Create XML structure
def create_xml(text):
root = ET.Element("melody")
for letter in text:
pitch = letter_to_pitch(letter)
note = ET.SubElement(root, "note")
ET.SubElement(note, "letter").text = letter
ET.SubElement(note, "pitch").text = pitch
tree = ET.ElementTree(root)
tree.write("melody.xml", encoding="utf-8", xml_declaration=True)
# Get user input and create XML file
user_input = input("Enter your text: ")
create_xml(user_input)
print("XML file 'melody.xml' created.")