Spring 2023

Simple BASH Linux Sampler

 

Hi all, here is something that I thought I would share after setting up a sampling approach for my music life.

 

 

Soon, hopefully my group will be gigging, and one of the things that I need to be able to do as the keyboard player is play samples of some of what we do at the appropriate time.  We play some numbers with signature definitive sounds, and since often times, the original is best, sampling is the way to go.

 

 

Not having a sampling keyboard, and being a strong believer in modularity as an avid linux user, I thought that I would see what was out there.  I thought about using a windows sampler using the wine compatibility layer but opted against that before trying other options.  Fortunately, after learning about some of the offerings, I actually decided on a more home brew approach.  Simplicity is often better to achieve the desired results, as I had learned in the past with the Unix philosophy.

 

 

 

So, in pursuit of a linux sampler, I quickly noted a number of approaches, but in particular (have to love that phrase "in particular" from learning comp. sci days, didn't like it then but has started to grow on me since) ... found the Linux Sampler.....

 

Linux Sampler

 

 

speaking of comp. sci, I check out my former classmate and colleague's approach here....
Joy of Coding

hoping to team up with her company in the future.....

 

 

 

 

 

Anyway, regarding the linux sampler, seeing that some of the project hasn't been maintained in a while, I tried a quick install, but with dependency issues and some compiler directives missing, I wasn't ready to try this on my production Budgie Linux main machine.  I also didn't feel like breaking out a VM at the moment.  I might return to this later.... but lol not today.

 

So after some further thought, I opted for a shell based solution.... which does exactly what I need it to do.

 

 

The problem is the need to play both waves and mp3s from the command line.  A quick search revealed a possible tool for this to be mpg123.  From the manual, it does this:

 

 

mpg123(1)                                                                                   General Commands Manual                                                                                   mpg123(1)

 

NAME

       mpg123 - play audio MPEG 1.0/2.0/2.5 stream (layers 1, 2 and 3)

 

SYNOPSIS

       mpg123 [ options ] file-or-URL...

 

DESCRIPTION

       mpg123  reads  one  or more files (or standard input if ``-'' is specified) or URLs and plays them on the audio device (default) or outputs them to stdout.  file/URL is assumed to be an MPEG audio bit

       stream.

 

OPERANDS

       The following operands are supported:

 

       file(s) The path name(s) of one or more input files.  They must be valid MPEG-1.0/2.0/2.5 audio layer 1, 2 or 3 bit streams.  If a dash ``-'' is specified, MPEG data will be read from the standard in‐

               put.  Furthermore, any name starting with ``http://'' is recognized as URL (see next section).

 

OPTIONS

       mpg123  options may be either the traditional POSIX one letter options, or the GNU style long options.  POSIX style options start with a single ``-'', while GNU long options start with ``--''.  Option

       arguments (if needed) follow separated by whitespace (not ``='').  Note that some options can be absent from your installation when disabled in the build process.

 

 

Perfect!  This is exactly what’s needed.  After a quick search and install

 

sudo apt-cache search mpg123

sudo apt-get install mpg123

 

I had it up and running, and tested with some mp3s I already had.  It is great that this handles mp3s so nicely whereas some other approaches require the conversion to wav….

 

So now I had a simple player… but how to link it with a keyboard listener?

 

… well a quick search for that… and the solution is “trivial” in comp. sci. parlance:

 

 

 

 

A quick adaptation of this to a bash script allows calls to play individual files.

 

 

Stack Overflow Respond to keypress

 

 

 

One last issue with it though…. how do get it to fire the samples faster than they would otherwise play in waiting to complete.  Some command line approaches for this or setting the terminal timeout are available, but a far better solution exists… simply run the command in the background and let the OS take care of the scheduling…. as it should be…  So now we have:

 



Couldn’t be simpler….. testing reveals that it works great, and and pretty much an unlimited number of sample configurations are available with assignment of samples to any key on the keyboard.

 

 

In the words of the Cars… Just What I Needed :)