Skip to content

Commit

Permalink
Merge 495a2d7 into 1aeaa5d
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcharrison committed Mar 3, 2019
2 parents 1aeaa5d + 495a2d7 commit 2e937a6
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: hrep
Title: Harmony Representations
Version: 0.7.0
Version: 0.8.0
Authors@R: person("Peter", "Harrison", email = "pmc.harrison@gmail.com", role = c("aut", "cre"))
Description: This package provides methods for representing and manipulating chord sequences.
Depends: R (>= 3.4.0)
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ S3method(pi_chord_type,pi_chord)
S3method(pi_chord_type,smooth_spectrum)
S3method(pi_chord_type,sparse_spectrum)
S3method(pitch,sparse_pi_spectrum)
S3method(play_pluck,default)
S3method(plot,smooth_spectrum)
S3method(plot,sparse_spectrum)
S3method(plot,wave)
Expand All @@ -161,6 +162,11 @@ S3method(represent,default)
S3method(represent,list)
S3method(represent,vec)
S3method(sample_rate,numeric)
S3method(save_wav_pluck,coded_vec)
S3method(save_wav_pluck,default)
S3method(save_wav_pluck,fr_chord)
S3method(save_wav_pluck,pi_chord)
S3method(save_wav_pluck,vec)
S3method(sparse_fr_spectrum,default)
S3method(sparse_fr_spectrum,list)
S3method(sparse_fr_spectrum,pi_chord)
Expand Down Expand Up @@ -254,8 +260,10 @@ export(pi_chord)
export(pi_chord_type)
export(pi_to_pc)
export(pitch)
export(play_pluck)
export(represent)
export(sample_rate)
export(save_wav_pluck)
export(sparse_fr_spectrum)
export(sparse_pi_spectrum)
export(sum_amplitudes)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# hrep 0.8.0

- Added methods for synthesising audio.

# hrep 0.7.0

- Added wave representation.
Expand Down
28 changes: 28 additions & 0 deletions R/play-pluck.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#' Play sound (pluck)
#'
#' Plays a sound with a plucked timbre.
#'
#' The sound is synthesised using \code{\link{save_wav_pluck}}
#' and saved to a temporary file, which is then played from the R session.
#'
#' @note
#' The command-line sound-processing program sox
#' (\url{http://sox.sourceforge.net/})
#' must be installed and available on the command line,
#' making available the commands \code{sox} and \code{play}.
#'
#' @param x Object to play (see \code{\link{save_wav_pluck}} for valid options).
#' @param ... Further parameters to pass to \code{\link{save_wav_pluck}}.
#'
#' @export
play_pluck <- function(x, ...) {
UseMethod("play_pluck")
}

#' @export
play_pluck.default <- function(x, ...) {
file <- tempfile(fileext = ".wav")
save_wav_pluck(x, file = file, ...)
system(paste0("play ", shQuote(file)))
file.remove(file)
}
60 changes: 60 additions & 0 deletions R/save-wav-pluck.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#' Save wav file (pluck)
#'
#' Saves object to a wav file using the 'pluck' timbre from sox
#' (\url{http://sox.sourceforge.net/}).
#'
#' @note
#' The command-line sound-processing program sox
#' (\url{http://sox.sourceforge.net/})
#' must be installed and available on the command line
#' under the command \code{sox}.
#'
#' @param x Object to save; methods exist for individual chords
#' and for vectors of chords (see \code{\link{vec}}).
#' Chords are coerced to a \code{\link{pi_chord}} representation.
#' @param file (Character scalar) Output file.
#' @param chord_length (Numeric scalar) Length of each output chord, in seconds.
#' @param ... Parameters passed to methods.
#'
#' @export
save_wav_pluck <- function(x, file, chord_length = 1) {
UseMethod("save_wav_pluck")
}

#' @export
save_wav_pluck.default <- function(x, ...) {
save_wav_pluck(pi_chord(x), ...)
}

#' @export
save_wav_pluck.vec <- function(x, file, ...) {
files <- paste("chord-", seq_along(x), "-", sep = "") %>%
tempfile(fileext = ".wav")
for (i in seq_along(x)) save_wav_pluck(x[[i]], files[i], ...)
cmd <- c("sox", shQuote(files), shQuote(file)) %>% paste(collapse = " ")
system(cmd)
file.remove(files)
}

#' @export
save_wav_pluck.coded_vec <- function(x, file, ...) {
save_wav_pluck(decode(x), file, ...)
}

#' @export
save_wav_pluck.pi_chord <- function(x, ...) {
save_wav_pluck(fr_chord(x), ...)
}

#' @export
save_wav_pluck.fr_chord <- function(x, file, chord_length = 1) {
checkmate::qassert(file, "S1")
checkmate::qassert(chord_length, "N1(0,)")
len <- sprintf("%.10f", as.numeric(chord_length))
freq <- sprintf("%.10f", as.numeric(x))
tones <- purrr::map_chr(freq,
~ sprintf('"|sox -n -p synth %s pluck %s"', len, .)) %>%
paste(collapse = " ")
cmd <- sprintf('sox --norm=-3 -m %s %s', tones, file)
system(cmd)
}
26 changes: 26 additions & 0 deletions man/play_pluck.Rd

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

29 changes: 29 additions & 0 deletions man/save_wav_pluck.Rd

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

0 comments on commit 2e937a6

Please sign in to comment.