R/notenames.R

Defines functions notenames

Documented in notenames

notenames <- function(notes, language = c("english", "german")){
    language <- match.arg(language)
    if(!is.numeric(notes))
        stop("notes must be integer values")
    rg <- range(notes)
    
    ## How is the note called?
    name <- switch(language,
        english = c(
            unlist(lapply(c(",,,", ",,", ",", ""),
                function(x) paste(c("C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"), x, sep=""))),
            unlist(lapply(c("", "'", "''", "'''", "''''", "'''''", "''''''"), 
                function(x) paste(c("c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b"), x, sep="")))),
        german = c(
            unlist(lapply(c(",,,", ",,", ",", ""),
                function(x) paste(c("C",  "Cis", "D", "Dis", "E", "F", "Fis", "G", "Gis", "A", "Ais", "H"), x, sep=""))),
            unlist(lapply(c("", "'", "''", "'''", "''''", "'''''", "''''''"), 
                function(x) paste(c("c", "cis", "d", "dis", "e", "f", "fis", "g", "gis", "a", "ais", "h"), x, sep="")))),
        stop("currently only notenames in english and german are implemented")
    )
    ## Now we know notes between -69 and 62 (more than the midi range):
    low <- -69 - rg[1]
    high <- rg[2] - 62

    ## cutting off below and above:
    if(low < 0) name <- name[(-1):low]
    if(high < 0) name <- name[(-length(name)-high-1):(-length(name))]
    ## adding unknown names:
    name <- c(if(low > 0) rep(" ", low), name, if(high > 0) rep(" ", high))
    
    ## now sorting the stuff according to input vector
    notes <- notes - min(notes) + 1
    return(name[notes])
}

Try the tuneR package in your browser

Any scripts or data that you put into this service are public.

tuneR documentation built on July 10, 2018, 1:02 a.m.