Basic PCM Recording

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



It seems like I must be missing something when it comes to recording
audio from a device. What I expect to happen is for me to set some
simple parameters (much like I do using OSS) and then have the API
tell me what the best buffer size is for the device so that I can read
reliably.  For example ...

#include <alsa/asoundlib.h>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
    unsigned int channels = 2;
    snd_pcm_t *pcm;
    assert(snd_pcm_open(&pcm, "hw:0,0", SND_PCM_STREAM_CAPTURE, 0) == 0);
    snd_pcm_hw_params_t *hwp;
    assert(snd_pcm_hw_params_malloc(&hwp) == 0);
    assert(snd_pcm_hw_params_any(pcm, hwp) == 0);
    assert(snd_pcm_hw_params_set_access(pcm, hwp,
SND_PCM_ACCESS_RW_INTERLEAVED) == 0);
    assert(snd_pcm_hw_params_set_format(pcm, hwp, SND_PCM_FORMAT_S16) == 0);
    assert(snd_pcm_hw_params_set_channels(pcm, hwp, channels) == 0);
    assert(snd_pcm_hw_params_set_rate(pcm, hwp, 32000, 0) == 0);
    assert(snd_pcm_hw_params(pcm, hwp) == 0);
    snd_pcm_uframes_t frames;
    assert(snd_pcm_hw_params_get_buffer_size(hwp, &frames) == 0);
    fprintf(stderr, "target=%lu\n", (unsigned long)frames);
    snd_pcm_hw_params_free(hwp);
    int16_t buffer[frames*channels];
    snd_pcm_sframes_t actual;
again:
    while ((actual = snd_pcm_readi(pcm, buffer, frames)) ==
(snd_pcm_sframes_t){ frames })
    {
        assert(fwrite(buffer, channels*sizeof(int16_t), actual,
stdout) == (size_t){ actual });
    }
    fprintf(stderr, "actual=%ld\n", (long)actual);
    if (actual > 0)
    {
        goto again;
    }
    snd_pcm_close(pcm);
    return 0;
}

I expect this code to run, without interruption, forever dumping audio
to standard output. (Assuming no hardware faults, etc.) However, after
a few seconds it fails when snd_pcm_mmap_read() returns a short read
and then an error of
-32 (-EPIPE).

Are there some calls I'm not making in this startup sequence that are
important? (Yes, I know, I'm not dealing with the mixer here ... one
small example at a time.) Why shouldn't this work? I've seen code
around that sets buffer times and periods and all that crap and if
that is what I must do, what are the basic settings that will apply to
any hardware I'm working with? (Onboard audio card, PCI tuner card,
USB sound card, etc.)

Once I figure this out I need to move on to S/PDIF recording and there
are some real challenges I see there.


Paul Braman

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Alsa-user mailing list
Alsa-user@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/alsa-user


[Index of Archives]     [ALSA Devel]     [Linux Audio Users]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]

  Powered by Linux