Skip to content
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: hrep
Title: Harmony Representations
Version: 0.16.1
Version: 0.20.0
Authors@R: person("Peter", "Harrison", email = "pmc.harrison@gmail.com", role = c("aut", "cre"))
Description: This package provides utilities for representing and manipulating
chord sequences for perceptually informed harmony modelling.
Expand All @@ -18,7 +18,7 @@ Suggests:
testthat (>= 2.1.0),
covr (>= 3.2.0),
ggplot2 (>= 3.0.0)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Imports:
plyr (>= 1.8.4),
Rdpack (>= 0.9.0),
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# hrep 0.20.0

# hrep 0.19.0

# hrep 0.18.0

# hrep 0.17.0

# hrep 0.16.1

- Remove unnecessary info messages.
Expand Down
44 changes: 23 additions & 21 deletions R/expand-harmonics.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' Number of harmonics (including the fundamental) to which
#' each tone should be expanded.
#'
#' @param roll_off (Numeric scalar) Parametrises the amount of amplitude roll-off
#' @param roll_off_dB (Numeric scalar) Parametrises the amount of amplitude roll-off
#' in the harmonics, with greater values corresponding to higher roll-off.
#'
#' @param digits
Expand All @@ -22,30 +22,35 @@
#' @param label_harmonics
#' If TRUE, then the harmonics in the resulting spectrum are labelled with their harmonic numbers.
#'
#' @param pseudo_octave
#' The octave ratio for stretching and compressing harmonics, defaults to 2.0.
#'
#' @rdname expand_harmonics
#'
#' @inheritParams collapse_summing_amplitudes
#' @export
expand_harmonics <- function(x,
num_harmonics = 11L,
roll_off = 1,
roll_off_dB = 1,
digits = 6,
label_harmonics = FALSE,
coherent = FALSE) {
coherent = FALSE,
pseudo_octave = 2.0) {
UseMethod("expand_harmonics")
}

