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.

arr = matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), ncol = 2)
dtype = HDataType$Float64
harray = HArray$new_from_values(arr, dtype)
print(harray)

Or decode from an existing audio file.

harmonium_path = system.file(package = "harmonium")
filepath = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.flac")
dtype = HDataType$Float64
decoded_audio = HFile$decode(filepath, dtype)
harray = decoded_audio$harray()
sr = decoded_audio$sr() # sampling rate
print(harray)

Verify the dimensions.

harray$len()
HAudioOp$nchannels(harray)
HAudioOp$nframes(harray)

Verify the data type.

harray$dtype()

Convert to an R array.

harray$collect()

Convert to mono.

HAudioOp$to_mono(harray)
print(harray)

Resample the haudio.

arr = matrix(as.double(1:8192), ncol = 2)
dtype = HDataType$Float64
harray = HArray$new_from_values(arr, dtype)
sr_in = 48000L
sr_out = 44100L
chunk_size = 1024L
sub_chunks = 2L
nbr_channels = 2L
res_type = HResamplerType$FftFixedIn
dtype = HDataType$Float64
hresampler = HResampler$new_fft(sr_in, sr_out, chunk_size, sub_chunks, nbr_channels, res_type, dtype)
hresampler$process(harray)

Play the haudio.

harmonium_path = system.file(package = "harmonium")
filepath = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.flac")
dtype = HDataType$Float32
decoded_audio = HFile$decode(filepath, dtype)
harray = decoded_audio$harray()
sr = decoded_audio$sr() # sampling rate
haudiosink = HAudioSink$new()
haudiosink$append_from_harray(harray, sr)

Or play directly from the file.

harmonium_path = system.file(package = "harmonium")
filepath = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.flac")
haudiosink = HAudioSink$new()
haudiosink$append_from_file(filepath)

Get the number of audios to be played.

haudiosink$len()

Double the audio volume and the playback speed.

haudiosink$set_volume(2)
haudiosink$set_speed(2)

Pause the playback and confirms it is paused.

haudiosink$pause()
haudiosink$is_paused()

It is also possible to get metadata from an audio file.

harmonium_path = system.file(package = "harmonium")
filepath = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.flac")
metatadatype = HMetadataType$Text
HFile$metadata(filepath, metatadatype)

And to get some audio parameters directly from a file.

harmonium_path = system.file(package = "harmonium")
filepath = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.flac")
params = HFile$params(filepath)
sr = params[[1]]
nframes = params[[2]]
nchannels = params[[3]]
duration = params[[4]]