Skip to content
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

Add ability to set different sample rate for different streams #1248

Open
chicco-carone opened this issue Jun 17, 2024 · 12 comments
Open

Add ability to set different sample rate for different streams #1248

chicco-carone opened this issue Jun 17, 2024 · 12 comments

Comments

@chicco-carone
Copy link

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is.
I stream all of my music from plexamp in flac lossless and have multiple players that could support different sample rates and i would like to stream a different rate in a different stream.

Describe the solution you'd like
A clear and concise description of what you want to happen.
The ability to set the sample rate per stream so it can exist a plexamp lossless with 96k/24 sample rate and a normal plexamp stream with normal quality. The main issue is that browser players don't support higher sample rates and don't support resampling so setting the main stream to a higher rate would make browser client useless for playback

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Using 44/16 which is supported by everything i tried,

Additional context
Add any other context or screenshots about the feature request here.
I don't think the server would need to resample since with pipewire (which i use to capture the audio for my server and send into a fifo) can resample the audio for multiple sources with enough cpu and it can create 2 fifos which can reproduce sound at the same time. An additional bonus would be to keep the client in the same group for group volume and just change the source of the necessary clients

@badaix
Copy link
Owner

badaix commented Jun 17, 2024

You can add sampleformat=<sampleformat> to any source. If not set, the global one will be used:
https://github.com/badaix/snapcast/blob/develop/doc/configuration.md
https://github.com/badaix/snapcast/blob/develop/README.md#server

@badaix badaix closed this as completed Jun 19, 2024
@chicco-carone
Copy link
Author

@badaix i tried to set in the stream settings 2 different sample rates and I pointed them to the same Fifo and the sound doesn't work in want of them it just crackles randomly

@badaix
Copy link
Owner

badaix commented Jul 1, 2024

Seems that I misunderstood you, my understanding was that you can capture plexamp's audio and forward it into two different fifos, using different formats (e.g. /tmp/snapfifo and /tmp/snapfifo_hires). Then you could define two sources:

source = pipe:///tmp/snapfifo?name=hifi&sampleformat=48000:16:2
source = pipe:///tmp/snapfifo_hires?name=hires&sampleformat=96000:24:2

If your browser based player is Snapweb, you can use Snapweb v0.7.50, which does support 96k/24/2 PCM and Flac. The player part of Snapweb was (and still is) just a proof of concept when WebSocket support was added to Snapcast, hacked on a rainy vacation day, without deeper knowledge of JS or TS.

Another solution would be #1228

@badaix badaix reopened this Jul 1, 2024
@chicco-carone
Copy link
Author

I tried a few things to try and fix the issue wihout success.

I created 2 different pipewire fifos with the different sample rates and used a combine sink to playback the same audio from plex into both. I set the config like before without success.

Command for the fifos
pactl load-module module-pipe-sink file=/tmp/plexamp sink_name=Fifo_Plexamp format=s16le rate=44100 && pactl load-module module-pipe-sink file=/tmp/plexamp-hires sink_name=Fifo_Plexamp_Hires format=s24le rate=96000
Command for the combine sink
pactl load-module module-combine-sink sink_name=Combined_Plexamp slaves=Fifo_Plexamp,Fifo_Plexamp_Hires

Server Config

source = pipe:///tmp/plexamp?name=Plexamp&sampleformat=44100:16:2&codec=flac
source = pipe:///tmp/plexamp-hires?name=Plexamp-HiRes&sampleformat=96000:24:2&codec=flac

When playing back if i use the normal stream it works without issues but if i use the other hires stream it sounds loud and distorted.

Here is a recording of the issue
https://github.com/user-attachments/assets/efb8561f-c736-47e0-ada0-9cff8384f965

Watching the server log during playback the on resync function gets called continuously.

