You are not logged in.
I wrote a recording program using C language combined with ALSA. When I set the sampling rate to 48000, the latency is approximately 1 second, and when set to 16000, the latency increases to around three seconds. However, when I directly use arecord and aplay, the latency is zero. What could be the reason for this?
If you need more information, Pls tell me!
Offline
And I use
'''C
snd_pcm_hw_params_set_period_time_near(handle, params, &buffer_time, 0);
'''
to control the period_time but nothing change occur.
Offline
I successfully save the processed data to a file in real-time without any issues, but there are anomalies when attempting to play the sound in real-time.
Offline
ALSA resamples to 48KHz by default, that can be over-ridden by forcing direct access to the hardware.
For example, I use this ~/.asoundrc to prevent ALSA ruining my music before sending to an external DAC (an Audiolab M-DAC+, identified as "A20" by ALSA):
pcm.Digital {
type hw
card A20
}
ctl.!default {
type hw
card A20
}
pcm.!default {
type plug
slave {
pcm "Digital"
rate "unchanged"
}
}
No idea about C though, I don't program (sorry).
Offline
Thank you very much for your response. So, did you add new entries in the ALSA settings?
TuttiC, you accidentally reported HoaS' post rather than replying.
Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD
Making lemonade from lemons since 2015.
Online
^ Thanks WorMzy :-)
@OP: I'm not sure what you mean by that, sorry.
I think it would probably be best if you share your C program here, we do have some members who are literate in that language.
Offline
TuttiC wrote:Thank you very much for your response. So, did you add new entries in the ALSA settings?
TuttiC, you accidentally reported HoaS' post rather than replying.
I'm sorry, I accidentally pressed the wrong button.
Offline
^ Thanks WorMzy :-)
@OP: I'm not sure what you mean by that, sorry.
I think it would probably be best if you share your C program here, we do have some members who are literate in that language.
All right,
Offline
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#define FORMAT SND_PCM_FORMAT_S16_LE
#define CHANNELS 1
#define SAMPLE_RATE 16000
#define FRAMES 160
#define BUFFER_FRAMES 5
int main() {
int rc;
short *buffer;
snd_pcm_t *handle;
snd_pcm_t *playbackhandle;
int write_index = 0;
int read_index = 0;
short circular_buffer[BUFFER_FRAMES][FRAMES * CHANNELS];
int flag = 0;
// 打开PCM设备录音
rc = snd_pcm_open(&handle, "hw:0,0", SND_PCM_STREAM_CAPTURE, SND_PCM_ASYNC);
if (rc < 0) {
printf("无法打开PCM设备: %s\n", snd_strerror(rc));
return -1;
}
// 设置硬件参数预缓存
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(handle, params);
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(handle, params, FORMAT);
// 设置硬件参数正式读取
snd_pcm_hw_params_set_channels(handle, params, CHANNELS);
unsigned int sampleRate = SAMPLE_RATE;
snd_pcm_hw_params_set_rate_near(handle, params, &sampleRate, 0);
snd_pcm_hw_params(handle, params);
// 打开PCM设备用于播放
rc = snd_pcm_open(&playbackhandle, "hw:0,0", SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
printf("无法打开PCM设备用于播放: %s\n", snd_strerror(rc));
return -1;
}
// 设置播放硬件参数
snd_pcm_hw_params_t *playbackParams;
snd_pcm_hw_params_alloca(&playbackParams);
snd_pcm_hw_params_any(playbackhandle, playbackParams);
snd_pcm_hw_params_set_access(playbackhandle, playbackParams, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(playbackhandle, playbackParams, FORMAT);
snd_pcm_hw_params_set_channels(playbackhandle, playbackParams, CHANNELS);
snd_pcm_hw_params_set_rate_near(playbackhandle, playbackParams, &sampleRate, 0);
snd_pcm_hw_params(playbackhandle, playbackParams);
// 写入音频文件
while (1) {
// 更新索引
if (flag < 5)
{
flag = flag +1;
}
else
// 播放一帧
{
rc = snd_pcm_writei(playbackhandle, circular_buffer[read_index], FRAMES);
if (rc == -EPIPE) {
// 检测到溢出,重新恢复PCM设备
snd_pcm_prepare(playbackhandle);
} else if (rc < 0) {
printf("写入音频数据错误: %s\n", snd_strerror(rc));
break;
}
read_index = (read_index + 1) % BUFFER_FRAMES;
}
// 读取数据到环形缓存
rc = snd_pcm_readi(handle, circular_buffer[write_index], FRAMES);
if (rc == -EPIPE) {
// 检测到溢出,重新恢复PCM设备
snd_pcm_prepare(handle);
} else if (rc < 0) {
printf("读取音频数据错误: %s\n", snd_strerror(rc));
break;
}
write_index = (write_index + 1) % BUFFER_FRAMES;
}
// 关闭 PCM 设备和文件,释放资源
snd_pcm_drain(playbackhandle);
snd_pcm_close(playbackhandle);
snd_pcm_close(handle);
return 0;
}
When I set the SAMPLE_RATE to 48000,the delay is about 1s. But when it's 16000, the delay is 3s.I want to reduce the delay
Last edited by TuttiC (2023-12-18 02:47:34)
Offline
Please edit your post and use [code][/code] tags for the, er, code. Thanks.
Offline
Please edit your post and use [code][/code] tags for the, er, code. Thanks.
OKey
Offline