rbn42 / panon

An Audio Visualizer Widget in KDE Plasma
GNU General Public License v3.0
192 stars 30 forks source link

[BUG] {title} Lagging when using Spotify #41

Closed filkata1996 closed 3 years ago

filkata1996 commented 3 years ago

Desktop (please complete the following information):

Describe the bug In the browsers and music players, the widget works perfectly fine.
However, when I play music from Spotify, the widget starts to lag heavily. I tried all the different settings but the issue seems to stay and it's kinda bugging me because I use mostly Spotify to listen to music.

Any error message shown in the console Please execute the following commands in the console and upload the outputs. no error messages

rbn42 commented 3 years ago

I have no idea why it is lagging.

It may require some time to debug on your system. I will write some python scripts, and you need to execute them on your system and tell me the results. Do you have time for this?

rbn42 commented 3 years ago

This is the first script. Tell me whether it lags.


class PyaudioSource:
    def __init__(self, channel_count, sample_rate, device_index, fps):
        self.channel_count = channel_count
        self.sample_rate = sample_rate
        self.chunk = self.sample_rate // fps
        if device_index is not None:
            device_index = int(device_index)
        self.device_index = device_index

        self.start()

    def read(self, fps=None):
        # Ignores PyAudio exception on overflow.
        if fps is None:
            c = self.chunk
        else:
            c = self.sample_rate // fps
        result = self.stream.read(c, exception_on_overflow=False)
        return result

    def start(self):
        import pyaudio
        p = pyaudio.PyAudio()
        self.stream = p.open(
            format=pyaudio.paInt16,
            channels=self.channel_count,
            rate=self.sample_rate,
            input=True,
            input_device_index=self.device_index,
        )

import asyncio

ps=PyaudioSource(2,44100,None,30)
async def timeout_callback():
    await asyncio.sleep(1.0/100)
    data=ps.read()
    n=(sum([i for i in data]))
    if n>0:
        print('\rSpotify is playing    ',end='')
    else:
        print('\rSpotify is not playing',end='')

async def main():
    while True:
        await asyncio.sleep(1.0/30)
        asyncio.ensure_future(timeout_callback())

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
    loop.run_until_complete(main())
finally:
    loop.run_until_complete(loop.shutdown_asyncgens())
    loop.close()
filkata1996 commented 3 years ago

I got the following error :

ALSA lib pcm_dsnoop.c:638:(snd_pcm_dsnoop_open) unable to open slave ALSA lib pcm_dmix.c:1075:(snd_pcm_dmix_open) unable to open slave ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side connect(2) call to /dev/shm/jack-1000/default/jack_0 failed (err=No such file or directory) attempt to connect to server failed connect(2) call to /dev/shm/jack-1000/default/jack_0 failed (err=No such file or directory) attempt to connect to server failed 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_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:1075:(snd_pcm_dmix_open) unable to open slave connect(2) call to /dev/shm/jack-1000/default/jack_0 failed (err=No such file or directory) attempt to connect to server failed

rbn42 commented 3 years ago

When audio is muted, the script will show "Spotify is not playing", when not muted, it will show "Spotify is playing". So you can verify whether the script lags by turning on and off Spotify.

rbn42 commented 3 years ago

I got the following error :

ALSA lib pcm_dsnoop.c:638:(snd_pcm_dsnoop_open) unable to open slave ALSA lib pcm_dmix.c:1075:(snd_pcm_dmix_open) unable to open slave ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side connect(2) call to /dev/shm/jack-1000/default/jack_0 failed (err=No such file or directory) attempt to connect to server failed connect(2) call to /dev/shm/jack-1000/default/jack_0 failed (err=No such file or directory) attempt to connect to server failed 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_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:1075:(snd_pcm_dmix_open) unable to open slave connect(2) call to /dev/shm/jack-1000/default/jack_0 failed (err=No such file or directory) attempt to connect to server failed

I got them too, not errors.

filkata1996 commented 3 years ago

As far as I can understand, I have to run the script and because it's a loop, It continues to run. Then turn off and on Spotify, to see the messages you say, but the only thing that I get when I run the script are the messages I posted. After that no matter what I do with Spotify I don't get any additional messages.

rbn42 commented 3 years ago

Your understanding is correct. It is odd to me that you didn't see those messages.

rbn42 commented 3 years ago

How about this script? What is its output? https://gist.github.com/rbn42/30767cb36741b9ccbcc661b0841eda8f

And is the PortAudio back-end available in your panon widget?

Screenshot_20210125_184522-1

filkata1996 commented 3 years ago

I have the setting PortAudio, but I use PulseAudio. If I switch to PortAudio the visual effects don't work. I just have an empty line on my screen which doesn't do anything.

I get printed continuously an error for division by 0. Tried to turn off and on Spotify again and with the PortAudio as well, but no messages.

ZeroDivisionError: division by zero call back start Task exception was never retrieved

rbn42 commented 3 years ago

I have the setting PortAudio, but I use PulseAudio. If I switch to PortAudio the visual effects don't work. I just have an empty line on my screen which doesn't do anything.

OK, so the script won't work, because it is based on PortAudio, not PulseAudio.

But are your using the default device of PulseAudio, or a specified one?

filkata1996 commented 3 years ago

For input devices, I use either Monitor of build-in audio analog stereo or Mixing all speakers. The other options + the default doesn't seem to produce any visual effects.

rbn42 commented 3 years ago

Please install the_silver_searcher
community/the_silver_searcher 2.2.0-2 [installed]
Code searching tool similar to Ack, but faster

and tell me the result of

cd ~/.config
ag \\.monitor | grep pulse

Oh, and only when you have selected Monitor of build-in audio analog stereo, not Mixing all speakers.

filkata1996 commented 3 years ago

plasma-org.kde.plasma.desktop-appletsrc:47:pulseaudioDevice=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor

rbn42 commented 3 years ago

plasma-org.kde.plasma.desktop-appletsrc:47:pulseaudioDevice=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor

Ok, now I have adjusted the script for your configuration

https://gist.github.com/rbn42/30767cb36741b9ccbcc661b0841eda8f

But you need install https://github.com/bastibe/SoundCard to execute the script.

rbn42 commented 3 years ago

You know what? I just found that after I selected PulseAudio, the panon widget on my system also lags.

And I found this error came with commit c823cb805c23e4aa93907006fcd322fb6acdefde , and this commit was to solve #36, or I hoped it can be solved by this commit.

So you can try reverse that commit on your panon widget in ~/.local/share/plasma/plasmoids/panon/, by adding that deleted line back.

filkata1996 commented 3 years ago

Now it works. When I play Spotify it prints "Spotify is playing", when I pause it prints "Spotify is not playing". The visuals still lag tho

rbn42 commented 3 years ago

~/.local/share/plasma/plasmoids/panon/contents/scripts/panon/backend/source.py

rbn42 commented 3 years ago

I am almost sure that commit should be reversed to solve your lagging. Please confirm it.

filkata1996 commented 3 years ago

Yes, it solved it. Now there are no lags from Spotify. Thank you