Include functions in multiple projects

One way is to use

run_file [path_to_file]

If you want the code to be available for all of your projects the more conveniant way is to add it to your SP startup file. Under Linux mine can be found at:

/home/[username]/.sonic-pi/init.rb

In my case it contains e. g.:

# Sonic Pi init file
# Code in here will be evaluated on launch.

load_snippets("~/.sonic-pi/snippets")

# to referenced e. g. like: 'sample atmos, 0'
define :atmos do
  atmos = "/path/to/some/sample/libraries/"
end

# more libraries

#################################################
# Custom functions ##############################
#################################################

# https://github.com/samaaron/sonic-pi/issues/1691
# modified by mb@mkblog.org
# Use: midi_chord chord(:c4, :major), sustain: 0.2
define :midi_chord do |notes, *args|
  if notes != :r
    notes.each do |note|
      midi note, *args
    end
  else
    play :r
  end
end

Hope this helps.

Martin

2 Likes