简体   繁体   English

在jupyter中播放音频,在for循环中

[英]Playing audio in jupyter, in a for loop

I have a ton of training data I need annotated, in order to do so I need to listen through a bunch of sound snippets and note what I hear.我有大量需要注释的训练数据,为此我需要通过一堆声音片段来聆听并记下我听到的内容。 I wrote a small script for this in a notebook.我在笔记本上为此写了一个小脚本。

My main issue is that IPython display dosent show in loops.我的主要问题是 IPython 在循环中显示剂量显示。 As an example:举个例子:

import numpy
import IPython.display as ipd

sr = 22050# sample rate
T = 2.0# seconds
t = numpy.linspace(0, T, int(T*sr), endpoint=False)# time variable
x = 0.5*numpy.sin(2*numpy.pi*440*t)
ipd.Audio(x, rate=sr)

will show up with an audio box, and I will be able to play the sine wave.将出现一个音频框,我将能够播放正弦波。

But trying to play anything in a for loop yields nothing (such as:)但是尝试在 for 循环中播放任何内容都不会产生任何结果(例如:)

for i in range(10000000):
    x = 0.5*numpy.sin(i*numpy.pi*440*t)
    ipd.Audio(x, rate=sr)

If anyone has a good solution for looping through (and listening) a bunch of audio files (one at a time, since I need to loop through potentially hundreds of thousands sound snippets), I would be very much appreciative!如果有人有一个很好的解决方案来循环(和收听)一堆音频文件(一次一个,因为我需要循环潜在数十万个声音片段),我将非常感激!

To display the audio files within the for loop, you need to use IPython.display.display with the Audio object like so: 要在for循环中显示音频文件,您需要将IPython.display.displayAudio对象一起使用,如下所示:

import numpy
import IPython.display as ipd


for i in range(10000000):
    x = 0.5*numpy.sin(i*numpy.pi*440*t)
    ipd.display(ipd.Audio(x, rate=sr))

my answer was deleted.我的回答被删除了。 but if you want a continuous loop you can use the method i described here https://stackoverflow.com/a/73425194/664456 which is Audio.autoplay_attr = lambda x: 'autoplay="autoplay" loop="loop"'但是如果你想要一个连续的循环,你可以使用我在这里描述的方法https://stackoverflow.com/a/73425194/664456这是Audio.autoplay_attr = lambda x: 'autoplay="autoplay" loop="loop"'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM