New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux Audio and Sonic Pi #1109

Closed
soslug opened this Issue Mar 31, 2016 · 13 comments

Comments

Projects
None yet
8 participants
@soslug
Copy link

soslug commented Mar 31, 2016

I love Sonic Pi and it angers me when it doesn't play on my main Linux Desktop build, this being Linux Mint LTS which at the time of writing this issue is 17.3 and I am using 64Bit.

Now that is out of the way I thought I would spend some time explaining why (as best as I can understand it) people are having trouble running Sonic-pi on main Linux builds such as Linux Mint, Fedora, Debian and so on. The reason appears to be this, it is - the Sound Server you are using no one distribution is necessarily the same.

Sonic-Pi seems to use the ALSA Sound Server or wants too, and Linux Mint uses or is using Pulseaudio so conflicts arise. Now as sound servers go, both are very good at what they do, and pulseaudo seems to be the direction distro are heading, the problem as I see it is that Sonic-Pi fails to use the default sound server insisting it uses Alsa instead, perhaps because this is what supercollider uses.

For most of us using the one sound server system you can effectively refer to the Sonic-Pi INSTALL-LINUX.md documentation and this should be enough to get this going. As two sound servers applications cannot run at the same time you need to kill or suspend the one you are using in favour of running the other. Most Linux operating systems favoured using the ALSA sound server and may still do, but of late, I know Linux Mint uses PulseAudio.

Note - the above Installation as listed packages did not work on LinuxMint for me without me first installing the package build-essential, so if you get build dependency problems you should first try that.

sudo apt-get install build-essential

If you are running Linux Mint 17.3 LTS you are using PulseAudio as your default Sound Server which is incompatible with Sonic-Pi, an acceptable means to get things running is one of the following methods. All methods will require the pre installation of jackd on Linux Mint, Ubuntu and Debian you can use (sudo apt-get install qjackctl) this will install most all the necessary packages:-

Method 1
Do not use "sudo" or superuser level or root user it is not needed to run these commands but will require a console terminal window open and ready for use.

pulseaudio -k
jackd -R -d alsa -d hw:0

This first command kills pulseaudio sound server and replaces it with jackd, the jackd command may look a little goofy but can be interpreted thus:

jackd - the Sound server using the devices specified by (-d alsa & -d hw:0) linux sees these as addressable devices.
-R - this specifies Jack is to run in realtime mode for low audio latency.
-d - specifies the devices we are using, either software or hardware, we are using two devices "Alsa" and the default "hw" output device as it is possible to address a different Hardware output device we specifically use "hw:0" to use the default hardware where only one audio sound card on the motherboard is available.

Then launch Sonic-Pi gui or from the command line

After ending you session in Sonic-Pi, to start Pulseaudio again you can use the following linux command as non superuser (ie do not run as sudo)

pulseaudio --start

Method 2
pasuspender -- jackd -R -d alsa -d hw:0

This method has an advantage in that it suspends PulseAudio whilst Jackd is running, the rest is the same as the first method above.

Method 3
QJackCtl, this is a gui (graphical user interface) if this is installed it is located on Menu under Sound & Video on Linuxmint anyway. Running Qjackctl looks very scary but on systems with one single hardware sound device it is really very simple. Usually it just requires you to launch qjackctl-gui from the Sound & Video menu and press start, then run Sonic-Pi. If you have problems running this, and you will know if you do you will get a flurry of message errors to the screen. Then you may still need to kill PulseAudio, press the Stop button or kill the qjackctl gui then with.

pulseaudio -k

This needs to run on the command line console there is no equivalent gui that you can use, then activate qjackctl from the menu and press start button for Qjackctl.

This covers the 3 basic methods for using Sonic-Pi after you have followed all the compile instructions for your build. You can find more help on Jack Audio here:-

http://linux.die.net/man/1/jackd

You might also want to build a startup bash script not only to setup jack but to start Sonic-Pi just copy this code below and save to a file in your own home directory.

#!/bin/bash

pulseaudio -k
killall jackd
/usr/bin/jackd -R -P40 -p128 -t5000 -dalsa -dhw:0 -r48000 -p2048 -n2 -S &
Downloads/sonic-pi/bin/sonic-pi
killall jackd

use chmod +x filename

