snd_pcm_hw_params_set_period_size_near не возвращает правильное значение

Я пытаюсь заключить Алсу в класс.
Проблема в том, когда я пытаюсь использовать:

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

0

Решение

Я только что придумал, как это работает. Но мне всегда интересно, почему функции не работают правильно.

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 метод.

Тогда я только добавил их в свой класс как частные участники:

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 monosnd_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+ работает хорошо

1

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector