naztronaut / dancyPi-audio-reactive-led

Real-time WS2812b LED strip music visualization using Python and the ESP8266 or Raspberry Pi.
https://www.easyprogramming.net/raspberrypi/audio_reactive_holiday_leds.php
MIT License
243 stars 86 forks source link

No audio detected #35

Open lucafossen opened 3 years ago

lucafossen commented 3 years ago

Hi, I'm trying to run the no_mic version and I'm running into some issues. Here is the log:

pi@raspberrypi:~/dancyPi-audio-reactive-led/python $ sudo python3 visualization.py scroll ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port ALSA lib pcm_a52.c:823:(_snd_pcm_a52_open) a52 is only for playback ALSA lib conf.c:5014:(snd_config_expand) Unknown parameters {AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0} ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0} ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card ALSA lib pcm_dmix.c:1043:(snd_pcm_dmix_open) The dmix plugin supports only playback stream ALSA lib pcm_dmix.c:1043:(snd_pcm_dmix_open) The dmix plugin supports only playback stream ALSA lib pcm_dmix.c:1043:(snd_pcm_dmix_open) The dmix plugin supports only playback stream ALSA lib pcm_dmix.c:1086:(snd_pcm_dmix_open) unable to create IPC shm instance Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock No audio input. Volume below threshold. Volume: 9.998333e-17 FPS 40 / 50 No audio input. Volume below threshold. Volume: 0.0

It then just repeats the last line about volume being 0.0 forever, with 33-36 FPS logs in between. Is something not installed? Thanks for any help :)

VonHirsch commented 3 years ago

is the no_mic version complete? (ok if it's not) I diffed it against master and I see that edit_alsa_conf() is commented out of install and edit_modules() was added. Looks like a loopback. Is some additional config needed to route properly?

lucafossen commented 3 years ago

is the no_mic version complete? (ok if it's not) I diffed it against master and I see that edit_alsa_conf() is commented out of install and edit_modules() was added. Looks like a loopback. Is some additional config needed to route properly?

I followed this: https://www.easyprogramming.net/raspberrypi/audio_reactive_holiday_leds.php it doesn't mention that edit_alsa_conf() is commented out. I'll check out what it does and see if it works!

naztronaut commented 3 years ago

Hey there, the no_mic version is complete, but you can consider it a version 1.0 since I still want to make it headless. The reason some things are commented out is because I'm trying to make the "mic" toggleable so that you can choose to use a mic or no mic during install or runtime. Once I have that done, I'll merge it into the master branch.

If you run the edit_alsa_conf() function in the no_mic version, the lights won't work at all because the alsa config is edited to use a different audio device that is not the virtual device. I should have mention why it was commented out in the tutorial, but in my mind, it's temporary until I get the toggle working.

The function also creates a backup of the alsa.conf file, you can restore it pretty easily.

A couple of questions for @lucafossen:

Just want to make sure things were installed properly.

Others reported similar issues and sometimes, reinstalling everything from scratch helped and other times, nothing worked. I reinstalled the no_mic version probably 8-10 times between my Pi 3 and Pi 4 and I can get it to run without issues every time. I usually follow my own videos to make sure I gave proper instructions. I'll keep trying to replicate and share a fix if I can get there.

VonHirsch commented 3 years ago

Ok thanks for that. I'm assuming the no_mic version allows one to feed the normal pi sound output (say HDMI) to the visualization. Is that correct?

naztronaut commented 3 years ago

Yeah that's correct, except I can't get it to work with HDMI or the 3.5mm audio jack output. This only works with Bluetooth and USB Speakers.

It might be possible to get this to run with HDMI output with some adjustments but the 3.5mm audio jack definitely won't work because there's some hardware/software limitation with the Pi (can't use that jack with a virtual mic).

VonHirsch commented 3 years ago

ok thanks, i'll look into it. If I find something working for HDMI i'll report back

explorerswb commented 3 years ago

I seem to be having the same problem. Raspberry Pi 4B 2gb, full version of the OS, followed the install video step by step, but when I run it I get this:

pi@raspberrypi:~ $ sudo python3 dancyPi-audio-reactive-led/python/visualization.py scroll ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port ALSA lib pcm_a52.c:823:(_snd_pcm_a52_open) a52 is only for playback ALSA lib conf.c:5014:(snd_config_expand) Unknown parameters {AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0} ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0} ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card ALSA lib pcm_dmix.c:1043:(snd_pcm_dmix_open) The dmix plugin supports only playback stream ALSA lib pcm_dmix.c:1043:(snd_pcm_dmix_open) The dmix plugin supports only playback stream ALSA lib pcm_dmix.c:1043:(snd_pcm_dmix_open) The dmix plugin supports only playback stream ALSA lib pcm_dmix.c:1086:(snd_pcm_dmix_open) unable to create IPC shm instance Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock No audio input. Volume below threshold. Volume: 9.996934e-17 No audio input. Volume below threshold. Volume: 0.0 No audio input. Volume below threshold. Volume: 0.0 No audio input. Volume below threshold. Volume: 0.0 No audio input. Volume below threshold. Volume: 0.0 No audio input. Volume below threshold. Volume: 0.0 No audio input. Volume below threshold. Volume: 0.0 FPS 36 / 50 No audio input. Volume below threshold. Volume: 0.0 No audio input. Volume below threshold. Volume: 0.0 No audio input. Volume below threshold. Volume: 0.0

I'm using a Soundcore 2 Bluetooth speaker, and accessing the pi through VNC Viewer. I've tried using VLC, mplayer, and the web browser. In all cases, the sound plays through the speaker, but the lights remain unlit. My LEDs are a strand of 50 WS2811s with a separate 10A power supply, and they function properly when I run strandtest.py

More info:

pi@raspberrypi:~ $ cat /proc/asound/cards 0 [Loopback ]: Loopback - Loopback Loopback 1 1 [b1 ]: bcm2835_hdmi - bcm2835 HDMI 1 bcm2835 HDMI 1 2 [Headphones ]: bcm2835_headphonbcm2835 Headphones - bcm2835 Headphones bcm2835 Headphones

pi@raspberrypi:~ $ cat /etc/modules /etc/modules: kernel modules to load at boot time. This file contains the names of kernel modules that should be loaded at boot time, one per line. Lines beginning with "#" are ignored. i2c-dev snd-aloop

pi@raspberrypi:~ $ arecord -l List of CAPTURE Hardware Devices card 0: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7 card 0: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7

pi@raspberrypi:~ $ aplay -l List of PLAYBACK Hardware Devices card 0: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7 card 0: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7 card 1: b1 [bcm2835 HDMI 1], device 0: bcm2835 HDMI 1 [bcm2835 HDMI 1] Subdevices: 4/4 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 card 2: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones] Subdevices: 4/4 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3

I've tried everything I can think of, including starting from scratch with a fresh OS several times. I'm at a loss at this point. Any help would be greatly appreciated!

naztronaut commented 3 years ago

Thanks for all the info @explorerswb! your setup looks exactly like mine (including the RPi 4 2gb).

I blocked some time this coming weekend to go through my own video and redo everything and I'm hoping I can replicate it. Maybe something changed with Raspberry Pi OS since I did this in December because the frequency of reports like this has gone up.

I'll record the set up as well and go through it from scratch.

explorerswb commented 3 years ago

On a whim, I downloaded both the 2020-12-02 and 2020-08-20 versions of Raspberry Pi OS Full. I didn't update anything before running the install script, and the results were the same. So if this problem is the result of a software update, it would seem to be in one of the packages downloaded by the install script.

I am also planning to try installing this on my RPi 3 B+ just to see if anything different happens, but that will have to wait for the weekend.

VonHirsch commented 3 years ago

Hey guys, just an update from my end - I'm trying to use this config:

"Record audio from an application while also routing the audio to an output device" https://trac.ffmpeg.org/wiki/Capture/ALSA#Recordaudiofromanapplication

But it doesn't appear to work. I saw at least one error message in syslog which led me to update the Pi4's firmware (see here) but that didn't resolve the issue.

I'm still an alsa newbie, so I'm going to keep digging in to see what other audio mixing mechanisms are available (and working) on raspberry pi.

lucafossen commented 3 years ago

Hi @naztronaut , thanks for the reply.

i2c-dev pi@raspberrypi:~ $ arecord -l List of CAPTURE Hardware Devices card 0: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7 card 0: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7 pi@raspberrypi:~ $ aplay -l List of PLAYBACK Hardware Devices card 0: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7 card 0: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7 card 1: b1 [bcm2835 HDMI 1], device 0: bcm2835 HDMI 1 [bcm2835 HDMI 1] Subdevices: 4/4 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 card 2: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones] Subdevices: 4/4 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 pi@raspberrypi:~ $



