使用 IPython.display.audio 在函数内部使用时无法在 jupyter notebook 中播放音频

Using IPython.display.audio to play audio in jupyter notebook not working when used inside a function

当使用下面的代码播放声音时:

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

但是当我在一个函数中使用它时它停止工作:

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()

我试过将音频分配给一个变量,return 它起作用了:

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

但我想在不同的功能中使用声音:

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()

如何进行这项工作以及导致此行为的原因是什么? 笔记本的内核是Python 3.

编辑: 我想在预定事件中播放声音:

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()

我不知道如何return声音并在同一功能内安排下一个事件。

2 件事:

  1. 您必须将音频对象 (sound) 设为全局,因为您 返回该对象中的值并且无法从外部访问 功能
  2. WhereIWantToUseTheSound() 你没有返回任何东西

代码:

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()

我建议在 WhereIWantToUseTheSound()

中使用另一个 object/var(sound) 名称

我遇到了同样的问题,我打电话的时候有声音:

from IPython.display import Audio 
Audio('/path/beep.mp3', autoplay=True)

但是当它在函数内部时它不起作用。问题是函数调用并没有真正播放声音,它实际上是由返回到 Jupyter 输出的结果 HTML 播放的。

因此,为了克服这个问题,您可以使用 IPython 中的 display( ) 函数强制函数呈现 HTML。这将起作用:

from IPython.display import Audio 
from IPython.core.display import display
def beep():
    display(Audio('/path/beep.mp3', autoplay=True))
beep();

对我有用的:

%matplotlib inline

使用笔记本顶部的 %matplotlib 魔法函数强制内联绘图帮助我渲染绘图,即使绘图对象的 show() 函数或display() Audio 的函数未被调用。

需要注意的是在其他jupyter安装上不会出现这个问题