簡體   English   中英

如何從谷歌雲存儲桶中讀取音頻文件,並在datalab筆記本中使用ipd播放

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

我想在datalab筆記本中播放聲音文件,我從谷歌雲存儲桶中讀取。 這個怎么做?

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM