Last active
August 18, 2024 09:55
-
-
Save Ziaeemehr/96fa52c48e6747a4faef3972d83bacb2 to your computer and use it in GitHub Desktop.
Convert Kalimba notes CHAR<->NUM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to convert musical notes to numerical values | |
def convert_notes_to_numbers(notes): | |
''' | |
Convert characters to numbers, keeping the punctuation unchanged | |
Parameters | |
---------- | |
notes: str - the string of notes to convert | |
Returns | |
------- | |
str - the string of numbers | |
Examples | |
-------- | |
input_notes = """ABAB,GAGA,FGF,FGBA(2)""" | |
converted_notes = convert_notes_to_numbers(input_notes) | |
print(converted_notes) | |
# output: 6767,6767,545,5476(2) | |
''' | |
# Define the mapping for C, D, E, F, G, A, B | |
note_to_number = {'C': '1', 'D': '2', 'E': '3', 'F': '4', 'G': '5', 'A': '6', 'B': '7'} | |
# Convert the notes based on the mapping, keeping punctuation unchanged | |
converted_notes = [] | |
for char in notes: | |
if char in note_to_number: | |
converted_notes.append(note_to_number[char]) | |
else: | |
converted_notes.append(char) | |
# Join the converted notes into a single string | |
return ''.join(converted_notes) | |
def convert_numbers_to_notes(numbers:str) -> str: | |
''' | |
Convert numbers to characters, keeping the punctuation unchanged | |
Parameters | |
---------- | |
numbers: str - the string of numbers to convert | |
Returns | |
------- | |
str - the string of characters | |
Examples | |
-------- | |
6767,6767,545,5476(2) -> CDEF,CDEF,EFG,EFAB(2) | |
1'71',671'7,2'1'76(24) -> C'BC',ABC'B,D'C'BA(DF) | |
''' | |
# Define the mapping for numbers to notes (C, D, E, F, G, A, B) | |
number_to_note = {'1': 'C', '2': 'D', '3': 'E', '4': 'F', '5': 'G', '6': 'A', '7': 'B'} | |
converted_notes = [] | |
inside_parentheses = False # Flag to track if we're inside parentheses | |
parentheses_content = [] # Store digits inside parentheses | |
for char in numbers: | |
if char == '(': # Entering parentheses | |
inside_parentheses = True | |
converted_notes.append(char) # Keep the '(' | |
parentheses_content = [] # Reset content storage for parentheses | |
elif char == ')': # Exiting parentheses | |
if len(parentheses_content) == 1: | |
# If there's only one digit, keep it as is | |
converted_notes.append(parentheses_content[0]) | |
else: | |
# Convert multiple digits inside parentheses | |
for digit in parentheses_content: | |
if digit in number_to_note: | |
converted_notes.append(number_to_note[digit]) | |
else: | |
converted_notes.append(digit) # Keep non-digits unchanged | |
converted_notes.append(char) # Keep the ')' | |
inside_parentheses = False | |
elif inside_parentheses: | |
# Collect characters inside parentheses | |
parentheses_content.append(char) | |
elif char in number_to_note: | |
converted_notes.append(number_to_note[char]) # Convert the number to a note | |
else: | |
converted_notes.append(char) # Keep punctuation, commas, etc., unchanged | |
output_file_name = "Buye_Eidi_Farhad.txt" | |
# Multiline string input of notes (this can be modified with any input) | |
input_notes = """ | |
EA,AAA | |
BG,GGG | |
BA,AAA | |
BG,GGG | |
EF,FFF | |
AE,EEE | |
ABGA,BGA | |
ABG,FGA | |
ABAB,GAGA,FGF,FGBA(2) | |
C'BC',ABC'B,D'C'BA(2) | |
E'D'C',D'D'C'B,C'C'BA | |
E'D'C',D'D'C'B,C'C'BA,BBAG,FGBA | |
AABB,BBAG,AAGF,FGBA | |
C'BC',ABC'B,D'C'BA(2) | |
""" | |
# Convert the input notes to numerical notes | |
converted_notes = convert_notes_to_numbers(input_notes) | |
# Write the converted notes to a text file | |
with open(output_file_name, 'w') as file: | |
file.write(converted_notes) | |
# Print a success message with the file name | |
print(f"Converted notes have been saved to '{output_file_name}'.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment