Play A Fairy Song by William Shakespeare

Just have a experiment to play each characters in the poem “A fairy song” by William Shakespeare.

fairy_song = “Over hill, over dale, Thorough bush, thorough brier, Over park, over pale, Thorough flood, thorough fire! I do wander everywhere, Swifter than the moon’s sphere; And I serve the Fairy Queen, To dew her orbs upon the green; The cowslips tall her pensioners be; In their gold coats spots you see; Those be rubies, fairy favours; In those freckles live their savours; I must go seek some dewdrops here, And hang a pearl in every cowslip’s ear.”

live_loop :poem do
with_fx :reverb do
with_fx :wobble do
use_synth :blade
fairy_song.each_char do |c|
#puts c.ord
if c == " "
sleep 0.125
elsif c == “,”
sleep 0.125
elsif c == “!”
sleep 0.5
elsif c == “.”
sleep 0.5
else
play c.ord, release: 0.125, amp: 3
sleep 0.125
end
end
end
end
end

live_loop :ambi do
sample :ambi_piano, cutoff: rrand(60, 120), amp: 0.5
sample :ambi_drone, release: 2, cutoff: rrand(60, 120), amp: 0.5
sleep [4, 8, 16].choose
end

1 Like

Fun idea! Poems are naturally repetitive so those can form interesting melodies.

