Last active
August 23, 2024 08:45
-
-
Save korakot/c21c3476c024ad6d56d5f48b0bca92be to your computer and use it in GitHub Desktop.
Record audio in Colab using getUserMedia({ audio: true })
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# all imports | |
from IPython.display import Javascript | |
from google.colab import output | |
from base64 import b64decode | |
from io import BytesIO | |
!pip -q install pydub | |
from pydub import AudioSegment | |
RECORD = """ | |
const sleep = time => new Promise(resolve => setTimeout(resolve, time)) | |
const b2text = blob => new Promise(resolve => { | |
const reader = new FileReader() | |
reader.onloadend = e => resolve(e.srcElement.result) | |
reader.readAsDataURL(blob) | |
}) | |
var record = time => new Promise(async resolve => { | |
stream = await navigator.mediaDevices.getUserMedia({ audio: true }) | |
recorder = new MediaRecorder(stream) | |
chunks = [] | |
recorder.ondataavailable = e => chunks.push(e.data) | |
recorder.start() | |
await sleep(time) | |
recorder.onstop = async ()=>{ | |
blob = new Blob(chunks) | |
text = await b2text(blob) | |
resolve(text) | |
} | |
recorder.stop() | |
}) | |
""" | |
def record(sec=3): | |
display(Javascript(RECORD)) | |
s = output.eval_js('record(%d)' % (sec*1000)) | |
b = b64decode(s.split(',')[1]) | |
audio = AudioSegment.from_file(BytesIO(b)) | |
return audio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# all imports | |
from IPython.display import Javascript | |
from google.colab import output | |
from base64 import b64decode | |
RECORD = """ | |
const sleep = time => new Promise(resolve => setTimeout(resolve, time)) | |
const b2text = blob => new Promise(resolve => { | |
const reader = new FileReader() | |
reader.onloadend = e => resolve(e.srcElement.result) | |
reader.readAsDataURL(blob) | |
}) | |
var record = time => new Promise(async resolve => { | |
stream = await navigator.mediaDevices.getUserMedia({ audio: true }) | |
recorder = new MediaRecorder(stream) | |
chunks = [] | |
recorder.ondataavailable = e => chunks.push(e.data) | |
recorder.start() | |
await sleep(time) | |
recorder.onstop = async ()=>{ | |
blob = new Blob(chunks) | |
text = await b2text(blob) | |
resolve(text) | |
} | |
recorder.stop() | |
}) | |
""" | |
def record(sec=3): | |
display(Javascript(RECORD)) | |
s = output.eval_js('record(%d)' % (sec*1000)) | |
b = b64decode(s.split(',')[1]) | |
with open('audio.wav','wb') as f: | |
f.write(b) | |
return 'audio.wav' # or webm ? |
# from https://gist.github.com/korakot/c21c3476c024ad6d56d5f48b0bca92be
from IPython.display import Javascript
from google.colab import output
from base64 import b64decode
# RECORD = """
# const sleep = time => new Promise(resolve => setTimeout(resolve, time))
# const b2text = blob => new Promise(resolve => {
# const reader = new FileReader()
# reader.onloadend = e => resolve(e.srcElement.result)
# reader.readAsDataURL(blob)
# })
# var record = time => new Promise(async resolve => {
# stream = await navigator.mediaDevices.getUserMedia({ audio: true })
# recorder = new MediaRecorder(stream)
# chunks = []
# recorder.ondataavailable = e => chunks.push(e.data)
# recorder.start()
# await sleep(time)
# recorder.onstop = async ()=>{
# blob = new Blob(chunks)
# text = await b2text(blob)
# resolve(text)
# }
# recorder.stop()
# })
# """
RECORD = """
const sleep = time => new Promise(resolve => {
setTimeout(resolve, time)
}, )
const b2text = blob => new Promise(resolve => {
const reader = new FileReader()
reader.onloadend = e => resolve(e.srcElement.result)
reader.readAsDataURL(blob)
})
var espacio = document.querySelector("#output-area")
var record = time => new Promise(async resolve => {
stream = await navigator.mediaDevices.getUserMedia({ audio: true })
recorder = new MediaRecorder(stream)
chunks = []
recorder.ondataavailable = e => chunks.push(e.data)
recorder.start()
var numerillo = (time/1000)-1
for (var i = 0; i < numerillo; i++) {
espacio.appendChild(document.createTextNode(numerillo-i))
await sleep(1000)
espacio.removeChild(espacio.lastChild)
}
recorder.onstop = async ()=>{
blob = new Blob(chunks)
text = await b2text(blob)
resolve(text)
}
recorder.stop()
})
"""
def record(sec, filename='audio.wav'):
display(Javascript(RECORD))
print("before s")
s = output.eval_js('record(%d)' % (sec))
print(s)
b = b64decode(s.split(',')[1])
with open(filename, 'wb+') as f:
f.write(b)
audio = 'audio.wav'
second = 5
print(f"Speak to your microphone {second} sec...")
record(1, audio)
print("Done!")
import librosa
import librosa.display
speech, rate = librosa.load(audio)
librosa.display.waveshow(speech, sr=rate)
import matplotlib.pyplot as plt
plt.show()
import pysndfile
pysndfile.sndio.write('audio_ds.wav', speech, rate=rate, format='wav', enc='pcm16')
from IPython.display import display, Audio
display(Audio(speech, rate=rate))
I am executing the above code. But the cell doesn't stop executing. And the output till now is as follows:
Speak to your microphone 5 sec...
before s
It doesn't change even if I use the commented section instead.
I am executing this in jupyter notebook locally. Is that the problem? or is it something else.
+1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is a basic example of how to continuously record audio until the user has finished speaking and then process that speech. You can customize and expand upon it for your specific use case or application.