Welcome to the most active Linux Forum on the web.
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 10-05-2005, 10:40 AM   #1
Registered: Jul 2005
Location: india,coimbatore
Distribution: Fedora core 4
Posts: 85

Rep: Reputation: 15
how to write a program to record audio in linux using c

Hello all,

Iam working on an application which requires recording and playing audio . Iam using the alsa driver and my kernel version is 2.6.11 OS version is fedora core 4. I found some sample code in the following link . The play back program worked fine i was able to play a wave file by forcing a sample wave file as an input to the executable. But i have problems recording audio in the same format. I tried to google and went thro the pcm api's but could not find any better solution. If some one has worked on the same lines or if u know something that wud help please let me know.

Old 10-05-2005, 10:47 AM   #2
Ross Clement
Registered: Jun 2003
Location: UK
Distribution: Redhat 9 (work) Fedora 1 (home)
Posts: 171

Rep: Reputation: 30
One possible library to use is portaudio. Portaudio is an advantage if you want to write programs that run on a variety of operating systems with a minimum of rewriting.


Old 10-06-2005, 01:48 AM   #3
Registered: Jul 2005
Location: india,coimbatore
Distribution: Fedora core 4
Posts: 85

Original Poster
Rep: Reputation: 15
Hi Ross,
Thanks for your reply . I went through the link u had given me , and i found that the port audio SDK will be compatible only for the OSS driver but since iam using ALSA driver , I think that this might not just work out and it is specific for my work to do it in the ALSA audio driver. Anyway thank u for ur reply.

S.Suresh Stephen
Old 07-09-2012, 08:20 PM   #4
LQ Newbie
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
Lightbulb Setting up alsa. and specifically that example.

Hi. I know this is an old thread but I just wanted to say that since that demo is still actually LIVE and people are still citing it.

And almost no decent demo's exist.

The following may well be the reason you didn't get your card working:
(I didn't do it all but you'll get the gist, and hopefully glean how to get other demo's working)


/// Many devices don't support "near" - mine didn't.
/// However you can just use snd_pcm_hw_params_set_rate and pick a rate THAT IS supported by your hardware.
/// Selecting the plug, plughw, plughw:0, plughw:0,0 device provide automagic conversion but may not be what was intended
/// 44100 and 48000 are good choices here generally - as is snd_pcm_hw_params_set_rate if you have the luxury of programming for known HW

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

  /// Again this set and get period size construct was not supported
  /// The program worked without them IIRC
  /// Mine defaulted to 8 frames anyway with no params for this.
  /// arecord -vv > test.wav throws out some stuff
  /// Determine your default frame time some other way if this seggies 
  /// or just ignore it and comment out the code if it's not essential to operation
  /* Set period size to 32 frames. */
  frames = 32;
                              params, &frames, &dir);

  /* Write the parameters to the driver */
  rc = snd_pcm_hw_params(handle, params);
  if (rc < 0) {
            "unable to set hw parameters: %s\n",

  /// This seggied on me - 8 was my default size IIRC
  /// Fudge it if you have to.
  /* Use a buffer large enough to hold one period */
  snd_pcm_hw_params_get_period_size(params, &frames,
  size = frames * 4; /* 2 bytes/sample, 2 channels */
  buffer = (char *) malloc(size);

Simply (ha!) work your way through the unsupported function replacing with similar things. set_near with set etc.
About 4 or 5 things don't work out-of-the-box on this, and other demo's, set_near is often a culprit.

Hope this helps relieve some frustration.

There are a few other decent demos out there.

Alan's demo here www . saunalahti . fi /~s7l/blog/2005/08/21/Full%20Duplex%20ALSA (remove the spaces) whilst incomplete is more discursive.
Hope that helps.


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
How to record audio in linux AndreTapxure Linux - Software 1 08-10-2004 05:17 AM
How to record audio AndreTapxure Linux - General 1 08-10-2004 03:50 AM
How to write a Vietnamese keyboard program for Linux Namhh Linux - Software 2 10-15-2003 09:31 AM
Linux in vague won't record audio. Help. roesingape Linux - Software 2 06-13-2003 10:08 AM
a program to record audio input matt_w_lambert Linux - Software 4 04-27-2003 04:46 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:37 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