snd_pcm_hw_params_set_period_size_near 不 return 物有所值

snd_pcm_hw_params_set_period_size_near doesn't return the good value

我正在尝试将 alsa 封装在 class 中。 问题是当我尝试使用时:

snd_pcm_hw_params_get_period_size(this->__params, &period_size, NULL);

snd_pcm_hw_params_get_period_time(this->__params, &time_period, NULL);

里面void alsa_control::record_to_file(std::string filename, int duration_in_us);

在 cpp 中,它在句点中发送 0,并且似乎未针对 period_time

进行修改

.h

#ifndef ALSA_RECORDING_H
#define ALSA_RECORDING_H

#include <iostream>
#include <alsa/asoundlib.h>
#include <wav_functions.h>

#define STEREO 2
#define MONO 1

using std::cout;
using std::endl;

class alsa_control {
public:

    void record_to_file(std::string filename, int duration_in_us);

    alsa_control(unsigned int rate, unsigned long frames, int bits, unsigned int stereo_mode);
    ~alsa_control();

private:
    unsigned int __rate;
    unsigned int __stereo_mode;
    int __bits;
    snd_pcm_uframes_t __frames;
    snd_pcm_hw_params_t *__params;
    snd_pcm_t *__handle;

    void open_pcm_device();
    void set_parameters_ALSA();

    alsa_control()=delete;
    alsa_control(const alsa_control&)=delete;

};

#endif /* ALSA_RECORDING_H */

和.cpp

#include <alsa_control.h>

alsa_control::alsa_control(unsigned int rate, unsigned long frames, int bits, unsigned int stereo_mode)
        : __rate(rate),
          __stereo_mode(stereo_mode),
          __bits(bits),
          __frames(frames) {

    this->open_pcm_device();
    snd_pcm_hw_params_alloca(&this->__params);
    this->set_parameters_ALSA();
}

alsa_control::~alsa_control() {
    snd_pcm_drain(this->__handle);
    snd_pcm_close(this->__handle);
}

void alsa_control::open_pcm_device() {
    int rc = snd_pcm_open(&this->__handle, "default", SND_PCM_STREAM_CAPTURE, 0);

    if (rc < 0) {
        cout << "ERROR :  unable to open pcm device: " << snd_strerror(rc) << endl;
        exit(1);
    }
}

void alsa_control::set_parameters_ALSA() {
    snd_pcm_hw_params_any(this->__handle, this->__params); // def values
    snd_pcm_hw_params_set_access(this->__handle, this->__params, SND_PCM_ACCESS_RW_NONINTERLEAVED); //non interleaved
    snd_pcm_hw_params_set_format(this->__handle, this->__params, SND_PCM_FORMAT_S16_LE); //16bits little-endian
    snd_pcm_hw_params_set_channels(this->__handle, this->__params, this->__stereo_mode); // stereo ou mono


    snd_pcm_hw_params_set_rate_near(this->__handle, this->__params, &this->__rate, NULL); // sample rate (freq echantillonage)
    auto ret = snd_pcm_hw_params_set_period_size_near(this->__handle, this->__params, &this->__frames, NULL); //frames pour une période

    int rc = snd_pcm_hw_params(this->__handle, this->__params);
    if (rc < 0) {
        cout << "ERROR - unable to set hw parameters: " << snd_strerror(rc) << endl;
        exit(1);
    }
}

