Solved

Simple ALSA Prgramming problem

Posted on 2004-04-26
13
2,269 Views
Last Modified: 2008-02-01
Hai all.....

    I am new to Linux Programming.......
I am trying to write a simple 'c'  program using ALSA(Advanced Linux Sound Architecture  v1.0.4rc1) Library API......
(ALSA Drivers are installed and works successfully).....And If I try to compile the following pogram i get the following error message........

error message:
-------------------

/tmp/ccY7c0Dq.o(.text+0x46): In function `main':
: undefined reference to `snd_pcm_open'
collect2: ld returned 1 exit status



Actual code:
-----------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include <errno.h>
#include <getopt.h>
#include <alsa/asoundlib.h>
#include <sys/time.h>

char *device = "plughw:0,0";                    /* playback device */
snd_pcm_format_t format = SND_PCM_FORMAT_S16;   /* sample format */
unsigned int rate = 44100;                      /* stream rate */
unsigned int channels = 1;                      /* count of channels */
unsigned int buffer_time = 500000;              /* ring buffer length in us */
unsigned int period_time = 100000;              /* period time in us */
double freq = 440;                              /* sinusoidal wave frequency in Hz */
int verbose = 0;                                /* verbose flag */
                                                                                                     
snd_pcm_sframes_t buffer_size;
snd_pcm_sframes_t period_size;
snd_output_t *output = NULL;
int main(int argc, char *argv[])
{
struct option long_option[] =
        {
                {"help", 0, NULL, 'h'},
                {"device", 1, NULL, 'D'},
                {"rate", 1, NULL, 'r'},
                {"channels", 1, NULL, 'c'},
                {"frequency", 1, NULL, 'f'},
                {"buffer", 1, NULL, 'b'},
                {"period", 1, NULL, 'p'},
                {"method", 1, NULL, 'm'},
                {"verbose", 1, NULL, 'v'},
                {NULL, 0, NULL, 0},
       };
        snd_pcm_t *handle;
        int err, morehelp;
        snd_pcm_hw_params_t *hwparams;
        snd_pcm_sw_params_t *swparams;
        int method = 0;
        signed short *samples;
        unsigned int chn;
        snd_pcm_channel_area_t *areas;
                                                                                                       
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
{
                printf("Playback open error: %s\n",0 );
                return 0;
}
                                                                                                       
}
0
Comment
Question by:soundar110
  • 7
  • 5
13 Comments
 
LVL 9

Accepted Solution

by:
Alf666 earned 200 total points
Comment Utility
Did you link against the alsa lib at compile time ?

like gcc myprogram.c -o myprogram -lasound

0
 

Expert Comment

by:kksoundar
Comment Utility
I got a right answer ..........
like u said i should add linker option.............
right....
but it seems that -lasound can't be added...........
instead i am adding -lalsatoss ............
I thought that i am using the OSS/Free emulator for ALSA..............
if so how can i add -lasound ...........
0
 
LVL 9

Expert Comment

by:Alf666
Comment Utility
It may be that you have not installed the proper alsa development libraries.

You should have a libasound.so somewhere.

How did you install alsa ? RPM or tar/configure/make/install ?

It appears that you have multiple EE accounts. This is not right. Moderators will not be very pleased with this...
0
 

Author Comment

by:soundar110
Comment Utility
I have installed ALSA using RPMs from its native site.
And I can able to see libasound.so in  \usr\lib directoy......
But i can't able to use that while compiling............
(bcoz  libasound.a file is not there as like libalsatoss.a)
0
 
LVL 9

Expert Comment

by:Alf666
Comment Utility
I don't know where you got your RPMs from as alsa does not distribute any.

What you may be missing is the alsa-devel package.

Which Linux distribution are you using ?
0
 

Author Comment

by:soundar110
Comment Utility
I am using Redhat 9 (Kernal version 2.4.20)
I have downloaded alsa packages from http://www.alsa-project.org/alsa/ftp/
I can't able to find the alsa-devel package there.......
could u tell me please where can i find it..........
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 9