024-07-13 18-25-28.894 [Info] (Server) onResync (Plexamp-HiRes): 16.6818 ms
2024-07-13 18-25-29.049 [Info] (Server) onResync (Plexamp-HiRes): 15.2876 ms
2024-07-13 18-25-29.158 [Info] (Server) onResync (Plexamp-HiRes): 6.49998 ms
2024-07-13 18-25-29.262 [Info] (Server) onResync (Plexamp-HiRes): 5.12185 ms
2024-07-13 18-25-29.369 [Info] (Server) onResync (Plexamp-HiRes): 5.47544 ms
2024-07-13 18-25-29.476 [Info] (Server) onResync (Plexamp-HiRes): 5.38651 ms
2024-07-13 18-25-29.584 [Info] (Server) onResync (Plexamp-HiRes): 7.44275 ms
2024-07-13 18-25-29.689 [Info] (Server) onResync (Plexamp-HiRes): 5.6277 ms
2024-07-13 18-25-29.796 [Info] (Server) onResync (Plexamp-HiRes): 5.53912 ms
2024-07-13 18-25-29.903 [Info] (Server) onResync (Plexamp-HiRes): 6.49207 ms
2024-07-13 18-25-30.009 [Info] (Server) onResync (Plexamp-HiRes): 5.53942 ms
2024-07-13 18-25-30.116 [Info] (Server) onResync (Plexamp-HiRes): 6.03503 ms
2024-07-13 18-25-30.222 [Info] (Server) onResync (Plexamp-HiRes): 5.0462 ms
2024-07-13 18-25-30.330 [Info] (Server) onResync (Plexamp-HiRes): 6.2857 ms
2024-07-13 18-25-30.436 [Info] (Server) onResync (Plexamp-HiRes): 5.75542 ms
2024-07-13 18-25-30.542 [Info] (Server) onResync (Plexamp-HiRes): 5.52079 ms

I could also notice that when i mismatchd the sample rates in the normal stream (snapserver was 44100 and pipewire was 48000) the function would get called like this but after matching them correctly it stopped calling it for the other stream.

I tried playing back both 44/16 and 96/24 from plexamp with the same issue

@erahhal
Copy link

erahhal commented Sep 26, 2024

I'm running into the same issue.

I've got a server setup as such:

snapserver --daemon --stream.stream=pipe:///run/snapserver/hires?name=Hires&sampleformat=48000:24:2

and run the following with a raw PCM file:

cat file_24_48k.pcm > /run/snapserver/hires

and all I hear is static.

I've confirmed that the file is proper raw PCM by importing raw data into Audacity and playing with the following settings:

image

I'm running version 0.29.0 of snapserver.

It works fine with 44100:16:2 data from other sources on a different pipe, so it's not an issue with my setup.

@erahhal
Copy link

erahhal commented Sep 26, 2024

@chicco-carone did you ever resolve this?

@erahhal
Copy link

erahhal commented Sep 26, 2024

I just looked at the server logs, and am seeing:

(PcmStream) PcmStream: Hires, sampleFormat: 48000:16:2 `

So it seems that it's ignoring the bit depth parameter of 24 and setting it to 16 instead. When I cat a 48000:16:2 file through the pipe, it's recognizable, but plays too fast and has glitchy sounds.

@erahhal
Copy link

erahhal commented Sep 26, 2024

I'm using the Android client to monitor by the way. Is it limited to certain sample rates and bit depths?

@erahhal
Copy link

erahhal commented Sep 26, 2024

Further detail: If sampleFormat is not set per stream, but as a global option (e.g. --stream.sampleformat=48000:24:2), then the log output is correct. It still outputs static though.

@chicco-carone
Copy link
Author

@erahhal unfortunately not even if I try to use 48/24 and my 2 sources are 48/16 and 48/24 one of them sounds distorted every time

@chicco-carone
Copy link
Author

@erahhal I think that the android client should have the same limitations as snapweb so 96k/24. For me i tried with the linux client (which doesn't have limitations from what i know and it still doesn't work)

@chicco-carone
Copy link
Author

chicco-carone commented Oct 30, 2024

@badaix After some trial and error i found a way i which it can work without issues but with a certaing bug.
I created the fifo with the maximum bitrate that plexamp would output which with flac lossless is s32le and set the config in the snapcast server with a sample rate of 96000:24:2. After playing some lossless files from plex i noticed a similar issue as before but without the constant calls to the onResync functions which caused the audio to stutter. The sound was correct but very distorted so in the kde audio settings i set the volume for the output device (the fifo) to 19% and it sounds normal without distorsion and the volume is the same as before. For the various clients the browser can handle it fine and the linux client can be resampled if needed to 44000:16 without audio issues and while being in sync but when connecting the audio lags behind a bit on both clients (tested with android and linux) but after a few seconds fixes itself. The main issues are the fact that the server is recieving the audio at a volume so high that is distorting a lot and the lag when adding a new client with a different rate (minor issue but present). I attached a screenshot of the output of pw-top and the snapclient on a client with plexamp streaming 192/24 which is being resampled to 96/24 by pipewire and then handled fine by the clients
Screenshot_20241030_213326

Edit: The delay after connecting another resampled client is only present in v0.28 and not on the latest one (didn't notice that i hadn't updated my server) but the volume issue is also present in v0.29

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

No branches or pull requests

3 participants