We can convert a number to audio based on provided audio file for that number.
speakers = {'1': 's01', '2': 's02', '3': 's03', '4': 's04', '5': 's05', '6': 's06', '7': 's07', '8': 's08',
'9': 's09', '10': 's10', '11': 's11', '12': 's12', '13': 's13', '14': 's14', '15': 's15', '16': 's16'}
numbers_dict = {'0': 'zero', '1': 'one', '2': 'two', '3': 'three',
'4': 'four', '5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine'}
# Input are strings
number_input = list(input("Enter number: "))
if any([i not in numbers_dict.keys() for i in number_input]):
print("Invalid Input")
else:
# Convert the input to number
converted = [numbers_dict[i] for i in number_input]
print("Convered to:", converted)
# We will allow the user to select which speaker they want to hear
s = input("Select Speaker (1 to 16): ")
if s not in speakers.keys():
print("Invalid Input")
else:
print("Speaker:", speakers[s])
Output:
Enter number: 1234567890
Convered to: ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'zero']
Select Speaker (1 to 16): 12
Speaker: s12
Enter number: 2465718901929
Convered to: ['two', 'four', 'six', 'five', 'seven', 'one', 'eight',
'nine', 'zero', 'one', 'nine', 'two', 'nine']
Select Speaker (1 to 16): 6
Speaker: s06
# Import this library at the beginning of the code
from pydub import AudioSegment
from pydub.playback import play
if any([i not in numbers_dict.keys() for i in number_input]):
print("Invalid Input")
else:
audio_number = []
# Convert the input to number
converted = [numbers_dict[i] for i in number_input]
print("Convered to:", converted)
# We will allow the user to select which speaker they want to hear
s = input("Select Speaker (1 to 16): ")
if s not in speakers.keys():
print("Invalid Input")
else:
print("Speaker:", speakers[s])
for k in converted:
sound = AudioSegment.from_file(f"Project_AudioNumber/digits/{speakers[s]}/{k}.wav",
format="wav")
audio_number.append(sound) # Append to list
# The sum() function will concatenate the sound files
# to create a full audio
play(sum(audio_number))
Output:
Enter number: 1234567890
Convered to: ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'zero']
Select Speaker (1 to 16): 16
Speaker: s16
Input #0, wav, from '/var/folders/gc/49grx5f90nv3f12yh4jhhf740000gn/T/tmpcuhzsfdj.wav':
Duration: 00:00:16.67, bitrate: 705 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 1 channels, s16, 705 kb/s
16.60 M-A: -0.000 fd= 0 aq= 0KB vq= 0KB sq= 0B
export_path = "audio.wav"
sum(audio_number).export("audio.wav", format="wav")
Sample audio file (input = 0123456789
, speaker = s016
):