How to read audio file from google cloud storage bucket and play with ipd in a datalab notebook

旧巷老猫 提交于 2020-01-04 13:44:32

问题


I want to play a sound file in a datalab notebook which I read from a google cloud storage bucket. How to do this?


回答1:


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)


来源:https://stackoverflow.com/questions/51413209/how-to-read-audio-file-from-google-cloud-storage-bucket-and-play-with-ipd-in-a-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!