Skip to content

Commit

Permalink
savvy
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellga committed Apr 3, 2024
1 parent f4b3a83 commit 25f0c43
Show file tree
Hide file tree
Showing 29 changed files with 339 additions and 0 deletions.
Binary file added r-harmonium/testfiles/gs-16b-1c-44100hz.aac
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-1c-44100hz.ac3
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-1c-44100hz.aiff
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-1c-44100hz.flac
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-1c-44100hz.m4a
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-1c-44100hz.mp3
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-1c-44100hz.ogg
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-1c-44100hz.wav
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-1c-44100hz.wma
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-1c-8000hz.amr
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-2c-44100hz.aac
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-2c-44100hz.ac3
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-2c-44100hz.aiff
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-2c-44100hz.flac
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-2c-44100hz.m4a
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-2c-44100hz.mp3
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-2c-44100hz.mp4
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-2c-44100hz.ogg
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-2c-44100hz.ts
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-2c-44100hz.wav
Binary file not shown.
Binary file added r-harmonium/testfiles/gs-16b-2c-44100hz.wma
Binary file not shown.
12 changes: 12 additions & 0 deletions r-harmonium/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/tests.html
# * https://testthat.r-lib.org/reference/test_package.html#special-files

library(testthat)
library(harmonium)

test_check("harmonium")
17 changes: 17 additions & 0 deletions r-harmonium/tests/testthat/test_db_to_amplitude.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test_that(
"db_to_amplitude works.",
{
check_db_to_amplitude = function(dtype, input, result) {
harray = HArray$new_from_values(input, dtype)
HAudioOp$db_to_amplitude(harray, 1, 1)
expect_true(all.equal(harray$collect(), result, tolerance = 1e-4))
}

input = matrix(c(1,2,3,4,5,6,7,8), 4, 2)
result = matrix(c(1.258925, 1.584893, 1.995262, 2.511886, 3.162278, 3.981072, 5.011872, 6.309574), 4, 2)
check_db_to_amplitude(HDataType$float32, input, result)
check_db_to_amplitude(HDataType$float64, input, result)
}
)


25 changes: 25 additions & 0 deletions r-harmonium/tests/testthat/test_harray.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
test_that(
"harray works",
{
check_harray = function(arr, dtype, dtype_result) {
harray = HArray$new_from_values(arr, dtype)

expect_true(harray$dtype() == dtype_result)
expect_equal(harray$len(), length(arr))
expect_equal(harray$shape(), c(ncol(arr), nrow(arr)))
expect_equal(harray$ndim(), length(dim(arr)))
expect_true(harray$eq(harray))
expect_false(harray$ne(harray))
expect_false(harray$is_shared())
expect_equal(harray$collect(), arr)
}

arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3, 4))
check_harray(arr, HDataType$float32, HDataType$float32)
check_harray(arr, HDataType$float64, HDataType$float64)

