5

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. 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 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!

NicolaiF
  • 1,283
  • 1
  • 20
  • 44
  • 1
    try `IPython.display(ipd.Audio(x, rate=sr))` instead of just `ipd.Audio(x, rate=sr)` – Anwarvic Jan 29 '19 at 09:25
  • Possible duplicate of [More than one Audio object in a Jupyter (IPython) Notebook cell](https://stackoverflow.com/questions/33048353/more-than-one-audio-object-in-a-jupyter-ipython-notebook-cell) – Anwarvic Jan 29 '19 at 09:28
  • That throws a type error: TypeError: 'module' object is not callable – NicolaiF Jan 29 '19 at 09:28
  • Not entirely duplicate, I dont want more than one box popping up at a time, I will be looping through potentially hundreds of thousands audio snippets. One box at a time... – NicolaiF Jan 29 '19 at 09:30
  • 1
    SORRY!!! use `IPython.display.display(ipd.Audio(x, rate=sr))` – Anwarvic Jan 29 '19 at 09:31
  • That works - in the way that it will show all the boxes, I had a look at the other post before posting this. But this is not very useful, for when I need to run it through the real data – NicolaiF Jan 29 '19 at 09:32
  • I added the answer for other people :) – Anwarvic Jan 29 '19 at 09:37

2 Answers2

17

To display the audio files within the for loop, you need to use IPython.display.display with the Audio object like so:

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))
Anwarvic
  • 12,156
  • 4
  • 49
  • 69
  • Do you know of a way of destroying the display after consuming it (there is nothing in the documentation, https://ipython.org/ipython-doc/3/api/generated/IPython.display.html)? This will print an awful lot of displays and destroy my RAM... :D – NicolaiF Jan 29 '19 at 09:41
  • 3
    Try `ipd.set_matplotlib_close(close=True)` after the `ipd.display()` – Anwarvic Jan 29 '19 at 09:52
  • strangely, this displays 10000000 players in my jupyter – Dima Lituiev Aug 14 '20 at 04:20
  • Have you tried `ipd.set_matplotlib_close(close=True)` after the `ipd.display()`? – Anwarvic Aug 14 '20 at 12:34
1

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 a = Audio(...); a.autoplay_attr = lambda: 'autoplay="autoplay" loop="loop"'

o17t H1H' S'k
  • 2,541
  • 5
  • 31
  • 52