An open API service indexing awesome lists of open source software.

https://github.com/urswilke/pyramidi

Generate & manipulate midi data in R data frames
https://github.com/urswilke/pyramidi

midi

Last synced: 5 months ago
JSON representation

Generate & manipulate midi data in R data frames

Awesome Lists containing this project

README

          

---
output: github_document
bibliography: grateful-refs.bib
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# pyramidi

[![Codecov test coverage](https://codecov.io/gh/urswilke/pyramidi/branch/master/graph/badge.svg)](https://app.codecov.io/gh/urswilke/pyramidi?branch=master)
[![R-CMD-check](https://github.com/urswilke/pyramidi/workflows/R-CMD-check/badge.svg)](https://github.com/urswilke/pyramidi/actions)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)

# Introduction

pyramidi is a very experimental package to generate / manipulate midi data from R.
Be aware that a lot of the code I've written some years ago hurts my eyes when I look at it now :)
Midi data is read into dataframes, using the python package [miditapyr](https://pypi.org/project/miditapyr/)
under the hood (which itself uses the excellent [mido](https://github.com/mido/mido)).
The notes' midi information (one line per `note_on`/`note_off` midi event)
is translated into a wide format (one line per note).
This format facilitates some manipulations of the notes' data and also plotting them in piano roll plots.
Finally, the modified dataframes can be written back to midi files (again using miditapyr).

Thus, you can manipulate all the intermediate dataframes and create midi files from R.
However, you need to make sure yourself that the midi files you write can be understood by your softsynth.
The data is not yet validated by pyramidi,
but mido (also used to write midi files) already catches some of the possible inconsistencies.

If you're new to midi, [mido's documentation](https://mido.readthedocs.io/en/latest/) might be a good start.

## New since version 0.2

The midi data can now be

* played live in the R console OR generate a sound file and a html audio player when knitting rmarkdown documents thanks to the excellent R packages [fluidsynth](https://github.com/ropensci/fluidsynth) (see the documentation of the [`play()`](https://urswilke.github.io/pyramidi/reference/MidiFramer.html#method-play-) method in the `MidiFramer` class and its helper function `player()` which use
* `fluidsynth::midi_convert()` to synthesize midi to wav files (needs [fluidsynth](https://www.fluidsynth.org/) installed, but if I understand correctly R will do that for you)

## Installation

You can install pyramidi from R-universe with:

``` r
install.packages('pyramidi', repos = c('https://urswilke.r-universe.dev', 'https://cloud.r-project.org'))
```

The python package [miditapyr](https://pypi.org/project/miditapyr/) also needs
to be installed in your python environment used by
[reticulate](https://github.com/rstudio/reticulate).

```sh
pip install miditapyr
```

But if everything works as I believe it should, miditapyr is automatically installed
if you install pyramidi, as soon as you access the module for the first time.

Otherwise, you can also install it in your reticulate python environment
with the included helper function:

```{r, eval=FALSE}
pyramidi::install_miditapyr()
```

*I'm not sure if that works on windows too. Perhaps there you have to manually configure your reticulate environment.*

## Usage

### Generate a `MidiFramer` object

We can create a `MidiFramer` object by passing the file path to the constructor method
([`new()`](https://urswilke.github.io/pyramidi/reference/MidiFramer.html#method-new)).

```{r, message=FALSE}
library(pyramidi)
library(dplyr)
midi_file_string <- system.file("extdata", "test_midi_file.mid", package = "pyramidi")
mfr <- MidiFramer$new(midi_file_string)
```

The object contains the midi data in various dataframe formats and an interface
to the miditapyr
[miditapyr.MidiFrames](https://miditapyr.readthedocs.io/en/latest/notebooks/midi_frame_usage.html)
object `mfr$mf`. You can write the midi file resulting of the `MidiFramer` object to disk:

```{r, eval=FALSE}
mfr$mf$write_file("/path/to/your/midifile.mid")
```

### Modifying midi data

In the `MidiFramer` object, we can modify `mfr$df_notes_wide`, the notes in note-wise wide format
(`note_on` & `note_off` events in the same line). Thus we don't need to worry which
midi events belong together

Let's look at a small example. We'll define a function to replace every
note with a random midi note between 60 & 71::

```{r}
mod <- function(dfn, seed) {
n_notes <- sum(!is.na(dfn$note))
dfn %>% mutate(note = ifelse(
!is.na(note),
sample(60:71, n_notes, TRUE),
note
))
}
```

When we call the `update_notes_wide()` method, the midi data in `mfr` is updated:

```{r, message=FALSE}
mfr$update_notes_wide(mod)
```

### Writing modified midi files

Thus, we can now save the modifications to a midi file:

```{r, eval=FALSE}
mfr$mf$write_file("mod_test_midi_file.mid")
```

### Synthesizing and playing audio

See the `vignette("pyramidi", package = "pyramidi")` to see how you can
synthesize the midi data to wav, convert to mp3 if you want, and then
embed a player in your rmarkdown html document with
```{r, eval=FALSE}
mfr$play("mod_test_midi_file.mp3")
```

```{r echo=FALSE}
htmltools::tags$audio(
controls = "",
htmltools::tags$source(
src = "https://urswilke.github.io/pyramidi/articles/mod_test_midi_file.mp3",
type = "audio/mp3"
)
)
```

*The player only appears in the [docs](https://urswilke.github.io/pyramidi/index.html#synthesizing-and-playing-audio).

Even if that sound is very weird, I was very happy not having to listen to the package midi file over and over again. :)

## Documentation

You can find the complete online documentation of the package [here](https://urswilke.github.io/pyramidi/).

* See the `vignette("pyramidi")` for a brief usage introduction how to manipulate midi data.
* The `vignette("compose")` shows a more extended example how to compose music and generate midi files from scratch.
* `vignette("package_workflow")` shows in detail the structure of the `MidiFramer` class and the functions of the pyramidi package.
* `vignette("functions_usage")` illustrates the low-level functions of the pyramidi package, that `MidiFramer` objects use under the hood.

## pyramidi out in the wild

To see examples where pyramidi is used for midi mangling in R dataframes etc. (amongst plenty other of his awesome writings about music),
please check out Matt Crump's [blog](https://homophony.quest/notes.html#category=midi)
and his package [midiblender](https://www.crumplab.com/midiblender/articles/Getting_started.html)!

## Related R packages

* The [tabr](https://github.com/leonawicz/tabr) package is a massive music notation, transcription and analysis program allowing to create musical scores (using Lilypond). It allows to read midi files (wrapping tuneR; see below) and also to export them (also using Lilypond).
* The [gm](https://github.com/flujoo/gm) package also allows to create and show musical scores using musescore. It also allows to export the music to audio (also using musescore) and to embed the players in html documents.
* The [noon package](https://github.com/ColinFay/noon) wraps node.js libraries and can be used to read live midi input port data. I wrote a small [blog post](https://urssblogg.netlify.app/post/2020-10-24-live-recording-of-a-midi-controller-via-mido-inport/) how reading a midi port can also be done in R with [mido](https://mido.readthedocs.io/en/latest/ports.html). Interestingly, the node.js libraries and mido rely on a the same C++ library [RtMidi](http://www.music.mcgill.ca/~gary/rtmidi/index.html).
* The [tuneR](https://cran.r-project.org/package=tuneR) package can also read in midi data. See the `vignette("tuner")`, for an example how you can transform the tuner format into the pyramidi format.

## R packages used

This package stands on the shoulders of giants. A big thank you to the authors of the following libraries!

```{r, echo=FALSE, warning=FALSE}
pkgs <- grateful::cite_packages(
output = "table",
out.dir = ".",
omit = c("pyramidi"),
)
knitr::kable(pkgs)
```