除了44.1kHz以外,WAV文件的声音都有问题

时间:2021-04-18 19:45:31

I'm using ALSA on Ubuntu to try to play a WAV file. Presently, I'm able to read the wav header to figure out the file's sampling rate and such then set the parameters on ALSA to correspond. This works perfectly for files with a 44.1kHz sampling rate, but other files with rates at ~11kHz or ~22kHz do not play correctly. I'm not sure that I am setting the sampling rate correctly.

我在Ubuntu上使用ALSA尝试运行一个WAV文件。目前,我可以读取wav头来计算文件的采样率,然后在ALSA上设置相应的参数。这适用于采样率为44.1kHz的文件,但是其他采样率为~11kHz或~22kHz的文件不能正常运行。我不确定我是否正确地设置了采样率。

val = realSampleRate;
    //Sampling rate to given sampling rate
    snd_pcm_hw_params_set_rate_max(handle, params, &val, &dir);
    cout << "sampling at " << val << " Hz \n";

This gives the correct output ("sampling at 22050 Hz") but if I follow it with this:

这给出了正确的输出(“22050 Hz采样”),但如果我这样做:

val = realSampleRate;
snd_pcm_hw_params_set_rate_min(handle, params, &val, &dir);
cout << "sampling at " << val << " Hz \n";

the output proceeds to say "sampling at 44100 Hz" which is obviously contradictory. I also tried using snd_pcm_hw_params_set_rate_near but that doesn't work either, it says sampling at 44100 Hz on a 22050 file, and the audio throughout all of those were very messed up.

输出接着说“44100赫兹的采样”,这显然是矛盾的。我也尝试过使用snd_pcm_hw_params_set_rate_near,但是这也不行,它说在22050文件上以44100 Hz采样,所有这些的音频都很混乱。

EDIT: One issue is incorrect sampling rates, which will speed up the playing, but the real issue comes from mono tracks. Mono tracks sound really distorted and very off.

编辑:一个问题是不正确的采样率,这会加快播放速度,但是真正的问题来自单声道。单声道音轨听起来真的很扭曲,很不正常。

EDIT: 8 Bit files are off too

编辑:8位文件也关闭

3 个解决方案

#1


1  

Looks to me like your hardware is not capable of handling a 22.05Khz sampling rate for playback. The fact that the API function returns a different value is a clue.

在我看来,你的硬件不能处理一个205khz采样率播放。API函数返回不同的值是一个线索。

ALSA is just an API. It can only do what your current underlying hardware is capable of supporting. Low-end, bottom-of-the-barrel, el-cheapo audio playback hardware will support a handful of sampling frequencies, and that's about it.

ALSA只是一个API。它只能执行当前底层硬件能够支持的功能。低端、最底层、廉价的音频播放硬件将支持少数采样频率,仅此而已。

I had some custom-written audio recording and playback software, that was sampling and recording audio at a particular rate, then playing it back using ALSA's aplay. When I got some new hardware, I found that the new hardware was still capable of supporting my sampling rate for recording, for playback it didn't, and aplay simply proceeded to play back the previously recorded audio at the nearest supportable playback level, with hillarious results. I had to change my custom-written stuff to record and playback at the supported rate.

我有一些定制的音频录制和回放软件,它以特定的速率采样和录制音频,然后使用ALSA的aplay回放。当我得到一些新的硬件时,我发现新的硬件仍然能够支持我的采样率进行录制,因为它没有回放,aplay只是在最近的可支持的回放级别上回放先前录制的音频,结果非常复杂。我必须更改我的自定义编写的内容,以支持的速率记录和回放。

If the hardware does not support your requested playback rate, ALSA won't resample your raw audio data. It's up to you to resample it, for playback.

如果硬件不支持所请求的播放速率,ALSA将不会重新采样原始音频数据。由你重新采样,重新播放。

#2


1  

snd_pcm_hw_params_set_rate_max() sets the maximum sample rate, i.e., when this functions succeeds, the device's sample rate will not be larger than what you've specified.

snd_pcm_hw_params_set_rate_max()设置最大采样率,即。,当此功能成功时,设备的采样率将不会超过您指定的值。

