Showing posts with label Audio. Show all posts
Showing posts with label Audio. Show all posts

Friday, April 22, 2022

Basic Audio Manipulation With Torchaudio

Recently, I moved my audio processing toolkit from librosa (and others) to Torchaudio. This short writing documented the very basics of torchaudio for audio manipulation: read, resample, and write an audiofile.

Load audio file (read)

The process of loading (reading) an audio file is straightforward, just pass the audio path to `torchaudio.load`. We need to import the needed modules first. Most audio files can be loaded by torchaudio (WAV, OGG, MP3, etc.).
import torchaudio
import torchaudio.transforms as T
wav0, sr0 = torchaudio.load("old_file_48k.wav", normalize=True) 
where wav0 is the output tensor (array) and sr0 is the original sampling rate. Argument `normalize=True` is optional to normalize the waveform. Note that one of my colleagues (a student) found that using `librosa.util.normalize()` resulted in better normalization (peak to peak waveform is -1 to 1) than this torchaudio normalization.
 

Resample

Resample a sampling rate to another sampling rate is done by a Class; the output is a function. Hence, we need to pass the old tensor to the resampler function. Here is an example to convert 48k tensor to 16k tensor.
sr1 = 16000
resampler = T.Resample(sr0, sr1)
wav1 = resampler(wav0)

Save as a new audio file (write)


The process of saving files is also straightforward, just pass the file name, tensor, and sampling rate in order.
torchaudio.save('new_file_16k.wav', wav1, sr1)
Then the new audio file appeared in the current directory. Just set the path and file name if you want to save it in another directory.
 

Reference:

[1] https://pytorch.org/tutorials/beginner/audio_preprocessing_tutorial.html

Tuesday, February 28, 2017

Ubuntu audio: konfigurasi dasar untuk perekaman

Sistem operasi Ubuntu mendukung sistem tata suara bawaan dari laptop. By default, perangkat lunak preinstalled Ubuntu mampu menjalankan suara output dan input (recording). Output dari laptop speaker (stereo) dan recording (mono) dari built-in microphone. Secara umum sistem audio pada sistem operasi berbasis Linux terdiri atas beberapa komponen di bawah ini.
Layer-layer Audio pada OS berbasis Linux, Sumber: tuxradar.com [1]

Default sound setting

Setelah menginstall Ubuntu, beberapa setting, apps dan konfigurasi dasar telah tersedia. Di pojok kanan atas, akan ada icon speaker yang bila di-klik menampilkan menu volume speaker, volume microphone, default music apps (Rhythmbox), dan sound setting. Jika ada diantara menu tersebut tidak muncul, artinya ada masalah dalam sound computer/laptop anda. Masalah yang biasanya muncul adalah, internal microphone tidak dikenali, sehingga tidak ada menu volume microphone.
Menu sound pada Ubuntu

Jika menu microphone tidak tersedia, langkah paling awal yang bisa dilakukan adalah menginsall pavucontrol (PulseAudio Volume Control). Caranya,
$ sudo apt install pavucontrol
Pada jendela pavu, anda bisa mengkonfigurasi sound/hardware yang terkoneksi dengan laptop, minimal built-in hardware dari laptop. Untuk masalah microphone, cek konfigurasi, pilih analog stereo duplex. Artinya, laptop kita bisa menerima input (mic) dan mengeluarkan output (speaker). Jika hanya ingin memfungsikan output saja, pilih analog stereo ouput. Dalam hal ini, built-in mic tidak akan bekerja.

Thursday, October 08, 2015

Installing Octave 4.0 in Ubuntu 14.04

So I already have Octave 3.8.1 in my machine, Ubuntu 14.04, with signal, control and audio package. I want to upgrade it to the newest one, Octave 4.0.0 which has GUI as default and some improvements. How to install Octave 4.0 on Trusty? Here is how, the defaults didn't works for me (actually it works but has some errors).

Octave 4.0.0 on Ubuntu 14.04 Trusty with GUI as default

Step-by-step: