[Draft] Audio Shortlinks


Note: this post is a work-in-progress.

There are about a zillion ways to quickly create a shortlink to any piece of content on the web. But if I wanted to quickly link to an audio clip which may or may not live on the internet? Not so easy! The workflow probably looks like:

  1. Acquire the audio file
  2. Crop it down to what you want to share in Garageband or whatever
  3. Upload it to a filesharing service that can host audio files
  4. Shortlink to the hosted file

Kind of a pain! It’d be much more convenient if could do the following:

  1. Hit a keyboard shortcut to record whatever is coming through our speakers and/or microphone.
  2. When we’re finished recording, the clip is automatically available online.

This scenario spontaneously appeared in my brain recently, and so I figured I’d try to hack together a quick solution that made this fast and easy.

First let’s try the approach suggested on this superuser thread.

You can use Soundflower to direct the output audio to SoX.

Install Soundflower, then go to “System Preferences > Sound”, and set Soundflower (2ch) as both the output device and input device. Then record 10 seconds using SoX, e.g. as suggested in this question and answer thread.

To switch to Soundflower as output/input device (and back) you can use the switchaudio-osx tool, which you can install via Homebrew.

Use the commands:

SwitchAudioSource -s 'Soundflower (2ch)' -t 'output'

SwitchAudioSource -s 'Soundflower (2ch)' -t 'input'

Using Soundflower, SoX, and switchaudio-osx together, it should be possible to write a script to do the whole process, with an argument for specifying the length of the recording.

This worked! I recorded 10 seconds of Once in a Lifetime. Check it out.

Exciting! But still plenty to be desired. Number one, we can’t hear the speaker output while we’re recording because the audio output is directed to Soundflower, not our speakers. This makes it really tough to identify the clip that we’d like to record. Number two, it’d be pretty cool if we could record from both the built-in mic and the audio output at once.

The first issue can be fixed in OS X by creating a MIDI Multi-output device, adding both Soundflower and the built-in headphone jack, and setting that as the default output source. The second issue can be fixed by using sox to merge the audio inputs from the built-in microphone and from Soundflower:

$ rec -V6 -m -t coreaudio "Soundflower" \
  -b 32 -e unsigned-integer -r 44.1k -c 2 --buffer $((44100*2*11)) \
  --clobber /tmp/soxrecording.wav trim 0 11

With this command, David Byrne and I were able to record a duet. (The mix with my built-in lapotp mic makes this sound pretty crappy. It’s probably possible to make this better with some of the built-in sox effects.)

So we have all the client-side tools for our audio shortlinker. We just need to figure out how to create the multi-output device programmatically, then package everything up. Dave Addey wrote a blog 12 years ago which describes how to create aggregate devices using the CoreAudio APIs.

To be continued…


comments powered by Disqus