I want to play a sound file in a datalab notebook which I read from a google cloud storage bucket. How to do this?
Asked
Active
Viewed 2,709 times
1 Answers
9
import numpy as np
import IPython.display as ipd
import librosa
import soundfile as sf
import io
from google.cloud import storage
BUCKET = 'some-bucket'
# Create a Cloud Storage client.
gcs = storage.Client()
# Get the bucket that the file will be uploaded to.
bucket = gcs.get_bucket(BUCKET)
# specify a filename
file_name = 'some_dir/some_audio.wav'
# read a blob
blob = bucket.blob(file_name)
file_as_string = blob.download_as_string()
# convert the string to bytes and then finally to audio samples as floats
# and the audio sample rate
data, sample_rate = sf.read(io.BytesIO(file_as_string))
left_channel = data[:,0] # I assume the left channel is column zero
# enable play button in datalab notebook
ipd.Audio(left_channel, rate=sample_rate)

netskink
- 4,033
- 2
- 34
- 46
-
Would you mind accepting your own answer? For future reference of the community, of course. I think that you have to wait 2 days to accept your own answer, if I'm not mistaken. – Mangu Jul 19 '18 at 08:14
-
1You are correct. I need to wait 2 days before I accept my own answer. It took me quite a while to do this, so I figured it would be good to post a solution in case someone had the same question. – netskink Jul 19 '18 at 12:57