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

raudiomate

Lifecycle: experimental CRAN status Codecov test coverage R-CMD-check R-CMD-check

The goal of raudiomate is to provide helper functions to generate wav audio files from midi files (with fluidsynth), convert them to mp3 (with ffmpeg), and play these audio files in rmarkdown html documents.

Installation

You can install the development version of raudiomate like so:

remotes::install_github("urswilke/raudiomate")

Further dependencies:

Example

This is a basic example how you can apply the functions. We'll use the midifile included in the package:

library(raudiomate)

midifile <- system.file("extdata", "test_midi_file.mid", package = "pyramidi")
audiofile <- "test.wav"

Synthesize a midi file

synthesize_midi(midifile, audiofile, verbose = TRUE)
#> FluidSynth runtime version 2.1.1
#> Copyright (C) 2000-2020 Peter Hanappe and others.
#> Distributed under the LGPL license.
#> SoundFont(R) is a registered trademark of E-mu Systems, Inc.
#> 
#> Rendering audio to file 'test.wav'..

I have tested this only on my machine (Arch Linux). I had to create a file (or a symbolic link) at /usr/share/soundfonts/default.sf2 in my file system (as explained here; otherwise you can also specify the path in the soundfont argument in synthesize_midi()):

ln -s /path/to/soundfont.sf2 /usr/share/soundfonts/default.sf2

Convert wav to mp3

convert_to_mp3(audiofile, verbose = TRUE)
#> ffmpeg version 4.2.4-1ubuntu0.1 Copyright (c) 2000-2020 the FFmpeg developers
#>   built with gcc 9 (Ubuntu 9.3.0-10ubuntu2)
#>   configuration: --prefix=/usr --extra-version=1ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
#>   WARNING: library configuration mismatch
#>   avcodec     configuration: --prefix=/usr --extra-version=1ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared --enable-version3 --disable-doc --disable-programs --enable-libaribb24 --enable-liblensfun --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libtesseract --enable-libvo_amrwbenc
#>   libavutil      56. 31.100 / 56. 31.100
#>   libavcodec     58. 54.100 / 58. 54.100
#>   libavformat    58. 29.100 / 58. 29.100
#>   libavdevice    58.  8.100 / 58.  8.100
#>   libavfilter     7. 57.100 /  7. 57.100
#>   libavresample   4.  0.  0 /  4.  0.  0
#>   libswscale      5.  5.100 /  5.  5.100
#>   libswresample   3.  5.100 /  3.  5.100
#>   libpostproc    55.  5.100 / 55.  5.100
#> Guessed Channel Layout for Input Stream #0.0 : stereo
#> Input #0, wav, from 'test.wav':
#>   Duration: 00:00:10.67, bitrate: 1411 kb/s
#>     Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
#> Stream mapping:
#>   Stream #0:0 -> #0:0 (pcm_s16le (native) -> mp3 (libmp3lame))
#> Press [q] to stop, [?] for help
#> Output #0, mp3, to 'test.mp3':
#>   Metadata:
#>     TSSE            : Lavf58.29.100
#>     Stream #0:0: Audio: mp3 (libmp3lame), 44100 Hz, stereo, s16p
#>     Metadata:
#>       encoder         : Lavc58.54.100 libmp3lame
#> size=     168kB time=00:00:10.68 bitrate= 128.5kbits/s speed=34.6x    
#> video:0kB audio:167kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.147640%
#> generated test.mp3

Play a file

audiofile %>% player()
htmltools::tags$audio(
  controls = "",
  htmltools::tags$source(
    src = "https://urswilke.github.io/raudiomate/articles/test.mp3",
    type = "audio/mp3"
  )
)

# needed as otherwise the chapters afterwards are not rendered in the github readme:
htmltools::br()

R packages used

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

References



urswilke/raudiomate documentation built on Feb. 15, 2024, 3:10 a.m.