I appreciate the time you take to help newbies like me!
lucafossen commented 3 years ago

I have installed the full OS now, still getting the same issue.

VonHirsch commented 3 years ago

@lucafossen sorry for hijacking your thread here, I considered opening a separate issue, but since it appears all of us here are cracking the same nut it made sense to keep posting on this thread. I hope you don't mind!

I was able to get dancyPi working without a mic, i'm posting the details below so @naztronaut can work this into his installer on the weekend.

First an important note about audio drivers on raspberry pi:

As of Decemeber 2020 Raspberry Pi OS now uses PulseAudio as it's primary instead instead of Alsa. This is important because all my testing has been done on a Pi4 using Raspberry Pi OS Lite headless over ssh and as far as I can tell the lite version still retains Alsa as it's primary, but if you install the desktop versions they probably include PulseAudio. I'm going to experiment with that later but for now I highly recommend you start with lite and go headless. This is the best way to narrow down any issues your having.

Also! Note that errors regarding jack server etc. are bogus and don't mean anything. They can be ignored.

Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started

Ok so here are the step-by-steps I used to get this working:

  1. Load fresh version of raspbian lite on the pi 4
  2. Add snd-aloop module to load at boot time (command on next step)
  3. sudo echo 'snd-aloop' >> /etc/modules
  4. reboot
  5. run aplay -l
  6. you will notice that card 0 is now the loopback device. at this point you won't get any sound bc nothing is hooked up
  7. sudo apt-get install -y git
  8. git clone https://github.com/naztronaut/dancyPi-audio-reactive-led.git
  9. we are NOT going to use the no_mic branch. this is intentional, stay on master
  10. sudo python3 dancyPi-audio-reactive-led/python/install/install.py
  11. make sure "USE_GUI = False" in python/config.py
  12. copy paste below config into /etc/asound.conf (replacing anything that's there) I've included tons of comments to explain what's going on.
###################################################
#  /etc/asound.conf
# This config will route (split actually) your default audio output to:
#       1 - your speakers (pcm.output)
#       2 - your default microphone input (capture.pcm)
#
# To enable loopback channels neccessary for this:
#       sudo echo 'snd-aloop' >> /etc/modules
#       reboot
#       loopbacks will now be visible in aplay -l
#
# Loopbacks simply loopback from one to the other bi-directionally
#   e.g.
#       hw:Loopback,0,0 <--> hw:Loopback,1,0
#
#       Anything dumped in one end will come out the other
#       By default snd-aloop creates 8 of these loopback pipes for you
#           
# Below the "Set audio pcm defaults" section will:
#       output audio by default to slave.pcm "multi"
#       capture audio by default from slave.pcm "loopout"
#
# The "multi" slave pcm device will copy the buffer equally to "output" and "loopin"
#       output is set to whatever output card you want listed in aplay -l output
#       loopin loops back to loopout (the default capture device)
#
# To test play in one terminal:
#       aplay piano2.wav
# Record in another:
#       arecord -f S16_LE -c 2 -r 44000 audio.wav
#       aplay audio.wav (play back recording)
#
#  Good Luck
###################################################

# The "multi" slave pcm device will copy the buffer equally to "output" and "loopin"
pcm.multi {
    type route;
    slave.pcm {
        type multi;
        slaves.a.pcm "output";
        slaves.b.pcm "loopin";
        slaves.a.channels 2;
        slaves.b.channels 2;
        bindings.0.slave a;
        bindings.0.channel 0;
        bindings.1.slave a;
        bindings.1.channel 1;
        bindings.2.slave b;
        bindings.2.channel 0;
        bindings.3.slave b;
        bindings.3.channel 1;
    }

    ttable.0.0 1;   # copy source channel 0 to slave a, channel 0
    ttable.1.1 1;   # copy source channel 1 to slave a, channel 1
    ttable.0.2 1;   # copy source channel 0 to slave b, channel 0
    ttable.1.3 1;   # copy source channel 1 to slave b, channel 1
}

# Set audio pcm defaults
pcm.!default {
  type asym
   playback.pcm {
     type plug
     slave.pcm "multi"      # output audio by default to slave.pcm "multi"
   }
   capture.pcm {
     type plug
     slave.pcm "loopout"    # capture audio by default from slave.pcm "loopout"
   }
}

# This sets the default control options to use *hw* card #1
ctl.!default {
        type hw
        card 1
}

# Output Alias - set this to the desired output card from aplay -l
pcm.output {
    type hw
    card 1
}

# Loopback 0,0 alias "loopin" - anything put in here will loop back out "loopout" below
pcm.loopin {
    type plug
    slave.pcm "hw:Loopback,0,0"
}

# Loopback 1,0 alias "loopout"
pcm.loopout {
    type plug
    slave.pcm "hw:Loopback,1,0"
}

@naztronaut below is some code that might interest you. You might want to incoporate this into your installer, like maybe it could prompt them to choose? I used it to determine what input_device_index the pyaudio stream should listen to:

info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
    if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
        print("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name'))

To test this code you can paste it into microphone.py underneath the line "p = pyaudio.PyAudio()"

It will output something like this:

Input Device id  0  -  Loopback: PCM (hw:0,0)
Input Device id  1  -  Loopback: PCM (hw:0,1)
Input Device id  4  -  Sennheiser USB Headset: Audio (hw:3,0)
Input Device id  5  -  sysdefault
Input Device id  6  -  default

You will want to select the id corresponding to Loopback: PCM (hw:0,1) which is where the above config dumps out a copy of the default audio.

To set the input_device_index, edit microphone.py like so:

stream = p.open(format=pyaudio.paInt16,
                channels=1,
                rate=config.MIC_RATE,
                input=True,
                frames_per_buffer=frames_per_buffer,
                input_device_index=1)

With the above asound.conf and input_device_index you should be able to play sound through the default Alsa input, hear it on your output, and see it while running the visualizer against the loopback:

Terminal 1:
sudo python3 visualization.py scroll

Terminal 2:
wget http://www.kozco.com/tech/piano2.wav
aplay piano2.wav

See the comments in the asound.conf for more test commands.

If you want to use something like omxplayer, make sure you specify that it outputs to Alsa like so:

omxplayer -o alsa:default <file.mp3>

which demonstrates that any program you use must leverage the Alsa driver in order for this to work.

naztronaut commented 3 years ago

Amazing work @VonHirsch! I'm going to try all of this out and make necessary updates to the project.

It's interesting to read about Alsa and Pulseaudio. It's also funny how the update happened in Dec 2020. I think my installation is from the previous build so I never ran into this. And during reinstalls, the Pi Imager has just been installing a cached version of RPi OS.

Good to know.

Thanks! I'll let you know how my testing goes. Will try to do it this weekend but may need to move it to next weekend. I adopted a cat which is taking up a lot of my free time now :)

VonHirsch commented 3 years ago

@naztronaut thanks! Glad you like it. Oh yes don't worry about it, family and pets always come first! =)

naztronaut commented 3 years ago

Hey @VonHirsch!

Just wanted to let you know that I tried our your changes this weekend and unfortunately couldn't get it to work on headless mode. I can either play music through my speaker or have the lights dance, but not both at once. Everything is a fresh install and I followed your steps above.

This is the issue I was having with the no_mic branch and why I required using the desktop/GUI. I'll keep playing around with it and see if I can figure it out.

It's interesting that it works for you so I have hope that we can come up with a generic version of this app that works for everyone.

VonHirsch commented 3 years ago

Thanks for the update. On a similar note I've also found out that the configuration I put above is unreliable as the audio cuts out after a minute or so of running audio through the multi & loopback, and I can't seem to find any errors or messages in the logs.

At first I thought it might have something to do with running the LED's, but I get the same cut-out behavior even if I don't launch any dancyPi code. I thought about running the alsa driver from source (which appears to be the only way to get debug output?) but that didn't seem like a simple proposition.

The next thing I'm going to try is using pulseaudio to do the mixing. It's a more software / gui approach, but maybe if I can get it working in the gui I'll be able to extract the needed configs to run in headless mode.

p.s. did you update your pi4's firmware?

explorerswb commented 3 years ago

So I managed to get this working, reliably and reproducibly, but I'm honestly not entirely sure how. I was playing with setting up pulseaudio loopbacks via the pactl command, which seemed to work, but when I went back to reproduce it I accidentally tried testing the lights without those loopbacks and...it somehow still worked. So here are the steps to reproduce my (apparent) success. Hopefully someone with more experience with pulseaudio can explain why this works.

Start with a fresh install of Pi OS Full. Set it up for VNC (or use KVM). sudo git clone https://github.com/naztronaut/dancyPi-audio-reactive-led.git Don't switch branches. Next, edit install.py sudo nano dancyPi-audio-reactive-led/python/install/install.py Comment out the last two lines: #replace_asound() #edit_alsa_conf()

At this point, the install script only installs dependencies. Run it. sudo python3 dancyPi-audio-reactive-led/python/install/install.py

Edit config.py as necessary for your setup. Reboot, and connect via VNC.

Connect your speaker, and select it from the audio menu. I used a Soundcore 2 via Bluetooth. Run the script. sudo python3 dancyPi-audio-reactive-led/python/visualization.py scroll Play some music. You're done.

No, really. That's all I did, and it seems to be working like a charm. I tested it with VLC, aplay, pianobar, the web browser...it just works, and I have no idea why.

VonHirsch commented 3 years ago

@explorerswb I was able to reproduce your results! Good lookin' out. I guess we (or maybe just me!) were overthinking this situation :D I'm not sure why it works either because arecord -l shows no devices. I added the code to have pyaudio list out the devices it sees and I got this:

Input Device id  6  -  pulse
Input Device id  10  -  default

Then I set the input_device_index to each one and both worked, so i'm assuming "default" is just an alias to "pulse" ... at the moment I don't know much at all about pulseaudio, guess i'll have to read up about why pyaudio can read this stream so easily, but yea, it's good!


On another note, before you posted I was also poking around with pulsesaudio. I started with the intermediate raspian image that comes with a desktop but not "recommended software." It didn't work out of the box (at least I don't think so) but I got the led's going by adding null sinks (a loopback basically?) and enabling simultaneous audio in paprefs as described here. After that dancypi worked with the default settings (I also commented out the replace_asound() and edit_alsa_conf() lines in install.py for these experiments)

Next step for me is to see if I can get RetroPie and pulseaudio working together. I'm not sure if that will be challenging or not.

naztronaut commented 3 years ago

Say what @explorerswb? This works without the long asound.conf file? I'll have to try it out this weekend. I wonder if the changes to alsa and pulseaudio in December did something because I tried this exact set up months ago and it wouldn't work without setting up the loopback device.

Who knew, I should have just waited another month to update my tutorial, would have been way easier. Thanks for t he tip!

Good luck with Retropie & pulseaudio @VonHirsch! Maybe it'll be easier than you think :)

D500844 commented 3 years ago

Hey guys, was hoping to try and get this to work for a show this upcoming weekend, but the audio input isn't working since the last time I tried this project- I get the same errors when I try to run visualizations as the ones written in the original post. I don't really need the LEDs to play off anything else and didn't need it to run in headless mode, but the old version of the tutorial did work with the mic and the new one doesn't is what I'm gathering?

I tried doing both tutorials though and I was unable to solve anything. The mic functionality seems to have disappeared and it was all I really needed.

naztronaut commented 3 years ago

Hi @D500844 - the mic function is still there. The Master branch hasn't been touched Are you getting any errors? The mic version requires you to specify the hardware in asound.conf: https://github.com/naztronaut/dancyPi-audio-reactive-led/blob/master/python/install/asound.conf

So make sure you have the correct hw selected. You can confirm the number with this command:

cat /proc/asound/cards
D500844 commented 3 years ago

I do have the same specifications in asound.conf as described in that link, the results of cat /proc/asound/cards is as follows;

0 [b1 ]: bcm2835_hdmi - bcm2835 HDMI 1 bcm2835_HDMI 1 1 [Headphones ]: bcm2835_headphonbcm2835 Headphones - bcm2835 Headphones bcm2835 Headphones

The error I get when I run the visuals is No audio input. Volume below threshold. Volume: 0.0 but the mic is plugged in. I've noticed it works if I run audio through the pi but it knocks the fps down to 21. The actual LED strip looks like its changing once every ~3 seconds. Barely running. Without the mic being the source of audio though I am out of shape.

naztronaut commented 3 years ago

Is your mic installed correctly? It's not on the list. By default, the install script will use hardware 1 as the default input device, in your case it's the headphones which isn't an input device.

Try plugging in your mic, restart your pi, then run that command again and see if your mic shows up.

If it does appear and it's not listed as hardware 1, edit /etc/asound.conf and change the hardware from 1 to whichever number it's on.

D500844 commented 3 years ago

I'm using the Fifine USB K669, after plugging in and restarting same result;

0 [b1 ]: bcm2835_hdmi - bcm2835 HDMI 1 bcm2835_HDMI 1 1 [Headphones ]: bcm2835_headphonbcm2835 Headphones - bcm2835 Headphones bcm2835 Headphones

gonna reinstall OS. *New Edit: I Believe I have it working now, OS install issue- sorry for the trouble but thank you for the help!

naztronaut commented 3 years ago

Awesome! Glad you got it working :) It's always something simple.

