Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcharrison committed Sep 19, 2019
1 parent 0e5c385 commit 026bf9b
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 32 deletions.
6 changes: 3 additions & 3 deletions R/alphabet-size.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ alphabet_size <- function(type) {
}

pc_chord_alphabet_size <- function() {
length(pc_chord_alphabet$by_id)
length(hrep::pc_chord_alphabet$by_id)
}

pc_set_alphabet_size <- function() {
length(pc_set_alphabet$by_id)
length(hrep::pc_set_alphabet$by_id)
}

pc_chord_type_alphabet_size <- function() {
pc_chord_alphabet_size() / 12L
length(hrep::pc_chord_type_alphabet$by_id)
}
18 changes: 9 additions & 9 deletions R/coded-vec.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ num_elements.coded_vec <- function(x) length(x)
#'
#' Transforms a given object into an integer-based encoding.
#'
#' @param x Object to transform, as created by
#' @param x Object to encode, as created by
#' \code{\link{pc_set}}, \code{\link{pc_set_type}}, \code{\link{pc_chord}},
#' or \code{\link{pc_chord_type}}.
#'
Expand Down Expand Up @@ -103,23 +103,23 @@ encode <- function(x) {

#' @rdname encode
#' @export
encode_pc_set <- function(pc_set) {
as.integer(sum((2L ^ (11:0)) * (0:11 %in% pc_set)))
encode_pc_set <- function(x) {
as.integer(sum((2L ^ (11:0)) * (0:11 %in% x)))
}

# Order-insensitive, little error checking
#' @rdname encode
#' @export
encode_pc_chord_type <- function(pc_chord_type) {
if (length(pc_chord_type) == 0) stop("invalid pc_chord_type")
as.integer(1L + sum((2L ^ (10:0)) * (1:11 %in% pc_chord_type)))
encode_pc_chord_type <- function(x) {
if (length(x) == 0) stop("invalid pc_chord_type")
as.integer(1L + sum((2L ^ (10:0)) * (1:11 %in% x)))
}

#' @rdname encode
#' @export
encode_pc_chord <- function(pc_chord) {
bass <- pc_chord[1]
chord_type <- (pc_chord - bass) %% 12L
encode_pc_chord <- function(x) {
bass <- x[1]
chord_type <- (x - bass) %% 12L
as.integer(2048L * bass + encode_pc_chord_type(chord_type))
}

Expand Down
50 changes: 50 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,38 @@
#' and accessing the object within this environment with this name.
#' The result will be a coded vector (see \code{\link{coded_vec}})
#' of length 1.
#'
#' @md
#' @name pc_chord_alphabet
#' @docType data
#' @keywords data
NULL

#' Pitch-class chord-type alphabet
#'
#' This dataset defines an alphabet for the type "pc_chord_type".
#' It is a list with two components,
#' defining a forward and backward
#' mapping between integers and "pc_chord_type" objects:
#' * \code{by_id}:
#' A list object.
#' The ith element of this list contains the pitch-class chord type
#' corresponding to integer i.
#' * \code{by_chord}:
#' An environment object.
#' A pitch-class chord type can be mapped to an integer by
#' taking the pitch-class chord type object,
#' converting it to a string representation (with \code{as.character}),
#' and accessing the object within this environment with this name.
#' The result will be a coded vector (see \code{\link{coded_vec}})
#' of length 1.
#'
#' @md
#' @name pc_chord_type_alphabet
#' @docType data
#' @keywords data
NULL

#' Pitch-class set alphabet
#'
#' This dataset defines an alphabet for the type "pc_set".
Expand All @@ -46,6 +72,30 @@ NULL
#' @keywords data
NULL

#' Pitch-class set type alphabet
#'
#' This dataset defines an alphabet for the type "pc_set_type".
#' It is a list with two components,
#' defining a forward and backward
#' mapping between integers and "pc_set_type" objects:
#' * \code{by_id}:
#' A list object.
#' The ith element of this list contains the pitch-class set type
#' corresponding to integer i.
#' * \code{by_chord}:
#' An environment object.
#' A pitch-class set type can be mapped to an integer by
#' taking the pitch-class set type object,
#' converting it to a string representation (with \code{as.character}),
#' and accessing the object within this environment with this name.
#' The result will be a coded vector (see \code{\link{coded_vec}})
#' of length 1.
#' @md
#' @name pc_set_type_alphabet
#' @docType data
#' @keywords data
NULL

#' pc_chord to pc_set map
#'
#' This integer vector provides the mapping between
Expand Down
8 changes: 4 additions & 4 deletions R/list-chords.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ list_chords <- function(type) {
tryCatch(n <- alphabet_size(type),
error = function(e) stop("alphabet not defined for this type"))
if (type == "pc_chord") {
pc_chord_alphabet$by_id
hrep::pc_chord_alphabet$by_id
} else if (type == "pc_set") {
pc_set_alphabet$by_id
hrep::pc_set_alphabet$by_id
} else if (type == "pc_set_type") {
pc_set_type_alphabet$by_id
hrep::pc_set_type_alphabet$by_id
} else if (type == "pc_chord_type") {
pc_chord_type_alphabet$by_id
hrep::pc_chord_type_alphabet$by_id
} else {
seq_len(n) %>%
coded_vec(type) %>%
Expand Down
4 changes: 2 additions & 2 deletions R/pc-chord-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ pc_chord_type.sparse_spectrum <- function(x) {
encode.pc_chord_type <- function(x) {
checkmate::qassert(x, "X")
key <- as.character(x)
i <- as.integer(pc_chord_type_alphabet$by_chord[[key]])
i <- as.integer(hrep::pc_chord_type_alphabet$by_chord[[key]])
coded_vec(i, "pc_chord_type")
}

decode.coded_vec_pc_chord_type <- function(x) {
checkmate::qassert(x, "X")
pc_chord_type_alphabet$by_id[x]
hrep::pc_chord_type_alphabet$by_id[x]
}

#' @export
Expand Down
4 changes: 2 additions & 2 deletions R/pc-chord.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ is.pc_chord <- function(x) {
encode.pc_chord <- function(x) {
checkmate::qassert(x, "X")
key <- as.character(x)
pc_chord_alphabet$by_chord[[key]]
hrep::pc_chord_alphabet$by_chord[[key]]
}

decode.coded_vec_pc_chord <- function(x) {
checkmate::qassert(x, "X")
pc_chord_alphabet$by_id[x]
hrep::pc_chord_alphabet$by_id[x]
}

#' Edit bass pitch class
Expand Down
4 changes: 2 additions & 2 deletions R/pc-set-norm-form.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ transposition.pc_set_type <- function(x) attr(x, "transposition")
encode.pc_set_type <- function(x) {
checkmate::qassert(x, "X[0,11]")
key <- as.character(x)
pc_set_type_alphabet$by_chord[[key]]
hrep::pc_set_type_alphabet$by_chord[[key]]
}

decode.coded_vec_pc_set_type <- function(x) {
checkmate::qassert(x, "X[1,351]")
pc_set_type_alphabet$by_id[x]
hrep::pc_set_type_alphabet$by_id[x]
}
8 changes: 4 additions & 4 deletions R/pc-set.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,20 @@ as.character.pc_set <- function(x, ...) {
encode.pc_set <- function(x) {
checkmate::qassert(x, "X")
key <- as.character(x)
pc_set_alphabet$by_chord[[key]]
hrep::pc_set_alphabet$by_chord[[key]]
}

# Vectorised
decode.coded_vec_pc_set <- function(x) {
max_id <- length(pc_set_alphabet$by_id)
max_id <- length(hrep::pc_set_alphabet$by_id)
if (!is.numeric(x) ||
any(is.na(x) |
x < 1 |
x > max_id |
round(x) != x)) {
stop("All pc_set ids must be integers between 1 and ", max_id, ".")
}
pc_set_alphabet$by_id[x]
hrep::pc_set_alphabet$by_id[x]
}

#' Map pitch-class chords to pitch-class sets
Expand All @@ -130,5 +130,5 @@ decode.coded_vec_pc_set <- function(x) {
#' @return Numeric vector of pitch-class set codes.
#' @export
map_pc_chord_id_to_pc_set_id <- function(pc_chord_id) {
pc_chord_id_to_pc_set_id_map[as.integer(pc_chord_id)]
hrep::pc_chord_id_to_pc_set_id_map[as.integer(pc_chord_id)]
}
4 changes: 2 additions & 2 deletions R/precompute-alphabets.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ precompute_alphabet <- function(alphabet_size,
}

precompute_pc_set_type_alphabet <- function() {
by_id_chr <- pc_set_type_alphabet <- 1:4095 %>%
by_id_chr <- 1:4095 %>%
purrr::map(decode_pc_set) %>%
purrr::map(pc_set_type) %>%
purrr::map_chr(as.character) %>%
Expand All @@ -35,7 +35,7 @@ precompute_pc_set_type_alphabet <- function() {
}

precompute_pc_chord_id_to_pc_set_id_map <- function(pc_chord_alphabet) {
pc_chord_alphabet$by_id %>%
hrep::pc_chord_alphabet$by_id %>%
purrr::map(pc_set) %>%
purrr::map_int(encode_pc_set)
}
Expand Down
8 changes: 4 additions & 4 deletions man/encode.Rd

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

27 changes: 27 additions & 0 deletions man/pc_chord_type_alphabet.Rd

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

27 changes: 27 additions & 0 deletions man/pc_set_type_alphabet.Rd

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

0 comments on commit 026bf9b

Please sign in to comment.