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!
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))
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"'
Related
Okay, I think I have better way to ask this question.
Say I want to scale up my object by 10 in some axis with some time interval gap between each scaling up, so I think I should use this script...
import bpy
import time
obj = bpy.context.active_object
for i in range(10):
time.sleep(0.5)
obj.scale[0]+=0.1
but this is showing the result (scaled object) once the whole script has been executed, what should I do to work it like updating as script goes on.
Got one of the solutions, but this might not be the best one I guess.
Add bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1) after a transformation has been applied.
So updated code will look like:
import bpy
import time
obj = bpy.context.active_object
for i in range(10):
obj.scale[0]+=0.1
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
time.sleep(0.5)
Also officially blender does not consider this a good practice so this might not work later. https://www.blender.org/api/blender_python_api_2_76_2/info_gotcha.html#can-i-redraw-during-the-script
I was programming a little something reading a file and playing it back. I need it to use librosa, it it's impossible i might be able to fix it. the simpleaudio bit is easier to replace. This is my code:
from pathlib import Path
import librosa
import simpleaudio as sa
def play_audio(audio, sampling_rate):
print("PLAYING AUDIO")
wave_obj = sa.WaveObject(audio, sample_rate=sampling_rate)
play_obj = wave_obj.play()
play_obj.wait_done()
in_fpath = Path("trump.wav")
original_wav, sampling_rate = librosa.load(in_fpath)
play_audio(original_wav, int(sampling_rate))
And it loads the data and plays it back, but it's just that if I play trump.wav file in Windows on the music player, it sounds like it should. When I do it in Python this way, however, it becomes EXTREMELY noisy. You can still hear what he is saying, but barely. Where is the problem? Librosa or SimpleAudio?
I have a suspicion. librosa.load returns an array with float data but simpleaudio needs integer data. Please try to change the dtype of audio:
import numpy as np
# [...]
audio *= 32767 / np.max(np.abs(audio)) # re-scaling
audio = audio.astype(np.int16) # change data type
wave_obj = sa.WaveObject(audio, sample_rate=sampling_rate)
# [...]
Also see the documentation of simpleaudio:
https://simpleaudio.readthedocs.io/en/latest/tutorial.html#using-numpy
I figured out the problem, i just needed to change the WaveObject or librosa.load (don't remember which) with a byte_rate (something with byte_) to 4 instead of the default of 2.
My main aim is to read in around 16k images for a Data science project and I am barely able to perform that serially.
I have performed some parallelization in c++, but I am unfamiliar with using it in python. Essentially, all I need is to parallelize a for loop that calls a function that reads in the image using the matplotlib.image package and returns the image object. I then simply append that object to list. Here is the function,
def read_img(name):
try:
img = mpimg.imread(name)
return img
except:
return("Did not find image")
I ran my code for 100, 1000 and then 5000 images in one go to see if it can run at all, and it ran fine until I ran it for 5000 and my jupyter notebook just crashed. My system has 24gb ram and 12 cores so I def need to find a way to parallelize this.
I know there are 2 modules in python for parallelization, multiprocessing and joblib but I am not sure how to approach this problem which I know is very basic but any guidance would be much appreciated.
You can use the python ThreadPoolExecutor link
Here is the general program which is not perfect but if you fill this should work
# import or some variable from your code mpimg
def read_img(name):
try:
img = mpimg.imread(name)
return img
except:
return("Did not find image")
from concurrent.futures import ThreadPoolExecutor, as_completed
# suppose the files contains th 16k file names
files = ['f1.jpg', 'f2.jpg']
future_to_file = {}
images_read = []
with ThreadPoolExecutor(max_workers=4) as executor:
for file in files:
future = executor.submit(read_img, file)
future_to_file[future] = file
for future in as_completed(future_to_file):
file = future_to_file[future]
img_read = future.result()
if img_read != 'Did not find image':
images_read.append((file, img_read))
Using https://pypi.org/project/av/ trying to open file for infinite playback.
But the cycle ends with the last frame.
After searching and reading manuals, test code looks as follows:
(Note: these options are expected to be passed down to aiortc.contrib.media.MediaPlayer and work similarly):
import av
av.open(file="file.mp4", options={"fflags": "+genpts", "loop": "-1"})
for frame in media.decode():
print(frame)
Question: What should be the options (and if it is possible) to play file in infinite loop? (NOT just once)
According to the documentation, you create a MediaPlayer using the parameter loop=True
from aiortc.contrib.media import MediaPlayer
player = MediaPlayer(file="file.mp4", options={"fflags": "+genpts"}, loop=True)
You don't need to call PyAV yourself. It is done in MediaPlayer (see source code).
I want to do procedural sounds in Python, and instantly play them back rather than save them to a file. What should I be using for this? Can I just use built-in modules, or will I need anything extra?
I will probably want to change pitch, volume, things like that.
Using numpy along with scikits.audiolab should do the trick. audiolab has a play function which supports the ALSA and Core Audio backends.
Here's an example of how you might produce a simple sine wave using numpy:
from __future__ import division
import numpy as np
def testsignal(hz,amplitude = .5,seconds=5.,sr=44100.):
'''
Create a sine wave at hz for n seconds
'''
# cycles per sample
cps = hz / sr
# total samples
ts = seconds * sr
return amplitude * np.sin(np.arange(0,ts*cps,cps) * (2*np.pi))
To create five seconds of a sine wave at 440 hz and listen to it, you'd do:
>>> from scikits.audiolab import play
>>> samples = testsignal(440)
>>> play(samples)
Note that play is a blocking call. Control won't be returned to your code until the sound has completed playing.
Check out this Python wiki page. Particularly the "Music programming in Python" section.
Pyglet could be a good option as it embeds functions for audio procedural:
Pyglet/media_procedural