How to know how much audio has been played?

未关闭
#458 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
15/100
Issue 类型
文档
描述清晰度
需要澄清
活跃度
停滞
技术栈
c, cpp, linux

调研方向

The issue is a usage question, not a reported defect, and it names no repository file. Start from the sample program's playback loop, which calls snd_pcm_delay and computes a played time from it, and check how that calculation relates to the ALSA delay semantics. Done means the question has a clear, verified answer, such as a documented way to get the playback position, reached with the maintainers' input. The thread has one comment and no reply in about 499 days.

由索引模型根据 Issue 内容生成。

描述

support

I tried to get an answer from AI, but the result has discrepancies.

#include <stdio.h>

#include <iostream>
#include <cmath>
#include <thread>

#include <alsa/asoundlib.h>

#define SAMPLE_RATE 44100
#define FREQUENCY 440.0 // A4 note
#define DURATION 5.0    // seconds

using namespace std::chrono_literals;

int main() {
    snd_pcm_t *handle;
    snd_pcm_hw_params_t *params;
    unsigned int rate = SAMPLE_RATE;
    int dir;
    int pcm;

    pcm = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
    if (pcm < 0) {
        std::cerr << "Unable to open PCM device: " << snd_strerror(pcm) << std::endl;
        return 1;
    }

    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, SND_PCM_FORMAT_S16_LE);
    snd_pcm_hw_params_set_rate_near(handle, params, &rate, &dir);
    snd_pcm_hw_params_set_channels(handle, params, 1);

    pcm = snd_pcm_hw_params(handle, params);
    if (pcm < 0) {
        std::cerr << "Unable to set HW parameters: " << snd_strerror(pcm) << std::endl;
        return 1;
    }

    int samples = static_cast<int>(DURATION * SAMPLE_RATE);
    int16_t *buffer = new int16_t[samples];

    for (int i = 0; i < samples; ++i) {
        buffer[i] = static_cast<int16_t>(32767 * sin(2 * M_PI * FREQUENCY * i / SAMPLE_RATE));
    }

    pcm = snd_pcm_writei(handle, buffer, samples);
    if (pcm < 0) {
        std::cerr << "Playback error: " << snd_strerror(pcm) << std::endl;
    }

    while (true) {
      snd_pcm_sframes_t delay;
      int rc = snd_pcm_delay(handle, &delay);
      if (rc == 0) {
        double played_sec = (samples - delay) / 44100.0f;
        printf("Played: %.2f sec\n", played_sec);
      } else {
        std::cerr << snd_strerror(rc) << std::endl;
        break;
      }
    }

    snd_pcm_drain(handle);

    delete[] buffer;
    snd_pcm_close(handle);

    return 0;
}

主要语言
C
星标
501
派生
227
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

alsa-project/alsa-lib 的其他 Issue

查看 alsa-project/alsa-lib 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。