Skip to content

Commit

Permalink
adding chord player
Browse files Browse the repository at this point in the history
  • Loading branch information
znmeb committed Aug 17, 2023
1 parent d58d7b0 commit 9056596
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 56 deletions.
2 changes: 0 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@
*.json binary
*.xml binary
*.rproj text eol=lf
NAMESPACE text eol=lf
DESCRIPTION text eol=lf
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generated by roxygen2: do not edit by hand

export(chord_WAVs)
export(chord_music_object)
export(chord_plot)
export(chord_sawtooth_WAVs)
export(chord_synth)
export(cps_chord_table)
export(cps_scale_table)
Expand Down
72 changes: 63 additions & 9 deletions R/output_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -341,41 +341,70 @@ scale_multisample <- function(

}

#' @title Create Chord Sawtooth WAV Files
#' @name chord_sawtooth_WAVs
#' @description Creates a `gm` music object for a given chord and keyboard map
#' @export chord_sawtooth_WAVs
#' @title Create Chord WAV Files
#' @name chord_WAVs
#' @description Creates WAV files for inversions of a chord between
#' two notes
#' @export chord_WAVs
#' @param chord a numeric vector with the scale degrees for the chord
#' @param keyboard_map the keyboard map for the scale
#' @param lowest_note the lowest MIDI note number to use. Default is 48.
#' @param highest_note the highest MIDI note number to use. Default is 84.
#' @param lowest_note the lowest MIDI note number to use, default
#' is 48.
#' @param highest_note the highest MIDI note number to use, default
#' is 84.
#' @param output_directory character, default "~/Multisample". This will
#' be created if it does not exist.
#' @param duration_sec how long to hold each note, default = 1
#' @param signal the `seewave` signal type, default is "saw"
#' @param duration_sec how long to hold each note, default = 4
#' @param velocity MIDI velocity, default = 100, max is 127
#' @param sample_rate_hz sample rate in hz, default = 48000
#' @param bit_width bit width of samples, default = 24
#' @returns the full path to output_directory
#' @examples
#' \dontrun{
#' eikosany <- cps_scale_table()
#' eikosany_chords <- cps_chord_table(eikosany)
#' eikosany_map <- keyboard_map(eikosany)
#' chord_degrees <- eikosany_chords$degrees
#' for (i in length(chord_degrees)) {
#' chord <- as.numeric(unlist(strsplit(chord_degrees[i], ":")))
#' folder_name <-
#' paste0("~/eikosany-chords/chord-", gsub(":", "-", chord_degrees[i]))
#' print(paste0("generating WAVs in folder ", folder_name))
#' chord_WAVs(
#' chord,
#' keyboard_map = eikosany_map,
#' lowest_note = 40,
#' highest_note = 80,
#' output_directory = folder_name
#' )
#' }
#' }
#'
chord_sawtooth_WAVs <- function(
chord_WAVs <- function(
chord,
keyboard_map,
lowest_note = 48,
highest_note = 84,
output_directory = "~/Multisample",
signal = "saw",
duration_sec = 1,
velocity = 100,
sample_rate_hz = 48000,
bit_width = 24
) {

# create the directory
dir.create(output_directory, recursive = TRUE)

# get the frequencies for all the notes in the chord
chord_map <- keyboard_map[
degree %in% chord &
note_number >= lowest_note &
note_number <= highest_note
]
frequencies <- sort(chord_map$freq)
note_numbers <- sort(chord_map$note_number)

# generate the chords
chord_span <- length(chord)
Expand All @@ -386,7 +415,32 @@ chord_sawtooth_WAVs <- function(
end_index <- root_index + chord_span - 1
if (end_index > last_index) { break }
chord_frequencies <- frequencies[root_index:end_index]
print(chord_frequencies)
chord_label <- paste(
note_numbers[root_index:end_index],
collapse = "_"
)
wave_object <- chord_synth(
chord_frequencies,
signal = signal,
duration_sec = 4,
velocity = 100,
sample_rate_hz = 48000,
bit_width = 24
)

# make file name -
file_name <- sprintf(
"chord-%s-%s.wav",
signal,
chord_label
)
file_path <- paste0(output_directory, "/", file_name)

# and write it!
summary(wave_object)
print(file_path)
tuneR::writeWave(wave_object, file_path)

root_index <- root_index + 1
}

Expand Down
72 changes: 72 additions & 0 deletions man/chord_WAVs.Rd

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

44 changes: 0 additions & 44 deletions man/chord_sawtooth_WAVs.Rd

This file was deleted.

0 comments on commit 9056596

Please sign in to comment.