Enjoy your project!

VonHirsch commented 3 years ago

btw @naztronaut, I ended up going with a hardware solution to drive led's for retropie. I use a USB Audio Dongle and headphone splitter to drive external speakers & a separate ESP32 circuit. The circuit and sketch are detailed in this repo.

A software solution (running dancyPi & retropie at the same time) would have been super slick! but a hardware "plug & play" solution is proving easier manage.

image

There's still setup on the pi / retropie side, I have to setup default pi and emulationstation audio to the usb card (aplay -l to find card #) and in one case I had to modify a shell script that uses omxplayer to use the correct card (omxplayer -o alsa:hw:2 ... or whatever your card # is) but it's manageable.

Still lovin' this repo tho. The boilerplate effects are a lot of fun. I might have to ponder on some of that code for ESP32 ;)

naztronaut commented 3 years ago

That's an awesome set up! Glad you have things working. Manageable is still better than not working at all :)

Keep me posted on your progress, it looks awesome. I have a bunch of ESP8266s, I wonder if I can get it running there. I probably won't do anything significant like you but I like playing with new things :)

I starred that repo. Will be back to it at some point.

Cookdominator commented 3 years ago

hello. i have a raspbery 4 8gb. ive tryed the video twice and i still get the no audio detected error. ive tryed several of the steps above but non of them fix it for me.

