Filtering Samples by directory

I’ve been getting started with Sonic Pi for the past week and was looking for ways to make it easier to access my sample libraries I would typically use in music production.

I’ve found that I can pre-define functions to resolve to paths to get a similar experience I’ve seen Sam use in .sonic-pi/init.rb

def sample_lib

def tech

def berlin

However, these sample libraries are organized into many categorical subfolders. Sometimes the filenames do not contain the same organizational data as the directories. For example:

I’ve noticed that the documentation regarding managing sample libraries explicitly states that all of the filters explicitly operate against the file basename, without extension. (

I keep wishing I could filter by full path, instead of just File.basename such as:

sample tech, 'loops', 'bass', 1
sample berlin, 'top_loops', '_124_', 3

Being new to SonicPi in general, there may be excellent reasons for this to not be the default behaviour that aren’t clear to me. The SampleLoader class looks really straightforward, so I’m curious if it would be reasonable to add this type of option in?

It seems that an alternative would be to do something that returns a lambda with a custom filter:

def path_filter(*pths)
  ->(candidates) {
    pths.reduce(candidates) do |out, path|
      candidates.keep_if do |cand|
        cand.downcase.include? path.downcase

This allows me to use:

sample berlin, path_filter('loops', 'drum')

But would this be something that could be (or is) better supported by default?

As an aside, what’s in play that allows me to use #define in my init.rb to persist vars, but I can’t just define a var like berlin = "sample_path..."