annotate libao2/ao_alsa5.c @ 1058:cab5ba9ffc6c

fixed AFMT_ stuff (inclue afmt.h)
author arpi_esp
date Fri, 08 Jun 2001 23:31:06 +0000
parents 50c49c976f23
children a3c3e1de6f02
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
996
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
1 /*
1046
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
2 ao_alsa5 - ALSA-0.5.x output plugin for MPlayer
996
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
3
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
4 (C) Alex Beregszaszi <alex@naxine.org>
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
5
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
6 Thanks to Arpi for helping me ;)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
7 */
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
8
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
9 #include <errno.h>
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
10 #include <sys/asoundlib.h>
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
11
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
12 #include "../config.h"
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
13
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
14 #include "audio_out.h"
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
15 #include "audio_out_internal.h"
1058
cab5ba9ffc6c fixed AFMT_ stuff (inclue afmt.h)
arpi_esp
parents: 1055
diff changeset
16 #include "afmt.h"
996
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
17
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
18 extern int verbose;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
19
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
20 static ao_info_t info =
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
21 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
22 "ALSA-0.5.x audio output",
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
23 "alsa5",
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
24 "Alex Beregszaszi <alex@naxine.org>",
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
25 ""
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
26 };
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
27
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
28 LIBAO_EXTERN(alsa5)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
29
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
30 /* global variables:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
31 ao_samplerate
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
32 ao_channels
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
33 ao_format
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
34 ao_bps
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
35 ao_outburst
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
36 ao_buffersize
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
37 */
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
38
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
39 static snd_pcm_t *alsa_handler;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
40 static snd_pcm_format_t alsa_format;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
41 static int alsa_rate = SND_PCM_RATE_CONTINUOUS;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
42
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
43 /* to set/get/query special features/parameters */
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
44 static int control(int cmd, int arg)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
45 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
46 return(CONTROL_UNKNOWN);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
47 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
48
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
49 /*
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
50 open & setup audio device
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
51 return: 1=success 0=fail
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
52 */
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
53 static int init(int rate_hz, int channels, int format, int flags)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
54 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
55 int err;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
56 int cards = -1;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
57 snd_pcm_channel_params_t params;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
58 snd_pcm_channel_setup_t setup;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
59 snd_pcm_info_t info;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
60 snd_pcm_channel_info_t chninfo;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
61
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
62 printf("alsa-init: requested format: %d Hz, %d channels, %s\n", rate_hz,
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
63 channels, audio_out_format_name(format));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
64
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
65 alsa_handler = NULL;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
66
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
67 if (verbose)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
68 printf("alsa-init: compiled for ALSA-%s (%d)\n", SND_LIB_VERSION_STR,
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
69 SND_LIB_VERSION);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
70
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
71 if ((cards = snd_cards()) < 0)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
72 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
73 printf("alsa-init: no soundcards found\n");
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
74 return(0);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
75 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
76
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
77 ao_format = format;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
78 ao_channels = channels - 1;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
79 ao_samplerate = rate_hz;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
80 ao_bps = ao_samplerate*(ao_channels+1);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
81 ao_outburst = OUTBURST;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
82 ao_buffersize = 16384;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
83
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
84 memset(&alsa_format, 0, sizeof(alsa_format));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
85 switch (format)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
86 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
87 case AFMT_S8:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
88 alsa_format.format = SND_PCM_SFMT_S8;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
89 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
90 case AFMT_U8:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
91 alsa_format.format = SND_PCM_SFMT_U8;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
92 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
93 case AFMT_U16_LE:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
94 alsa_format.format = SND_PCM_SFMT_U16_LE;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
95 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
96 case AFMT_U16_BE:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
97 alsa_format.format = SND_PCM_SFMT_U16_BE;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
98 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
99 case AFMT_S16_LE:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
100 alsa_format.format = SND_PCM_SFMT_S16_LE;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
101 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
102 case AFMT_S16_BE:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
103 alsa_format.format = SND_PCM_SFMT_S16_BE;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
104 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
105 default:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
106 alsa_format.format = SND_PCM_SFMT_MPEG;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
107 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
108 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
109
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
110 switch(alsa_format.format)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
111 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
112 case SND_PCM_SFMT_S16_LE:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
113 case SND_PCM_SFMT_U16_LE:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
114 ao_bps *= 2;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
115 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
116 case -1:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
117 printf("alsa-init: invalid format (%s) requested - output disabled\n",
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
118 audio_out_format_name(format));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
119 return(0);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
120 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
121
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
122 switch(rate_hz)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
123 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
124 case 8000:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
125 alsa_rate = SND_PCM_RATE_8000;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
126 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
127 case 11025:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
128 alsa_rate = SND_PCM_RATE_11025;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
129 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
130 case 16000:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
131 alsa_rate = SND_PCM_RATE_16000;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
132 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
133 case 22050:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
134 alsa_rate = SND_PCM_RATE_22050;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
135 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
136 case 32000:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
137 alsa_rate = SND_PCM_RATE_32000;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
138 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
139 case 44100:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
140 alsa_rate = SND_PCM_RATE_44100;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
141 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
142 case 48000:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
143 alsa_rate = SND_PCM_RATE_48000;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
144 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
145 case 88200:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
146 alsa_rate = SND_PCM_RATE_88200;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
147 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
148 case 96000:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
149 alsa_rate = SND_PCM_RATE_96000;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
150 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
151 case 176400:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
152 alsa_rate = SND_PCM_RATE_176400;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
153 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
154 case 192000:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
155 alsa_rate = SND_PCM_RATE_192000;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
156 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
157 default:
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
158 alsa_rate = SND_PCM_RATE_CONTINUOUS;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
159 break;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
160 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
161
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
162 alsa_format.rate = ao_samplerate;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
163 alsa_format.voices = ao_channels*2;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
164 alsa_format.interleave = 1;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
165
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
166 if ((err = snd_pcm_open(&alsa_handler, 0, 0, SND_PCM_OPEN_PLAYBACK)) < 0)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
167 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
168 printf("alsa-init: playback open error: %s\n", snd_strerror(err));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
169 return(0);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
170 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
171
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
172 if ((err = snd_pcm_info(alsa_handler, &info)) < 0)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
173 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
174 printf("alsa-init: pcm info error: %s\n", snd_strerror(err));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
175 return(0);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
176 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
177
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
178 printf("alsa-init: %d soundcard%s found, using: %s\n", cards,
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
179 (cards == 1) ? "" : "s", info.name);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
180
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
181 if (info.flags & SND_PCM_INFO_PLAYBACK)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
182 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
183 bzero(&chninfo, sizeof(chninfo));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
184 chninfo.channel = SND_PCM_CHANNEL_PLAYBACK;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
185 if ((err = snd_pcm_channel_info(alsa_handler, &chninfo)) < 0)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
186 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
187 printf("alsa-init: pcm channel info error: %s\n", snd_strerror(err));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
188 return(0);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
189 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
190 if (chninfo.buffer_size)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
191 ao_buffersize = chninfo.buffer_size;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
192 if (verbose)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
193 printf("alsa-init: setting preferred buffer size from driver: %d bytes\n",
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
194 ao_buffersize);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
195 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
196
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
197 memset(&params, 0, sizeof(params));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
198 params.channel = SND_PCM_CHANNEL_PLAYBACK;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
199 params.mode = SND_PCM_MODE_STREAM;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
200 params.format = alsa_format;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
201 params.start_mode = SND_PCM_START_DATA;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
202 params.stop_mode = SND_PCM_STOP_ROLLOVER;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
203 params.buf.stream.queue_size = ao_buffersize;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
204 params.buf.stream.fill = SND_PCM_FILL_NONE;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
205
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
206 if ((err = snd_pcm_channel_params(alsa_handler, &params)) < 0)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
207 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
208 printf("alsa-init: error setting parameters: %s\n", snd_strerror(err));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
209 return(0);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
210 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
211
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
212 memset(&setup, 0, sizeof(setup));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
213 setup.channel = SND_PCM_CHANNEL_PLAYBACK;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
214 setup.mode = SND_PCM_MODE_STREAM;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
215 setup.format = alsa_format;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
216 setup.buf.stream.queue_size = ao_buffersize;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
217 setup.msbits_per_sample = ao_bps;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
218
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
219 if ((err = snd_pcm_channel_setup(alsa_handler, &setup)) < 0)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
220 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
221 printf("alsa-init: error setting up channel: %s\n", snd_strerror(err));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
222 return(0);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
223 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
224
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
225 if ((err = snd_pcm_channel_prepare(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
226 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
227 printf("alsa-init: channel prepare error: %s\n", snd_strerror(err));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
228 return(0);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
229 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
230
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
231 printf("AUDIO: %d Hz/%d channels/%d bps/%d bytes buffer/%s\n",
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
232 ao_samplerate, ao_channels+1, ao_bps, ao_buffersize,
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
233 snd_pcm_get_format_name(alsa_format.format));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
234 return(1);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
235 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
236
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
237 /* close audio device */
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
238 static void uninit()
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
239 {
1055
50c49c976f23 Added missing declaration of variables 'err'.
bertrand
parents: 1046
diff changeset
240 int err;
50c49c976f23 Added missing declaration of variables 'err'.
bertrand
parents: 1046
diff changeset
241
1046
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
242 if ((err = snd_pcm_playback_drain(alsa_handler)) < 0)
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
243 {
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
244 printf("alsa-uninit: playback drain error: %s\n", snd_strerror(err));
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
245 return;
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
246 }
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
247
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
248 if ((err = snd_pcm_channel_flush(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0)
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
249 {
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
250 printf("alsa-uninit: playback flush error: %s\n", snd_strerror(err));
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
251 return;
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
252 }
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
253
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
254 if ((err = snd_pcm_close(alsa_handler)) < 0)
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
255 {
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
256 printf("alsa-uninit: pcm close error: %s\n", snd_strerror(err));
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
257 return;
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
258 }
996
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
259 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
260
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
261 /* stop playing and empty buffers (for seeking/pause) */
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
262 static void reset()
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
263 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
264 int err;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
265
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
266 if ((err = snd_pcm_playback_drain(alsa_handler)) < 0)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
267 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
268 printf("alsa-reset: playback drain error: %s\n", snd_strerror(err));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
269 return;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
270 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
271
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
272 if ((err = snd_pcm_channel_flush(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
273 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
274 printf("alsa-reset: playback flush error: %s\n", snd_strerror(err));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
275 return;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
276 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
277
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
278 if ((err = snd_pcm_channel_prepare(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
279 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
280 printf("alsa-reset: channel prepare error: %s\n", snd_strerror(err));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
281 return;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
282 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
283 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
284
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 996
diff changeset
285 /* stop playing, keep buffers (for pause) */
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 996
diff changeset
286 static void audio_pause()
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 996
diff changeset
287 {
1046
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
288 int err;
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
289
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
290 if ((err = snd_pcm_playback_drain(alsa_handler)) < 0)
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
291 {
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
292 printf("alsa-pause: playback drain error: %s\n", snd_strerror(err));
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
293 return;
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
294 }
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
295
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
296 if ((err = snd_pcm_channel_flush(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0)
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
297 {
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
298 printf("alsa-pause: playback flush error: %s\n", snd_strerror(err));
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
299 return;
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
300 }
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 996
diff changeset
301 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 996
diff changeset
302
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 996
diff changeset
303 /* resume playing, after audio_pause() */
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 996
diff changeset
304 static void audio_resume()
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 996
diff changeset
305 {
1055
50c49c976f23 Added missing declaration of variables 'err'.
bertrand
parents: 1046
diff changeset
306 int err;
1046
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
307 if ((err = snd_pcm_channel_prepare(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0)
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
308 {
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
309 printf("alsa-resume: channel prepare error: %s\n", snd_strerror(err));
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
310 return;
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
311 }
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 996
diff changeset
312 }
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 996
diff changeset
313
996
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
314 /*
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
315 plays 'len' bytes of 'data'
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
316 returns: number of bytes played
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
317 */
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
318 static int play(void* data, int len, int flags)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
319 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
320 if ((len = snd_pcm_write(alsa_handler, data, len)) != len)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
321 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
322 if (len == -EPIPE) /* underrun? */
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
323 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
324 printf("alsa-play: alsa underrun, resetting stream\n");
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
325 if ((len = snd_pcm_channel_prepare(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
326 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
327 printf("alsa-play: playback prepare error: %s\n", snd_strerror(len));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
328 return(0);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
329 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
330 if ((len = snd_pcm_write(alsa_handler, data, len)) != len)
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
331 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
332 printf("alsa-play: write error after reset: %s - giving up\n",
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
333 snd_strerror(len));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
334 return(0);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
335 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
336 return(len); /* 2nd write was ok */
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
337 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
338 printf("alsa-play: output error: %s\n", snd_strerror(len));
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
339 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
340 return(len);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
341 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
342
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
343 /* how many byes are free in the buffer */
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
344 static int get_space()
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
345 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
346 snd_pcm_channel_status_t ch_stat;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
347
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
348 ch_stat.channel = SND_PCM_CHANNEL_PLAYBACK;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
349
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
350 if (snd_pcm_channel_status(alsa_handler, &ch_stat) < 0)
1046
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
351 return(0); /* error occured */
996
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
352 else
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
353 return(ch_stat.free);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
354 }
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
355
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
356 /* how many unplayed bytes are in the buffer */
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
357 static int get_delay()
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
358 {
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
359 snd_pcm_channel_status_t ch_stat;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
360
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
361 ch_stat.channel = SND_PCM_CHANNEL_PLAYBACK;
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
362
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
363 if (snd_pcm_channel_status(alsa_handler, &ch_stat) < 0)
1046
59fc1f75e486 audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents: 1038
diff changeset
364 return(ao_buffersize); /* error occured */
996
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
365 else
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
366 return(ch_stat.count);
a97eb50a2c3b initial alsa support by al3x
pontscho
parents:
diff changeset
367 }