Statistics
| Branch: | Revision:

chunker-player / capture / audio.c @ 1e69ae95

History | View | Annotate | Download (2.6 KB)

1
#include "audio.h"
2
#include "capture.h"
3

    
4
int init_audio(char* audio_dev_name)
5
{
6
    /* Open PCM device for recording (capture). */
7
    if ((err = snd_pcm_open (&capture_handle, audio_dev_name, SND_PCM_STREAM_CAPTURE, 0)) < 0)
8
    {
9
        fprintf (stderr, "cannot open audio device %s (%s)\n", audio_dev_name, snd_strerror (err));
10
        return err;
11
    }
12
    /* Allocate a hardware parameters object. */
13
    snd_pcm_hw_params_alloca(&hw_params);
14

    
15
    /* Fill it in with default values. */
16
    snd_pcm_hw_params_any(capture_handle, hw_params);
17

    
18
    /* Set the desired hardware parameters. */
19

    
20
    /* Interleaved mode */
21
    snd_pcm_hw_params_set_access(capture_handle, hw_params, AUDIO_INTERLEAVED);
22

    
23
    /* Signed 16-bit little-endian format */
24
    snd_pcm_hw_params_set_format(capture_handle, hw_params, AUDIO_FORMAT);
25

    
26
    /* Two channels (stereo) */
27
    snd_pcm_hw_params_set_channels(capture_handle, hw_params, AUDIO_CHANNELS);
28

    
29
    /* bits/second sampling rate */
30
    int val = AUDIO_SAMPLE_RATE;
31
    int dir;
32
    snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &val, &dir);
33

    
34
    /* Set period size to AUDIO_PERIOD frames. */
35
    frames = AUDIO_PERIOD;
36
    snd_pcm_hw_params_set_period_size_near(capture_handle, hw_params, &frames, &dir);
37

    
38
    /* Write the parameters to the driver */
39
    err = snd_pcm_hw_params(capture_handle, hw_params);
40
    if (err < 0) {
41
        fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(err));
42
        return err;
43
    }
44

    
45
    /* Use a buffer large enough to hold one period */
46
    snd_pcm_hw_params_get_period_size(hw_params, &frames, &dir);
47
    buffer_size = frames * 4; /* 2 bytes/sample, 2 channels */
48
    audio_buffer = (char *) malloc(buffer_size);
49

    
50
    snd_pcm_hw_params_get_period_time(hw_params, &val, &dir);
51

    
52
    audio_s_bit = snd_pcm_hw_params_get_sbits(hw_params);
53
}
54

    
55
void read_sample()
56
{
57
    int rc = snd_pcm_readi(capture_handle, audio_buffer, frames);
58
    if (rc == -EPIPE) {
59
      /* EPIPE means overrun */
60
      fprintf(stderr, "overrun occurred\n");
61
      snd_pcm_prepare(capture_handle);
62
    } else if (rc < 0) {
63
      fprintf(stderr, "error from read: %s\n", snd_strerror(rc));
64
    } else if (rc != (int)frames) {
65
      fprintf(stderr, "short read, read %d frames\n", rc);
66
    }
67
    else
68
    {
69
        //rc = write(1, audio_buffer, buffer_size);
70
        //if (rc != buffer_size) fprintf(stderr, "short write: wrote %d bytes\n", rc);
71

    
72
        int gotbytes = snd_pcm_frames_to_bytes(capture_handle, rc);
73
        process_sample(audio_buffer, gotbytes);
74
    }
75
}
76

    
77
void close_audio_device()
78
{
79
    snd_pcm_drain(capture_handle);
80
    snd_pcm_close(capture_handle);
81
    free(audio_buffer);
82
}