This allows you to run the script without using a shell like "sh" in front ./filename
This is essentially no different to our examples above and if it works for you I would say use it, the one problem I have yet to address is that the above code works well, but does not exit the script cleanly which for me I have to use "control c" to terminate the script file properly.

So what if your Linux Desktop has more than one sound card and you specifically want the audio coming out of the device you are using. How can your Desktop have more than one sound card? Well if your Desktop is installed with something like an Nvidia Graphics card and you are using HDMI or DVI to HDMI then it may surprise you to know you actually have three sound hardware devices recognised by Linux which you can use independently to one another.

You can actually test this out for yourself, but first you need to know how to address your additional devices. We can get a rough idea by using "aplay" on the command line terminal console window.

Open a new terminal window and type the following:

aplay -l (this i lower case L by the way you can use capital L instead which has the effect of giving a more comprehensive list)
With "apaly -l" you might get a response something like this:-

**** List of PLAYBACK Hardware Devices ****
card 0: Intel [HDA Intel], device 0: STAC9200 Analog [STAC9200 Analog]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: NVidia [HDA NVidia], device 3: HDMI 0 [HDMI 0]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: NVidia [HDA NVidia], device 7: HDMI 0 [HDMI 0]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: NVidia [HDA NVidia], device 8: HDMI 0 [HDMI 0]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: NVidia [HDA NVidia], device 9: HDMI 0 [HDMI 0]
Subdevices: 1/1
Subdevice #0: subdevice #0

As you can see we have many more devices than we can actually use with 2 cards and 5 so called devices accessible. Lets first look at card 0 this refers to my internal sound card on my computers motherboard if i want to access this via jack I would use the following:-

jackd -R -d alsa -d hw:0

How can we test this well even if we use pulse audio we can still determine the hardware device to output from try this simple test.

speaker-test -c2 -twav (this uses the systems default sound card)
This is a repetitive test that will work over and over call a sample file to output to the selected audio device for front-left and front-right.

speaker-test - is the command
c2 - how many channels are attached usually two but if you may have surround so may have 8 so c8
twav - is a set predefined wav files used, you can:-

man speaker-test - for more info

We can also specify the exact sound hardware we want to use with "-D" on our command line string

speaker-test -c2 -Dhw:1,9 -twav

Breaking this down again

speaker-test - is the command
c2 - channels (if surround use c8)
Dhw:1,9 - card 1 device 9 which corresponds to a hdmi output channel I can use.
twav - predefinned wav files

You can now alter the same bash script above with this new hardware in mind

#!/bin/bash

pulseaudio -k
killall jackd
/usr/bin/jackd -R -P40 -p128 -t5000 -dalsa -dhw:1,9 -r48000 -p2048 -n2 -S &
Downloads/sonic-pi/bin/sonic-pi
killall jackd

use chmod +x filename

And everthing should work if it doesn't you might need to install

sudo apt-get install pulseaudio-module-jack

It may work without it but should not cause harm if installed or not please do not alter the default.pa file located in /etc/pulse any helpful comments would be appreciated I will do my best to answer any questions as they arise, relating to the above topic as and when I can. Feel free to answer any question under this topic.

@hzulla

This comment has been minimized.

Copy link
Collaborator

hzulla commented Mar 31, 2016

First of all, thanks for the very helpful writeup.

Here's my tl;dr-summary of my own research into the topic.

  • Sonic Pi uses Supercollider.
  • on Linux, Supercollider uses JACK.
  • on Linux, Sonic Pi starts JACK before starting Supercollider.
  • on Linux, neither Sonic Pi nor Supercollider use ALSA or PulseAudio at the moment.
  • Except for one hack: Sonic Pi contains hardcoded calls to ALSA's amixer to control the RPi soundcard volume on Raspberry Pi, bypassing JACK
    (my Debian packaging contains a patch to remove that call when not running on RPi)
  • I tried pulseaudio-module-jack, but it breaks the system's main volume slider on PulseAudio, so it didn't seem like a proper solution.
@hzulla

This comment has been minimized.

Copy link
Collaborator

hzulla commented Mar 31, 2016

