There should be an easy way to play audio files in the browser, with the option of being ephemeral.
Currently the code to do this is:
import os
import socket
import threading
import IPython
import portpicker
from six.moves import SimpleHTTPServer
from six.moves import socketserver
from google.colab import output
class _V6Server(socketserver.TCPServer):
address_family = socket.AF_INET6
class _FileHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
"""SimpleHTTPRequestHandler with a couple tweaks."""
def translate_path(self, path):
# Client specifies absolute paths.
return path
def log_message(self, fmt, *args):
# Suppress logging since it's on the background. Any errors will be reported
# via the handler.
pass
def end_headers(self):
# Do not cache the response in the notebook, since it may be quite large.
self.send_header('x-colab-notebook-cache-control', 'no-cache')
SimpleHTTPServer.SimpleHTTPRequestHandler.end_headers(self)
def play_audio(filename):
"""Downloads the file to the user's local disk via a browser download action.
Args:
filename: Name of the file on disk to be downloaded.
"""
started = threading.Event()
port = portpicker.pick_unused_port()
def server_entry():
httpd = _V6Server(('::', port), _FileHandler)
started.set()
# Serve multiple requests, in case the audio is played more than once.
httpd.serve_forever()
thread = threading.Thread(target=server_entry)
thread.start()
started.wait()
output.eval_js("""
(()=> {
const audio = document.createElement('audio');
audio.controls = true;
audio.autoplay = true;
audio.src = `https://localhost:%(port)d%(path)s`;
document.body.appendChild(audio);
})()
"""% {'port': port, 'path': os.path.abspath(filename)})
print('playing')
play_audio('noise2.wav')
I followed the rich output example, and it works for me.
from IPython.display import Audio
Audio(url="http://www.nch.com.au/acm/8k16bitpcm.wav")
This one generates 2 sine waves.
import numpy as np
max_time = 3
f1 = 220.0
f2 = 224.0
rate = 8000.0
L = 3
times = np.linspace(0,L,rate*L)
signal = np.sin(2*np.pi*f1*times) + np.sin(2*np.pi*f2*times)
Audio(data=signal, rate=rate)
Hi,
I still struggle to play a midi file in a colaboratory notebook. All the examples i found so far are jupyter notebooks (with music21 for example) that require the installation of some additional programs like musescore etc. on the local machine to play the midi files. These notebooks did not work in the colaboratory environment.
Has anybody succeeded to play a midi-file in the colaboratory environment? It would be nice to see such a notebook (with all the required installation steps apt-get...).
I have tried and found 2 methods to play midi in Colab.
The simpler one is to convert midi to wav as in this gist.
Another one use midi.js to play midi file directly.
See my "unorganized" notebook. (a few failures with other methods)
https://colab.research.google.com/drive/1iliUIN1Jn8552XEiYBYEYimSm7yiOJ8x
This is great! With your code i was able to play a midi file (i.e. convert to wav and show it in the notebook) like this:
!apt install fluidsynth
!cp /usr/share/sounds/sf2/FluidR3_GM.sf2 ./font.sf2
!pip install midi2audio
from midi2audio import FluidSynth
from IPython.display import Audio
FluidSynth("font.sf2").midi_to_audio('test.midi', 'test.wav')
Audio("test.wav")
Thanks a lot!
How do we play sound using numpy data through python file on Notebook in google colab?
thanks this really helped :)
This really works. Thanks so much!
Most helpful comment
I followed the rich output example, and it works for me.
This one generates 2 sine waves.