#' @rdname expand_harmonics
#' @export
expand_harmonics.sparse_fr_spectrum <- function(x,
num_harmonics = 11L,
roll_off = 1,
roll_off_dB = 1,
digits = 6,
label_harmonics = FALSE,
coherent = FALSE) {
coherent = FALSE,
pseudo_octave = 2.0) {
expand_harmonics(sparse_pi_spectrum(x),
num_harmonics = num_harmonics,
roll_off = roll_off,
roll_off_dB = roll_off_dB,
digits = digits,
label_harmonics = label_harmonics,
coherent = coherent) %>%
Expand All @@ -56,18 +61,20 @@ expand_harmonics.sparse_fr_spectrum <- function(x,
#' @export
expand_harmonics.sparse_pi_spectrum <- function(x,
num_harmonics = 11L,
roll_off = 1,
roll_off_dB = 1,
digits = 6,
label_harmonics = FALSE,
coherent = FALSE) {
template <- pi_harmonic_template(num_harmonics, roll_off)
coherent = FALSE,
pseudo_octave = 2.0) {
purrr::map2(pitch(x), amp(x),
function(pitch, amp) {
n <- seq_len(num_harmonics)
f0 <- midi_to_freq(pitch)
df <- data.frame(
x = pitch + template$interval,
y = amp * template$amplitude
x = freq_to_midi(f0 * pseudo_octave ^ log2(n)),
y = 1 * 10 ^ ( -roll_off_dB * log2(n) / 20)
)
if (label_harmonics) df$labels <- seq_along(template$interval)
if (label_harmonics) df$labels <- seq_along(df$x)
df
}) %>%
collapse_summing_amplitudes(digits = digits, coherent = coherent) %>%
Expand All @@ -78,19 +85,14 @@ expand_harmonics.sparse_pi_spectrum <- function(x,
#' @export
expand_harmonics.pi_chord <- function(x,
num_harmonics = 11L,
roll_off = 1,
roll_off_dB = 1,
digits = 6,
label_harmonics = FALSE,
coherent = FALSE) {
coherent = FALSE,
pseudo_octave = 2.0) {
sparse_pi_spectrum(x,
num_harmonics = num_harmonics,
roll_off = roll_off,
roll_off_dB = roll_off_dB,
digits = digits,
label_harmonics = label_harmonics)
}

pi_harmonic_template <- function(num_harmonics, roll_off, digits = 6) {
tibble::tibble(n = seq_len(num_harmonics),
interval = 12 * log(.data$n, base = 2),
amplitude = 1 / (.data$n ^ roll_off))
}
6 changes: 3 additions & 3 deletions R/smooth-pc-spectrum.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ smooth_pc_spectrum <- function(
...,
sigma = 6.83,
num_harmonics = 11L,
roll_off = 1,
roll_off_dB = 1,
coherent = FALSE
) {
UseMethod("smooth_pc_spectrum")
Expand All @@ -88,12 +88,12 @@ smooth_pc_spectrum.default <- function(
...,
sigma = 6.83,
num_harmonics = 11L,
roll_off = 1,
roll_off_dB = 1,
coherent = FALSE
) {
smooth_pc_spectrum(sparse_pc_spectrum(x,
num_harmonics = num_harmonics,
roll_off = roll_off,
roll_off_dB = roll_off_dB,
coherent = coherent),
sigma = sigma,
coherent = coherent,
Expand Down
4 changes: 2 additions & 2 deletions R/smooth-pi-spectrum.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ smooth_pi_spectrum <- function(x, sigma = 6.83, ...) {
smooth_pi_spectrum.default <- function(x,
sigma = 6.83,
num_harmonics = 11L,
roll_off = 1,
roll_off_dB = 1,
coherent = FALSE,
...) {
smooth_pi_spectrum(sparse_pi_spectrum(x,
num_harmonics = num_harmonics,
roll_off = roll_off,
roll_off_dB = roll_off_dB,
coherent = coherent,
...),
sigma = sigma,
Expand Down
16 changes: 13 additions & 3 deletions R/sparse-spectrum.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,24 @@ set_labels.sparse_spectrum <- function(x, labels) {
}

#' @export
plot.sparse_spectrum <- function(x, ggplot = FALSE, xlim = NULL, ...) {
plot.sparse_spectrum <- function(x, cochlea=FALSE, ggplot = FALSE, xlim = NULL,
trans='identity', ...) {
df <- as.data.frame(x)
if (cochlea) {
greenwood <- function(frequency) {
100 * (1 - (log10( frequency / 165.4 + 0.88 ) / 2.1))
}
df$x = greenwood(df$x)
x_label = 'Cochlea (%)'
} else {
x_label = x_lab(x)
}
if (ggplot) {
assert_installed("ggplot2")
ggplot2::ggplot(df, ggplot2::aes_string(x = "x", xend = "x",
y = 0, yend = "y")) +
ggplot2::geom_segment() +
ggplot2::scale_x_continuous(x_lab(x), limits = xlim) +
ggplot2::scale_x_continuous(x_label, limits = xlim, transform=trans) +
ggplot2::scale_y_continuous(y_lab(x))
} else {
n <- nrow(df)
Expand All @@ -95,7 +105,7 @@ plot.sparse_spectrum <- function(x, ggplot = FALSE, xlim = NULL, ...) {
df2$x[I + 1:3] <- df$x[i]
df2$y[I + 2L] <- df$y[i]
}
plot(df2$x, df2$y, xlab = x_lab(x), ylab = y_lab(x),
plot(df2$x, df2$y, xlab = x_label, ylab = y_lab(x),
type = "l", xlim = xlim, ...)
if (!is.null(df$labels)) {
for (i in seq_len(nrow(df))) {
Expand Down
24 changes: 15 additions & 9 deletions man/expand_harmonics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/play_wav.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/save_wav.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/smooth_pc_spectrum.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/smooth_pi_spectrum.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/sparse_fr_spectrum.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/sparse_pc_spectrum.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/sparse_pi_spectrum.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/wave.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading