0

I am learning python. I thought of a fun and do-able(for me as a noob) exercise to test myself with.

I want to write a script that asks the user for a string.. and then it translates the string into morse code and plays it. I just dont know how to access/play a sound file in Python... Doesnt really matter what type.. .wav .mp3 or whatever, just whatever works best with Python. It obviously will just be a short beep that will be played for a certain(very short) time length. I saw that people suggest using pygame.. is that the only option? For educational purposes I'd like to know how to do this with the standard library(if possible). If not a module is fine.

Or better yet, is there a way to create a beep sound with only python code? In other words no sound file needed at all. A function that says "make a sound at x frequency for x time"?

Spock
  • 1
  • 1
  • 1
    Look at [this](https://stackoverflow.com/questions/25462213/play-wav-file-python-3). – cs95 Jun 04 '17 at 19:53
  • What OS? Or do you want a platform-independent method? – anonymoose Jun 04 '17 at 19:56
  • 1
    You can generate your sounds directly, this is a pretty solid tutorial: https://zach.se/generate-audio-with-python/ . Just instead of writing to a file, write it to a `pyaudio` stream and you're all set. – zwer Jun 04 '17 at 19:58
  • @Shiva thank you for that. Pyaudio looks like an excellent and easy to use option! How about using python code to create a sound? – Spock Jun 04 '17 at 19:59
  • @user8109732 I believe the link zwer posted looks promising, if you want to create a sound file. However, directly emitting sound from code is not possible with the default package. – cs95 Jun 04 '17 at 20:05
  • @anonymoose Windows 10. Forgot to mention. – Spock Jun 04 '17 at 20:26
  • Thanks guys/gals! Very helpful responses. – Spock Jun 04 '17 at 20:28

1 Answers1

0

You could try the winsound library (built in).

By using winsound.PlaySound(filename,winsound.SND_FILENAME) you are able to play WAV audio files.

By using winsound.Beep(frequency(in Hz),duration(in miliseconds)) you should be able to produce a beeping sound.

There is a slight chance the Beep() function won't work, as is in my case, and i am currently looking into it.

Take a look at the docs here: https://docs.python.org/3.7/library/winsound.html

Akenaten
  • 91
  • 9