项目作者: shill1729

项目描述 :
Synthesize sounds and stochastic compositions in R
高级语言: R
项目地址: git://github.com/shill1729/synthesizer.git
创建时间: 2020-04-18T20:49:59Z
项目社区:https://github.com/shill1729/synthesizer

开源协议:Other

下载


synthesizer

The synthesizer package provides functions for synthesizing melodies, chords, and stochastic compositions directly in R. Note that this project is a work in progress and will likely be missing features for quite some time and errors will propagate in e.g. counterpoint functions that will simply break the program rather than handled in the global composer-function call.

Table of contents

  1. Installation

  2. Synthesizer basics

  3. Stochastic fugue generation

Installation

You can install the GitHub version with

  1. devtools::install_github("shill1729/synthesizer")

You will also need to installl “seewave”, “tuneR”, and markovChains.

The former two can be installed via CRAN while the latter through GitHub

  1. install.packages("seewave")
  2. install.packages("tuneR")
  3. devtools::install_github("shill1729/markovChains")

Setting up

How to set up for usage:

  1. library(synthesizer)
  2. library(seewave)
  3. library(tuneR)
  4. # Set player for Windows OS
  5. setWavPlayer(shQuote("C:/Program Files/Windows Media Player/wmplayer.exe"))
  6. # Sample rate for Windows
  7. f <- 48000
  8. # Set player for MAC OSX
  9. setWavPlayer('/usr/bin/afplay')
  10. # Sample rate for Mac
  11. f <- 44100

Synthesizer basics

Playing a pure tone

How to synthesize and listen to a pure tone

  1. library(synthesizer)
  2. library(tuneR)
  3. library(seewave)
  4. library(markovChains)
  5. # Set player for Windows OS
  6. setWavPlayer(shQuote("C:/Program Files/Windows Media Player/wmplayer.exe"))
  7. # Sample rate for Windows
  8. f <- 48000
  9. # Play middle C for one measure
  10. x <- tone(midi = 60, rhythm = 1)
  11. listen(x, f = f)

Synthesizing a melody

How to synthesize and listen to a melody with constant harmonics

  1. library(synthesizer)
  2. library(tuneR)
  3. library(seewave)
  4. library(markovChains)
  5. # Set player for Windows OS
  6. setWavPlayer(shQuote("C:/Program Files/Windows Media Player/wmplayer.exe"))
  7. # Sample rate for Windows
  8. f <- 48000
  9. # Play a simple melody: C D E F E D C at middle C
  10. midi <- c(0, 2, 4, 5, 4, 2, 0)+60
  11. # In quarter notes
  12. r <- 1/4
  13. x <- tones(midi = midi, rhythms = r)
  14. # Listen
  15. listen(x, f = f)

Synthesizing a chord

How to synthesize and listen to a chord with constant harmonics

  1. library(synthesizer)
  2. library(tuneR)
  3. library(seewave)
  4. library(markovChains)
  5. # Set player for Windows OS
  6. setWavPlayer(shQuote("C:/Program Files/Windows Media Player/wmplayer.exe"))
  7. # Sample rate for Windows
  8. f <- 48000
  9. # Play a Cm11 cluster
  10. midi <- c(0, 7, 10, 0+12, 2 +12, 3 +12, 5 +12, 10+12, 12+12, 14+12, 15+12)+60
  11. # For one measure
  12. r <- 1
  13. x <- tones(midi = midi, rhythms = r, type = "chord")
  14. # Listen
  15. listen(x, f = f)

Poisson processes

Subjecting the number of chord changes over time to follow a Poisson process, identifying the waiting times between arrivals of the Poisson process with the durations of the chords.

  1. library(synthesizer)
  2. library(markovChains)
  3. library(tuneR)
  4. library(seewave)
  5. # Set player for Windows OS
  6. setWavPlayer(shQuote("C:/Program Files/Windows Media Player/wmplayer.exe"))
  7. # Sample rate for Windows
  8. sampleRate <- 48000
  9. # Possible chords to transition between: C, F/C, Dm7/C
  10. chord_states <- list(c(60, 64, 67), c(60, 65, 69), c(60, 62, 65))
  11. # Probability transition matrix
  12. p <- 0.6
  13. P <- rbind(c(0, p, 1-p),
  14. c(p, 0, 1-p),
  15. c(1-p, p, 0))
  16. # Initial distribution
  17. initial_chord <- c(1, rep(0, 2))
  18. # duration of progression and mean number of changes
  19. tt <- 10 # in measures
  20. lambda <- 4 # changes per measure
  21. # Default n = 4 for 4/4 time and bpm = 120
  22. cprog <- poisson_chords(tt, lambda, chord_states, P, initial_chord)
  23. listen(cprog, f = sampleRate)

Stochastic fugue generation

Two voice fugue with first species counterpoint

May stop on a counterpoint error, rerun it until it succeeds (won’t take long). Eventually, these errors will be caught and handled…
Also, this is mostly set up for subjects in a major key. The function will likely error out for minor keys, or just sound awful/unintended.

  1. library(synthesizer)
  2. library(tuneR)
  3. library(seewave)
  4. library(markovChains)
  5. # Set player for Windows OS
  6. setWavPlayer(shQuote("C:/Program Files/Windows Media Player/wmplayer.exe"))
  7. # Sample rate for Windows
  8. f <- 48000
  9. # Number of statements
  10. N <- 10
  11. # Fugue subject
  12. subject <- c(0, 2, 4, 5, 4, 2, 0)
  13. # Rhythm of fugue subject
  14. rhythms <- 1/8
  15. # Try also random rhythms
  16. # rhythms <- rexp(n = length(subject), rate = 4)
  17. # Register of voices
  18. registers <- c(48, 60)
  19. # Probability transition matrices
  20. P.voice <- rbind(c(0, 1),
  21. c(1, 0))
  22. I <- 0.1
  23. R <- 0.1
  24. RI <- 0.1
  25. id <- 1-(RI+I+R)
  26. P.transform <- rbind(c(id, I, R, RI),
  27. c(id, I, R, RI),
  28. c(id, I, R, RI),
  29. c(id, I, R, RI))
  30. keys <- c(0, 2, 4, 9, NA)
  31. P.key <- rbind(c(0, 0, 0, 0, 1),
  32. c(0, 0, 0, 0, 1),
  33. c(0, 0, 0, 0, 1),
  34. c(0, 0, 0, 0, 1),
  35. c(0.7, 0.1, 0.1, 0.1, 0))
  36. answers <- c(0, 2, 4, 5, 7)
  37. P.answer <- rbind(c(0, 0.1, 0.1, 0.1, 0.7),
  38. c(1, 0, 0, 0, 0),
  39. c(1, 0, 0, 0, 0),
  40. c(1, 0, 0, 0, 0),
  41. c(1, 0, 0, 0, 0)
  42. )
  43. transition_matrices <- list(P.voice = P.voice, P.transform = P.transform, P.key = P.key, P.answer = P.answer)
  44. # Generate stochastic fugue
  45. s <- stochasticFugue(subject = subject, rhythms = rhythms, N = N, registers = registers, transition_matrices = transition_matrices, subjectKeys = keys, answerKeys = answers)
  46. listen(wave = s$wave, f = f)