karlstav / cava

Cross-platform Audio Visualizer
MIT License
4.12k stars 231 forks source link

Start CAVA at boot #110

Closed AlessandroFerri closed 8 years ago

AlessandroFerri commented 8 years ago

how can I start CAVA at boot, for only raw ascii output on pipe file? I tried it with a standard init script, but I did not succeed.

karlstav commented 8 years ago

I'm affraid I don't know. But the raw output mode should perhaps include an option to run as daemon.

Only thing i can think of is to make sure that cava starts after pulseaudio.

AlessandroFerri commented 8 years ago

Thank you for your answer. First, if there is an option to run cava as a daemon, where I can find it? I'm working with a raspberry Pi2 and old Wheezy Kernell 3.18.16 operating system. I have installed pulseaudio, but I do not need, because I run cava with config file:

## Configuration file for CAVA. Default values are commented out.  Use either ; or # for commenting.

[general]

# smoothing mode, can be normal, scientific or waves.
; mode = normal

# Framerate Default: 60. Accepts only non-negative values.
; framerate = 60 

# Sensitivity in  %. If you think the bars are to low/response is to little, try
# increasing this value 200 means double height. Accepts only non-negative values.
sensitivity = 2000

# Autosens will attempt to decrease sensitivity if cava peaks. 1 = on, 0 = off
; autosens = 1

# The number of bars (0-200). 0 (default) sets it to auto (fill up console).
# width of bars and space between bars in number of characters. Default width 3 and space 1.
bars = 10
bar_width = 1
bar_spacing = 0

# Lower and higher cutoff frequencies for lowest and highest bars
# the bandwith of the visualizer, defaults to 50 - 10000 Hz
# Note: there is a minimum total bandwith of 43Mhz x number of bars. 
# Cava will automatically increase the higher cuttoff if a too low band is specified.
lower_cutoff_freq = 50 
higher_cutoff_freq = 15000 

[input]

# method of capturing audio, possible methods are: 'pulse', 'alsa' or 'fifo'.
# Defaults to 'pulse', 'alsa' or 'fifo', in that order, dependent on what support cava was built with
#
# All input methods uses the same config variable 'source' 
# to define where it should get the audio.
#
# for pulseaudio 'source' will be the source. Default: 'auto', which uses the monitor source of the default sink
# (all pulseaudio sinks(outputs) have 'monitor' sources(inputs) associated with them)
# 
# for alsa 'source' will be the capture device. Default: 'hw:Loopback,1'
# for fifo 'source' will be the path to fifo-file. Default: '/tmp/mpd.fifo' 
; method = pulse
; source = auto

method = alsa
source = hw:Loopback,1

; method = fifo
; source = /tmp/mpd.fifo

[output]

# ouput method may be ncurses, noncurses or raw. noncurses is for systems that does not suport ncurses. 
# raw is a 16 bit data stream of the bar heights that can be used to send to other applications
# raw defaults to 200 bars, can be adjusted in the bars option above
; method = ncurses
method = raw

# visual styles, may be 'stereo' or 'mono'. 
# Stereo mirrors both channels with low frequencies in center.
# Mono averages both channels and outputs left to right lowest to highest frequencies
style = mono 

# raw output target defaults to stdout, a fifo will be created if target does not exist
; raw_target = /dev/stdout
raw_target = /home/pi/Test

# raw data format, can be binary or ascii
data_format = ascii

# binary bit format, can be '8bit' (0-255) or '16bit' (0-65530) 
; bit_format = 16bit

# ascii max value, in ascii mode range will run from 0 to value specified here
ascii_max_range = 256

# ascii delimiters, in ascii format each bar and frame is separated by a delimiters.
# use decimal value in ascii table (i.e. 59 = ';' and 10 = '\n' (line feed)) 
; bar_delimiter = 5
; frame_delimiter = 10

[color]

# supported colors are: red, green, yellow, magenta, cyan, white, blue, black.
; background = black
; foreground = cyan

[smoothing]

# multiplier for the integral smoothing calculations. Takes values from 0 - 0.99.
# Higher values means smoother, but less precise. 0 to disable.
integral = 0.7

# disables or enables the so-called "Monstercat smoothing". Default: 1. Set to 0 to disable.
monstercat = 0

# Set gravity multiplier for "drop off". Higher values means bars will drop faster. 
# Accepts only non-negative values. 0.5 means half gravity, 2 means double. Set to 0 to disable "drop off".
; gravity = 0.5 

# In bar height, bars that would have been lower that this will not be drawn.
; ignore = 0 

[eq]

# This one is tricky. You can have as much keys as you want. 
# remember to uncomment more then one key! More keys = more precision.
# Look at readme.md on github for further explanations and examples.
#1 = 1 # bass
#2 = 1
#3 = 1 # midtone
#4 = 1
#5 = 1 # treble

have tried various alternatives, which always runs after pulseadio, the more simple is to run etc/rc.local at boot with line:

