Musical Braille Cipher

Description:


Luis  =  B3 G#4 G#3 F#4


Run the Python Program

Python Code

# Morse Code and Braille Dictionaries

MORSE_CODE_DICT = {

    'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',

    'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',

    'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',

    'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',

    'Y': '-.--', 'Z': '--..',

    '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', 

    '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.',

    '.': '.-.-.-', ',': '--..--', '?': '..--..', "'": '.----.', '!': '-.-.--',

    '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...', ':': '---...', 

    ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', 

    '_': '..--.-', '"': '.-..-.', '@': '.--.-.', ' ': '/'

}


BRAILLE_DICT = {

    'A': '100000', 'B': '101000', 'C': '110000', 'D': '110100', 'E': '100100',

    'F': '111000', 'G': '111100', 'H': '101100', 'I': '011000', 'J': '011100',

    'K': '100010', 'L': '101010', 'M': '110010', 'N': '110110', 'O': '100110',

    'P': '111010', 'Q': '111110', 'R': '101110', 'S': '011010', 'T': '011110',

    'U': '100011', 'V': '101011', 'W': '011101', 'X': '110011', 'Y': '110111',

    'Z': '100111',

    '0': '011111', '1': '100000', '2': '101000', '3': '110000', '4': '110100',

    '5': '100100', '6': '111000', '7': '111100', '8': '101100', '9': '011000',

    ' ': '000000', '.': '100001', ',': '110000', '?': '001001', '!': '011010',

    "'": '001000', '-': '100001', '/': '001101', '(': '010010', ')': '010011',

    '&': '101000', ':': '010110', ';': '010011', '=': '100011', '+': '101001',

    '_': '100101', '"': '001010', '@': '101100'

}


VALID_NOTES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B", "Ab", "Bb"}


def text_to_morse(text):

    """Convert text to Morse code."""

    return [MORSE_CODE_DICT.get(char.upper(), '') for char in text]


def text_to_braille(text):

    """Convert text to Braille."""

    return [BRAILLE_DICT.get(char.upper(), '000000') for char in text]


def print_morse_formatted(input_text, morse_code):

    """Print formatted Morse code representation."""

    print("Morse Code Representation:")

    for char, code in zip(input_text.upper(), morse_code):

        print(f"\t\t{char} : {code}")


def print_braille_formatted(input_text, braille_code, translated_notes):

    """Print formatted Braille representation with translated notes."""

    print("Braille Representation:")

    for char, cell, notes in zip(input_text.upper(), braille_code, translated_notes):

        print(f"\t\t{char} : {cell}\t{notes}")


def display_help():

    """Display help information."""

    print("""

    Welcome to the Text Converter!


    Options:

    1. Convert to Morse Code

       - Enter text to convert to Morse code.

       - The Morse code will be printed.


    2. Convert to Braille

       - Enter text to convert to Braille representation.

       - The Braille code will be printed.


    9. Help

       - Displays this help information.


    Notes:

    - Morse Code: Uses dots (.) and dashes (-) to represent characters.

    - Braille: Uses patterns of raised dots to represent characters.

    - When entering a scale, ensure notes are separated by spaces (e.g., C D E F# G# Ab).

    """)


def validate_notes(scale):

    """Validate the notes in the scale."""

    for note in scale:

        if note not in VALID_NOTES:

            raise ValueError(f"The scale contains invalid notes: {note}. Please ensure all notes are correctly formatted and are part of the following set: {', '.join(VALID_NOTES)}.")


def check_scale_length(braille_code_list, scale):

    """Check if the scale has enough notes to cover the Braille code positions."""

    num_positions = len(braille_code_list[0])  # Assume all Braille code entries have the same length

    num_notes = len(scale)

    if num_notes < num_positions:

        raise ValueError("The scale provided has fewer notes than the number of positions in the Braille code. Please provide a scale with at least as many notes as there are positions in the Braille code.")


def handle_scale(scale, braille_code_list):

    """

    Process the scale input and replace Braille code with notes from the scale.

    """

    scale = scale.split()  # Convert input string to list

    try:

        validate_notes(scale)  # Validate notes in the scale

        check_scale_length(braille_code_list, scale)  # Check scale length

    except ValueError as e:

        print(f"Error: {e}")

        print("Ensure the scale notes are separated by spaces.")

        return None


    # Map Braille code to scale

    translated_notes = []

    for braille in braille_code_list:

        notes = [scale[i] for i, char in enumerate(braille) if char == '1']

        translated_notes.append(' '.join(notes))


    return translated_notes


def main():

    """Main function."""

    print("Welcome to the Text Converter!")

    print("Instructions:")

    print("1 - Convert to Morse Code")

    print("2 - Convert to Braille")

    print("9 - Help")

    

    choice = input("Enter your choice (1, 2, or 9): ").strip()


    if choice == '9':

        display_help()

        return


    if choice not in ['1', '2']:

        print("Invalid choice. Please select 1, 2, or 9.")

        return


    input_text = input("Enter text to convert: ")


    if choice == '1':

        morse_code_list = text_to_morse(input_text)

        morse_code = ' '.join(morse_code_list)

        print(f"\nInput Text:       {input_text}")

        print(f"Morse Code:       {morse_code}")  # Line with the complete Morse code

        print_morse_formatted(input_text, morse_code_list)

    

    elif choice == '2':

        braille_code_list = text_to_braille(input_text)

        braille_code = ' '.join(braille_code_list)

        print(f"\nInput Text:       {input_text}")

        print(f"Braille Code:   {braille_code}")


        # New User Input for Scale/Encoder

        scale = input("Enter notes for Scale/Encoder (e.g., C D E F# G# Ab): ").strip()

        translated_notes = handle_scale(scale, braille_code_list)


        if translated_notes is not None:

            print_braille_formatted(input_text, braille_code_list, translated_notes)


if __name__ == "__main__":

    main()