How to hear the different kind of random_sources

Hello! The idea came to me when I was looking at Mr Bomb’s video @mrbombmusic about random_source. I wanted to hear really the differences between them. So I wrote this inspired by his tutorial and it works, so if you see something to improve, it will be with pleasure… :nerd_face:

use_bpm 95

use_random_source :pink 
##change with perlin, etc


a = []


with_fx :reverb, mix: 0.8 do
  200.times do
    n = rand
    a.push(n)
    play n * 150, amp: 0.5
    sleep 0.25
  end
end

puts a
4 Likes

Very interesting and useful!
Here’s a tweaked version that steps through them all:

use_bpm 95


randomsources = [:white, :pink, :perlin, :light_pink, :dark_pink]
use_random_source :pink
##change with perlin, etc


a = []

randomsources.each do |x|
  use_random_source x
  with_fx :reverb, mix: 0.8 do
    40.times do
      n = rand
      a.push(n)
      play n * 150, amp: 0.5
      sleep 0.25
    end
  end
  
  puts a
  sleep 2
end #each

I especially like perlin and dark_pink for melodies that don’t jump around too much.
Is there a clever way to constrain these random notes to a given scale?

2 Likes

Good Harry @HarryLeBlanc and thanks for sharing your amelioration of my initial patch…It has a better look now! :grinning:
And it’s right, you can use one of them for a compostion but I find fascinating the idea in itself to be able to hear all these “discret” numbers, so small and inaudible at the beginning!

Hi Harry,

Please explain what a.push(n) does?

Best regards,
Rob

Hi @robst247

n is a random float between 0 and 1, a is an initially empty array, and a.push(n) simply appends a new random float to that array:

use_bpm 95

use_random_source :pink
##change with perlin, etc


a = []
puts a
sleep 1

5.times do
  n = rand
  a.push(n)
  puts n
  play n * 150, amp: 0.5
  sleep 1
end

puts a

HTH

PD-Pi

1 Like

https://ruby-doc.org/core-2.7.0/Array.html#method-i-push

a = [1,2,3]
puts a
sleep 1

a = a.push(17,-42,"bla")
puts a
1 Like

Crystal clear - thanks :slight_smile:
Where does the ‘push’ function come from?
I’ve not seen it before, and it’s not described in Sonic Pi help.
Is this from Ruby?

Cheers, Rob

1 Like

I’ve found the answer:

It simply appends elements to a pre-defined array.

Now I’m wondering how to quantize a series of random floats in an array to a musical scale and also how to impose lower and upper limits, in order to create a random-note sequencer.

Hi
look for “scale” in the tutorial and also in the Help/Lang document. One can get tied up in knots using ‘custom’ arrays or rings for constrained random melodies. Sonic Pi provides many useful functions for creating such patterns.

Look at the examples (and mr bomb’s response) in this post:

PD-Pi

1 Like

Thanks for these suggestions. I am aware of them and have used them in my own Sonic Pi coding, but are they truly random or are they pseudo-random (i.e. with some repetition)? I’m still intrigued by the possibility of scale-quantising truly random series of floating point numbers; however, I have not yet conceived of a means of achieving this.

Hi
I’m sure you’ve done this already, but if not, try a keyword search in this forum - I am certain the topic has come up before in various contexts.

HTH

PD-Pi

1 Like

I came up with a method to find the closest note to a scale. Here it is. (It relies on a function, cleanchordorscale, which can be found in my library YummyFillings.rb. It’s on github, under my username, HarryLeBlanc.)

eval_file "e:/yummy/yummyfillings.rb"

define :closestnote do |thisnote, thisscale|
  thisscale = cleanchordorscale thisscale
  #debugprint thisscale
  foundnote = nil
  thisstep = 0
  thisneg = true
  while !foundnote
    thissign = (thisneg ? -1 : 1)
    #debugprint "test value: ", thisnote + (thisstep * thissign)
    if thisscale.include? thisnote + (thisstep * thissign)
      #debugprint "got a match!"
      foundnote = thisnote + (thisstep * thissign)
    elsif thisneg
      #debugprint "got a negative, incrementing step"
      thisstep += 1
      #debugprint "thisstep: :", thisstep
    end #if foundnote or thisneg
    thisneg = !thisneg
    #debugprint "thisneg: ", thisneg
  end #while
  return foundnote
end #define closestnote


thisscale = scale(:a0, :minor, num_octaves: 6)

use_random_seed Time.now.to_i
thisnote = rand_i(89) #full piano range
debugprint "thisnote: ", thisnote

debugprint "closestnote: ",  closestnote(thisnote, thisscale)


Hope you find this useful. Should allow you to constrain random integers to any arbitrary scale or array of notes.
I think I’ll add it to YummyFillings.rb for the next release.
Cheers,
H