Make electronic music with randomness with Ruby

This article is the 18th day of iCARE Advent Calendar 2020.

Write music in Ruby

"I was making music!" "I'm touching my DAW!" I think this is really the case in the engineering community. Why lol I am one of them, and I make songs of genres called electronica and IDM.

By the way, do you all know ** Sonic Pi **? When I learned that this tool can be used to make music in Ruby, I found it to be a very interesting and useful tool. And this article says ** If you keep this down, you can make something like that with Sonic Pi **.

Sonic Pi As mentioned above, download and use Sonic Pi. If you search on Qiita for an overview, you will find various information, so I will not introduce it in detail, but You can play sounds by writing and executing Ruby in this environment. スクリーンショット 2020-12-15 20.35.08.png

Sonic Pi is suitable for electronic music

I feel that electronic music and experimental music are more songs that use sounds that are intentionally generated by chance than songs that are carefully calculated. Strange three-dimensional effect, mysterious sound, messy but comfortable rhythm, noise, etc. And Sonic Pi makes it easy to generate such random sounds. it's the best. Yes.

I made a glitchy beat

What is a glitch?

An error in a digital device, the noise caused by it, or a method of creating a work using them. This technique appeared in music in the mid-1990s and began to be referred to by the term "glitch", meaning accidental and minor flaws. https://artscape.jp/artword/index.php/%E3%82%B0%E3%83%AA%E3%83%83%E3%83%81

Environment construction / basic operation

I will omit it in this article. It is written in detail in this article, so if you are interested, please. It is very easy to understand.

Source

#Four beat of the kick
live_loop :kick do
  use_bpm 60
  sample :bd_haus, rate: 0.8, release: 0.4, amp: 0.5
  sleep 1 #Rest one beat
end

#Hit the same kick by shifting the top
live_loop :kick2 do
  sleep 0.75
  sample :bd_haus, rate: 0.5, amp: 0.7
end

#Create an atmosphere with ambience and soft sounds
live_loop :ambi_dark do
  sample  :ambi_dark_woosh, rate: 0.5, finish: 0.2
  sleep 0.5
end

#percussion
live_loop :perc do
  #Apply distortion effect
  with_fx :distortion do
    sample :glitch_perc1, rate: 0.2, finish: 0.05, amp: 0.05
    sleep 1.5
  end
end

#Percussion 2
live_loop :perc2 do
  #Randomize the rate and change the expression for each playback
  sample :glitch_perc5, rate: rrand(0.1, 1.5), amp: 0.1, slice: 0.1
  sleep 0.125
end

Commentary

What you are doing is simple

  1. Select a sample
  2. Set rate
  3. Adjustment of amp, release, etc.
  4. Set rrand for the key element About.

Choose a sample

Sonic Pi provides many sound samples by default You can easily call the sample just by typing sample hoge.

rate rate can change the speed of the sound. 1 is the standard length, 0.5 is the sound played at half the standard speed. Playing at half speed not only slows down, but also lowers the pitch. When you hit the glass with chopsticks, it sounds like a chain,

sample :chine, rate: 0.1

If so, it will sound as jiiiiiiiiiiiiiiiiiiiiinnnnn. Also, if you set a negative value, it will be a favorite of electronic musicians, reverse playback.

sample :chine, rate: -1

Nnnuuuuuuiiiiiiiii ↑↑ Is it like that? I'm sorry I'm not good at expressing. It is an image that if you rate the cymbal: -1, it becomes a reverse cymbal.

The rate option that changes the tone and pitch at once is wonderful.

amp, release amp is the volume 1 is standard, 0.5 is half the volume. Release is the so-called ADSR (Attack, Decay, Sustain, Release) Release, which refers to the attenuated sound. Is it close to the afterglow? image.png

At the beginning of the source above, I set release to 0.4. I aimed for a sharp kick with less lingering sound so as not to interfere with other elements that give a glitchy feeling.

#Basic kick
live_loop :kick do
  use_bpm 60
  sample :bd_haus, rate: 0.8, release: 0.4, amp: 0.5
  sleep 1 #1 beat rest
end

Set rrand for the key element

As you can guess from the name, it is possible to randomize the number set with the rrand option. At the end of the source, I use it at the rate of percussion 2.

#Percussion 2
live_loop :perc2 do
  sample :glitch_perc5, rate: rrand(0.1, 1.5), amp: 0.1, slice: 0.1
  sleep 0.125
end

By setting rate: rrand (0.1, 0.5), a value between 0.1 and 0.5 will be set randomly each time you loop in one bar. By doing this, you can change the sound in each measure ... In other words, you can ** sublimate from a mechanically monotonous sound to a live sound **. it's the best.

The result of running the source

I recorded it for about 1 minute and uploaded it to soundcloud. (Earphone recommended) https://soundcloud.com/tenten1105/sonic_pi-beat

I think I made something very good! The Sonic Pi also has a recording function, and it is nice that the executed data is immediately written out with wav on the spot.

Afterword

The Sonic Pi that can do this with that much code is really great.

I used sample this time, but of course you can also use a recording of data or environmental sounds created in your own DAW, process it with rrand, and then return it to your DAW for production. The possibilities are endless! Thank you, Ruby. Thank you, Sonic Pi.

Recommended Posts

Make electronic music with randomness with Ruby
Make a typing game with ruby
Make Ruby Kurosawa with Ruby (Ruby + LINE Messaging API)
Let's make draw poker with ruby ~ Preparation ~
Let's make a smart home with Ruby!
Let's make draw poker with ruby ~ test-unit preparation ~
Install Ruby 3.0.0 with asdf
How to make LINE messaging function made with Ruby
Let's make a LINE Bot with Ruby + Sinatra --Part 2
Getting Started with Ruby
Let's make a LINE Bot with Ruby + Sinatra --Part 1
11th, Classification with Ruby
Evolve Eevee with Ruby
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Ruby version switching with rbenv
Solve Google problems with Ruby
I tried DI with Ruby
GraphQL Client starting with Ruby
Ruby: Send email with Starttls
Leap year judgment with Ruby
Format Ruby with VS Code
Integer check method with ruby
Microservices with DevOps Make Changes
Ruby Learning # 8 Working With String
[Ruby] problem with if statement
Studying with CodeWar (ruby) ⑤ proc
Use Ruby with Google Colab
[Ruby] REPL-driven development with pry
Getting Started with Ruby Modules
[ruby] Method call with argument