snd_pcm_hw_params_set_rate_min() sets the minimum sample rate.

snd_pcm_hw_params_set_rate_min()设置最小采样率。

snd_pcm_hw_params_set_rate_near() searches for the nearest sample rate that is actually supported by the device, sets it, and returns it.

snd_pcm_hw_params_set_rate_near()搜索设备实际支持的最近的采样率,设置它,然后返回它。

If you have audio data with a specific sample rate, and cannot do resampling, you must use snd_pcm_hw_params_set_rate().

如果您有特定采样率的音频数据,并且不能进行重新采样,那么必须使用snd_pcm_hw_params_set_rate()。

#3


0  

Using "default" instead of "hw:0,0" solves this, including the sampling rate being too slow. "plughw:0,0" works as well, and it's better because you can select the different devices/cards programmatically whereas default just uses the default.

使用“default”而不是“hw:0,0”可以解决这个问题,包括采样速度太慢。“plughw:0,0”也很好用,因为您可以通过编程方式选择不同的设备/卡,而默认情况下只使用默认。

#1


1  

Looks to me like your hardware is not capable of handling a 22.05Khz sampling rate for playback. The fact that the API function returns a different value is a clue.

在我看来,你的硬件不能处理一个205khz采样率播放。API函数返回不同的值是一个线索。

ALSA is just an API. It can only do what your current underlying hardware is capable of supporting. Low-end, bottom-of-the-barrel, el-cheapo audio playback hardware will support a handful of sampling frequencies, and that's about it.

ALSA只是一个API。它只能执行当前底层硬件能够支持的功能。低端、最底层、廉价的音频播放硬件将支持少数采样频率,仅此而已。

I had some custom-written audio recording and playback software, that was sampling and recording audio at a particular rate, then playing it back using ALSA's aplay. When I got some new hardware, I found that the new hardware was still capable of supporting my sampling rate for recording, for playback it didn't, and aplay simply proceeded to play back the previously recorded audio at the nearest supportable playback level, with hillarious results. I had to change my custom-written stuff to record and playback at the supported rate.

我有一些定制的音频录制和回放软件,它以特定的速率采样和录制音频,然后使用ALSA的aplay回放。当我得到一些新的硬件时,我发现新的硬件仍然能够支持我的采样率进行录制,因为它没有回放,aplay只是在最近的可支持的回放级别上回放先前录制的音频,结果非常复杂。我必须更改我的自定义编写的内容,以支持的速率记录和回放。

If the hardware does not support your requested playback rate, ALSA won't resample your raw audio data. It's up to you to resample it, for playback.

如果硬件不支持所请求的播放速率,ALSA将不会重新采样原始音频数据。由你重新采样,重新播放。

#2


1  

snd_pcm_hw_params_set_rate_max() sets the maximum sample rate, i.e., when this functions succeeds, the device's sample rate will not be larger than what you've specified.

snd_pcm_hw_params_set_rate_max()设置最大采样率,即。,当此功能成功时,设备的采样率将不会超过您指定的值。

snd_pcm_hw_params_set_rate_min() sets the minimum sample rate.

snd_pcm_hw_params_set_rate_min()设置最小采样率。

snd_pcm_hw_params_set_rate_near() searches for the nearest sample rate that is actually supported by the device, sets it, and returns it.

snd_pcm_hw_params_set_rate_near()搜索设备实际支持的最近的采样率,设置它,然后返回它。

If you have audio data with a specific sample rate, and cannot do resampling, you must use snd_pcm_hw_params_set_rate().

如果您有特定采样率的音频数据,并且不能进行重新采样,那么必须使用snd_pcm_hw_params_set_rate()。

#3


0  

Using "default" instead of "hw:0,0" solves this, including the sampling rate being too slow. "plughw:0,0" works as well, and it's better because you can select the different devices/cards programmatically whereas default just uses the default.

使用“default”而不是“hw:0,0”可以解决这个问题,包括采样速度太慢。“plughw:0,0”也很好用,因为您可以通过编程方式选择不同的设备/卡,而默认情况下只使用默认。