| 
 | 1 | +#  | 
 | 2 | +# Copyright (c) 2021 Arm Limited and Contributors. All rights reserved.  | 
 | 3 | +#  | 
 | 4 | +# SPDX-License-Identifier: Apache-2.0  | 
 | 5 | +#  | 
 | 6 | + | 
 | 7 | +from IPython import display   | 
 | 8 | +from google.colab import output  | 
 | 9 | +import base64  | 
 | 10 | +from datetime import datetime  | 
 | 11 | +from os import path  | 
 | 12 | + | 
 | 13 | +def record_wav_file(folder):  | 
 | 14 | +  def save_wav_file(folder, s):  | 
 | 15 | +    b = base64.b64decode(s.split(',')[1])  | 
 | 16 | +      | 
 | 17 | +    file_path = path.join(folder, datetime.now().strftime("%d-%m-%Y-%H-%M-%S.wav"))  | 
 | 18 | +      | 
 | 19 | +    print(f'Saving file: {file_path}')  | 
 | 20 | +      | 
 | 21 | +    with open(file_path, 'wb') as f:  | 
 | 22 | +      f.write(b)  | 
 | 23 | + | 
 | 24 | +  output.register_callback('notebook.save_wav_file', save_wav_file)  | 
 | 25 | + | 
 | 26 | +  display.display(display.Javascript("""  | 
 | 27 | +    const recorderJsScript = document.createElement("script");  | 
 | 28 | +    const audioInputSelect = document.createElement("select");  | 
 | 29 | +    const recordButton = document.createElement("button");  | 
 | 30 | +
  | 
 | 31 | +    recorderJsScript.src = "https://sandeepmistry.github.io/Recorderjs/dist/recorder.js";  | 
 | 32 | +    recorderJsScript.type = "text/javascript";  | 
 | 33 | +
  | 
 | 34 | +    recordButton.innerHTML = "⏺ Start Recording";  | 
 | 35 | +
  | 
 | 36 | +    document.body.append(recorderJsScript);  | 
 | 37 | +    document.querySelector("#output-area").appendChild(audioInputSelect);  | 
 | 38 | +    document.querySelector("#output-area").appendChild(recordButton);  | 
 | 39 | +
  | 
 | 40 | +    async function updateAudioInputSelect() {  | 
 | 41 | +      while (audioInputSelect.firstChild) {  | 
 | 42 | +        audioInputSelect.removeChild(audioInputSelect.firstChild);  | 
 | 43 | +      }  | 
 | 44 | +
  | 
 | 45 | +      const deviceInfos = await navigator.mediaDevices.enumerateDevices();  | 
 | 46 | +
  | 
 | 47 | +      for (let i = 0; i !== deviceInfos.length; ++i) {  | 
 | 48 | +        const deviceInfo = deviceInfos[i];  | 
 | 49 | +        const option = document.createElement("option");  | 
 | 50 | +          | 
 | 51 | +        option.value = deviceInfo.deviceId;  | 
 | 52 | +          | 
 | 53 | +        if (deviceInfo.kind === "audioinput") {  | 
 | 54 | +          option.text = deviceInfo.label || "Microphone " + (audioInputSelect.length + 1);  | 
 | 55 | +          option.selected = (option.text.indexOf("MicNode") !== -1);  | 
 | 56 | +          audioInputSelect.appendChild(option);  | 
 | 57 | +        }  | 
 | 58 | +      }  | 
 | 59 | +    }  | 
 | 60 | +
  | 
 | 61 | +    const blob2text = (blob) => new Promise(resolve => {  | 
 | 62 | +      const reader = new FileReader();  | 
 | 63 | +      reader.onloadend = e => resolve(e.srcElement.result);  | 
 | 64 | +      reader.readAsDataURL(blob)  | 
 | 65 | +    });  | 
 | 66 | +
  | 
 | 67 | +    let recorder = undefined;  | 
 | 68 | +    let stream = undefined;  | 
 | 69 | +
  | 
 | 70 | +    // https://www.html5rocks.com/en/tutorials/getusermedia/intro/  | 
 | 71 | +    recordButton.onclick = async () => {  | 
 | 72 | +      if (recordButton.innerHTML != "⏺ Start Recording") {  | 
 | 73 | +        recordButton.disabled = true;  | 
 | 74 | +        recorder.stop();  | 
 | 75 | +
  | 
 | 76 | +        recorder.exportWAV(async (blob) => {  | 
 | 77 | +          const text = await blob2text(blob);  | 
 | 78 | +
  | 
 | 79 | +          google.colab.kernel.invokeFunction('notebook.save_wav_file', ['###folder###', text], {});  | 
 | 80 | +            | 
 | 81 | +          recordButton.innerHTML = "⏺ Start Recording";  | 
 | 82 | +          recordButton.disabled = false;  | 
 | 83 | +
  | 
 | 84 | +          stream.getTracks().forEach(function(track) {  | 
 | 85 | +          if (track.readyState === 'live') {  | 
 | 86 | +              track.stop();  | 
 | 87 | +            }  | 
 | 88 | +          });  | 
 | 89 | +        });  | 
 | 90 | +
  | 
 | 91 | +        return;  | 
 | 92 | +      }  | 
 | 93 | +
  | 
 | 94 | +      const constraints = {  | 
 | 95 | +        audio: {  | 
 | 96 | +          deviceId: {  | 
 | 97 | +          },  | 
 | 98 | +          sampleRate: 16000  | 
 | 99 | +        },  | 
 | 100 | +        video: false  | 
 | 101 | +      };  | 
 | 102 | +
  | 
 | 103 | +      stream = await navigator.mediaDevices.getUserMedia(constraints);  | 
 | 104 | +
  | 
 | 105 | +      if (audioInputSelect.value === "") {  | 
 | 106 | +        await updateAudioInputSelect();  | 
 | 107 | +
  | 
 | 108 | +        stream.getTracks().forEach(function(track) {  | 
 | 109 | +          if (track.readyState === 'live') {  | 
 | 110 | +            track.stop();  | 
 | 111 | +          }  | 
 | 112 | +        });  | 
 | 113 | +
  | 
 | 114 | +        constraints.audio.deviceId.exact = audioInputSelect.value;  | 
 | 115 | +        stream = await navigator.mediaDevices.getUserMedia(constraints);  | 
 | 116 | +      }  | 
 | 117 | +
  | 
 | 118 | +      const audioContext = new AudioContext({  | 
 | 119 | +        sampleRate: 16000  | 
 | 120 | +      });  | 
 | 121 | +        | 
 | 122 | +      const input = audioContext.createMediaStreamSource(stream);  | 
 | 123 | +
  | 
 | 124 | +      recorder = new Recorder(input, {  | 
 | 125 | +         numChannels: 1  | 
 | 126 | +      });  | 
 | 127 | +
  | 
 | 128 | +      recordButton.innerHTML = "⏹ Stop Recording";  | 
 | 129 | +
  | 
 | 130 | +      recorder.record();  | 
 | 131 | +    };  | 
 | 132 | +
  | 
 | 133 | +    updateAudioInputSelect();  | 
 | 134 | +  """.replace('###folder###', folder)))  | 
0 commit comments