naztronaut commented 3 years ago

Did you try @explorerswb's method as well? https://github.com/naztronaut/dancyPi-audio-reactive-led/issues/35#issuecomment-774602843

You may have to start fresh, in case you were trying to override what you did before attempting that.

I'm just gonna copy/paste some of my questions from earlier if that still doesn't work for you:

Cookdominator commented 3 years ago

yes im running the full raspbery os. i ran the visialozation.py in the terminal where i typed all the comands in. i can hear the sound coming from the speakerand the outputs: pi@raspberrypi:~ $ cat /proc/asound/cards 0 [Loopback ]: Loopback - Loopback Loopback 1 1 [b1 ]: bcm2835_hdmi - bcm2835 HDMI 1 bcm2835 HDMI 1 2 [Headphones ]: bcm2835_headphonbcm2835 Headphones - bcm2835 Headphones bcm2835 Headphones pi@raspberrypi:~ $

pi@raspberrypi:~ $ cat /etc/modules

/etc/modules: kernel modules to load at boot time.

#

This file contains the names of kernel modules that should be loaded

at boot time, one per line. Lines beginning with "#" are ignored.

i2c-dev snd-alooppi@raspberrypi:~ $

snd-alooppi@raspberrypi:~ $ arecord -l List of CAPTURE Hardware Devices card 0: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7 card 0: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7

