When using the code below the sound plays:
import IPython.display as ipd
import numpy
sr = 22050 # sample rate
T = 0.5 # seconds
t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
x = 0.5*numpy.sin(2*numpy.pi*440*t) # pure sine wave at 440 Hz
ipd.Audio(x, rate=sr, autoplay=True) # load a NumPy array
But when I use it inside a function it stops working:
import IPython.display as ipd
import numpy
def SoundNotification():
sr = 22050 # sample rate
T = 0.5 # seconds
t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
x = 0.5*numpy.sin(2*numpy.pi*440*t) # pure sine wave at 440 Hz
ipd.Audio(x, rate=sr, autoplay=True) # load a NumPy array
SoundNotification()
I've tried to assign the audio to a variable and return it which works:
import IPython.display as ipd
import numpy
def SoundNotification():
sr = 22050 # sample rate
T = 0.5 # seconds
t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
x = 0.5*numpy.sin(2*numpy.pi*440*t) # pure sine wave at 440 Hz
sound = ipd.Audio(x, rate=sr, autoplay=True) # load a NumPy array
return sound
sound = SoundNotification()
sound
But I want to use the sound in a different function:
import IPython.display as ipd
import numpy
def SoundNotification():
sr = 22050 # sample rate
T = 0.5 # seconds
t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
x = 0.5*numpy.sin(2*numpy.pi*440*t) # pure sine wave at 440 Hz
sound = ipd.Audio(x, rate=sr, autoplay=True) # load a NumPy array
return sound
def WhereIWantToUseTheSound():
sound = SoundNotification()
sound
WhereIWantToUseTheSound()
How do I make this work and what causes this behavior? The kernel for the notebook is Python 3.
Edit: I want to play the sound in a scheduled event:
import IPython.display as ipd
import numpy
import sched, time
sound = []
def SoundNotification():
sr = 22050 # sample rate
T = 0.5 # seconds
t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
x = 0.5*numpy.sin(2*numpy.pi*440*t) # pure sine wave at 440 Hz
sound = ipd.Audio(x, rate=sr, autoplay=True) # load a NumPy array
return sound
def do_something(sc):
print("Doing stuff...")
# do your stuff
sound_ = SoundNotification()
s.enter(interval, 1, do_something, (sc,))
return sound_
s = sched.scheduler(time.time, time.sleep)
interval = int(input("Interval between captures in seconds: "))
s.enter(0, 1, do_something, (s,))
s.run()
I don't know how to return the sound and schedule the next event within the same function.
Bases: IPython.core.display.DisplayObject. Create an audio object. When this object is returned by an input cell or passed to the display function, it will result in Audio controls being displayed in the frontend (only works in the notebook). Parameters: data : numpy array, list, unicode, str or bytes.
To play video in jupyter notebook: I do: from IPython. display import * Audio("linktomp3. mp3", autoplay=True) This creates an audio player like feel and you can play the mp3.
Jupyter doesn't load or doesn't work in the browserTry in another browser (e.g. if you normally use Firefox, try with Chrome). This helps pin down where the problem is. Try disabling any browser extensions and/or any Jupyter extensions you have installed. Some internet security software can interfere with Jupyter.
I was having this same problem, the sound was played when I called:
from IPython.display import Audio
Audio('/path/beep.mp3', autoplay=True)
But it didn't work when it was inside a function. The problem is that the function call doesn't really play the sound, it's actually played by the resulting HTML that is returned to Jupyter output.
So to overcome this, you can force the function to render the HTML using display( ) function from IPython. This will work:
from IPython.display import Audio
from IPython.core.display import display
def beep():
display(Audio('/path/beep.mp3', autoplay=True))
beep();
2 Things:
sound
) global because you are
returning values in that object and it's not accessible from outside
of functionWhereIWantToUseTheSound()
you are not returning anythingCode:
import IPython.display as ipd
import numpy
sound = []
def SoundNotification():
global sound
sr = 22050 # sample rate
T = 0.5 # seconds
t = numpy.linspace(0, T, int(T*sr), endpoint=False) # time variable
x = 0.5*numpy.sin(2*numpy.pi*440*t) # pure sine wave at 440 Hz
sound = ipd.Audio(x, rate=sr, autoplay=True) # load a NumPy array
return sound
def WhereIWantToUseTheSound():
sound = SoundNotification()
return sound
WhereIWantToUseTheSound()
I would suggest to use another object/var(sound
) name in WhereIWantToUseTheSound()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With