Reputation: 87
I am looking for a way to play any sound after finishing my code. I have tried many ways to do it.
For example:
os.system('say "your program has finished"')
sys.stdout.write('\a')
sys.stdout.flush()
winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
print ("\a")
But neither of them are working in conda (ipython notebook)
Do you know any way to do it in conda, any sound Ideal will be not using music file, using something from system sounds, but it doesn't matter. What does matter is fast execution.
I am using Python 3 on windows 10.
Thanks
Upvotes: 0
Views: 5843
Reputation:
There is a speech
library in conda which can solve your query as well as provide extra set of features:
First you need to install the package:
pip install -i https://pypi.anaconda.org/pypi/simple speech
After that simply try:
import speech
speech.say('Completed coding', 'es_ES')
On windows 10, it has a speech engine included.
Install the module win32com
, then you can use this code:
import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
speak.Speak("Finished coding")
For more information check out: https://anaconda.org/pypi/speech
Hope it helps....Happy coding
Upvotes: 3
Reputation: 287
In the same winsound module, there is a method called Beep(Hz, ms) which excecutes sinewaves when it is called in the frequency written in hertz, and for the amount of time mentioned in miliseconds
Your code can be like this:
os.system('say "your program has finished"')
sys.stdout.write('\a')
sys.stdout.flush()
winsound.Beep(440, 150) # A sine wave of 440Hz will be heard by your
# speakers for 150ms
print ("\a")
Upvotes: 0
Reputation: 489
Have you tried using IPython.display.Audio? This is the proper way of playing sounds inside an ipython notebook.
You can generate sounds from data using numpy, e.g
# Generate a sound
import numpy as np
framerate = 44100
t = np.linspace(0,5,framerate*5)
data = np.sin(2*np.pi*220*t) + np.sin(2*np.pi*224*t))
Audio(data,rate=framerate)
Upvotes: 1