Code Compositions

Share your compositions here :slight_smile:

I hope that this is the appropriate forum and location for this content.

Modules for Harmony and Melodic Inversion

These modules add functions to Sonic Pi that provide harmony and melodic inversion.

Note: Once the modules are loaded, the only way to clear them is to restart Sonic PI.
If there is a way to unload them, let me know?

Module Link

The two modules are named HI.rb and HI2D.rb

There are two test programs testHI.rb and testHI2D.rb

A recording of testHI2D can be found here
Test HI2D

Here is a listing of testHI2D

#testHI2D.rb
# 30 Sep 2017
# Test of HI and HI2D modules
# To remove the modules exit and restart Sonic Pi
##############
#Define tempo and note lengths and release fraction
#####
tempo=1.0
#define note timings
whole=1.0
half=whole/2.0
dothalf=half*1.5
quart=half/2.0
dotquart=quart*1.5
eighth=quart/2.0
doteighth=eighth*1.5
#define the release fraction
rel=2.0 #0.8 #controls note release
################################################
transp = 5 # use this to transpose the key by integer halftones on chromatic scale
# Helper functions
###############
define :playit do|nte|
  b=note(nte[0])
  if b != nil
    b+=transp
  end
  b=HI.midi2note(b)
  puts transp,nte[0],b,nte[1]
  a=tempo*nte[1]
  play(b,release:a*rel)
  sleep a
end

###############
def playitt(tune)
  i=0
  while i<tune.length
    playit tune[i]
    i+=1
  end
end
########### end helper function
# Load and link the two modules
if (defined? HI) == nil
  puts "HI is nil"
  require "C:/MySPmodules/HI.rb"
  HI.link(self)
else
  puts "HI Exists"
end
if (defined? HI2D) == nil
  puts "HI2D is nil"
  require "C:/MySPmodules/HI2D.rb"
  HI2D.link(self,HI)
else
  puts "HI2D Exists"
end
###################
keyscale=scale :C4, :major # used by harmony portion
transpose =0
## define musical themes
theme1=[
  [:C4,quart],
  # [:r,whole],
  [:D4,quart],
  [:E4,quart],
  [:C4,quart],
  [:E4,quart],
  [:D4,eighth],
  [:C4,eighth],
  [:D4,quart],
  [:G4,quart],
  [:C4,quart],
  [:D4,quart],
  [:E4,quart],
  [:C4,quart],
  [:E4,quart],
  [:D4,eighth],
  [:C4,eighth],
  [:D4,quart],
  [:G3,quart],
] # end theme1
theme2=[
  [:C4,half],
  [:C4,half],
  [:C4,whole],
] #end theme2
i=0
use_synth :fm
live_loop :LL1 do
  with_fx :level, amp: 0.3 do
    puts "SSSSSSSSSSSSSSSSSSS",i
    playitt(theme1)
    playitt(theme1)
    playitt(theme1)
    playitt(theme1)
    playitt(theme1)
    i+=1
    if i==1 then theme1=theme1.reverse end
    if i==2 then theme1=HI2D.revtime(theme1) end
    if i==3 then theme1=theme1.reverse end
    if i==4 then theme1=HI2D.revtime(theme1) end
    #i+=1
    if i>4 # then i=0 end
      playitt(theme2)
      stop
    end
  end
end
live_loop :LL2 do
  sync :LL1
  with_fx :level, amp: 0.3 do
    playitt(HI2D.insertRest(theme1))
    playitt(HI2D.invert(theme1,:C4,keyscale))
    playitt(HI2D.invert(theme1,:C5,keyscale))
    playitt(HI2D.invert(theme1,:G5,keyscale))
    playitt(HI2D.harmonize(theme1,keyscale,2))
    if i>4 # then i=0 end
      playitt(theme2)
      stop
    end
  end
end
4 Likes

Thanks for posting. This is very interesting. I have worked with a script to convert Midi to SonicPi, but not used ABC format. I look forward to playing with your code. I like the harmony stuff.

#Montford
#by KniKnoo
live_loop :bass do
  sync :drum if one_in(6)
  use_synth :tb303
  use_synth_defaults amp: 0.3, res: 0.8, wave: 1, cutoff_attack: 0.03125
  nlen = [0.125, 0.25, 0.5].choose
  control amp: 0
  notes = scale(:f1, :minor_pentatonic).choose
  play notes, release: nlen * 1.125, cutoff: rrand(75, 110)
  sleep nlen
end

live_loop :drum do
  sample :bd_boom
  sleep 0.5#[0.25,0.5, 0.75].choose
  sample :sn_dolf
  sleep 0.5#[0.25, 0.5, 0.75].choose
end

live_loop :hats do
  nlen = [0.0625, 0.125, 0.25].choose
  4.times do
    sample :drum_cymbal_closed, sustain: 0, release: 0.03
    sleep nlen
  end
end

live_loop :chords do
  #use_random_seed 3068
  use_synth :dsaw
  use_synth_defaults coef: 0.1
  nlen = [1,2,3].choose
  cho = (chord_degree, [:i, :iii, :vi, :v].ring.tick, :f3, :minor, 4)
  if rand() > 0.7 then
    3.times do
      play scale([:f3, :f4].choose, :minor_pentatonic).choose, amp: 0.25, release: nlen / 2.0
      sleep nlen / 4.0
    end
  else
    play chord_invert( cho, [-1, 0, 1].choose), amp: 0.6, release: nlen
    sleep nlen
  end
end

And here it is being played. https://soundcloud.com/hieronymus-bots/montford-acid

2 Likes

This is so awesome. I love the sophisticated element of improve that you use here within a seemingly simple chord progression. Also love seeing other people using the chord degree function.

1 Like

I’ve been wandering round your gist, Kniknoo… so much good stuff.

I prefer ‘No way back’ personally… I notice it’s tagged as ‘by Adonis’… you?

Anyways, drop the BPM, throw in a few changes, add a xylophone and you
get some great chill music… still a work in progresss, but it stands well as is.

Eli…

1 Like

Realizing that chord degree was there was a game changer for me. :slight_smile:
Thank you, those chords on Montford made me really happy when I worked that out.

OMG, Eli, that makes my head bounce nicely! :slight_smile: Adonis is a House artist from the 80s, one of the Trax Records artists. Not as well known as DJ Pierre and Phuture, but a legend in his own right.

Original version of it is here.

Interesting on the Adonis thing… I was a heavy clubber in the 80’s, but I dont remember it… Probably
didn’t make the UK scene except for a few hot import DJ’s in London.

Hmmm… nothing on the track that couldn’t really be duplicated in SP… veeeeeeery interesting.
I’m sensing another Just Eli… remix. :slight_smile:

Eli…

1 Like