0

I'm trying to record sound on my linux (debian) embedded device with alsa library. My embedded hardware is this [1], and according to its datasheet page 33 [2],

Analog audio signals are featured by the on-SOM TLV320AIC3106 audio codec.

and the datasheet of this Texas Instruments audio codec [3],

Supports Rates From 8 kHz to 96 kHz

I use the example application code for alsa lib, for initial work I didn't change the code. In the example code, the sampling rate was set to 44100Hz. I successfully recorded sound and played after. For now, I think, I can record sound with alsa-lib with the sampling rate of 8000Hz based on the datasheets. I set the sampling rate to 8000Hz but while the alsa configuration, it changes to 16000Hz. I set sampling rate to 8000Hz;

snd_pcm_hw_params_set_rate_near(handle, params, &(record_params->rate), &dir);
snd_pcm_hw_params_set_channels(handle, params, record_params->channel);
rc = snd_pcm_hw_params(handle, params);

But after invoking this method;

snd_pcm_hw_params_get_period_time(params, &(record_params->rate), &dir);

it changes to 16000. There is no other method call between above. Are my settings wrong or may be the codec doesn't support for 8kHz?

UPDATE: When I set rate to 16000, it changes to 8000. I'm really confused more.

[1] = http://www.variscite.com/products/system-on-module-som/cortex-a9/dart-mx6-cpu-freescale-imx6

[2] = http://www.variscite.com/images/stories/DataSheets/DART-MX6/DART-MX6_v1_2_datasheet_v2_1.pdf

[3] = http://www.ti.com/lit/ds/symlink/tlv320aic3106.pdf

Black Glix
  • 689
  • 2
  • 11
  • 26
  • You must always check error codes. Why are you asking for the period time? And what are the values of `rate` and `dir` before and after the call? – CL. Feb 17 '17 at 13:09
  • `dir` is 0 and the `rate` is 8000 as I wanted when I set. As I said, after asking for period, it changes to 16000. The reason why I ask for period is to decide the size of buffer. I will check the error codes of my invokes and will update the post if anything wrong – Black Glix Feb 17 '17 at 13:14
  • Are those the values *before* the call, or *after* the call, or both? And why do you assume that the rate is related to the period time? – CL. Feb 17 '17 at 13:31
  • Why are you trying to store the period time into the field `record_params->rate`? – Ctx Feb 17 '17 at 14:06
  • @Ctx, I messed up my code so you're definitely right. I don't understand what is happening with period related methods. I'm starting over. – Black Glix Feb 21 '17 at 07:48

1 Answers1

0

Period time and rate are two different things.

The period of a PCM is basically the amount of frames that get transferred between device interrupts. It's done this way because making data transfers to a device frame by frame would be extremely inefficient.

The ALSA library allows the setting of the period size to be specified by microseconds (using snd_pcm_get_period_time) or by frame count (using snd_pcm_get_period_size).

If you're trying to calculate what size buffer to allocate for reading or writing to a PCM, it would be more intuitive to use snd_pcm_get_period_size (which returns the number of frames in a period) and then call snd_pcm_frames_to_bytes, which converts a frame count of a PCM to a byte count.

tay10r
  • 4,234
  • 2
  • 24
  • 44
  • I'm confused about the alsa library terminology, about frame and period especially. I want to record sound with the parameters of (1 channel [mono], 8bit PCM and rate of 8000Hz). With this parameters, my buffer should be 1 (channel) * 8 (bit per sample) * 8000 (rate) = 64000 bit, 8000 byte for one second, shouldn't be? I don't understand the frame and period parameters exactly. – Black Glix Feb 21 '17 at 07:45
  • @BlackGlix your buffer size calculation would be correct. In ALSA, a frame for a two channel audio buffer is two samples, for a three channel audio buffer it is three channels. Period size is the size of the audio buffer that is transferred between the computer and the audio device. – tay10r Feb 21 '17 at 20:03