You are not logged in.

#1 2023-12-16 04:29:27

TuttiC
Member
Registered: 2023-12-16
Posts: 7

The recording program has high latency under Linux.

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

#2 2023-12-16 07:53:00

TuttiC
Member
Registered: 2023-12-16
Posts: 7

Re: The recording program has high latency under Linux.

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

#3 2023-12-16 09:41:24

TuttiC
Member
Registered: 2023-12-16
Posts: 7

Re: The recording program has high latency under Linux.

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

#4 2023-12-16 10:35:54

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 7,771
Website

Re: The recording program has high latency under Linux.

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

#5 2023-12-17 12:41:27

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 12,047
Website

Re: The recording program has high latency under Linux.

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.


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.

Offline

#6 2023-12-17 12:54:58

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 7,771
Website

Re: The recording program has high latency under Linux.

^ 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

#7 2023-12-17 13:20:00

TuttiC
Member
Registered: 2023-12-16
Posts: 7

Re: The recording program has high latency under Linux.

WorMzy wrote:
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

#8 2023-12-17 13:20:37

TuttiC
Member
Registered: 2023-12-16
Posts: 7

Re: The recording program has high latency under Linux.

Head_on_a_Stick wrote:

^ 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

#9 2023-12-17 13:21:57

TuttiC
Member
Registered: 2023-12-16
Posts: 7

Re: The recording program has high latency under Linux.

#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(&params);
    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

#10 2023-12-17 15:42:30

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 7,771
Website

Re: The recording program has high latency under Linux.

Please edit your post and use [code][/code] tags for the, er, code. Thanks.

Offline

#11 2023-12-18 12:23:17

TuttiC
Member
Registered: 2023-12-16
Posts: 7

Re: The recording program has high latency under Linux.

Head_on_a_Stick wrote:

Please edit your post and use [code][/code] tags for the, er, code. Thanks.


OKey

Offline

Board footer

Powered by FluxBB