Skip to content

Commit

Permalink
multiline on one line.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentqb committed Nov 17, 2020
1 parent b93e277 commit 7f098ab
Showing 1 changed file with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -481,39 +481,39 @@ def predict(tensor):
#


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_colab(seconds=1):

from google.colab import output as colab_output
from base64 import b64decode
from io import BytesIO
from pydub import AudioSegment

display(ipd.Javascript(RECORD))
RECORD = (
b"const sleep = time => new Promise(resolve => setTimeout(resolve, time))\n"
b"const b2text = blob => new Promise(resolve => {\n"
b" const reader = new FileReader()\n"
b" reader.onloadend = e => resolve(e.srcElement.result)\n"
b" reader.readAsDataURL(blob)\n"
b"})\n"
b"var record = time => new Promise(async resolve => {\n"
b" stream = await navigator.mediaDevices.getUserMedia({ audio: true })\n"
b" recorder = new MediaRecorder(stream)\n"
b" chunks = []\n"
b" recorder.ondataavailable = e => chunks.push(e.data)\n"
b" recorder.start()\n"
b" await sleep(time)\n"
b" recorder.onstop = async ()=>{\n"
b" blob = new Blob(chunks)\n"
b" text = await b2text(blob)\n"
b" resolve(text)\n"
b" }\n"
b" recorder.stop()\n"
b"})"
)
RECORD = RECORD.decode("ascii")

print(f"Recording started for {seconds} seconds.")
display(ipd.Javascript(RECORD))
s = colab_output.eval_js("record(%d)" % (seconds * 1000))
print("Recording ended.")
b = b64decode(s.split(",")[1])
Expand Down

0 comments on commit 7f098ab

Please sign in to comment.