Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-30-2011, 03:23 AM   #1
LQ Newbie
Registered: Jun 2008
Posts: 4

Rep: Reputation: 0
snd_pcm_hw_params_set_rate_near() returns huge value

I wrote a sample program as follows to OPEN ALSA sound device and set its hw params -
    /* Open PCM device for playback. */
    rc = snd_pcm_open(&handle, "default",
                    SND_PCM_STREAM_PLAYBACK, 0);
    if (rc < 0) {
            "unable to open pcm device: %s\n",
      return 1;

    /* Allocate a hardware parameters object. */

    /* Fill it in with default values. */
    snd_pcm_hw_params_any(handle, params);

    /* Set the desired hardware parameters. */

    /* Interleaved mode */
    snd_pcm_hw_params_set_access(handle, params,  SND_PCM_ACCESS_RW_INTERLEAVED);

    /* Signed 16-bit little-endian format */
    snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);

    /* One channel (mono) */
    snd_pcm_hw_params_set_channels(handle, params, 1);

    /* 8000 bits/second sampling rate  */
    val = 8000;
    snd_pcm_hw_params_set_rate_near(handle, params,
                                  &val, &dir);

    /* Set period size to 32 frames. */
    frames = 32;
                              params, &frames, &dir);

    /* Write the parameters to the driver */
    rc = snd_pcm_hw_params(handle, params);
Above lines of code works fine if I run it as an independent test program.
When I use the same lines of code in my application, it is failing in snd_pcm_hw_params_any(). It returns "Operation not permitted". So I changed the device name to "plughw:0,0", instead of "default".
With this change, its failing at snd_pcm_hw_params_set_rate_near(). It returns some huge value 192000. Also, following snd_pcm_hw_params_set_period_size() also fails with 699050.

Any idea, why I'm getting this error? Is link to some other library or some header file inclusion, in my application causing this error? I am trying to play 8k, 16 bit PCM audio.
I appreciate your help on this.

Old 08-31-2011, 11:58 AM   #2
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
192000 = 8000 * 24. 24 frames per second of 8000 bits each??

As for snd_pcm_hw_params_set_period_size()
0 otherwise a negative error code if configuration space is empty
So should that be a signed value but you're treating it as unsigned?
Actually both methods are documented as returning like that.
Old 09-02-2011, 05:12 AM   #3
LQ Newbie
Registered: Jun 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Thank you for your response.
So should that be a signed value but you're treating it as unsigned?
Actually both methods are documented as returning like that.
I've assigned the return value to an integer and also printing as integer. It is strange that when I run it as a independent test program, it works fine. Only when I integrate it with my application it is failing. While compiling my test program, I linked with all the libraries I use in my application. Still my test program works fine. I am trying to figure out under what conditions it fails.

Old 09-02-2011, 05:59 AM   #4
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Operation not permitted
An access problem? Is your integrated version running under a different user or environment than your independent test one?
Also again I may not be good with C, but when it returns 0 you'll not notice a problem with signed vs unsigned, but why would a method that's documented to return a negative error value be giving you a large positive value unless you're handling the representation wrong? A quick google indicates you can compile C ints to be signed or unsigned, there's a default but not a strict signedness. I would try get the correct negative error number/code and see what that tells you.

Last edited by Proud; 09-02-2011 at 06:07 AM.
Old 02-18-2016, 07:20 AM   #5
omer levin
LQ Newbie
Registered: Feb 2016
Posts: 1

Rep: Reputation: Disabled
I realize this thread is 5 years old but I've run into the same issue myself.
ALSA has two API versions. It looks like in some cases a weird link issue can occur and cause the wrong version of a function to be called. For example, snd_pcm_hw_params_set_rate_near() in the old API expects the sample rate argument as an integer rather than an int*, and returns the newly set rate - which is why you're getting 192000 instead of 0.

I've posted a question about this to Stack Overflow, with the title "ALSA unexpected results when called from shared library" (sorry, can't post a URL here).
1 members found this post helpful.
Old 02-18-2016, 12:23 PM   #6
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,300

Rep: Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Slackware 13.1 huge smp is not huge cs_strong Slackware 4 11-16-2010 08:41 AM
huge.s genhappyok Slackware 9 04-14-2010 05:57 PM
Huge amount of "PHP Notice:" -> huge logs Braynid Linux - Software 4 03-18-2008 07:35 AM
dd never returns in BackTrack2 manolakis Linux - Newbie 17 03-01-2007 07:53 AM
Huge Huge Problem With Forums!!! The_Insomniac Linux - General 1 06-07-2004 09:15 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:34 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration