16
16
17
17
"""
18
18
19
- # Uncomment the following line to run in Google Colab
19
+ # Uncomment the line corresponding to your "runtime type" to run in Google Colab
20
20
21
21
# CPU:
22
22
# !pip install torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
23
23
24
24
# GPU:
25
25
# !pip install torch==1.7.0+cu101 torchvision==0.8.1+cu101 torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
26
26
27
- # For interactive demo at the end:
28
- # !pip install pydub
29
-
30
27
import torch
31
28
import torch .nn as nn
32
29
import torch .nn .functional as F
33
30
import torch .optim as optim
34
31
import torchaudio
32
+ import sys
35
33
36
34
import matplotlib .pyplot as plt
37
35
import IPython .display as ipd
@@ -482,13 +480,8 @@ def predict(tensor):
482
480
# will record one second of audio and try to classify it.
483
481
#
484
482
485
- from google .colab import output as colab_output
486
- from base64 import b64decode
487
- from io import BytesIO
488
- from pydub import AudioSegment
489
-
490
483
491
- RECORD = ( """
484
+ RECORD = """
492
485
const sleep = time => new Promise(resolve => setTimeout(resolve, time))
493
486
const b2text = blob => new Promise(resolve => {
494
487
const reader = new FileReader()
@@ -509,10 +502,16 @@ def predict(tensor):
509
502
}
510
503
recorder.stop()
511
504
})
512
- """ )
505
+ """
513
506
514
507
515
508
def record (seconds = 1 ):
509
+
510
+ from google .colab import output as colab_output
511
+ from base64 import b64decode
512
+ from io import BytesIO
513
+ from pydub import AudioSegment
514
+
516
515
display (ipd .Javascript (RECORD ))
517
516
print (f"Recording started for { seconds } seconds." )
518
517
s = colab_output .eval_js ("record(%d)" % (seconds * 1000 ))
@@ -525,6 +524,33 @@ def record(seconds=1):
525
524
return torchaudio .load (filename )
526
525
527
526
527
+ def record_noncolab (seconds = 1 ):
528
+
529
+ import sounddevice
530
+ import scipy .io .wavfile
531
+
532
+ sample_rate = 44100
533
+
534
+ print (f"Recording started for { seconds } seconds." )
535
+ myrecording = sounddevice .rec (
536
+ int (seconds * sample_rate ), samplerate = sample_rate , channels = 1
537
+ )
538
+ sounddevice .wait ()
539
+ print ("Recording ended." )
540
+
541
+ filename = "_audio.wav"
542
+ scipy .io .wavfile .write (filename , sample_rate , myrecording )
543
+ return torchaudio .load (filename )
544
+
545
+
546
+ # Detect whether notebook runs in google colab
547
+ if "google.colab" in sys .modules :
548
+ # !pip install pydub
549
+ record = record_colab
550
+ else :
551
+ record = record_noncolab
552
+
553
+
528
554
waveform , sample_rate = record ()
529
555
print (f"Predicted: { predict (waveform )} ." )
530
556
ipd .Audio (waveform .numpy (), rate = sample_rate )
0 commit comments