MidiFramer | R Documentation |
The class MidiFramer
can be used to read midi files to
dataframes in order to facilitate to manipulate the data from R. You can also
create midi data from R without reading it from a file.
The data is transformed to various formats.
One of the MidiFramer
fields is a
MidiFrames
object of the python miditapyr package. Its method
write_file()
can be used to write the data back to a midi file.
See the vignette("pyramidi")
for a brief usage introduction how to manipulate midi data.
The vignette("compose")
shows a more extended example how to generate midi files from scratch.
vignette("package_workflow")
shows in detail the structure of the MidiFramer
class.
vignette("functions_usage")
illustrates the low-level functions of the pyramidi package.
that MidiFramer
objects use under the hood.
midi_file_string
Path to the midi file.
mf
miditapyr$MidiFrames
object.
dfm
result of tab_measures()
.
df_notes_long
Result of pivot_long_notes()
.
df_meta, df_not_notes, df_notes_wide
Results of split_midi_frame()
.
midi_frame_mod
Result of merge_midi_frames()
.
params
Parameters used in internal functions; Named list; params$columns_to_add is passed to tab_measures(columns_to_add)
.
df_meta, df_not_notes, df_notes_wide
Results of split_midi_frame()
.
ticks_per_beat
Set ticks per beat of MidiFrames()$mf$midi_file
.
The value of ticks_per_beat
passed should be integer.
When a value is passed, the field mf$midi_file$ticks_per_beat
is modified.
new()
Initialize a MidiFramer object
MidiFramer$new(midi_file_string = NULL)
midi_file_string
Path to the midi file; if NULL (the default), an empty MidiFramer
object is created.
update_notes_wide()
Update a MidiFramer object with modified notes
MidiFramer$update_notes_wide(mod)
mod
Dataframe or function returning a dataframe of the format of df_notes_wide
.
\dontrun{ midi_file_string <- system.file("extdata", "test_midi_file.mid", package = "pyramidi") mfr <- MidiFramer$new(midi_file_string) # Function to replace every note with a random midi note between 60 & 71: mod <- function(dfn) { n_notes <- sum(!is.na(dfn$note)) dfn %>% dplyr::mutate(note = ifelse( !is.na(note), sample(60:71, n_notes, TRUE), note )) } set.seed(123) mfr$update_notes_wide(mod) mfr$play() # You can pass functions to the $update_notes_wide() method (as above), but # you can also modify the dataframe directly and pass it. Therefore, the # following results in the same: set.seed(123) df_mod <- mod(mfr$df_notes_wide) mfr$update_notes_wide(df_mod) mfr$play() }
populate_r_fields()
Populate the fields of a MidiFramer object
This can also be used to recalculate all the object's attributes, when a value in params is changed (see examples).
MidiFramer$populate_r_fields()
\dontrun{ midi_file_string <- system.file("extdata", "test_midi_file.mid", package = "pyramidi") mfr <- MidiFramer$new(midi_file_string) mfr$params$columns_to_add <- c("m", "b", "t", "time") mfr$populate_r_fields() }
play()
Play midi from MidiFramer object.
Writes a midi file and either playing it in the R console (live = TRUE
),
or otherwise (live = FALSE
) writes an audio file
and adding an html audio player in an Rmarkdown (/quarto?) document.
Calls player()
helper function.
WARNING: Setting overwrite = TRUE
(the default!!) will DELETE the specified audio files!!!
(see more details below)
MidiFramer$play( audiofile = tempfile("mf_out_", fileext = ".mp3"), soundfont = fluidsynth::soundfont_path(), midifile = gsub("\\....$", ".mid", audiofile), live = interactive(), verbose = FALSE, overwrite = TRUE, ... )
audiofile
Path to the audiofile to be synthesized. If audiofile of type mp3, it will first be synthesized to wav, and then converted to mp3 with ffmpeg; (character string).
soundfont
path to sf2 sound font (character string); if not specified,
the default soundfont of the fluidsynth package (fluidsynth::soundfont_path()
) will be (downloaded if not present and) used.
midifile
Path to the midi file to synthesize on; (character string).
live
logical; if TRUE
the synthesized midi is directly played in
the console. If FALSE
an audio html tag is written. This will generate
a small audio player when knitting an Rmd document
(and probably also Quarto qmd files; I didn't check).
verbose
logical whether to print command line output; defaults to FALSE
overwrite
logical; defaults to TRUE; if file exists and overwrite = FALSE, the existing files will not be overwritten and the function errors out.
...
Arguments passed to the fluidsynth functions
(fluidsynth::midi_play
or fluidsynth::midi_convert
depending on the value of live
).
@seealso
player
midi_file_string <- system.file("extdata", "test_midi_file.mid", package = "pyramidi") mfr <- MidiFramer$new(midi_file_string) mfr$play() # The play method does basically this: \dontrun{ midi_out <- "my_output.mid" mp3file <- "test.mp3" mfr$mf$write_file(midi_out) fluidsynth::midi_convert(midi_out, output = mp3file) # `overwrite` = TRUE overwrites midi_out & mp3file }
clone()
The objects of this class are cloneable with this method.
MidiFramer$clone(deep = FALSE)
deep
Whether to make a deep clone.
## Not run:
## Create a MidiFramer object from a midi file:
midi_file_string <- system.file("extdata", "test_midi_file.mid", package = "pyramidi")
MidiFramer$new(midi_file_string)
## ------------------------------------------------
## Create empty MidiFramer object to illustrate
## the use of the `ticks_per_beat` active binding:
## ------------------------------------------------
mfr <- MidiFramer$new()
# Print default value of empty MidiFile object:
mfr$mf$midi_file$ticks_per_beat
# Modify it with the active binding ticks_per_beat:
mfr$ticks_per_beat <- 960L
# Print it again:
mfr$mf$midi_file$ticks_per_beat
## End(Not run)
## ------------------------------------------------
## Method `MidiFramer$update_notes_wide`
## ------------------------------------------------
## Not run:
midi_file_string <- system.file("extdata", "test_midi_file.mid", package = "pyramidi")
mfr <- MidiFramer$new(midi_file_string)
# Function to replace every note with a random midi note between 60 & 71:
mod <- function(dfn) {
n_notes <- sum(!is.na(dfn$note))
dfn %>% dplyr::mutate(note = ifelse(
!is.na(note),
sample(60:71, n_notes, TRUE),
note
))
}
set.seed(123)
mfr$update_notes_wide(mod)
mfr$play()
# You can pass functions to the $update_notes_wide() method (as above), but
# you can also modify the dataframe directly and pass it. Therefore, the
# following results in the same:
set.seed(123)
df_mod <- mod(mfr$df_notes_wide)
mfr$update_notes_wide(df_mod)
mfr$play()
## End(Not run)
## ------------------------------------------------
## Method `MidiFramer$populate_r_fields`
## ------------------------------------------------
## Not run:
midi_file_string <- system.file("extdata", "test_midi_file.mid", package = "pyramidi")
mfr <- MidiFramer$new(midi_file_string)
mfr$params$columns_to_add <- c("m", "b", "t", "time")
mfr$populate_r_fields()
## End(Not run)
## ------------------------------------------------
## Method `MidiFramer$play`
## ------------------------------------------------
midi_file_string <- system.file("extdata", "test_midi_file.mid", package = "pyramidi")
mfr <- MidiFramer$new(midi_file_string)
mfr$play()
# The play method does basically this:
## Not run:
midi_out <- "my_output.mid"
mp3file <- "test.mp3"
mfr$mf$write_file(midi_out)
fluidsynth::midi_convert(midi_out, output = mp3file)
# `overwrite` = TRUE overwrites midi_out & mp3file
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.