arr = array(c(1+2i,3+4i,5-6i,7+8i,9-10i,10+11i,11-12i,12+13i), c(2, 4))
check_harray(arr, HDataType$complex32, HDataType$complex32)
check_harray(arr, HDataType$complex64, HDataType$complex64)
}
)
52 changes: 52 additions & 0 deletions r-harmonium/tests/testthat/test_haudiosink.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
test_that(
"haudiosink works.",
{
harmonium_path = system.file(package = "harmonium")
filepath = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.wav")

# haudiosink from harray
haudiosink = HAudioSink$new()
expect_true(haudiosink$is_empty())
l = HFile$decode(filepath, dtype = HDataType$float32)
expect_silent(haudiosink$append_from_harray(l[[1]], l[[2]]))
expect_false(haudiosink$is_empty())

# haudiosink from file.
haudiosink = HAudioSink$new()
expect_true(haudiosink$is_empty())
expect_equal(haudiosink$len(), 0)
haudiosink$append_from_file(filepath)
expect_false(haudiosink$is_empty())
haudiosink$append_from_file(filepath)
expect_equal(haudiosink$len(), 2)
haudiosink$skip_one()
haudiosink$len() == 1
haudiosink$set_speed(2)
haudiosink$set_volume(2)
expect_equal(haudiosink$speed(), 2)
expect_equal(haudiosink$volume(), 2)
haudiosink$pause()
expect_true(haudiosink$is_paused())
haudiosink$play()
haudiosink$stop()
expect_false(haudiosink$is_paused())
haudiosink$append_from_file(filepath)
haudiosink$append_from_file(filepath)
haudiosink$play()
expect_equal(haudiosink$len(), 2)
haudiosink$clear()
expect_equal(haudiosink$len(), 0)
expect_true(haudiosink$is_paused())
haudiosink$append_from_file(filepath)
expect_equal(haudiosink$len(), 1)

# needed so no song is played when test is run.
rm(haudiosink)
gc()

# haudiosink audio_configs.
expect_true(class(HAudioSink$audio_supported_configs()) == "character")
expect_true(class(HAudioSink$audio_default_device()) == "character")
expect_true(class(HAudioSink$audio_output_devices()) == "character")
}
)
37 changes: 37 additions & 0 deletions r-harmonium/tests/testthat/test_hfile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
test_that(
"hfile works.",
{
harmonium_path = system.file(package = "harmonium")
filepath = file.path(harmonium_path, "testfiles", "gs-16b-2c-44100hz.flac")
expect_equal(HFile$params(filepath), c(44100.00000,698194.00000,2.00000,15.8320635))
expect_equal(HFile$verify(filepath), "passed")
expect_equal(HFile$metadata(filepath, HMetadataType$text), list(c(tag_key = "title", tag_std_key = "TrackTitle", tag_value = "Galway"
), c(tag_key = "artist", tag_std_key = "Artist", tag_value = "Kevin MacLeod"
), c(tag_key = "encoder", tag_std_key = "Encoder", tag_value = "Lavf56.40.101"
)))
expect_equal(HFile$metadata(filepath, HMetadataType$visual), list())

# wav file having "\0" character which is not supported by R.
filepath2 = file.path(harmonium_path, "testfiles", "gs-16b-1c-44100hz.wav")
expect_error(HFile$metadata(filepath2, HMetadataType$text))

# Decode and stream tests.
dtype = HDataType$float32
l = HFile$decode(filepath, dtype)
expect_equal(l[[1]]$shape(), c(2, 698194))
expect_equal(l[[2]], 44100L)

decoder_stream = HFile$decode_stream(filepath, 1000L, dtype)
harray = decoder_stream$stream()
expect_equal(harray$shape(), c(2, 1000))

for(i in 1:698) {
decoder_stream$stream()
}

null = decoder_stream$stream()
expect_null(null)


}
)
153 changes: 153 additions & 0 deletions r-harmonium/tests/testthat/test_hresampler.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
test_that(
"Resample works.",
{
# SincFixedIn. Test example from rubato repo.
arr = matrix(0, nrow = 512, ncol = 2)
harray = HArray$new_from_values(arr, dtype = HDataType$float64)
hparams = HSincInterpolationParams$new(256, 0.95, 256, "linear", "blackmanharris2")

res = HResampler$new_sinc(48000 / 44100, 2, hparams, 512L, HAudioOp$nchannels(harray), HResamplerType$sinc_fixed_in, HDataType$float32)
expect_error(res$process(harray))

res = HResampler$new_sinc(48000 / 44100, 2, hparams, 512L, HAudioOp$nchannels(harray), HResamplerType$sinc_fixed_in, HDataType$float64)
expect_true(res$res_type() == HResamplerType$sinc_fixed_in)
expect_true(res$dtype() == HDataType$float64)
expect_no_error(res$process(harray))

expect_no_error(res$set_resample_ratio(1, FALSE))
expect_no_error(res$set_resample_ratio(2, FALSE))
expect_no_error(res$set_resample_ratio(1, TRUE))
expect_no_error(res$set_resample_ratio(2, TRUE))
expect_no_error(res$set_resample_ratio_relative(0.5, FALSE))
expect_no_error(res$set_resample_ratio_relative(2, FALSE))
expect_no_error(res$set_resample_ratio_relative(0.5, TRUE))
expect_no_error(res$set_resample_ratio_relative(2, TRUE))

# SincFixedOut.
arr = matrix(0, nrow = 1024, ncol = 2)
harray = HArray$new_from_values(arr, dtype = HDataType$float64)
hparams = HSincInterpolationParams$new(256, 0.95, 256, "linear", "blackmanharris2")

res = HResampler$new_sinc(48000 / 44100, 2, hparams, 512L, HAudioOp$nchannels(harray), HResamplerType$sinc_fixed_out, HDataType$float32)
expect_error(res$process(haudio, sr_out = 48000))

res = HResampler$new_sinc(48000 / 44100, 2, hparams, 512L, HAudioOp$nchannels(harray), HResamplerType$sinc_fixed_out, HDataType$float64)
expect_true(res$res_type() == HResamplerType$sinc_fixed_out)
expect_true(res$dtype() == HDataType$float64)
expect_no_error(res$process(harray))

expect_no_error(res$set_resample_ratio(1, FALSE))
expect_no_error(res$set_resample_ratio(2, FALSE))
expect_no_error(res$set_resample_ratio(1, TRUE))
expect_no_error(res$set_resample_ratio(2, TRUE))
expect_no_error(res$set_resample_ratio_relative(0.5, FALSE))
expect_no_error(res$set_resample_ratio_relative(2, FALSE))
expect_no_error(res$set_resample_ratio_relative(0.5, TRUE))
expect_no_error(res$set_resample_ratio_relative(2, TRUE))

# FftFixedIn
arr = matrix(0, nrow = 1024, ncol = 2)
harray = HArray$new_from_values(arr, dtype = HDataType$float64)

res = HResampler$new_fft(44100L, 48000L, 1024L, 2L, 2L, HResamplerType$fft_fixed_in, HDataType$float32)
expect_error(res$process(haudio, sr_out = 48000))

res = HResampler$new_fft(44100L, 48000L, 1024L, 2L, 2L, HResamplerType$fft_fixed_in, HDataType$float64)
expect_true(res$res_type() == HResamplerType$fft_fixed_in)
expect_true(res$dtype() == HDataType$float64)
expect_no_error(res$process(harray))

expect_error(res$set_resample_ratio(1, FALSE))
expect_error(res$set_resample_ratio(2, FALSE))
expect_error(res$set_resample_ratio(1, TRUE))
expect_error(res$set_resample_ratio(2, TRUE))
expect_error(res$set_resample_ratio_relative(0.5, FALSE))
expect_error(res$set_resample_ratio_relative(2, FALSE))
expect_error(res$set_resample_ratio_relative(0.5, TRUE))
expect_error(res$set_resample_ratio_relative(2, TRUE))

# FftFixedInOut
arr = matrix(0, nrow = 1024, ncol = 2)
harray = HArray$new_from_values(arr, dtype = HDataType$float64)

res = HResampler$new_fft(44100L, 48000L, 512L, 2L, 2L, HResamplerType$fft_fixed_in_out, HDataType$float32)
expect_error(res$process(harray))

res = HResampler$new_fft(44100L, 48000L, 512L, 2L, 2L, HResamplerType$fft_fixed_in_out, HDataType$float64)
expect_true(res$res_type() == HResamplerType$fft_fixed_in_out)
expect_true(res$dtype() == HDataType$float64)
expect_no_error(res$process(harray))

expect_error(res$set_resample_ratio(1, FALSE))
expect_error(res$set_resample_ratio(2, FALSE))
expect_error(res$set_resample_ratio(1, TRUE))
expect_error(res$set_resample_ratio(2, TRUE))
expect_error(res$set_resample_ratio_relative(0.5, FALSE))
expect_error(res$set_resample_ratio_relative(2, FALSE))
expect_error(res$set_resample_ratio_relative(0.5, TRUE))
expect_error(res$set_resample_ratio_relative(2, TRUE))

# FftFixedOut
arr = matrix(0, nrow = 1024, ncol = 2)
harray = HArray$new_from_values(arr, dtype = HDataType$float64)

res = HResampler$new_fft(44100L, 48000L, 512L, 2L, 2L, HResamplerType$fft_fixed_out, HDataType$float32)
expect_error(res$process(harray))

res = HResampler$new_fft(44100L, 48000L, 512L, 2L, 2L, HResamplerType$fft_fixed_out, HDataType$float64)
expect_true(res$res_type() == HResamplerType$fft_fixed_out)
expect_true(res$dtype() == HDataType$float64)
expect_no_error(res$process(harray))

expect_error(res$set_resample_ratio(1, FALSE))
expect_error(res$set_resample_ratio(2, FALSE))
expect_error(res$set_resample_ratio(1, TRUE))
expect_error(res$set_resample_ratio(2, TRUE))
expect_error(res$set_resample_ratio_relative(0.5, FALSE))
expect_error(res$set_resample_ratio_relative(2, FALSE))
expect_error(res$set_resample_ratio_relative(0.5, TRUE))
expect_error(res$set_resample_ratio_relative(2, TRUE))

# FastFixedIn. Test example from rubato repo.
arr = matrix(0, nrow = 1024, ncol = 2)
harray = HArray$new_from_values(arr, dtype = HDataType$float64)

res = HResampler$new_fast(48000 / 44100, 2, HPolynomialDegree$linear, 512L, HAudioOp$nchannels(harray), HResamplerType$fast_fixed_in, HDataType$float32)
expect_error(res$process(harray))

res = HResampler$new_fast(48000 / 44100, 2, HPolynomialDegree$linear, 512L, HAudioOp$nchannels(harray), HResamplerType$fast_fixed_in, HDataType$float64)
expect_true(res$res_type() == HResamplerType$fast_fixed_in)
expect_true(res$dtype() == HDataType$float64)
expect_no_error(res$process(harray))

expect_no_error(res$set_resample_ratio(1, FALSE))
expect_no_error(res$set_resample_ratio(2, FALSE))
expect_no_error(res$set_resample_ratio(1, TRUE))
expect_no_error(res$set_resample_ratio(2, TRUE))
expect_no_error(res$set_resample_ratio_relative(0.5, FALSE))
expect_no_error(res$set_resample_ratio_relative(2, FALSE))
expect_no_error(res$set_resample_ratio_relative(0.5, TRUE))
expect_no_error(res$set_resample_ratio_relative(2, TRUE))

# FastFixedOut. Test example from rubato repo.
arr = matrix(0, nrow = 512, ncol = 2)
harray = HArray$new_from_values(arr, dtype = HDataType$float64)

res = HResampler$new_fast(48000 / 44100, 2, HPolynomialDegree$linear, 512L, HAudioOp$nchannels(harray), HResamplerType$fast_fixed_out, HDataType$float32)
expect_error(res$process(harray))

res = HResampler$new_fast(48000 / 44100, 2, HPolynomialDegree$linear, 512L, HAudioOp$nchannels(harray), HResamplerType$fast_fixed_out, HDataType$float64)
expect_true(res$res_type() == HResamplerType$fast_fixed_out)
expect_true(res$dtype() == HDataType$float64)
expect_no_error(res$process(harray))

expect_no_error(res$set_resample_ratio(1, FALSE))
expect_no_error(res$set_resample_ratio(2, FALSE))
expect_no_error(res$set_resample_ratio(1, TRUE))
expect_no_error(res$set_resample_ratio(2, TRUE))
expect_no_error(res$set_resample_ratio_relative(0.5, FALSE))
expect_no_error(res$set_resample_ratio_relative(2, FALSE))
expect_no_error(res$set_resample_ratio_relative(0.5, TRUE))
expect_no_error(res$set_resample_ratio_relative(2, TRUE))
}
)
25 changes: 25 additions & 0 deletions r-harmonium/tests/testthat/test_slice.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
test_that(
"slice works",
{
check_slice = function(arr, dtype, dtype_result) {
harray = HArray$new_from_values(arr, dtype)

slice = harray$slice(list(c(0L, 2L, 1L), c(0L, 2L, 1L)))
expect_equal(slice$shape(), c(2, 2))
expect_equal(harray$mem_adress(), slice$mem_adress())

# Out-of-bounds check.
expect_error(harray$slice(list(c(0L, 5L, 1L), c(0L, 2L, 1L))))
# Step = 0 check.
expect_error(harray$slice(list(c(0L, 2L, 0L), c(0L, 2L, 1L))))
}

arr = array(c(1,2,3,4,5,6,7,8,9,10,11,12), c(3, 4))
check_slice(arr, HDataType$float32, HDataType$float32)
check_slice(arr, HDataType$float64, HDataType$float64)

arr = array(c(1+2i,3+4i,5-6i,7+8i,9-10i,10+11i,11-12i,12+13i,14+15i,16+17i,18+19i, 20+21i), c(3, 4))
check_slice(arr, HDataType$complex32, HDataType$complex32)
check_slice(arr, HDataType$complex64, HDataType$complex64)
}
)
18 changes: 18 additions & 0 deletions r-harmonium/tests/testthat/test_to_mono.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test_that(
"as_mono works.",
{
# f32
values = c(1,2,3,4,5,6,7,8,9,10,11,12)
arr = matrix(values, ncol = 3)
harray = HArray$new_from_values(arr, HDataType$float32)
HAudioOp$to_mono(harray)
expect_equal(harray$collect(), as.array(rowMeans(arr)))

# f64
values = c(1,2,3,4,5,6,7,8,9,10,11,12)
arr = matrix(values, ncol = 3)
harray = HArray$new_from_values(arr, HDataType$float64)
HAudioOp$to_mono(harray)
expect_equal(harray$collect(), as.array(rowMeans(arr)))
}
)

0 comments on commit 25f0c43

Please sign in to comment.