# Include functions in multiple projects

**URL:** <https://in-thread.sonic-pi.net/t/include-functions-in-multiple-projects/1683>\
**Category:** Support, Help & Resources\
**Created:** [November 11, 2018, 2:59pm UTC](https://in-thread.sonic-pi.net/t/include-functions-in-multiple-projects/1683 "2018-11-11T14:59:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tttgrosser](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tttgrosser](https://in-thread.sonic-pi.net/u/tttgrosser)\
**Post date:** [November 11, 2018, 2:59pm UTC](https://in-thread.sonic-pi.net/t/include-functions-in-multiple-projects/1683/1 "2018-11-11T14:59:01Z")

</div>

Hey, I’ve looked around a bit whether my questions was asked before, but I couldn’t see it … so …

I’ve written some functions of my own which I’d like to use in multiple projects. So, I save those functions to a dedicated file `MyFuncs.rb`. Now I’m trying to include or load that fie.

Sounds easy, but how to do that?

- using `load_buffer /path/to/MyFuncs.rb` -\> this will completely replace the current buffer - not what I want.
- using Ruby’s native `load` or `require` -\> this will lead to error "undefined method `define`". I guess the included file does not know about the SonicPi system and its functions.
- In one project, it says "GUI Error: buffer full … use `load_file`", but also there’s no such function `load_file`. So how can I break things up properly?

---

<div class="post-metadata">

**Author:** ![Martin](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/martin/32/157_2.png) [@Martin](https://in-thread.sonic-pi.net/u/Martin)\
**Post date:** [November 11, 2018, 3:46pm UTC](https://in-thread.sonic-pi.net/t/include-functions-in-multiple-projects/1683/2 "2018-11-11T15:46:23Z")

</div>

One way is to use

```ruby
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:

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

```

In my case it contains e. g.:

```ruby
# 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

---

<div class="post-metadata">

**Author:** ![tttgrosser](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tttgrosser](https://in-thread.sonic-pi.net/u/tttgrosser)\
**Post date:** [November 11, 2018, 6:23pm UTC](https://in-thread.sonic-pi.net/t/include-functions-in-multiple-projects/1683/3 "2018-11-11T18:23:32Z")

</div>

Thanks Martin, that’s what I needed! 🙂

---

<div class="post-metadata">

**Author:** ![Martin](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/martin/32/157_2.png) [@Martin](https://in-thread.sonic-pi.net/u/Martin)\
**Post date:** [November 11, 2018, 7:39pm UTC](https://in-thread.sonic-pi.net/t/include-functions-in-multiple-projects/1683/4 "2018-11-11T19:39:56Z")

</div>

Just a remark. I am working still on my [LiveLooper](https://github.com/mbutz/sonic-pi-live-looper) project (incorporating the [Arturia BeatStep](https://controllers.cc/beatstep), which is a nifty device). I had some strange latencies, which matter, because I am recording live and need to play back these samples in time.

I observed, if I put all code in the main buffer (without using the `run_file` command) I do not have these latencies or at least very much reduced and correctable with `time_warp`. This might be due to poor code (or code organisation) but if you encounter a similar issue I’d be gratefull to hear about it.

Martin
