This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

how to play sound using user sample application instead of aplay

i am using omap lcdk.. to play audio.. when i use aplay with .wav files i can hear sound.. but instead of aplay .. i want to create user sample application using this link.. 

http://processors.wiki.ti.com/index.php/AM335x_Audio_Driver's_Guide#Sample_Applications

copied the the playback command open, set params,etc..and compiled with this executable how i can run .wav  files.. 

  • Hi Thangameena,

    Why don't you try like this (just using simple "system" command) ?

    Or Is there any intention to use "ALSA 'C' app ?

    int main() {
        system("aplay sound.wav");
        return 0;
    }

    You can also feed the ".wav" file while executing the binary through "argv", you have to modify the "argc and argv stuffs in 'C'' code.

  • this is my code.. 

    #include <stdio.h>
    #include <stdlib.h>
    #include <alsa/asoundlib.h>

    #define BUFF_SIZE 4096

    int main (int argc, char *argv[])
    {
    int err;
    short buf[BUFF_SIZE];
    int rate = 44100; /* Sample rate */
    unsigned int exact_rate; /* Sample rate returned by */
    /* Handle for the PCM device */
    snd_pcm_t *playback_handle;
    /* Playback stream */
    snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
    /* This structure contains information about the hardware and can be used */
    /*tospecify the configuration to be used for */
    /* the PCM stream. */
    snd_pcm_hw_params_t *hw_params;

    static char *device = "default"; /* playback device */

    /* Open PCM. The last parameter of this function is the mode. */
    //if ((err = snd_pcm_open (&playback_handle, device, stream, 0)) < 0) {
    if ((err = snd_pcm_open (&playback_handle,device, stream, 0)) < 0) {
    fprintf (stderr, "cannot open audio device (%s)\n", snd_strerror (err));
    exit (1);
    }

    /* Allocate the snd_pcm_hw_params_t structure on the stack. */
    if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
    fprintf (stderr, "cannot allocate hardware parameters (%s)\n", snd_strerror (err));
    exit (1);
    }

    /* Init hwparams with full configuration space */
    if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
    fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err));
    exit (1);
    }

    /* Set access type. */
    if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
    fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err));
    exit (1);
    }

    /* Set sample format */
    if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
    fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err));
    exit (1);
    }

    /* Set sample rate. If the exact rate is not supported by the hardware, use nearest possible rate. */
    exact_rate = rate;
    if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, &exact_rate, 0)) < 0) {
    fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err));
    exit (1);
    }

    if (rate != exact_rate) {
    fprintf(stderr, "The rate %d Hz is not supported by your hardware.\n ==> Using %d Hz instead.\n", rate, exact_rate);
    }

    /* Set number of channels */
    if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 2)) < 0) {
    fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err));
    exit (1);
    }

    /* Apply HW parameter settings to PCM device and prepare device. */
    if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
    fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err));
    exit (1);
    }

    snd_pcm_hw_params_free (hw_params);

    if ((err = snd_pcm_prepare (playback_handle)) < 0) {
    fprintf (stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror (err));
    exit (1);
    }

    /* Write some junk data to produce sound. */
    if ((err = snd_pcm_writei (playback_handle, buf, BUFF_SIZE/2)) != BUFF_SIZE/2) {
    fprintf (stderr, "write to audio interface failed (%s)\n", snd_strerror (err));
    exit (1);
    } else {
    fprintf (stdout, "snd_pcm_writei successful\n");
    }
    snd_pcm_close (playback_handle);

    exit (0);
    }

    gcc -o audio audio.c -lasound

    ./audio  -D 'hw:0,0' -r 48000 -f S16_LE -c 2  bird.wav

     but its not playing..   

    ya i need to use alsa c app

  • This program is not playing anything because it never writes anything into buf.

    What do you want to do in your program that aplay doesn't do?

  •  ya now i changed the code .. to write into buffer..

    now compiled and run ./audio < bird.wav.. now its playing in my pc.. then i complied for my target board  using arm-arago..  but its not playing  rather i get some noise .. 

    this is the changes i done

    {

    loops = 5000000 / val;

    while (loops > 0) {
    loops--;
    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);
    }
    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);

    }

  • Does aplay work with this file?

    Does the program print anything?

  • ya aplay works.. 

    in target i will give aplay bird.wav.. i can hear sound..

    if i give my compiled binary  to the target

    ./audio  < bird.wav 

    it will produce noise..

    underrun occurred
    underrun occurred
    underrun occurred
    underrun occurred
    underrun occurred
    underrun occurred
    underrun occurred
    short read: read 28 bytes
    end of file on input

  • Hi Ranjana,

    The "underrun" issue seems to be the code is only for loopback the data for 5 seconds.

    So, you can record and play simultaneously through piping.

    Change

    size = frames * 4; /* 2 bytes/sample, 2 channels */

    TO

    size = frames * sizeof(double);

  • its still underrun.. i changed the size.. working in lcdk.. so according to audio ic .. did i need to change any files.. 

    0272.example.c

    compiled using ..arm-arago-linux-gnueabi-gcc -o example  example.c -lasound

    run ./example < musi.wav

  • What board and processor are you using ?

    custom or TI EVM ?

  • hi stalin,

    i am using omap l138 lcdk evm..   

    the same program playing musing in my pc.. 

    my doubt is did i need to put any library files  in lcdk.??