spell-music / csound-expression

Haskell Framework for Electronic Music
320 stars 31 forks source link

PulseAudio: errors outputting any sound #57

Closed asheshambasta closed 4 years ago

asheshambasta commented 4 years ago

I'm on NixOS 19.09 and I'm thinking this is a system related issue; but in my case, any output doesn't work.

Repl:

λ> dac $ osc 300 
0dBFS level = 32768.0
--Csound version 6.13 (double samples) Jan  1 1970
[commit: none]
libsndfile-1.0.28
UnifiedCSD:  tmp.csd
STARTING FILE
Creating options
Creating orchestra
closing tag
Creating score
rtaudio: ALSA module enabled
rtmidi: ALSA Raw MIDI module enabled
Elapsed time at end of orchestra compile: real: 0.001s, CPU: 0.001s
sorting score ...
    ... done
Elapsed time at end of score sort: real: 0.001s, CPU: 0.001s
displays suppressed
0dBFS level = 1.0
orch now loaded
audio buffered in 256 sample-frame blocks
ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused

 *** Cannot open device 'default' for audio input: Connection refused
Failed to initialise real time audio input
inactive allocs returned to freespace
end of score.          overall amps:  0.00000
       overall samples out of range:        0
1 errors in performance
Elapsed time at end of performance: real: 0.726s, CPU: 0.004s
*Main Lib Csound.Base
λ> 

And I can see some logs related to PA:

Dec 15 16:27:54 quasar-nixos-tr pulseaudio[38142]: [pulseaudio] socket-server.c: bind(): Address already in use
Dec 15 16:27:54 quasar-nixos-tr pulseaudio[38142]: [pulseaudio] module.c: Failed to load module "module-esound-protocol-unix" (argument: ""): initialization failed.
Dec 15 16:27:54 quasar-nixos-tr pulseaudio[38142]: [pulseaudio] main.c: D-Bus name org.PulseAudio1 already taken.
Dec 15 16:27:54 quasar-nixos-tr pulseaudio[38139]: [pulseaudio] main.c: Daemon startup failed.

And I'm not sure how to debug this.

Any clues to what I might be doing wrong?


Edit: sound output works perfectly using other applications; Spotify, FF etc.

Another thing to note: I'm using a USB audio interface. I'm not sure if I need to specify special settings for that.


A follow up question: I'm assuming that csound talks to Alsa to route audio. Alsa routes this audio to some interface called 'default' on PA; which either doesn't exist or is not enabled. But in that case, the error message doesn't really agree with my hypothesis.

Is there something I can set in the options to route audio differently?

anton-k commented 4 years ago

Hi!

Maybe you have jack by default on your system. And Csound audio-engine expects it to be PulseAudio which is standard for most of the linux systems nowadays.

there are two routes to fix it.

1) To make Pulse audio work and check that Csound on your system can work with it. To check it out you can try out any Csound example to check that it works by itself (without CE). This one for example: http://www.csounds.com/manual/html/oscil3.html Try to copy file with example and invoke Csound on it.

2) Find out which flags of Csound work with your system and use them in CE. With this module: https://hackage.haskell.org/package/csound-expression-5.3.2/docs/Csound-Options.html. This module describes native Csound flags that are documented here http://www.csounds.com/manual/html/CommandFlagsCategory.html

anton-k commented 4 years ago

I configure audio with Cadence. It's nice tool to configure it with UI.

anton-k commented 4 years ago

I also use external USB audio card

anton-k commented 4 years ago

So the first step is to be sure that Csound can work on your system. Check it out with some example csound file. To invoke it you can run

csound oscil3.csd

Csound file to download. It plays pure sine waves:

www.csounds.com/manual/html/examples/oscil3.csd

asheshambasta commented 4 years ago

@anton-k Hi! First of all, thanks for replying and the wonderful library. I've managed to find the issue and I'll put my findings here in case anyone else stumbles on it.

So I'm using NixOS (and hence nix); and I started off by creating a stack project and adding the dependencies. I also added a shell-stack.nix file and enabled nix in stack; and in those dependencies I added the csound nixpkg to be pulled.

However, I noticed the following scenario: see my SO question for details.

And that led me to drill things down a bit further.

What was happening was:

  1. when I booted into the system and fired up stack ghci; stack was firing up a nix-shell using the --pure flag; therefore:
       --pure
           If this flag is specified, the environment is almost entirely cleared before the interactive shell is started, so you get an environment that more closely corresponds to the “real” Nix build. A few variables, in particular HOME, USER and DISPLAY, are retained. Note that ~/.bashrc and (depending on your Bash installation) /etc/bashrc are still sourced, so any
           variables set there will affect the interactive shell.

So any calls to csound that would eventually lead to looking for some sound server (PulseAudio), did not work. In a fresh session after a reboot; this spawned PulseAudio (needs confirmation if this was the case) but the rest of my system couldn't connect to it and was trying to spawn a new PA process.


The fix, in the end, was merely to have the option nix.pure = false set in stack.yaml.

That retains the environment variable that lets the processes discover PA and hence connect to an already spawned instance instead of attempting to spawn one, and failing.

anton-k commented 4 years ago

I'm glad that it works for you now! Wish you many happy musical discoveries!

iensu commented 3 years ago

Hi! Found this because I'm also getting issues with Connection refused from ALSA when running with --pure. Is there a way to allow access while keeping the --pure flag?

anton-k commented 3 years ago

I'm not aware on NixOS details, but can I help somehow?

iensu commented 3 years ago

Not sure, I think it has to do with permissions etc set by NixOS so might be very specific to that environment :)