pi@raspberrypi:~ $ aplay -l List of PLAYBACK Hardware Devices card 0: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7 card 0: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM] Subdevices: 8/8 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 Subdevice #4: subdevice #4 Subdevice #5: subdevice #5 Subdevice #6: subdevice #6 Subdevice #7: subdevice #7 card 1: b1 [bcm2835 HDMI 1], device 0: bcm2835 HDMI 1 [bcm2835 HDMI 1] Subdevices: 4/4 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3 card 2: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones] Subdevices: 4/4 Subdevice #0: subdevice #0 Subdevice #1: subdevice #1 Subdevice #2: subdevice #2 Subdevice #3: subdevice #3

naztronaut commented 3 years ago

The 'snd-aloop` line in your modules file means that you may not have done a fresh install. Can you reinstall Raspberry Pi OS and try what's posted here: https://github.com/naztronaut/dancyPi-audio-reactive-led/issues/35#issuecomment-774602843

A few of us were able to replicate so it might work for you too.

Cookdominator commented 3 years ago

do i need to set it up for vnc?

Cookdominator commented 3 years ago

ok, it worked for me, but its only 25fps. is there a way to make it better?

naztronaut commented 3 years ago

Great, glad you got it working!

How many lights are you trying to run and how much power are you providing? Low FPS is typically a power issue. if you're powering your lights directly from the Pi, try powering the lights from an external power source.

