aboutsummaryrefslogtreecommitdiff
path: root/alsa.c
blob: 727c5a12748709336f559b2e9358e24ad5c34266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* $Id$ */
/*
 * Copyright (c) 2013 Dimitri Sokolyuk <demon@dim13.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <assert.h>
#include <err.h>
#include <stdint.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>

#include "sio.h"

#define STEREO	2
#define RATE	48000
#define FPS	25

static snd_pcm_t *hdl;
static snd_pcm_hw_params_t *par;
static int16_t *buffer;
static unsigned int samples;

struct data {
	int16_t left;
	int16_t right;
};

int
init_sio(void)
{
	snd_pcm_uframes_t round;
	unsigned int rate;
	int rc;

	rc = snd_pcm_open(&hdl, "default", SND_PCM_STREAM_CAPTURE, 0);
	if (rc < 0)
		errx(1, "unable to open pcm device: %s", snd_strerror(rc));

	snd_pcm_hw_params_malloc(&par);
	snd_pcm_hw_params_any(hdl, par);
	snd_pcm_hw_params_set_access(hdl, par,
		SND_PCM_ACCESS_RW_INTERLEAVED);
	snd_pcm_hw_params_set_format(hdl, par,
		SND_PCM_FORMAT_S16_LE);
	snd_pcm_hw_params_set_channels(hdl, par, STEREO);
	snd_pcm_hw_params_set_rate(hdl, par, RATE, 0);

	rc = snd_pcm_hw_params(hdl, par);
	if (rc < 0)
		errx(1, "unable to set hw parameters: %s", snd_strerror(rc));
	
	snd_pcm_hw_params_get_period_size(par, &round, NULL);
	snd_pcm_hw_params_get_rate(par, &rate, 0);
	snd_pcm_hw_params_free(par);
	snd_pcm_prepare(hdl);

	samples = rate / FPS;
	samples -= samples % round;
	if (samples < rate / FPS)
		samples += round;
	warnx("%s round/rate/samples: %lu/%u/%u", __func__,
		round, rate, samples);
	buffer = calloc(samples * STEREO, sizeof(int16_t));
	assert(buffer);

	return samples;
}

size_t
read_sio(int **out, size_t n)
{
	snd_pcm_sframes_t rc;
	int16_t *tmp;
	int i;

	if (n > samples)
		n = samples;

	rc = snd_pcm_readi(hdl, buffer, samples);
	if (rc != samples) {
		warnx("audio read error: %s", snd_strerror(rc));
		if (rc == -EPIPE)
			snd_pcm_prepare(hdl);
	}

	tmp = &buffer[samples - n];

	/* split */
	for (i = 0; i < n * STEREO; i++)
		out[i % STEREO][i / STEREO] = tmp[i];

	return n;
}

void
free_sio(void)
{
	snd_pcm_drain(hdl);
	snd_pcm_close(hdl);
	free(buffer);
}