Linux中的音频流采样率 [英] audio stream sampling rate in linux

查看:424
本文介绍了Linux中的音频流采样率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C/C ++从Linux中的音频麦克风读取和存储样本.使用PCM ioctl,我使用SOUND_PCM_WRITE_RATE ioctl等将设备设置为具有一定的采样率,例如10Khz.设备正确设置,并且在使用"read"进行设置后无法从设备读回.

Im trying read and store samples from an audio microphone in linux using C/C++. Using PCM ioctls i setup the device to have a certain sampling rate say 10Khz using the SOUND_PCM_WRITE_RATE ioctl etc. The device gets setup correctly and im able to read back from the device after setup using the "read".

int got = read(itsFd, b.getDataPtr(), b.sizeBytes());

我遇到的问题是,在设置了适当的采样率之后,我有一个线程不断从/dev/dsp1读取并存储这些样本,但是记录1秒后获得的样本数量却偏离了采样且始终比设定的采样率高几个数量级.有什么想法从哪里开始找出可能是什么问题?

The problem i have is that after setting the appropriate sampling rate i have a thread that continuously reads from /dev/dsp1 and stores these samples, but the number of samples that i get for 1 second of recording are way off the sampling rate and always orders of magnitude more than the set sampling rate. Any ideas where to begin on figuring out what might be the problem?

部分源代码:

/////////main loop
while(goforever) {

    // grab a buffer:
    AudioBuffer<uint16> buffer;
    agb->grab(buffer);

    pthread_mutex_lock(&qmutex_data);
    rec.push(buffer);
    pthread_mutex_unlock(&qmutex_data);
    if(tim.getSecs()>=5)
      goforever =false;
}

////////////grab function:

template <class T>
void AudioGrabber::grab(AudioBuffer<T>& buf) const
{

  AudioBuffer<T> b(itsBufsamples.getVal(),
                   itsStereo.getVal() ? 2U : 1U,
                   float(itsFreq.getVal()),
                   NO_INIT);
  int got = read(itsFd, b.getDataPtr(), b.sizeBytes());

  if (got != int(b.sizeBytes()))
    PLERROR("Error reading from device: got %d of %u requested bytes",
            got, b.sizeBytes());

  buf = b;
}

推荐答案

仅仅因为您要求10kHz的采样率,并不意味着您的硬件支持该采样率.许多声卡仅支持一个或两个采样率-例如,我的声卡仅支持以下采样率:

Just because you ask for a 10kHz sampling rate, it doesn't mean that your hardware supports it. Many sound cards only support one or two sampling rates - mine for example only supports these:

$ grep -rH rates /proc/asound/ | cut -d : -f 2- | sort -u
    rates [0x160]: 44100 48000 96000
    rates [0x560]: 44100 48000 96000 192000
    rates [0x5e0]: 44100 48000 88200 96000 192000

因此,您必须检查 SOUND_PCM_WRITE_RATE ioctl()的返回值,以验证您是否已获得所需的汇率,如

Therefore, you have to check the return value of the SOUND_PCM_WRITE_RATE ioctl() to verify that you got the rate that you wanted, as mentioned here:

SOUND_PCM_WRITE_RATE

SOUND_PCM_WRITE_RATE

设置每秒的采样率.记住所有声音 卡有范围限制;这 司机会将价格四舍五入到 所支持的最近速度 硬件,返回实际 (四舍五入)率.

Sets the sampling rate in samples per second. Remember that all sound cards have a limit on the range; the driver will round the rate to the nearest speed supported by the hardware, returning the actual (rounded) rate in the argument.

这篇关于Linux中的音频流采样率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