Skip to content

Instantly share code, notes, and snippets.

@madskjeldgaard
Last active February 1, 2024 12:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madskjeldgaard/c5731e95bc5be9b3e2789b14b1149b6e to your computer and use it in GitHub Desktop.
Save madskjeldgaard/c5731e95bc5be9b3e2789b14b1149b6e to your computer and use it in GitHub Desktop.
Tune Raspberry Pi for audio
#!/usr/bin/env sh
# Install utils for cpu freq
sudo apt-get install cpufrequtils
sudo cpufreq-set -r -g performance
sudo echo "ENABLE="true"
GOVERNOR="performance"
MAX_SPEED="0"
MIN_SPEED="0" " | sudo tee -a /etc/default/cpufrequtils
# Install other useful tools
sudo apt-get install htop git perl vim
# Set CPU governor
sudo sed -i 's/exit 0/sudo cpufreq-set -r -g performance/g' /etc/rc.local
sudo echo "exit 0" | sudo tee -a /etc/rc.local
# Set realtime priority and memlock
sudo echo "
@audio nice -15
@audio - rtprio 90 # maximum realtime priority
@audio - memlock unlimited # maximum locked-in-memory address space (KB)
" | sudo tee -a /etc/security/limits.conf
# Set swappiness
# This setting changes the so-called swappiness of your system,
# or in other words, the moment when your system starts to use its swap partition.
sudo echo "
vm.swappiness = 10
fs.inotify.max_user_watches = 524288
" | sudo tee /etc/sysctl.conf
@scientificsound
Copy link

scientificsound commented Jun 21, 2020

Hi Mads,

First off, thanks so much for your guide to setting up a RPI 4 for audio work. I'm working on a multichannel installation, and it's been super helpful so far. I've just run into one issue, and I was hoping you could point me in the right direction...

I'm attempting to run supercollider headless with a cheap 8 channel usb audio interface, but I can only get sound out of channels 0 & 1, nothing from 2-7. I've verified that all 8 channels are recognized by the Pi using speaker-test -t pink -c8 -Dhw:1,0 but adding a -o8 flag to ~/.jackdrc has no effect when I run the test.scd program that you have in the blog post. The JackDriver is still booting with 2 ins and 2 outs even though, as you can see below, the call to create the ALSA driver is with 0 ins (I guess that's the default b/c I didn't change it in the file) and 8 outs.

...

audio_reservation_init
Acquire audio card Audio1
creating alsa driver ... hw:1|hw:1|256|3|44100|0|8|nomon|swmeter|-|32bit
configuring for 44100Hz, period = 256 frames (5.8 ms), buffer = 3 periods
ALSA: final selected sample format for capture: 16bit little-endian
ALSA: use 3 periods for capture
ALSA: final selected sample format for playback: 16bit little-endian
ALSA: use 3 periods for playback
JackDriver: client name is 'SuperCollider'
SC_AudioDriver: sample rate = 44100.000000, driver's block size = 256
JackDriver: connected  system:capture_1 to SuperCollider:in_1
JackDriver: connected  system:capture_2 to SuperCollider:in_2
JackDriver: connected  SuperCollider:out_1 to system:playback_1
JackDriver: connected  SuperCollider:out_2 to system:playback_2
SuperCollider 3 server ready.
JackDriver: max output latency 17.4 ms

...

I'm guessing that system:playback_3-8 just don't exist by default; do you know what file I have to edit to change the configuration? Whenever I've used Jack in the past, I setup the number of channels directly using qjackctl, but that doesn't seem to be an option with the headless operation. I've been searching all day and can't seem to sort this out, so any help you could provide would be greatly appreciated! Thanks.

EDIT:
jack_lsp returns the following where the number of system playback outputs is determined by the -o8 flag:

pi@raspberrypi:~ $ jack_lsp 
system:capture_1
system:capture_2
system:playback_1
system:playback_2
system:playback_3
system:playback_4
system:playback_5
system:playback_6
system:playback_7
system:playback_8
SuperCollider:in_1
SuperCollider:in_2
SuperCollider:out_1
SuperCollider:out_2

and I can connect to additional outputs using pi@raspberrypi:~ $ jack_connect SuperCollider:out_2 system:playback_3, but I'm not sure how to edit the default number of output busses created by supercollider in headless operation mode...

cheers,
-eric

@madskjeldgaard
Copy link
Author

madskjeldgaard commented Jun 21, 2020

Hi Eric
Thanks for your message. It seems like you have done everything correctly. The only missing part of the puzzle is to tell the SuperCollider server to boot with the amount of channels you need. By default it will be set to 2 but you can set it to 8 or any arbitrary number and then use a multi channel panning UGEN by changing the test file's code to something like:

(
// Change number of hardware channels
~numChannels = 8;
s.options.numOutputBusChannels_(~numChannels);

// Boot server and play sound
s.waitForBoot{
	play{ PanAz.ar(~numChannels, PinkNoise.ar(0.5), LFSaw.kr(0.01)) }
}
)

Hope that helps!

@scientificsound
Copy link

Hi Mads,
That worked, thanks so much!

That shoulda been obvious I suppose, I was slowly getting there... ha

cheers,
-eric

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment