ALSA (Advanced Linux Sound architecture): A simple example __linux

Source: Internet
Author: User

ALSA is the abbreviation for Advanced Linux Sound architecture, the Advanced Linux Sound architecture, which provides audio and MIDI on Linux operating systems (musical instrument Digital, Music Device Digital Interface) support

For more information, refer to this article:
http://mysuperbaby.iteye.com/blog/932729

Here are two simple examples to achieve playback, recording functions, respectively

/****************************************************************** ALSA Simple Playback Example ************************** /#define ALSA_PCM_NEW_HW_PARAMS_API #include <alsa/asoundlib.h> int
    Main () {long loops;
    int RC;
    int size;
    Snd_pcm_t *handle;
    snd_pcm_hw_params_t *params;
    unsigned int val;
    int dir;
    snd_pcm_uframes_t frames;

    Char *buffer; /* Open PCM device for playback.
    * * * Open PCM playback equipment/rc = Snd_pcm_open (&handle, "Default", Snd_pcm_stream_playback, 0); if (RC < 0) {fprintf (stderr, "Unable to open PCM device:%s\n", Snd_strerror (R
        c));
    Exit (1); }/* Allocate a hardware Parameters object.

    *///////* Assign a hardware parameter structure body * * SND_PCM_HW_PARAMS_ALLOCA (&AMP;PARAMS); /* Fill it in with default values.

    *////////////Snd_pcm_hw_params_any (handle, params) using default parameters; /* Set the desired hardware parameters. * * * SetSet Hardware Parameters * * * Interleaved mode/* snd_pcm_hw_params_set_access (handle, params, snd_

    pcm_access_rw_interleaved);
                                /* Signed 16-bit Little-endian format *////* Data format is 16-bit small end * * Snd_pcm_hw_params_set_format (handle, params,

    Snd_pcm_format_s16_le);

    /* Two channels (stereo)/////////* Two channels/* snd_pcm_hw_params_set_channels (handle, params, 2);
    /* 44100 Bits/second sampling rate (CD quality)////* sample rate is 44100 * * val = 44100;

    Snd_pcm_hw_params_set_rate_near (handle, params, &val, &dir); /* Set period size to frames.
    * * Set a period of 32 frames/* frames = 32;

    Snd_pcm_hw_params_set_period_size_near (handle, params, &frames, &dir);
    * Write the parameters to the driver * * * to the previously set parameters to the playback device * * rc = Snd_pcm_hw_params (handle, params);
 if (RC < 0) {fprintf (stderr, "Unable to set HW parameters:%s\n",           Snd_strerror (RC));
    Exit (1); }/* Use a buffer large enough to hold one period * * to get a period of data length/Snd_pcm_hw_params_get_period_size (para

    MS, &frames, &dir); * * Because we are 16-bit two channels, so to *2*2 that is *4 * * * size = frames * 4;

    /* 2 Bytes/sample, 2 channels */buffer = (char *) malloc (size); /* We want to loop for 5 seconds * * * To get a period of time length/* Snd_pcm_hw_params_get_period_time (params, &A
    Mp;val, &dir);

    /* 5 seconds in microseconds divided by * period time * * loops = 5000000/val;
        while (Loops > 0) {loops--;
        * * Get data from standard input/rc = read (0, buffer, size);
            if (rc = = 0) {fprintf (stderr, "End of File on input\n");
        Break
        else if (RC!= size) {fprintf (stderr, "short read:read%d bytes\n", RC);
        } * * Play these data/rc = Snd_pcm_writei (handle, buffer, frames); if (RC= =-epipe) {/* epipe means underrun/fprintf (stderr, "underrun occurred\n");
        Snd_pcm_prepare (handle); else if (RC < 0) {fprintf (stderr, "error from Writei:%s\n", snd
        _strerror (RC));
        else if (RC!= (int) frames) {fprintf (stderr, "short write, write%d frames\n", RC);
    } snd_pcm_drain (handle);
    Snd_pcm_close (handle);

    Free (buffer);
return 0;
 }
/********************************************************************* ALSA Simple recording function capture capture ******************** ///* Use the newer ALSA API/* * * #define ALSA_PCM_NEW_HW_PARAMS_API #incl
    Ude <alsa/asoundlib.h> int Main () {long loops;
    int RC;
    int size;
    Snd_pcm_t *handle;
    snd_pcm_hw_params_t *params;
    unsigned int val;
    int dir;
    snd_pcm_uframes_t frames;

    Char *buffer; /* Open PCM Device for recording (capture). * * * Open PCM Capture capture device/rc = Snd_pcm_open (&handle, "Default", Snd_pcm_stream_capt
    URE, 0); if (RC < 0) {fprintf (stderr, "Unable to open PCM device:%s\n", Snd_strerror (R
        c));
    Exit (1); }/* Allocate a hardware Parameters object.

    *///////* Assign a hardware parameter structure body * * SND_PCM_HW_PARAMS_ALLOCA (&AMP;PARAMS); /* Fill it in with default values. */////* Use default parameter/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_r

    w_interleaved);
                                  /* Signed 16-bit Little-endian format *////* 16-bit small end * * Snd_pcm_hw_params_set_format (handle, params,

    Snd_pcm_format_s16_le);

    /* Two channels (stereo)///* Dual Channel * Snd_pcm_hw_params_set_channels (handle, params, 2);
    /* 44100 Bits/second sampling rate (CD quality)/* sample rate/val = 44100;

    Snd_pcm_hw_params_set_rate_near (handle, params, &val, &dir); /* Set period size to frames.
    * * * A period of 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); /* Use a buffer large enough to hold one period * * * to get a period of data size * * SND_PCM_HW_PARAMS_GET_PERIOD_SIZE (para
    MS, &frames, &dir); * * 16-bit dual channel, so to *4/size = frames * 4;

    /* 2 Bytes/sample, 2 channels */buffer = (char *) malloc (size);
                                             /* We want to loop for 5 seconds * * Wait a period of time length * * * SND_PCM_HW_PARAMS_GET_PERIOD_TIME (params,
    &val, &dir);

    loops = 5000000/val;
        while (Loops > 0) {loops--;
        /* Capture data */rc = Snd_pcm_readi (handle, buffer, frames);
            if (rc = =-epipe) {/* Epipe means overrun * * fprintf (stderr, "overrun occurred\n");
        Snd_pcm_prepare (handle); else if (RC < 0) {fprintf (stderr, "error from read:%s")\ n ", Snd_strerror (RC));
        else if (RC!= (int) frames) {fprintf (stderr, "short read, read%d frames\n", RC);
        */* Write to standard output/rc = write (1, buffer, size);
    if (RC!= size) fprintf (stderr, "short write:wrote%d bytes\n", RC);
    } snd_pcm_drain (handle);
    Snd_pcm_close (handle);

    Free (buffer);
return 0;
 }

You can try to run the capture program and direct the output to a file, and then run playback to play the sound data in the file:

./capture > Sound.raw
./playback < Sound.raw

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.