sudo -u pi /.../cava -p /.../config
or
sudo -u pi /.../cava -p /.../config >/dev/null
or
sudo /.../cava -p /.../config
or
sudo /.../cava -p /.../config >/dev/null

but it does not work. Any suggestions that can be of help?

karlstav commented 8 years ago

There is no option to run it as daemon, it would be somthing that would needed to be created.

I'm no expert here, but does it need to be run as super user?

AlessandroFerri commented 8 years ago

Hi Karl, as I found these difficulties, I made a simple test. If I run from a terminal the command:

pi@Ani ~ $ sudo -u pi nice -n 10 /home/pi/cava-master/cava -p /home/pi/cava-master/example_files/config &
[1] 3468
pi@Ani ~ $ open file /home/pi/Test for writing raw output

cava was executed and running:

pi@Ani ~ $ ps axg | grep cava
 3468 pts/2    S      0:00 sudo -u pi nice -n 10 /home/pi/cava-master/cava -p /home/pi/cava-master/example_files/config
 3469 pts/2    SNl    0:01 /home/pi/cava-master/.libs/lt-cava -p /home/pi/cava-master/example_files/config

and I kill it with:

pi@Ani ~ $ sudo kill -9 3468
[1]+  Killed                  sudo -u pi nice -n 10 /home/pi/cava-master/cava -p /home/pi/cava-master/example_files/config

But if I create a simple bush script for example:

#!/bin/bash
#Thist is a Test script
echo " Start cava..."
sudo -u pi nice -n 10 /home/pi/cava-master/cava -p /home/pi/cava-master/example_files/config &
echo "cava started..."
exit 0

run with command: ./Test or sudo ./Test cava does not execute and no run. You have an idea why? maybe have I to indicate some library? Considering that I have no execute the install, that is when I built cava I have not run the command: make install

AlessandroFerri commented 8 years ago

I also wanted to ask you how I can disable pulseaudio during ./cofigure

karlstav commented 8 years ago

I did some testing with the script, it starts if you remove the &, but I guess that's no good. No idea why it does not work. I whould try to ask somewhere else.

The only way to disable pulseaudio from configure is to uninstall it fom your system.

AlessandroFerri commented 8 years ago

Hi Karl, after several tests I found the source of the issue. It seems the issue is originated somewhere in the initialisation of ncourse (variable om = 1,2,3) in the code cava.c, even when the config output is set to raw (om variable = 4). Now I have commented all the parts that do not relate to a raw output mode and now works properly, but this is not the right way to proceed. I will attack the cava.c file with my corrections that I made (only same line comment), so you can test it yourself. I leave to you the burden of correctly making changes to the code, so that it can be run as a daemon when the output mode is set to raw on pipe file. I hope to be available in the next release! cava.txt

karlstav commented 8 years ago

@AlessandroFerri cool that you figured it out.

Are you sure that everything of the stuff that you commented is necessary?

Most of it should not run anyway as long as output method is raw. The only thing that will run either way is the stuff around line 632 (checking to see if we are running in a virtual console). Actually this chunk of code should be moved down to after the config is loaded and only run if output method is not raw.

Can you try to uncomment everything except the stuff around line 632 and see if it still works?

AlessandroFerri commented 8 years ago

No, definitely not necessary to comment on everything that I have I commented. What I have ultimately commented, is necessary only for my project, and since I was there I have commented all the stuff that I not need. I will test what you asked me, and I will turn a bit around the issue.

AlessandroFerri commented 8 years ago

Hi Karl, you were right, I commented on the lines from the number 630 to the number 636 and, I do not know why, but everything works properly. Remains just this warning during the make:

cava.c: In function ‘main’:
cava.c:953:18: warning: ‘pr’ may be used uninitialized in this function [-Wuninitialized]
cava.c:1077:25: warning: ‘fp’ may be used uninitialized in this function [-Wuninitialized]
cava.c:981:10: warning: ‘fr’ may be used uninitialized in this function [-Wuninitialized]
cava.c: At top level:
cc1: warning: unrecognized command line option "-Wno-maybe-uninitialized" [enabled by default]

to anyone helpful, I attack the init script with which I run the daemon at boot. I remember that it is for the old SystemV. etc_init.d_cava.txt

karlstav commented 8 years ago

@AlessandroFerri try it now. Didn't actually mean to close this right away, but hopefully it will work now.

typo in the commit message also, typical.

AlessandroFerri commented 7 years ago

Hi Karl, I find another issue. I copy cava in /usr/local/bin and I start it as daemon. The file pipe now is not a ascii mode, indeed even seems that not even start the same program that I copied (/usr/local/bin/cava), but another and I do not know which one. I have the same issue if I start it with command: sudo /usr/local/bib/cava -p /etc/cava.conf I do not understand why, but if I do the make, after modifying cava, and then I copy cava: sudo cp cava /usr/local/bin/cava seems that the previous command does not share the same application. I have to open another issue?

karlstav commented 7 years ago

@AlessandroFerri I'm not following you 100% here.

You copy the binary to /usr/local/bin/ and it changes behavior? But you are still using the same config?

Why do you copy it manually and not use the sudo make install command?

AlessandroFerri commented 7 years ago

Hi Karl, I managed to run all, by running make install. No issue now, it seems all right. It also works without installing libncursesw5-dev. There are a big problem, cava uses a lot of CPU both with pulseaudio than with shairport-sync, is this normal? I send you a screenshot of top:

pi@Ani ~ $ top

top - 16:33:59 up 21 min,  2 users,  load average: 0.90, 0.86, 0.56
Tasks: 129 total,   1 running, 128 sleeping,   0 stopped,   0 zombie
%Cpu(s): 15.1 us,  7.1 sy,  0.6 ni, 77.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:    949332 total,   201508 used,   747824 free,    22780 buffers
KiB Swap:   102396 total,        0 used,   102396 free,    90824 cached

  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND           
 2176 pulse      9 -11  101m 6604 5440 S  54.1  0.7   3:32.63 pulseaudio        
 2352 root      20   0 18332 4492 3988 S  26.4  0.5   3:05.43 cava              
 2354 root      30  10 61028 1828 1628 S   5.6  0.2   0:26.77 LightSone         
 2719 root      20   0 28440  15m 7344 S   1.3  1.7   0:06.56 Xorg              
  364 root       0 -20     0    0    0 S   1.0  0.0   0:01.48 kworker/u9:1      
 4905 pi        20   0  4676 2508 2136 R   1.0  0.3   0:06.04 top               
  380 root      20   0     0    0    0 S   0.3  0.0   0:00.92 kworker/u8:2      
 1910 nobody    20   0  2024 1384 1268 S   0.3  0.1   0:00.05 thd               
 2086 root      20   0     0    0    0 S   0.3  0.0   0:01.15 RTW_CMD_THREAD    
 2843 pi        20   0 14664 9768 7888 S   0.3  1.0   0:00.75 openbox           
 2848 pi        20   0 97040  18m  16m S   0.3  2.0   0:03.31 lxpanel           
 4880 pi        20   0 90516  16m  14m S   0.3  1.8   0:02.36 lxterminal        
    1 root      20   0  2148 1312 1208 S   0.0  0.1   0:01.96 init              
    2 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kthreadd          
    3 root      20   0     0    0    0 S   0.0  0.0   0:00.52 ksoftirqd/0       
    5 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kworker/0:0H      
    6 root      20   0     0    0    0 S   0.0  0.0   0:01.09 kworker/u8:0      
pi@Ani ~ $
karlstav commented 7 years ago

yup, I have a similar setup with rpi2 and cava using pulseaudio and yes it does uses a lot of cpu.

You can try changing the resampling method in the pulseaudio configuration, I think that might help. Also reducing the framerate in the cava should reduce cpu usage.

AlessandroFerri commented 7 years ago

OK. I can not change the pulseaudio resampling method, I have a perfect sound quality. However I have the same issue with shairport. Where and how can I reduce the framerate in the cava? So I do some tests

AlessandroFerri commented 7 years ago

I found it and I did some tests. In fact it decreases, approximately CPU 15% with framerate = 30. Still high but still clear the mechanism. Have you tried using GPU_FFT library of Andrew Holme? You have found some benefit? If you have not tried, I could try, if you help me a little.

karlstav commented 7 years ago

the GPU_FFT looks brilliant, but it might require some work to get it done,You can create a separate issue for the work on GPU_FFT. And I can explain how the fftw is implemented in cava.

However this will not solve the cpu issue for pulseaudio...

AlessandroFerri commented 7 years ago

Ok, it is clear to me what you're suggesting. The CPU is absorbed by the while loop of the reading streaming. At each I repeat that even if on the screenshot of top that I sent you see pulse audio, the cava input is always:

method = alsa
source = hw: Loopback, 1

Also when I run the stream by pulseaudio, the resamplig method of pulseaudio is irrelevant, and the absorption of the CPU occurs equally with aplay and shairport.

karlstav commented 7 years ago

Are you using the combine module in pulseaudio to output the audio to cava and your audio device at the same time?

I know that the combine module will eat a lot of cpu. Have you tried to use pulseaudio to grab the audio directly instead? source should be detected automatically so just try to uncomment it:

method = pulse
;source = hw: Loopback, 1
AlessandroFerri commented 7 years ago

Hi Karl, sorry if I answer only now, however no, I not use the combine module of pulseaudio. Instead I configured ALSA in /etc/asound.conf, so that both shairport that pulseaudio flow on the same sound card and on this I made a loopback. That way pulseaudio, if well set-up, does not eat more CPU that if grap the audio card directly, as well as shairport works well, and also with cava I'll take the sound of both. From the various tests I noticed that cava always uses the same CPU in all different configurations. If you would like, I send you the asound.conf setup and pulseaudio system.pa scrip. It works perfectly. In the past I too was using the libraries pulseaudio (pulse.c) to calculate the FFT and did flow shairport on pulseaudio. But in this way I was losing the shairport-sync sync. In this new, I keep everything.

Let me know if you care to know.