載入 wav 資料
若要用 Dask bags 處理任何非標準格式的資料,你通常需要自己撰寫不少函式。這個任務要分析音訊資料,因此你需要一個自訂函式來載入音檔。
有些錄音失敗, resulting 音訊是靜音。一般的音訊資料看起來像是波形,振幅會出現很大的正值與負值。因此,要檢查錄音是否為靜音,可以檢查整段音訊的振幅是否整體都非常小。
scipy.io.wavfile 模組已匯入為 wavfile,而 numpy 已匯入為 np。
本練習屬於課程
在 Python 中使用 Dask 進行平行程式設計
練習說明
- 在
load_wav()函式內,使用wavfile.read()載入音訊資料與取樣頻率。 - 在
load_wav()內,組出並回傳字典結果。 - 在
not_silent()函式內,回傳布林值:檢查輸入字典中的'audio'陣列,其平均絕對值是否大於 100;請使用numpy的abs()與mean()函式。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def load_wav(filename):
# Load in the audio data
sampling_freq, audio = ____
# Add the filename, audio data, and sampling frequency to the dictionary
data_dict = {
'filename': ____,
'audio': ____,
'sample_frequency': ____,
}
return data_dict
def not_silent(data_dict):
# Check if the audio data is silent
return ____