Btw, you should format your code with markdown by wrapping text inside ``` or by painting and clicking the “preformatted text” button.

I added a line that assigns midi notes to each letter. ord-method played bit too high notes for my ears. This example one “Cradle song” by William Blake:

sweet_dreams = "Sweet dreams form a shade, O'er my lovely infants head. Sweet dreams of pleasant streams, By happy silent moony beams  Sweet sleep with soft down. Weave thy brows an infant crown. Sweet sleep Angel mild, Hover o'er my happy child.  Sweet smiles in the night, Hover over my delight. Sweet smiles Mothers smiles, All the livelong night beguiles.  Sweet moans, dovelike sighs, Chase not slumber from thy eyes, Sweet moans, sweeter smiles, All the dovelike moans beguiles.  Sleep sleep happy child, All creation slept and smil'd. Sleep sleep, happy sleep. While o'er thee thy mother weep  Sweet babe in thy face, Holy image I can trace. Sweet babe once like thee. Thy maker lay and wept for me  Wept for me for thee for all, When he was an infant small. Thou his image ever see. Heavenly face that smiles on thee,  Smiles on thee on me on all, Who became an infant small, Infant smiles are His own smiles, Heaven & earth to peace beguiles. "

h = Hash[('a'..'z').to_a.each_with_index.map  { |e,i| [e, 50+i] }]

live_loop :poem do
  with_fx :reverb do
    with_fx :wobble do
      use_synth :chipbass
      sweet_dreams.each_char do |c|
        c = c.downcase
        if c == " "
          sleep 0.125
        elsif c == ","
          sleep 0.25
        elsif c == "!"
          sleep 0.5
        elsif c == "."
          sleep 0.5
        else
          play h[c], release: 0.125, amp: 3
          sleep 0.125
        end
      end
    end
  end
end

live_loop :beat do
  sample :bd_haus
  sleep 0.5
end

live_loop :ambi do
  sample :ambi_drone, release: 2, cutoff: rrand(60, 120), amp: 1
  sleep [4, 8, 16].choose
end
1 Like

Nice! It’s much more better to map the letters within a pitch range. :smiley:

Hi guys,

Both reasonable ways of doing things, but you
could just have easily:

play c.ord - 24, release: 0.125, amp: 3

or some value that brought it down enough not
to hurt you ears :slight_smile:

Eli…

True. Using lowercase letters ord ranges from 97 to 122. Im usually overcomplicating things :slight_smile:

However, using hash as a note map for letters can be useful in many ways. You can actually randomize the notes because its already pretty random. This way you can use random_seed to find more pleasant tunes. Using certain key and scale also helps producing certain feel to the music.

Here’s one more example where you can change random seed, key and scale:

# Poem creates the repetition of the melody
poem = "Sweet dreams form a shade, O'er my lovely infants head. Sweet dreams of pleasant streams, By happy silent moony beams  Sweet sleep with soft down. Weave thy brows an infant crown. Sweet sleep Angel mild, Hover o'er my happy child.  Sweet smiles in the night, Hover over my delight. Sweet smiles Mothers smiles, All the livelong night beguiles.  Sweet moans, dovelike sighs, Chase not slumber from thy eyes, Sweet moans, sweeter smiles, All the dovelike moans beguiles.  Sleep sleep happy child, All creation slept and smil'd. Sleep sleep, happy sleep. While o'er thee thy mother weep  Sweet babe in thy face, Holy image I can trace. Sweet babe once like thee. Thy maker lay and wept for me  Wept for me for thee for all, When he was an infant small. Thou his image ever see. Heavenly face that smiles on thee,  Smiles on thee on me on all, Who became an infant small, Infant smiles are His own smiles, Heaven & earth to peace beguiles. "

# Change seed to find pleasant notes
use_random_seed 12345

# Change key and scale depending on the mood
poem_key = :e3
poem_scale = :minor

# Letters to be played
letters = ('a'..'z').to_a

# Create random notes within 3 octaves for each letter in some key and scale.
notes = Array.new(letters.length) {degree(rrand_i(1,scale(poem_key, poem_scale).length-1),poem_key-[-12,0,12].choose,poem_scale) }

h = Hash[letters.zip(notes)]

live_loop :poem do
  with_fx :reverb do
    with_fx :wobble do
      use_synth :chipbass
      poem.each_char do |c|
        c = c.downcase
        if c == " "
          sleep 0.125
        elsif c == ","
          sleep 0.25
        elsif c == "!"
          sleep 0.5
        elsif c == "."
          sleep 0.5
        else
          play h[c], release: 0.125, amp: 3
          sleep 0.125
        end
      end
    end
  end
end

live_loop :beat do
  sample :bd_haus
  sleep 0.5
end

live_loop :ambi do
  sample :ambi_drone, release: 2, cutoff: rrand(60, 120), amp: 1
  sleep [4, 8, 16].choose
end

I like it! This approach can make much more nice melody. Looking for more variations of this idea. BTW the code is a little bit difficult for me. :sweat_smile:

Maybe treating each sentences or paragraphs as patterns and rearrange and repeat the patterns in a structure will make it like a song. Would it? :thinking:

Good question. What separates song from random melody? I rather like randomness so i would say imagination but repetition and self similar sequences makes the random journey more pleasant.

Ruby array methods are really useful, but often not necessary if you think the code other way around. Here’s basically the same code but this time it assings the notes on the fly.

I tried to write something more repetitive to make it a song. Still pretty random but fun:

# Poem creates the repetition of the melody
poem = '
Just try to code something fun,
and Sonic Pi will make it run!

I like Sonic Pi,
Dibi daba duu,
You like Sonic Pi,
Dibi daba duu,
We like Sonic Pi,
Bada budi bum!

Who doesnt like Sonic Pi?

Dibi daba duu,
Bada bum bum.
'

# Change seed to find pleasant notes
use_random_seed 1

# Change key and scale depending on the mood
poem_key = :c3
poem_scale = :yu

# Collect randomized notes to hash for each character on the fly
h = {}

live_loop :poem do
  print "Start"
  with_fx :reverb do
    with_fx :wobble do
      use_synth :chipbass
      poem.each_char do |c|
        c = c.downcase
        if c == " "
          sleep 0.125
        elsif c == ","
          sleep 0.25
        elsif c == "!"
          sleep 0.5
        elsif c == "."
          sleep 0.5
        else
          if h[c] then
            play h[c], release: 0.125, amp: 3
          elsif
            # Randomize degree between 1 and number of possible degrees
            rdegree = rrand_i(1,scale(poem_key, poem_scale).length-1)
            # Randomize used octave
            roctave = poem_key - [-12,0,12].choose
            # Create midi note using degree and store to hash
            h[c] = degree(rdegree,roctave,poem_scale)
            play h[c], release: 0.125, amp: 3
          end
          sleep 0.125
        end
      end
    end
  end
end

live_loop :beat do
  sample :bd_haus
  sleep 0.5
end

live_loop :ambi do
  sample :ambi_drone, release: 2, cutoff: rrand(60, 120), amp: 1
  sleep [4, 8, 16].choose
end
2 Likes

There needs to be a way to :hearts: something more than once.

Lovin it, Amiika. :slight_smile:

Eli…