I've been digging into the code of SuperCollider recently. It contains an alternative to the default JACK driver, using the PortAudio audio library. PortAudio can connect to ALSA and PulseAudio, but currently I get audio issues and buffer underruns using SuperCollider with PortAudio on Linux. There might be a fix for that, so we might get a working jackless version of Sonic Pi some time in the future.

@soslug

This comment has been minimized.

Copy link
Author

soslug commented Mar 31, 2016

Thanks for the feedback I also have tried pulseaudio-module-jack and indeed volume is problem, but one I can control via a remote, but yes you are correct none of the sliders for volume seem to work. Supercollider and Sonic-Pi may not use Alsa directly but Jack does in fact I cannot find any sound server that works better if at all, least not on my system.

@xavriley xavriley added the Linux label Apr 11, 2016

@THS-on

This comment has been minimized.

Copy link

THS-on commented Jul 12, 2017

I've created a little script to use Pulseaudio with Sonic Pi. It uses a alsa loopback interface with jack and streams that to Pulseaudio. I know this is a little hacky and introduces some latency but it's better than to have a user to fiddle with alsa and jack.

#!/bin/sh

sudo modprobe snd-aloop index=10 pcm_substreams=1 || true
jackd -d alsa -d hw:10 &
sleep 3
arecord -D plughw:10,1 -r 48000 -f FLOAT_LE -c 2  | aplay - &
sonic-pi
killall -9 jackd
killall -9 arecord
@ChiBia

This comment has been minimized.

Copy link

ChiBia commented Jul 23, 2017

Thank you THS-on! your script did the job for me. I've worked on running sonic-pi on my laptop for like a week with no success.

@samaaron

This comment has been minimized.

Copy link
Owner

samaaron commented Jul 23, 2017

Wonderful! I'll close this issue now - have fun :-)

@samaaron samaaron closed this Jul 23, 2017

@THS-on

This comment has been minimized.

Copy link

THS-on commented Jul 27, 2017

I think we should keep this issue open because there is no real integration of Sonic Pi with Pulseaudio and my script is only a workaround. Maybe this can documented somewhere outside this issue?
The problem is that a casual Linux user cannot use Sonic Pi "out of the box" without some knowledge about the audio system and jack. This is probably not going to change because of supercollider/supercollider#2848
Maybe when Portaudio adds the Pulseaudio hostapi this issue can be fully solved.

@MarzioPi

This comment has been minimized.

Copy link

MarzioPi commented Mar 21, 2018

Hi THS-on, your script would be very usefull for what am I doing. There are some packages necessary or something to modify to be able to use Pulseaudio through it? I am trying its usage with a raspberry pi3 with NOOBS but it seems impossible. I am not a master in linux usage, sorry if i’m bothering

@THS-on

This comment has been minimized.

Copy link

THS-on commented Mar 21, 2018

@MarzioPi for the RasPi you shouldn't need my script because Sonic Pi supports audio directly on the RasPi. What are you trying to achieve?

For using my script you need the a running and working Pulseaudio installation, jackd and the kernel module snd-aloop must be available.

@MarzioPi

This comment has been minimized.

Copy link

MarzioPi commented Mar 21, 2018

@THS-on thank you very much for your reply. I am working with sonic-pi and a software wich get datas spitted out from Pulseaudio. When I connect jack i can hear sound from sonic-pi but it seems not out from Pulseaudio, because in the software no data are read. But if i try with a youtube video it works perfectly. I have no ideas

@THS-on

This comment has been minimized.

Copy link

THS-on commented Mar 21, 2018

I think Sonic Pi starts it's own jackd server on Raspbian instead of using an existing one.
I don't know if aplay really uses pulseaudio or directly asla, so arecord -D plughw:10,1 -r 48000 -f FLOAT_LE -c 2 | aplay - & might not work on a RasPi and I can't test that currently.

@MarzioPi

This comment has been minimized.

Copy link

MarzioPi commented Mar 21, 2018

@THS-on Ok, thank you again for your reply. I think I’ll choose another way to pass through this issue. Thanks for your attention

@barneyElDinosaurio

This comment has been minimized.

Copy link

barneyElDinosaurio commented Apr 9, 2018

Iouuuuuuuuuuuuuuuuuu dude... this line was cool for me, just shut this line in the terminal...
usermod -a -G audio theusername
Add ur user in the audio group,that was my solution. Cheers!!!.

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