Travis build status

drumr: Turn R into a Drum Machine

drumr allows you to play drum beats from within R. At present the package contains only two functions – beat(), which plays a beat from a given kit and drum, and tempo(), which sets the spacing between beats.

Installation

To install the development version of drumr, use the install_github function from the devtools package:

library(devtools)
install_github("jamesmartherus/drumr")

To install the latest release version of drumr, use the install.packages function:

install.packages("drumr")

Requirements

The beat function requires the audio package.

beat()

beat() takes two arguments - kit and drum. The kit argument selects a drumset, and the drum argument selects a specific piece from that kit. As present, there are four kits available:

Each kit includes four pieces:

Usage

beat(kit = "acoustic", drum = "snare")

tempo()

tempo() is a basic wrapper for Sys.time() that sets the time between calls to beat() in beats per minute (bpm). tempo() takes one argument - bpm.

Usage

tempo(bpm = 120)

Example Script

The following script will play a basic four-bar drum beat:

# Bars 1-3
for(i in 1:3){
  
  beat(drum="kick",kit="acoustic")
  
  tempo(240)
  
  beat(drum="kick",kit="acoustic")
  
  tempo(240)
  
  beat(drum="snare",kit="acoustic")
  
  tempo(240)
  
  beat(drum="kick",kit="acoustic")
  
  tempo(120)
  
  beat(drum="kick",kit="acoustic")
  
  tempo(240)
  
  beat(drum="snare",kit="acoustic")
  
  tempo(120)
  
}

#Bar 4
beat(drum="kick",kit="acoustic")

tempo(240)

beat(drum="kick",kit="acoustic")

tempo(240)

beat(drum="snare",kit="acoustic")

tempo(240)

beat(drum="kick",kit="acoustic")

tempo(120)

beat(drum="kick",kit="acoustic")

tempo(240)

beat(drum="snare",kit="acoustic")

tempo(240)

beat(drum="snare",kit="acoustic")

tempo(480)

beat(drum="snare",kit="acoustic")

Example with tidyverse

library(tidyverse)
library(drumr)

drum <- c("kick",
          "snare",
          "hihat",
          "crash")

pattern1 <- tribble(~drum,~notes,
        1,2,
        1,2,
        2,2,
        1,2,
        1,1,
        1,2,
        2,1) 

pattern2 <- tribble(~drum,~notes,
                 1,2,
                 1,2,
                 2,2,
                 1,1,
                 1,2,
                 2,2,
                 2,4) 

song <- bind_rows(map_dfr(seq_len(3), ~ pattern1), pattern2 )

 walk2(song$drum, song$notes,~  { beat(drum=drum[.x],kit="acoustic"); tempo(120 * .y) } )

Acknowledgments

mirror server hosted at Truenetwork, Russian Federation.