-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.