简体   繁体   English

在C ++中使用Alsa的dmix插件进行音频混合

[英]Audio mixing with alsa's dmix plugin in c++

I trying to play two wav files at the same time using alsa. 我试图使用alsa同时播放两个wav文件。 Note that the wav files have a different sample rate. 请注意,wav文件具有不同的采样率。 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. (我正在嵌入式Linux设备上进行开发。)但是一个流的播放速度比正常播放快了两倍。 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. 我在/etc/asound.conf中启用了带有dmix插件的默认设备,并将采样率设置为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. 但是据我了解,ALSA在内部将所有流重新采样为48khz,并在将它们再次下采样至我所需的输出速率(在我的情况下为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? 使用alsa-lib播放音频文件时,是否需要为该特定的wav文件设置所有参数?

For example: 8000hz mono 16-bits 例如:8000hz单声道16位

  • set snd_pcm_hw_params_set_rate() to 8000hz 将snd_pcm_hw_params_set_rate()设置为8000hz
  • snd_pcm_hw_params_set_format to 16bits LE/BE/signed/unsigned snd_pcm_hw_params_set_format到16bits LE / BE / signed / unsigned
  • snd_pcm_hw_params_set_channels for mono 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] 我可能会误解以下内容:[ALSA]

When software mixing is enabled, ALSA is forced to resample everything to the same frequency (48000 by default when supported). 启用软件混合后,将强制ALSA将所有内容重新采样为相同的频率(如果支持,则默认为48000)。 dmix uses a poor resampling algorithm which produces noticeable sound quality loss. dmix使用较差的重采样算法,会产生明显的音质损失。

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? 很清楚,如果我将dmix设备的asound.conf中的速率更改为44100,应该自动将所有内容重新采样为44100并混合吗?

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? 因此,我的两个混合音频文件之一速度不正确的原因可能是由于使用alsa-lib的流设置不正确?

Because if I play one wav file at a time , both streams seem correct. 因为如果一次播放一个wav文件,则两个流似乎都是正确的。 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. 仅当第一个播放时,同时我在流中混合另一个时,第一个wav文件的速度才会改变。 Note that hw settings are the same at this time. 请注意,此时的硬件设置相同。 Why does setting hw parameters (and playing) of stream2 changes something in stream1? 为什么设置stream2的硬件参数(和播放)会改变stream1中的某些内容?

ALSA does not have a fixed 48 kHz resampling. ALSA没有固定的48 kHz重采样。

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. dmix装置使用一个固定的采样速率和格式,但所有使用它通常使用的设备plug插件来启用自动转换。

When using alsa-lib, you must set all parameters that are important to you; 使用alsa-lib时,必须设置对您很重要的所有参数。 for any parameters not explicitly set, alsa-lib chooses a somewhat random value. 对于未明确设置的任何参数,alsa-lib选择一个稍微随机的值。

Different streams can use different parameters. 不同的流可以使用不同的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM