LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to write a program to record audio in linux using c (https://www.linuxquestions.org/questions/programming-9/how-to-write-a-program-to-record-audio-in-linux-using-c-369997/)

stephenwalter 10-05-2005 10:40 AM

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 http://www.linuxjournal.com/article/6735 . 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.

Regards,
S.SureshStephen

Ross Clement 10-05-2005 10:47 AM

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.

http://www.portaudio.com/

Cheers,

Ross-c

stephenwalter 10-06-2005 01:48 AM

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.

Regards,
S.Suresh Stephen

twobob 07-09-2012 08:20 PM

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)

FROM THE DEMO: WITH EXTRA NOTES Added as ///:


/// 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
///

Code:


  /* 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;
  snd_pcm_hw_params_set_period_size_near(handle,
                              params, &frames, &dir);

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

  /// 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,
                                    &dir);
  size = frames * 4; /* 2 bytes/sample, 2 channels */
  buffer = (char *) malloc(size);

ELIDED....

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.


All times are GMT -5. The time now is 01:13 AM.