1
votes

I trying to play two wav files at the same time using alsa. Note that the wav files have a different sample rate. This is possible, and audio streams are mixed and send to the audio chip. (I'm developing on an embedded linux device.) But one stream is being played a couple times faster then normal. So I guess there's a problem with resampling.

I have a default device with dmix plugin enabled in /etc/asound.conf and set the sample rate to 44100Hz. But to my understanding ALSA resamples all streams internally to 48khz and mixes them before downsampling them again to my desired output rate, in my case 44.1khz.

Is this correct?

When using the alsa-lib for playing audio files, do I need to set all parameters for that specific wav file?

For example: 8000hz mono 16-bits

  • set snd_pcm_hw_params_set_rate() to 8000hz
  • snd_pcm_hw_params_set_format to 16bits LE/BE/signed/unsigned
  • snd_pcm_hw_params_set_channels for mono

Does this change the hardware settings for the device or only for this specific audio stream?

Any clarification would be appreciated.

EDIT:

I might have misinterpreted the following: [ALSA]

When software mixing is enabled, ALSA is forced to resample everything to the same frequency (48000 by default when supported). dmix uses a poor resampling algorithm which produces noticeable sound quality loss.

So to be clear, if I change the rate in asound.conf of the dmix device to 44100, everything should be automagically be resampled to 44100 and mixed?

Thus the reason that one of my two mixed audio files has a incorrect speed is probably caused by incorrect stream settings using alsa-lib?

Because if I play one wav file at a time , both streams seem correct. It's only when the first one is playing and at the same time I mix the other one in the stream, the speed of the first wav file is changed. Note that hw settings are the same at this time. Why does setting hw parameters (and playing) of stream2 changes something in stream1?

1

1 Answers

1
votes

ALSA does not have a fixed 48 kHz resampling.

A dmix device uses a fixed sample rate and format, but all the devices using it typically use the plug plugin to enable automatic conversions.

When using alsa-lib, you must set all parameters that are important to you; for any parameters not explicitly set, alsa-lib chooses a somewhat random value.

Different streams can use different parameters.