Harmonium
Harmonium is an audio interface inspired by Python’s librosa.
Basic usage
Load the library.
library(harmonium)
Create an HArray
from an existing array.
= matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), ncol = 2)
arr = HDataType$Float64
dtype = HArray$new_from_values(arr, dtype)
harray print(harray)
Or decode from an existing audio file.
= system.file(package = "harmonium")
harmonium_path = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.flac")
filepath = HDataType$Float64
dtype = HFile$decode(filepath, dtype)
decoded_audio = decoded_audio$harray()
harray = decoded_audio$sr() # sampling rate
sr print(harray)
Verify the dimensions.
$len()
harray$nchannels(harray)
HAudioOp$nframes(harray) HAudioOp
Verify the data type.
$dtype() harray
Convert to an R array.
$collect() harray
Convert to mono.
$to_mono(harray)
HAudioOpprint(harray)
Resample the haudio.
= matrix(as.double(1:8192), ncol = 2)
arr = HDataType$Float64
dtype = HArray$new_from_values(arr, dtype)
harray = 48000L
sr_in = 44100L
sr_out = 1024L
chunk_size = 2L
sub_chunks = 2L
nbr_channels = HResamplerType$FftFixedIn
res_type = HDataType$Float64
dtype = HResampler$new_fft(sr_in, sr_out, chunk_size, sub_chunks, nbr_channels, res_type, dtype)
hresampler $process(harray) hresampler
Play the haudio.
= system.file(package = "harmonium")
harmonium_path = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.flac")
filepath = HDataType$Float32
dtype = HFile$decode(filepath, dtype)
decoded_audio = decoded_audio$harray()
harray = decoded_audio$sr() # sampling rate
sr = HAudioSink$new()
haudiosink $append_from_harray(harray, sr) haudiosink
Or play directly from the file.
= system.file(package = "harmonium")
harmonium_path = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.flac")
filepath = HAudioSink$new()
haudiosink $append_from_file(filepath) haudiosink
Get the number of audios to be played.
$len() haudiosink
Double the audio volume and the playback speed.
$set_volume(2)
haudiosink$set_speed(2) haudiosink
Pause the playback and confirms it is paused.
$pause()
haudiosink$is_paused() haudiosink
It is also possible to get metadata from an audio file.
= system.file(package = "harmonium")
harmonium_path = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.flac")
filepath = HMetadataType$Text
metatadatype $metadata(filepath, metatadatype) HFile
And to get some audio parameters directly from a file.
= system.file(package = "harmonium")
harmonium_path = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.flac")
filepath = HFile$params(filepath)
params = params[[1]]
sr = params[[2]]
nframes = params[[3]]
nchannels = params[[4]] duration