0

I'm trying to encalpsulate alsa inside a class. The problem is when i try to use :

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

or

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

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

in the cpp it send 0 in period and seems to be unmodified for period_time

The .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 */

and the .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);
}
Waxo
  • 1,816
  • 16
  • 25
  • Totally unrelated to your question and your problem, but you need to read about [the rules regarding leading underscore in C++ names](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – Some programmer dude Feb 17 '15 at 13:08
  • You are using [alloca](http://man7.org/linux/man-pages/man3/alloca.3.html) wrong. – CL. Feb 17 '15 at 16:16

1 Answers1

1

I've just figured one way to make it works. But i'm always wondering why the functions don't works correctly.

snd_pcm_hw_params_get_period_size(this->_params, period_size, NULL); and snd_pcm_hw_params_get_period_time(this->_params, time_period, NULL); works fine when inside the set_parameters_ALSA method.

Then i've just added them to my class as private members :

snd_pcm_uframes_t _period_size; and unsigned int _time_period;

And changed the two functions to :

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);
}

and

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);
}

EDIT :
This problem seems to be related to driver.
Using : Advanced Linux Sound Architecture Driver Version k3.16.0-31-generic don't work with the question version
Using : Advanced Linux Sound Architecture Driver Version k3.18.8+ works good

Waxo
  • 1,816
  • 16
  • 25