R/sonaes.R

Defines functions sonaes

Documented in sonaes

##' Create mappings and parameters for sonification objects
##' 
##' Creates a \code{sonaes} object, which is a list containing the desired
##' mappings of data columns or constants onto each sound parameter. This is
##' intended be added to a \code{sonify} object (or included in its
##' construction).
##' 
##' Setting sonic parameters to \code{data.frame} columns is the heart
##' of making the sonification. The sound parameters that can be the
##' arguments of \code{sonaes} depend on the kinds of layers present;
##' see \code{\link{sonify}} for a list of the possible kinds of
##' layers.
##'
##' @seealso \code{\link{sonify}}. Also, see \code{\link[ggplot2]{aes}}
##' from the \pkg{ggplot2} package, which inspired this function.
##' @examples
##' 
##' ## Maps Petal.Width onto tempo,
##' ## Sepal.Width onto pitch,
##' ## and sets scalings
##' ## (not terribly informative, since the order is
##' ## arbitrary)
##' x <- sonify(iris, sonaes(tempo=Petal.Width,
##'             pitch=Sepal.Width)) + shape_scatter()
##' summary(x)
##' \dontrun{x ## Quite long!}
##' 
##' ## Use a different tempo scaling than default
##' ## to hear more of the data faster
##' \dontrun{x + scale_tempo_continuous(c(300, 1000))}
##' 
##' ## Map Petal.Width onto 'time' instead
##' y <- x + sonaes(time=Petal.Width)
##' summary(y)
##' \dontrun{y}
##' 
##' ## Seems clipped, so increase duration
##' \dontrun{y + sonaes(dur=4)}
##' 
##' @export
##' @param \dots Sonic parameter mappings, specific to the shape being
##' rendered. All arguments must be named, with the argument names
##' sonic parameters and the values either numeric values (to set the
##' sonic parameter to that value as a constant) or a column of the
##' data.frame that is to be sonified (with or without quotes).
##' @return A \code{sonaes} object, used in or added to a
##' \code{sonify} object or \code{sonlayer}.
sonaes <- function(...) {
  ## 'sonaes' objects are lists and are used as the top-level
  ## 'mapping' slot of sonify and sonlayer objects
  ## This mouthful puts args in list safely without evaluating them,
  ## to allow for specifying data.frame names without quotes:
  out <- as.list(substitute(list(...)))[-1L]

  ## Deparse (convert to string) any unquoted string arguments, which R
  ## interpreted as mode "name" in above:
  out <- lapply(out, function(x) {
    if(is.name(x))
      x <- deparse(x)
    return(x)
  })
  
  if(length(out) > 0
     && is.null(names(out)) | any(names(out) == ""))
    stop("All arguments must be named.")

  .checkSoundParams(names(out))
  
  class(out) <- c("sonaes")
  out
}
statisfactions/playitbyr documentation built on Jan. 27, 2024, 1:33 p.m.