Thread: ALSA programming

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    ALSA programming

    hello, i try to write a program to record a sound stream from my audiocard(integrated realtek), briefly, without error processing it looks like:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <alsa/asoundlib.h>
    int main(int argc, char **argv) //argv[1] = "hw:0,0"
    int i;  
    int err;
    long buf[128];
    int exact_rate = 44100;
    int time_s = 3;
    static unsigned int channels = 2;        
    snd_pcm_t *playback_handle;
    snd_pcm_hw_params_t *hw_params;    
    char *pcm_name = strdup("plughw:0,0");        
    snd_pcm_t *capture_handle;    
        
    snd_pcm_open (&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0);
    snd_pcm_hw_params_malloc (&hw_params);
    snd_pcm_hw_params_any (capture_handle, hw_params);
    snd_pcm_hw_params_set_access(capture_handle,hw_params,SND_PCM_ACCESS_RW_INTERLEAVED));
    nd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE);
    snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &exact_rate, 0);
    snd_pcm_hw_params_set_channels (capture_handle, hw_params, channels);
    snd_pcm_hw_params (capture_handle, hw_params);
        
    snd_pcm_hw_params_free (hw_params);
    snd_pcm_prepare (capture_handle);
        
        
    int fd = open("file_rec", O_WRONLY), b;
    if(!fd)
     printf("file didn't open\n");
            
            
    for (i = 0; i < time_s*44100/128; i++)
        {            
            b = snd_pcm_readi(capture_handle, buf, 128);              
                    write(fd,buf,128);
        }        
    close(fd);    
    snd_pcm_close (capture_handle);
    snd_pcm_open(&playback_handle,pcm_name,SND_PCM_STREAM_PLAYBACK, 0);
    snd_pcm_hw_params_malloc (&hw_params);
    snd_pcm_hw_params_any (playback_handle, hw_params);
    snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
    snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE);        
    snd_pcm_hw_params_set_rate_near(playback_handle, hw_params, &exact_rate,0);
    snd_pcm_hw_params_set_channels (playback_handle, hw_params, 2);
    snd_pcm_hw_params (playback_handle, hw_params);
        
    snd_pcm_hw_params_free (hw_params);
    snd_pcm_prepare (playback_handle);
    
    fd = open("file_rec", O_RDONLY);
    while((b=read(fd, buf, 128)) > 0)        
      snd_pcm_writei (playback_handle, buf, b);            
            
    snd_pcm_close (playback_handle);
    close(fd);
    exit (0);
    }
    i'm recording a stream in standard audiorecording programm(from mic) and ones in my programm, and get in my programm sound much dirty with a lot of noises. can you help, what can i do??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first thing I would do would be to fix your buffer sizes.

    128 bytes (which is all you're saying, not what you actually have) is pitifully small for 16-bit stereo - it's only 32 samples.

    Coupled with a high sample rate of 44100, you're looking at around 0.75mS to fill the buffer (and for you to deal with it).

    Compare a 10 second recording using standard s/w with a 10 second recording using your s/w. Is there a big difference in file size?
    If yours is a much smaller file, then you're missing data!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    so, i increasaed buffer up to 65536 bytes(with bigger ones for some reason it doesn't work proper), now i get enough distinct sound, but the output is cycled, something like it every time writes in file the same first buf
    what may be wrong?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    This could happen if you are using a non-blocking mode audio stream, so that your attempts to write() to the device are just replacing the data in the ring buffer, before it can be fully played back. The ring buffer will then repeatedly play back its last contents, making a looping noise.

    Make sure the stream (both the capture and playback) are in blocking mode instead of async mode.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  2. Guitar Hero ALSA MIDI output
    By redxine in forum Linux Programming
    Replies: 0
    Last Post: 01-24-2009, 11:03 PM
  3. New 2.6 kernel, ALSA problems
    By bludstayne in forum Tech Board
    Replies: 8
    Last Post: 02-23-2004, 09:58 PM
  4. ALSA sound problem in Linux
    By MathFan in forum Tech Board
    Replies: 0
    Last Post: 04-24-2003, 10:05 AM
  5. Programming with the ALSA API.
    By WebmasterMattD in forum Linux Programming
    Replies: 0
    Last Post: 01-24-2003, 06:54 PM

Tags for this Thread