4

OSError Traceback (most recent call last)

<ipython-input-21-4159a88154c9> in <module>()
      7 response = google_images_download.googleimagesdownload()
      8 r = sr.Recognizer()
----> 9 with sr.Microphone() as source:
     10     print("Say something!")
     11     audio = r.listen(source)

/usr/local/lib/python3.6/dist-packages/speech_recognition/init.py in init(self, device_index, sample_rate, chunk_size)

 84 assert 0 <= device_index < count, "Device index out of range ({} devices available; device index should be between 0 and {} inclusive)".format(count, count - 1)
     85 if sample_rate is None:  # automatically set the sample rate to the hardware's default sample rate if not specified
---> 86 device_info = audio.get_device_inf  o_by_index(device_index) if device_index is not None else audio.get_default_input_device_info()
     87 assert isinstance(device_info.get("defaultSampleRate"), (float, int)) and device_info["defaultSampleRate"] > 0, "Invalid device info returned from PyAudio: {}".format(device_info)
     88 sample_rate = int(device_info["defaultSampleRate"])
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Sudhanshu Sharma
  • 43
  • 1
  • 1
  • 4
  • It looks like Google Colab cannot detect your microphone. `device_index` is set to nothing, try allowing your browser to detect and access your microphone. – Michael Heidelberg Apr 16 '19 at 13:24
  • Does this answer your question? ["OSError: No Default Input Device Available" on Google Colab](https://stackoverflow.com/questions/59013743/oserror-no-default-input-device-available-on-google-colab) – Gino Mempin Feb 17 '21 at 12:26

2 Answers2

1

Here is a simple snippet

from IPython.display import HTML, Audio
from google.colab.output import eval_js
from base64 import b64decode
import numpy as np
from scipy.io.wavfile import read as wav_read
import io
import ffmpeg

AUDIO_HTML = """
<script>
var my_div = document.createElement("DIV");
var my_p = document.createElement("P");
var my_btn = document.createElement("BUTTON");
var t = document.createTextNode("Press to start recording");

my_btn.appendChild(t);
//my_p.appendChild(my_btn);
my_div.appendChild(my_btn);
document.body.appendChild(my_div);

var base64data = 0;
var reader;
var recorder, gumStream;
var recordButton = my_btn;

var handleSuccess = function(stream) {
  gumStream = stream;
  var options = {
    //bitsPerSecond: 8000, //chrome seems to ignore, always 48k
    mimeType : 'audio/webm;codecs=opus'
    //mimeType : 'audio/webm;codecs=pcm'
  };            
  //recorder = new MediaRecorder(stream, options);
  recorder = new MediaRecorder(stream);
  recorder.ondataavailable = function(e) {            
    var url = URL.createObjectURL(e.data);
    var preview = document.createElement('audio');
    preview.controls = true;
    preview.src = url;
    document.body.appendChild(preview);

    reader = new FileReader();
    reader.readAsDataURL(e.data); 
    reader.onloadend = function() {
      base64data = reader.result;
      //console.log("Inside FileReader:" + base64data);
    }
  };
  recorder.start();
  };

recordButton.innerText = "Recording... press to stop";

navigator.mediaDevices.getUserMedia({audio: true}).then(handleSuccess);


function toggleRecording() {
  if (recorder && recorder.state == "recording") {
      recorder.stop();
      gumStream.getAudioTracks()[0].stop();
      recordButton.innerText = "Saving the recording... pls wait!"
  }
}

// https://stackoverflow.com/a/951057
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

var data = new Promise(resolve=>{
//recordButton.addEventListener("click", toggleRecording);
recordButton.onclick = ()=>{
toggleRecording()

sleep(2000).then(() => {
  // wait 2000ms for the data to be available...
  // ideally this should use something like await...
  //console.log("Inside data:" + base64data)
  resolve(base64data.toString())

});

}
});
      
</script>
"""

def get_audio():
  display(HTML(AUDIO_HTML))
  data = eval_js("data")
  binary = b64decode(data.split(',')[1])
  
  process = (ffmpeg
    .input('pipe:0')
    .output('pipe:1', format='wav')
    .run_async(pipe_stdin=True, pipe_stdout=True, pipe_stderr=True, quiet=True, overwrite_output=True)
  )
  output, err = process.communicate(input=binary)
  
  riff_chunk_size = len(output) - 8
  # Break up the chunk size into four bytes, held in b.
  q = riff_chunk_size
  b = []
  for i in range(4):
      q, r = divmod(q, 256)
      b.append(r)

  # Replace bytes 4:8 in proc.stdout with the actual size of the RIFF chunk.
  riff = output[:4] + bytes(b) + output[8:]

  sr, audio = wav_read(io.BytesIO(riff))

  return audio, sr

then run this

audio, sr = get_audio()

you might need to install this one

!pip install ffmpeg-python
Sadaf Shafi
  • 1,016
  • 11
  • 27
0

Here's an example that shows how to access a user's camera and microphone:

https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=2viqYx97hPMi

The snippet you linked above attempts to access a microphone in Python. That won't work because there's no microphone attached to the virtual machine which executes Python code in Colab.

Instead, you want to access the microphone of the computer running the web browser. Then, capture data there, and pass it back to the virtual machine for processing in Python.

That's what's shown in the snippet linked above.

Bob Smith
  • 36,107
  • 11
  • 98
  • 91
  • 3
    I can not find the snippet to enable or use microphone in the link you provided. @bob-smith – biligunb Sep 05 '19 at 01:45
  • I think tutorial is what you're looking for: https://ricardodeazambuja.com/deep_learning/2019/03/09/audio_and_video_google_colab/ – Eden Trainor Apr 19 '20 at 19:50