-1

I have been trying to get my python code to store sounds based on inputs. i am struggling to develop the code and all resources I've found online haven't been helpful. Can anyone give me some suggesttions?

I am trying to create a program that stores a users name and asks them to say it and the sound is stored for each particular name. It can also be played back as well.

  • Pygame can store audio and play it later as long as you dont have to generate it. Not sure about recording however. – Mercury Platinum Apr 23 '18 at 16:10
  • Too broad of a question for Stackoverflow. Please check [asking](https://stackoverflow.com/help/asking) – Morse Apr 23 '18 at 16:11
  • I think this should help you out: https://stackoverflow.com/questions/892199/detect-record-audio-in-python – wpercy Apr 23 '18 at 16:11
  • This is a very vague question. Python does not convert sound to data. Presumably, you have something else that is producing sound files that you want Python to manipulate. A discussion of what that something else is and what you're trying to do would be very helpful. – Acccumulation Apr 23 '18 at 16:12
  • Im sorry if the question is vague but what im trying to do is the following: I am trying to create a program that helps with the pronounciation of names. But I know it will be difficult to actually get python to pronounce Scandanvian names for example correctly so instead im trying to store a specific recording with a specific name and i want to be able to replay those inputs. Im not trying to convert the sound to data. – Tlhogi Dube Apr 23 '18 at 19:13

1 Answers1

0

So from what I understand, you want to save audio file for each names and then ask user for his/her name. If name matches, then you want to play file containing his/her name. If above understanding is correct, you can achieve that in several ways.

Easiest way will be to store names you desire in some audio format (eg. wav). Here I've got audio pronunciation of few US cities from web. Next you store these files in a folder.

For demo purpose I've code and audio files of names in same folder.

Next using python we create a dictionary with key as names and its corresponding audio files as values.

You can make it fancy by reading through a folder and generating dictionary on the fly with file name as key (an excersize for you).

Since you've used Directsound tag, I am assuming you are using windows. You can use winsound library(which I believe is standard library)

Then you can ask user for his/her name. If name matches with key value in dictionary , you can play the name, else play error file.

Working Code

import winsound

names = {'NewYork' : 'NewYork.wav',
         'LosAngeles' : 'LosAngeles.wav',
         'Denver' : 'Denver.wav',
         'Dallas' : 'Dallas.wav'         
         }

def error():
    print("{} not in database".format(name))
    winsound.Beep(2500, 1000) #freq, duration in ms

name = raw_input("enter name:")
if name in names:
    winsound.PlaySound(names.get(name), winsound.SND_FILENAME)
else:
    error()
Anil_M
  • 10,893
  • 6
  • 47
  • 74
  • If my answer solves ur question u may want to accept it to close the loop – Anil_M Jul 16 '18 at 03:08
  • I just had one more question how would do the same thing on a Mac because on them you wouldnt have access to winsound? – Tlhogi Dube Jul 17 '18 at 00:24
  • You can try installing external libraries such as `pydub` or `pygame`. See here for one example: https://stackoverflow.com/questions/41517432/python-winsound-equivalent-for-mac .If you want specific answer for your case, you may want to ask another question for mac. – Anil_M Jul 17 '18 at 15:15