Attachment 'pcm_minimal.c'
Download 1 /*
2 * pcm_minimal.c - simple noise generator as ALSA application
3 * Copyright (c) 2006 Jaroslav Kysela <perex@perex.cz>
4 * 2013 Takashi Sakamoto <o-takashi@sakamocchi.jp>
5 *
6 * This application is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * NOTE: this application is based on /test/pcm_min.c in alsa-lib package.
21 */
22
23 /*
24 * In debian, 'libasound2-dev' package includes this header
25 */
26 #include <alsa/asoundlib.h>
27
28 /*
29 * compile:
30 * $ gcc ./pcm_minimal.c -lasound -o ./noise
31 */
32
33 /* playback PCM node */
34 static char *device = "default";
35
36 /* random samples */
37 unsigned char buffer[16 * 1024] = {0};
38
39 int main(void)
40 {
41 unsigned int i;
42 snd_pcm_t *handle;
43 snd_pcm_hw_params_t *hw_params;
44 snd_pcm_sframes_t frames;
45
46 /* generate noise samples */
47 for (i = 0; i < sizeof(buffer); i++)
48 buffer[i] = random() & 0xff;
49
50 /* open with blocking mode */
51 snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0);
52
53 /* set hardware parameters */
54 snd_pcm_hw_params_malloc(&hw_params);
55 snd_pcm_hw_params_any(handle, hw_params);
56 snd_pcm_hw_params_set_format(handle, hw_params, SND_PCM_FORMAT_U8);
57 snd_pcm_hw_params_set_access(handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
58 snd_pcm_hw_params_set_channels(handle, hw_params, 1);
59 snd_pcm_hw_params_set_rate(handle, hw_params, 48000, 0);
60 snd_pcm_hw_params(handle, hw_params);
61 snd_pcm_hw_params_free(hw_params);
62
63 /* write samples into buffer */
64 for (i = 0; i < 20; i++) {
65 frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
66 if (frames < 0)
67 frames = snd_pcm_recover(handle, frames, 0);
68 if (frames < 0) {
69 ; /* cannot resolve XRUN */
70 }
71 if (frames > 0 && frames < (long)sizeof(buffer)) {
72 ; /* cannot write all of samples */
73 }
74 }
75
76 snd_pcm_drain(handle);
77 snd_pcm_close(handle);
78 return 0;
79 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.
