-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_alsa.c
65 lines (58 loc) · 2.07 KB
/
utils_alsa.c
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
// utils_alsa.c
//
// Copyright 2014 Kurt Taylor
// Copyright 2011 Linaro Limited
//
//
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
int get_sample(char *device_name, int num_samples, int sample_rate, unsigned short sine_input[])
{
int rc;
snd_pcm_hw_params_t *parms;
snd_pcm_t *handle;
/* Open the device */
if ((rc = snd_pcm_open (&handle, device_name, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "%s: cannot open pcm device: %s \n", snd_strerror (rc), device_name);
return (-1);
}
//fprintf (stdout, "Device Opened %s \n", device_name);
/* Configure the device */
if ((rc = snd_pcm_hw_params_malloc (&parms)) < 0) {
fprintf (stderr, "%s: cannot malloc parms struct\n", snd_strerror (rc));
return (-1);
}
if ((rc = snd_pcm_hw_params_any (handle, parms)) < 0) {
fprintf (stderr, "%s: cannot set default parms\n", snd_strerror (rc));
return (-1);
}
if ((rc = snd_pcm_hw_params_set_access (handle, parms, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
fprintf (stderr, "%s: cannot set interleaved access\n", snd_strerror (rc));
return (-1);
}
if ((rc = snd_pcm_hw_params_set_format (handle, parms, SND_PCM_FORMAT_U8)) < 0) {
fprintf (stderr, "%s: cannot set format\n", snd_strerror (rc));
return (-1);
}
if ((rc = snd_pcm_hw_params_set_channels (handle, parms, 2)) < 0) {
fprintf (stderr, "%s: cannot set channels\n", snd_strerror (rc));
return (-1);
}
if ((rc = snd_pcm_hw_params_set_rate_near (handle, parms, &sample_rate, 0)) < 0) {
fprintf (stderr, "%s: cannot set rate\n", snd_strerror (rc));
return (-1);
}
if ((rc = snd_pcm_hw_params (handle, parms)) < 0) {
fprintf (stderr, "%s: cannot set parms\n", snd_strerror (rc));
return (-1);
}
snd_pcm_hw_params_free (parms);
/* Get samples of the sine wave */
if ((rc = snd_pcm_readi (handle, sine_input, num_samples)) != num_samples) {
fprintf (stderr, "%s: read failed\n", snd_strerror (rc));
return (-1);
}
snd_pcm_close (handle);
return 0;
}