Cookdominator commented 3 years ago

yea i have 300 leds ant they are powered from a 20 amp power suply. i get low fps before even pluging them in and i have 8gb of ram and my pi is overclocked so i dont know what else could be the issue

naztronaut commented 3 years ago

Are you powering them through the Pi or are you using external power? If you're doing it through the pi, try powering them externally. The Pi can't pass too much current through its board (I don't know what the limit is). I typically use external power if I need to use more than 3A of current.

If you still get low Fps, try setting the LED count to 100 or something and see if it helps. Just to rule out hardware.

lassi-niemisto commented 3 years ago

I am also struggling with this.

1st attempt was with the no_mic branch. Toggling the "audio output device" in VLC, I could either get the BT play sound OR the PCM Loopback device deliver the sound to visualization.py but not at the same time.

2nd attempt: Since my rpi was already all mixed up with various attempts and I found this topic, I tried to follow the master branch solution posted by @explorerswb and start from fresh Raspberry OS installation and exactly the given commands. It didn't work for me: trying to run the visualization.py results in pyaudio fault self._stream = pa.open(**arguments) OSError: [Errno -9996] Invalid input device (no default output device)

Curiously, I found that in my setup the devices listed by pyaudio are different with and without sudo. The error reproduces with message OSError: No Default Input Device Available using sudo with the following snippet:

import pyaudio
from pprint import pprint

p = pyaudio.PyAudio()

for i in range(p.get_device_count()):
    pprint(p.get_device_info_by_index(i))

print("Device count: " + str(p.get_device_count()))
print("Default input device:")
pprint(p.get_default_input_device_info())

When running without sudo, the snippet finds a default input device called default.

Blaming the user stuff I tried the following:

Before I jump back to trying out the no_mic again, could you @explorerswb answer the following:

explorerswb commented 3 years ago

I haven't looked at this for a while, so it's possible updates to the OS have made the solution I posted obsolete. My Pi is currently in use on another project, so I sadly can't test it out myself right now. I do have the working setup on an SD card, so if I get a chance I'll try to hook it up and test it out this weekend. In answer to your questions though:

explorerswb commented 3 years ago

@lassi-niemisto, I just had a thought, and I wanted to check with you before I started digging into this. I think I remember that error message. Are you connecting to the Pi via SSH? As far as I know, nobody has managed to get this working headless. You MUST connect either remotely via VNC Viewer or with a keyboard and monitor physically connected to the Pi, or else it will give you an error message that, if I'm remembering correctly, is very similar to that one.

lassi-niemisto commented 3 years ago

Thanks for your quick replies @explorerswb!

Yeah I am running the stuff over VNC as instructed (tightvnc server to be precise, wouldnt believe the choice of VNC is meaningful here).

lassi-niemisto commented 3 years ago

@naztronaut - since it is clearly challenging to get the audio shared from output device to an input device, have you considered the following approach: Use pyaudio to write the audio file to output device directly and analyze the written samples for led control (no need to read them from audio device)?

lassi-niemisto commented 3 years ago

Tested my above idea of write instead of read with positive results. Also found a way to run the visualization without root user which makes accessing the bluetooth speaker much easier.

For non root operation: sudo setcap 'cap_sys_rawio+eip' /usr/bin/python3.7 sudo chmod a+rw /dev/mem (needs to be repeated after boot) !! Note that this way is not better than running root but avoids some pulseaudio problems !!

Gonna post the direct-feed-audio version of microphone.py when I get it a bit more robust.

However at least I can run this completely headless now 😎

VonHirsch commented 3 years ago

nice, good job, looking forward to the PR!

explorerswb commented 3 years ago

Sounds like you've made really nice progress. I'm very interested in seeing what you've come up with. I assume you don't need me to dig into my old configuration anymore (honestly, you've gone far deeper into the guts of this than I ever did), but if you do still need anything from me just let me know.

lassi-niemisto commented 3 years ago

Yeah no need for more investigation @explorerswb , thanks anyway!

Pull request for direct audio player mode is here https://github.com/naztronaut/dancyPi-audio-reactive-led/pull/48

VonHirsch commented 3 years ago

Does the "Pull request for direct audio player mode #48" only allow for visualization of pre-recorded audio?

lassi-niemisto commented 3 years ago

@VonHirsch There is a config.py setting for enabling the "audio player mode" and passing the directory of .wav files. Otherwise it should work still with microphone also, but I haven't tested it.

VonHirsch commented 3 years ago

Ok, my misunderstanding then. When you said you were working on a solution I thought it was about addressing the "live mode" rather than adding on an "audio player mode"

raymondjacobson commented 2 years ago

@naztronaut - since it is clearly challenging to get the audio shared from output device to an input device, have you considered the following approach: Use pyaudio to write the audio file to output device directly and analyze the written samples for led control (no need to read them from audio device)?

I was curious to try this as well because I've hit the same issues this thread is discussing -- but I can't get pyaudio to produce output either (tried all of the output device indexes). What was your config for the pyaudio stream?

Edit: @lassi-niemisto I've reduced my situation to pulse audio not working under sudo. Struggling to figure out why this doesn't work and how to restart pulseaudio as --system (because I do believe we need sudo for neopixel)

Edit2: Yup - okay, lassi's changes worked in https://github.com/naztronaut/dancyPi-audio-reactive-led/pull/48 by basically giving the pi user ability to do whatever. Tried for a while to get pulseaudio to run in --system mode, but one of the side effects is bricking pulseaudio for pi, which makes the OS pretty unusable in GUI mode (no top bar, etc. etc.)