Expert Comment

by:Alf666
Comment Utility
So, they are not RPMs. They are tar files.

Which means that you have compiled and installed them yourself ?

In this case, it is normal that you don't have a static (.a) library file as the alsa distrib has a small bug in it's libtool scripts.

But you should be able to link against the shared lib.

Please, post the command you use to compile, and the errors you get.
0
 

Author Comment

by:soundar110
Comment Utility
Yeh its  all tar files.........
and i installed them by myself.....
it shows no error while i using
   cc test.c -lalsatoss
but if i use -lasound instead of -lalsatoss it shows error.......
I got following result if ia m trying lsmod command .......

Module                  Size  Used by    Not tainted

snd-pcm-oss            44900   0  (autoclean)
snd-mixer-oss          16728   0  (autoclean) [snd-pcm-oss]
snd-intel8x0           24744   0  (autoclean)
snd-ac97-codec         59900   0  (autoclean) [snd-intel8x0]
snd-mpu401-uart         5360   0  (autoclean) [snd-intel8x0]
snd-rawmidi            18720   0  (autoclean) [snd-mpu401-uart]
snd-seq-device          6204   0  (autoclean) [snd-rawmidi]
snd-pcm                84384   0  (autoclean) [snd-pcm-oss snd-intel8x0]
snd-timer              19812   0  (autoclean) [snd-pcm]
snd                    46116   0  (autoclean) [snd-pcm-oss snd-mixer-oss snd-intel8x0 snd-ac97-codec snd-mpu401-uart snd-rawmidi snd-seq-device snd-pcm snd-timer]
gameport                3364   0  (autoclean) [snd-intel8x0]
soundcore               6404   4  (autoclean) [snd]
snd-page-alloc          9300   0  (autoclean) [snd-intel8x0 snd-pcm]
ide-cd                 35708   0  (autoclean)
cdrom                  33728   0  (autoclean) [ide-cd]
agpgart                47776   4  (autoclean)
parport_pc             19076   1  (autoclean)
lp                      8996   0  (autoclean)
parport                37056   1  (autoclean) [parport_pc lp]
autofs                 13268   0  (autoclean) (unused)
3c59x                  30704   1
nls_iso8859-1           3516   1  (autoclean)
nls_cp437               5116   1  (autoclean)
vfat                   13004   1  (autoclean)
fat                    38808   0  (autoclean) [vfat]
keybdev                 2944   0  (unused)
mousedev                5492   1
hid                    22148   0  (unused)
input                   5856   0  [keybdev mousedev hid]
usb-uhci               26348   0  (unused)
usbcore                78784   1  [hid usb-uhci]
ext3                   70784   1
jbd                    51892   1  [ext3]
0
 
LVL 9

Expert Comment

by:Alf666
Comment Utility
Could you be more specific about the errors ?

What happens at compile time ? Errors ? Please cut/paste.
0
 

Author Comment

by:soundar110
Comment Utility
Hurrah.......
    This time I can get able to compile without errors (using -lasound itself)........I donno what miracle happened..............
  Dear  Alf666 .......Surely I will give u Whole points ...........
I want just another simple help...........
I got the sample programs from  http://www.alsa-project.org/alsa-doc/alsa-lib/examples.html
In that examples I can't able to understand the terms they are using like
period time,period size, stream rate, buffer size,buffer period etc.,I got into many sites to get help ........But I can't able to get the clear idea .......... It would be very helpful to me if U help me in this regard............

Soundar
0
 
LVL 9

Expert Comment

by:Alf666
Comment Utility
You should read the docs in :

http://www.alsa-project.org/alsa-doc/alsa-lib/

I honestly can not help you in this matter as it's related to audio waves formats and I'm not good at this :-)
0
 

Author Comment

by:soundar110
Comment Utility
Thanx Very much Alf666

Soundar
0
 
LVL 9

Expert Comment

by:Alf666
Comment Utility
Glad it worked.

Just one question though.

Why a grade of "B" when you got the exact answer to your question ?
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now