geoffreybennett/alsa-scarlett-gui

Add sample rate information

Closed this issue · 6 comments

The official Focusrite software allows the user to adjust the sample rate of the device and includes some warnings about features (such as mix buses) that aren't supported at a particular sample rate on a particular device.

With this application there does not appear to be any information about sample rate. Unavailable features still seem to be supported, but do not work.

If actually adjusting the sample rate is too complex, maybe we could simply report the sample rate and add a warning to the mix window when the feature is not supported.

Hello, when I saw the first time the windows Focusrite Control I also was thinking it would be nice to add this feature to the alsa-scarlett-gui.
Then I thought that alsa-scarlett-gui has been created to manage Scarlett Specific Controls and Sample Rate is not specific to that card manufacturer.

That said, as you opened an issue, I watched if it is possible to do it.
Shortly, I do not think it is possible, but I do not know alsa.

It seems that Windows and GNU/Linux has different way to manage the usb_audio devices.
On GNU/Linux you have to open the device to configure/read the sample rate information.
Moreover, if you open it, you open it exclusively and other apps won't be able to work with the device.
So it seems to be the role of your DAW (with ALSA or PulseAudio) or jackd to configure it.

To realize it, plug your device in and power it on.
Do not start a jackd or any DAW software.

Display the capture parameters:

$ cat /proc/asound/card1/pcm0c/sub0/hw_params
closed

Display the playback parameters:

$ cat /proc/asound/card1/pcm0p/sub0/hw_params
closed

Open alsa-scarlett-gui.

$ cat /proc/asound/card1/pcm0c/sub0/hw_params
$ cat /proc/asound/card1/pcm0p/sub0/hw_params

=> Still returns "closed", alsa-scarlett-gui does not call snd_pcm_open()

Now open your DAW and open the device with ALSA or PulseAudio, the software allows you to set the sample rate at this stage.

$ cat /proc/asound/card1/pcm0c/sub0/hw_params
$ cat /proc/asound/card1/pcm0p/sub0/hw_params

=> gives you the parameters you entered in the DAW.

Close the DAW, and hw_params will say again "closed".

Open qjackctl, open the device and configure the sample rate.

$ cat /proc/asound/card1/pcm0c/sub0/hw_params
$ cat /proc/asound/card1/pcm0p/sub0/hw_params

=> gives you the parameters you entered in qjackctl.

Close qjackctl, and hw_params will say again "closed".

I played a bit with the following two examples:
https://gist.github.com/alpereira7/020ded221d24ed9fcc8d92a902efc20f
https://gist.github.com/ghedo/963382

/* 
* file:           get_rate.c
* compile:        gcc -o get_rate get_rate.c -lasound
* execute:        ./get_rate
*/

#include <alsa/asoundlib.h>
#include <stdio.h>

#define PCM_DEVICE  "hw:1,0"
#define SAMPLE_RATE 44100

int main(int argc, char **argv) {
	unsigned int rate, dir;
	int err;
	snd_pcm_t *pcm_handle;
	snd_pcm_hw_params_t *params;


	/* Open the PCM device in playback mode */
	err = snd_pcm_open(&pcm_handle, PCM_DEVICE,	SND_PCM_STREAM_PLAYBACK, 0);
	if (err < 0)
		printf("ERROR: Can't open \"%s\" PCM device. %s\n",	PCM_DEVICE, snd_strerror(err));

	/* Allocate parameters object and fill it with default values*/
	snd_pcm_hw_params_alloca(&params);

	snd_pcm_hw_params_any(pcm_handle, params);
	
	/* Set parameters */
	err = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
	if (err < 0)
		printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(err));

	err = snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S16_LE);
	if (err < 0)
		printf("ERROR: Can't set format. %s\n", snd_strerror(err));

	/* Resume information */
	printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
	printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));

	
	
	/* Get rate returns "Invalid argument" status */
	err = snd_pcm_hw_params_get_rate(params, &rate, 0);
	if (err < 0)
		printf("ERROR: Can't get rate. %s\n", snd_strerror(err));
	else
		printf("rate: %d bps\n", rate);

	
	
	/* You have to set the parameters by yourself before to get it */
	rate = SAMPLE_RATE;
	err = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0);
	if (err < 0)
		printf("ERROR: Can't set rate. %s\n", snd_strerror(err));

	/* Write parameters */
	err = snd_pcm_hw_params(pcm_handle, params);
	if (err < 0)
		printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(err));


	
	/* Get rate now working */
	err = snd_pcm_hw_params_get_rate(params, &rate, 0);
	if (err < 0)
		printf("ERROR: Can't get rate. %s\n", snd_strerror(err));
	else
		printf("rate: %d bps\n", rate);


	printf("\n");
	printf("Open a new terminal and run the following command :\n");
	printf("cat /proc/asound/card1/pcm0p/sub0/hw_params\n");
	printf("\n");
	printf("Press any key to close the device and finish.\n");
	getc(stdin);

	/* Close the device (and lose the params) */
	snd_pcm_close(pcm_handle);

	return 0;
}

First off- Excellent Job!! A novice like me has been able to get this up and running for my Clarett + 4Pre. I don't use Jack, Carla... I have been able to record all 8 analog inputs simultaneously on Audacity. I am currently setting it up on Ardour. The Clarett sample rate is not selectable from these DAWs (that I can see) The Focusrite control is used to set the sample rate in windows applications. It would be very helpful if that could be added to this GUI. I don't need any warning for function loss at the highest sample rates. That is easily remembered. But to be able to switch between the sample rates without having to bring it to a windows machine would be nice. Thanks again!!!

Hi @card308,

Please see https://github.com/geoffreybennett/alsa-scarlett-gui/blob/master/FAQ.md#where-are-the-options-to-set-the-sample-rate-and-buffer-size

It’s important to note that the Scarlett2 driver and alsa-scarlett-gui have nothing to do with audio input/output to and from the device. This task is managed by the generic part of the ALSA USB soundcard driver.

Audio settings like the sample rate and buffer size are chosen by the application which is using the soundcard. In most cases, that is a sound server such as PulseAudio, JACK, or PipeWire.

In regard to this:

to be able to switch between the sample rates without having to bring it to a windows machine would be nice

Changing the sample rate in Windows will not affect the sample rate in Linux. The first audio app that opens the soundcard in Linux (again, usually a sound server) will reset the sample rate.

Regards,
Geoffrey.

In commit f00de1b I added sample rate display by polling /proc/asound/cardN/stream0.