DEVJYOT SIDHU
DEVJYOT SIDHU

Reputation: 23

Is there any way to input microphone on Azure Notebooks/ Google Collab?

I've been trying to build a speech to text application using Python Notebooks on Google Collab and Azure Notebook. The problem I'm facing is that the cloud based VM does not have an audio port for using my microphone. Due to this, I'm even unable to install PyAudio Package. Any suggestions?

Upvotes: 1

Views: 470

Answers (1)

korakot
korakot

Reputation: 40818

Here is the code to record and save the file in Colab.

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'

Upvotes: 2

Related Questions