void alsa_control::record_to_file(std::string filename, int duration_in_us) {
    std::ofstream f;
    int rc;
    int nb_ech = 0;
    snd_pcm_uframes_t period_size;
    unsigned int time_period;


    filename += ".wav";
    f.open(filename, std::ios::binary);
    write_header_wav(f, this->__rate, (short) this->__bits, (short) this->__stereo_mode, 10000); //10000 is a constant because we don't know the size of the recording

    snd_pcm_hw_params_get_period_size(this->__params, &period_size, NULL);
    period_size = 2048;
    snd_pcm_uframes_t size = period_size * 2; /* 2 bytes/sample, 1 channels */
    char *buffer = (char *) malloc(size);


    snd_pcm_hw_params_get_period_time(this->__params, &time_period, NULL);
    time_period = 128000;
    long loops = duration_in_us / time_period;

    while (loops-- > 0) {
        rc = (int) snd_pcm_readi(this->__handle, buffer, period_size);
        if (rc == -EPIPE) {
            cout << "ERROR - overrun occurred" << endl;
            snd_pcm_prepare(this->__handle);
        } else if (rc < 0) {
            cout << "ERROR - error from read: " << snd_strerror(rc) << endl;
        } else if (rc != (int) period_size) {
            cout << "ERROR - short read, read " << rc << " frames" << endl;
        }

        if (!(loops % 10))cout << loops << endl;

        f.write(buffer, rc * 2);
        nb_ech += rc;
    }

    f.close();
    f.open(filename, std::ios::binary | std::ios::in);
    write_header_wav(f, this->__rate, (short) this->__bits, (short) this->__stereo_mode, nb_ech);
    f.close();
    free(buffer);
}

我刚刚想出了一种方法让它起作用。但是我一直想知道为什么这些功能不能正常工作。

snd_pcm_hw_params_get_period_size(this->_params, period_size, NULL);snd_pcm_hw_params_get_period_time(this->_params, time_period, NULL);set_parameters_ALSA 方法中工作正常。

然后我将他们作为私人成员添加到我的 class 中:

snd_pcm_uframes_t _period_size;unsigned int _time_period;

并将两个函数更改为:

void alsa_control::set_parameters_ALSA() {
    snd_pcm_hw_params_any(this->_handle, this->_params); // def values
    snd_pcm_hw_params_set_access(this->_handle, this->_params, SND_PCM_ACCESS_RW_NONINTERLEAVED); //non interleaved
    snd_pcm_hw_params_set_format(this->_handle, this->_params, SND_PCM_FORMAT_S16_LE); //16bits little-endian
    snd_pcm_hw_params_set_channels(this->_handle, this->_params, this->_stereo_mode); // stereo ou mono


    snd_pcm_hw_params_set_rate_near(this->_handle, this->_params, &this->_rate, NULL); // sample rate (freq echantillonage)
    snd_pcm_hw_params_set_period_size_near(this->_handle, this->_params, &this->_frames, NULL); //frames pour une période

    int rc = snd_pcm_hw_params(this->_handle, this->_params);
    if (rc < 0) {
        cout << "ERROR - unable to set hw parameters: " << snd_strerror(rc) << endl;
        exit(1);
    }

    snd_pcm_hw_params_get_period_size(this->_params, &this->_period_size, NULL);
    snd_pcm_hw_params_get_period_time(this->_params, &this->_time_period, NULL);
}

void alsa_control::record_to_file(std::string filename, int duration_in_us) {
    std::ofstream f;
    int rc;
    int nb_ech = 0;

    filename += ".wav";
    f.open(filename, std::ios::binary);
    write_header_wav(f, this->_rate, (short) this->_bits, (short) this->_stereo_mode, 10000); //10000 is an arbitrary constant because we don't know the size of the recording

    snd_pcm_uframes_t size = this->_period_size * 2; /* 2 bytes/sample, 1 channels */

    char *buffer = (char *) malloc(size);
    long loops = duration_in_us / this->_time_period;

    while (loops-- > 0) {
        rc = (int) snd_pcm_readi(this->_handle, buffer, this->_period_size);
        if (rc == -EPIPE) {
            cout << "ERROR - overrun occurred" << endl;
            snd_pcm_prepare(this->_handle);
        } else if (rc < 0) {
            cout << "ERROR - error from read: " << snd_strerror(rc) << endl;
        } else if (rc != (int) this->_period_size) {
            cout << "ERROR - short read, read " << rc << " frames" << endl;
        }

        f.write(buffer, rc * 2);
        nb_ech += rc;
    }

    f.close();
    f.open(filename, std::ios::binary | std::ios::in);
    write_header_wav(f, this->_rate, (short) this->_bits, (short) this->_stereo_mode, nb_ech);
    f.close();
    free(buffer);
}

编辑:
这个问题好像和驱动有关。
使用:Advanced Linux Sound Architecture Driver Version k3.16.0-31-generic 不适用于问题版本
使用:Advanced Linux Sound Architecture Driver Version k3.18.8+ 效果很好