Octave 是 Linux 上功能近似 MATLAB 的數值計算環境,內建大量函式,足以勝任聲音擷取、錄製、播放與數位信號處理,廣泛應用於娛樂、研究、醫學等領域。本系列以 Ubuntu 上的 Octave 4.0.0 為例,先讀取音頻檔案,再自行合成信號並播放,從設計與音頻工程的角度理解整個流程。
也就是說,重點不在「怎麼安裝音頻軟體」,而在「它是怎麼運作的」。
透過 PPA 安裝 Octave:
sudo apt-add-repository ppa:octave/stable sudo apt-get update sudo apt-get install octave
點擊應用程式圖示啟動 Octave,並可透過下拉式按鈕切換工作路徑。後續所有指令都在 Octave 終端機視窗中執行。
用 audioinfo 取得檔案的屬性摘要:
>> info = audioinfo ('testing.ogg')
本文以 ogg 檔為例,可讀出取樣率、音頻類型(stereo / mono)、通道數等資訊。先將檔名指派給變數——注意,檔案必須位於 Octave 的工作路徑中。
>> file = 'yourfile.ogg' >> [M, fs] = audioread(file)
其中 M 是一欄或兩欄的矩陣(取決於通道數),fs 為取樣率。
更完整的呼叫形式有:
>> [y, fs] = audioread (filename, samples) >> [y, fs] = audioread (filename, datatype) >> [y, fs] = audioread (filename, samples, datatype)
samples 指定起始與結束影格,datatype 指定回傳的資料型別:
>> samples = [1, fs]; >> [y, fs] = audioread (filename, samples) >> [y, Fs] = audioread (filename, 'native')
當值為 native 時,資料型別依檔案內實際的儲存形式而定。
接著用一個餘弦信號合成 ogg 檔:取樣率 44100 Hz、長度 10 秒、頻率 440 Hz。
>> filename = 'cosine.ogg'; >> fs = 44100; >> t = 0:1/fs:10; >> w = 2*pi*440*t; >> signal = cos(w); >> audiowrite(filename, signal, fs);
工作路徑中便會出現 cosine.ogg。播放它會聽到 440 Hz 的音,正好是樂理上的 A 音。若要檢視寫入的數值,一樣用 audioread 讀回來即可。後續文章會示範如何讀取雙通道音頻。
Octave 內建一個預設播放器,可直接用來測試:
>> [y, fs] = audioread('yourfile.ogg');
>> player = audioplayer(y, fs, 8)
scalar structure containing the fields:
BitsPerSample = 8
CurrentSample = 0
DeviceID = -1
NumberOfChannels = 1
Running = off
SampleRate = 44100
TotalSamples = 236473
>> play(player);
下一篇會進入音頻處理的進階特性,並觸及一些科學與商業應用的實例。
作者:David Duarte 譯者:vim-kakali 校對:wxy