• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * \file pcm/pcm_mulaw.c
3  * \ingroup PCM_Plugins
4  * \brief PCM Mu-Law Conversion Plugin Interface
5  * \author Abramo Bagnara <abramo@alsa-project.org>
6  * \date 2000-2001
7  */
8 /*
9  *  PCM - Mu-Law conversion
10  *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
11  *
12  *
13  *   This library is free software; you can redistribute it and/or modify
14  *   it under the terms of the GNU Lesser General Public License as
15  *   published by the Free Software Foundation; either version 2.1 of
16  *   the License, or (at your option) any later version.
17  *
18  *   This program is distributed in the hope that it will be useful,
19  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *   GNU Lesser General Public License for more details.
22  *
23  *   You should have received a copy of the GNU Lesser General Public
24  *   License along with this library; if not, write to the Free Software
25  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26  *
27  */
28 
29 #include "bswap.h"
30 #include "pcm_local.h"
31 #include "pcm_plugin.h"
32 
33 #include "plugin_ops.h"
34 
35 #ifndef PIC
36 /* entry for static linking */
37 const char *_snd_module_pcm_mulaw = "";
38 #endif
39 
40 #ifndef DOC_HIDDEN
41 
42 typedef void (*mulaw_f)(const snd_pcm_channel_area_t *src_areas,
43 			snd_pcm_uframes_t src_offset,
44 			const snd_pcm_channel_area_t *dst_areas,
45 			snd_pcm_uframes_t dst_offset,
46 			unsigned int channels, snd_pcm_uframes_t frames,
47 			unsigned int getputidx);
48 
49 typedef struct {
50 	/* This field need to be the first */
51 	snd_pcm_plugin_t plug;
52 	unsigned int getput_idx;
53 	mulaw_f func;
54 	snd_pcm_format_t sformat;
55 } snd_pcm_mulaw_t;
56 
57 #endif
58 
val_seg(int val)59 static inline int val_seg(int val)
60 {
61 	int r = 0;
62 	val >>= 7;
63 	if (val & 0xf0) {
64 		val >>= 4;
65 		r += 4;
66 	}
67 	if (val & 0x0c) {
68 		val >>= 2;
69 		r += 2;
70 	}
71 	if (val & 0x02)
72 		r += 1;
73 	return r;
74 }
75 
76 /*
77  * s16_to_ulaw() - Convert a linear PCM value to u-law
78  *
79  * In order to simplify the encoding process, the original linear magnitude
80  * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
81  * (33 - 8191). The result can be seen in the following encoding table:
82  *
83  *	Biased Linear Input Code	Compressed Code
84  *	------------------------	---------------
85  *	00000001wxyza			000wxyz
86  *	0000001wxyzab			001wxyz
87  *	000001wxyzabc			010wxyz
88  *	00001wxyzabcd			011wxyz
89  *	0001wxyzabcde			100wxyz
90  *	001wxyzabcdef			101wxyz
91  *	01wxyzabcdefg			110wxyz
92  *	1wxyzabcdefgh			111wxyz
93  *
94  * Each biased linear code has a leading 1 which identifies the segment
95  * number. The value of the segment number is equal to 7 minus the number
96  * of leading 0's. The quantization interval is directly available as the
97  * four bits wxyz.  * The trailing bits (a - h) are ignored.
98  *
99  * Ordinarily the complement of the resulting code word is used for
100  * transmission, and so the code word is complemented before it is returned.
101  *
102  * For further information see John C. Bellamy's Digital Telephony, 1982,
103  * John Wiley & Sons, pps 98-111 and 472-476.
104  */
105 
s16_to_ulaw(int pcm_val)106 static unsigned char s16_to_ulaw(int pcm_val)	/* 2's complement (16-bit range) */
107 {
108 	int mask;
109 	int seg;
110 	unsigned char uval;
111 
112 	if (pcm_val < 0) {
113 		pcm_val = 0x84 - pcm_val;
114 		mask = 0x7f;
115 	} else {
116 		pcm_val += 0x84;
117 		mask = 0xff;
118 	}
119 	if (pcm_val > 0x7fff)
120 		pcm_val = 0x7fff;
121 
122 	/* Convert the scaled magnitude to segment number. */
123 	seg = val_seg(pcm_val);
124 
125 	/*
126 	 * Combine the sign, segment, quantization bits;
127 	 * and complement the code word.
128 	 */
129 	uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0x0f);
130 	return uval ^ mask;
131 }
132 
133 /*
134  * ulaw_to_s16() - Convert a u-law value to 16-bit linear PCM
135  *
136  * First, a biased linear code is derived from the code word. An unbiased
137  * output can then be obtained by subtracting 33 from the biased code.
138  *
139  * Note that this function expects to be passed the complement of the
140  * original code word. This is in keeping with ISDN conventions.
141  */
ulaw_to_s16(unsigned char u_val)142 static int ulaw_to_s16(unsigned char u_val)
143 {
144 	int t;
145 
146 	/* Complement to obtain normal u-law value. */
147 	u_val = ~u_val;
148 
149 	/*
150 	 * Extract and bias the quantization bits. Then
151 	 * shift up by the segment number and subtract out the bias.
152 	 */
153 	t = ((u_val & 0x0f) << 3) + 0x84;
154 	t <<= (u_val & 0x70) >> 4;
155 
156 	return ((u_val & 0x80) ? (0x84 - t) : (t - 0x84));
157 }
158 
159 #ifndef DOC_HIDDEN
160 
snd_pcm_mulaw_decode(const snd_pcm_channel_area_t * dst_areas,snd_pcm_uframes_t dst_offset,const snd_pcm_channel_area_t * src_areas,snd_pcm_uframes_t src_offset,unsigned int channels,snd_pcm_uframes_t frames,unsigned int putidx)161 void snd_pcm_mulaw_decode(const snd_pcm_channel_area_t *dst_areas,
162 			  snd_pcm_uframes_t dst_offset,
163 			  const snd_pcm_channel_area_t *src_areas,
164 			  snd_pcm_uframes_t src_offset,
165 			  unsigned int channels, snd_pcm_uframes_t frames,
166 			  unsigned int putidx)
167 {
168 #define PUT16_LABELS
169 #include "plugin_ops.h"
170 #undef PUT16_LABELS
171 	void *put = put16_labels[putidx];
172 	unsigned int channel;
173 	for (channel = 0; channel < channels; ++channel) {
174 		const unsigned char *src;
175 		char *dst;
176 		int src_step, dst_step;
177 		snd_pcm_uframes_t frames1;
178 		const snd_pcm_channel_area_t *src_area = &src_areas[channel];
179 		const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
180 		src = snd_pcm_channel_area_addr(src_area, src_offset);
181 		dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
182 		src_step = snd_pcm_channel_area_step(src_area);
183 		dst_step = snd_pcm_channel_area_step(dst_area);
184 		frames1 = frames;
185 		while (frames1-- > 0) {
186 			int16_t sample = ulaw_to_s16(*src);
187 			goto *put;
188 #define PUT16_END after
189 #include "plugin_ops.h"
190 #undef PUT16_END
191 		after:
192 			src += src_step;
193 			dst += dst_step;
194 		}
195 	}
196 }
197 
snd_pcm_mulaw_encode(const snd_pcm_channel_area_t * dst_areas,snd_pcm_uframes_t dst_offset,const snd_pcm_channel_area_t * src_areas,snd_pcm_uframes_t src_offset,unsigned int channels,snd_pcm_uframes_t frames,unsigned int getidx)198 void snd_pcm_mulaw_encode(const snd_pcm_channel_area_t *dst_areas,
199 			  snd_pcm_uframes_t dst_offset,
200 			  const snd_pcm_channel_area_t *src_areas,
201 			  snd_pcm_uframes_t src_offset,
202 			  unsigned int channels, snd_pcm_uframes_t frames,
203 			  unsigned int getidx)
204 {
205 #define GET16_LABELS
206 #include "plugin_ops.h"
207 #undef GET16_LABELS
208 	void *get = get16_labels[getidx];
209 	unsigned int channel;
210 	int16_t sample = 0;
211 	for (channel = 0; channel < channels; ++channel) {
212 		const char *src;
213 		char *dst;
214 		int src_step, dst_step;
215 		snd_pcm_uframes_t frames1;
216 		const snd_pcm_channel_area_t *src_area = &src_areas[channel];
217 		const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
218 		src = snd_pcm_channel_area_addr(src_area, src_offset);
219 		dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
220 		src_step = snd_pcm_channel_area_step(src_area);
221 		dst_step = snd_pcm_channel_area_step(dst_area);
222 		frames1 = frames;
223 		while (frames1-- > 0) {
224 			goto *get;
225 #define GET16_END after
226 #include "plugin_ops.h"
227 #undef GET16_END
228 		after:
229 			*dst = s16_to_ulaw(sample);
230 			src += src_step;
231 			dst += dst_step;
232 		}
233 	}
234 }
235 
236 #endif /* DOC_HIDDEN */
237 
snd_pcm_mulaw_hw_refine_cprepare(snd_pcm_t * pcm,snd_pcm_hw_params_t * params)238 static int snd_pcm_mulaw_hw_refine_cprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
239 {
240 	snd_pcm_mulaw_t *mulaw = pcm->private_data;
241 	int err;
242 	snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_SHM };
243 	err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
244 					 &access_mask);
245 	if (err < 0)
246 		return err;
247 	if (mulaw->sformat == SND_PCM_FORMAT_MU_LAW) {
248 		snd_pcm_format_mask_t format_mask= { SND_PCM_FMTBIT_LINEAR };
249 		err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
250 						 &format_mask);
251 	} else {
252 		err = _snd_pcm_hw_params_set_format(params,
253 						   SND_PCM_FORMAT_MU_LAW);
254 	}
255 	err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
256 	if (err < 0)
257 		return err;
258 	params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
259 	return 0;
260 }
261 
snd_pcm_mulaw_hw_refine_sprepare(snd_pcm_t * pcm,snd_pcm_hw_params_t * sparams)262 static int snd_pcm_mulaw_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
263 {
264 	snd_pcm_mulaw_t *mulaw = pcm->private_data;
265 	snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
266 	_snd_pcm_hw_params_any(sparams);
267 	_snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
268 				   &saccess_mask);
269 	_snd_pcm_hw_params_set_format(sparams, mulaw->sformat);
270 	_snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
271 	return 0;
272 }
273 
snd_pcm_mulaw_hw_refine_schange(snd_pcm_t * pcm ATTRIBUTE_UNUSED,snd_pcm_hw_params_t * params,snd_pcm_hw_params_t * sparams)274 static int snd_pcm_mulaw_hw_refine_schange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
275 					    snd_pcm_hw_params_t *sparams)
276 {
277 	int err;
278 	unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
279 			      SND_PCM_HW_PARBIT_RATE |
280 			      SND_PCM_HW_PARBIT_PERIOD_SIZE |
281 			      SND_PCM_HW_PARBIT_BUFFER_SIZE |
282 			      SND_PCM_HW_PARBIT_PERIODS |
283 			      SND_PCM_HW_PARBIT_PERIOD_TIME |
284 			      SND_PCM_HW_PARBIT_BUFFER_TIME |
285 			      SND_PCM_HW_PARBIT_TICK_TIME);
286 	err = _snd_pcm_hw_params_refine(sparams, links, params);
287 	if (err < 0)
288 		return err;
289 	return 0;
290 }
291 
snd_pcm_mulaw_hw_refine_cchange(snd_pcm_t * pcm ATTRIBUTE_UNUSED,snd_pcm_hw_params_t * params,snd_pcm_hw_params_t * sparams)292 static int snd_pcm_mulaw_hw_refine_cchange(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params,
293 					    snd_pcm_hw_params_t *sparams)
294 {
295 	int err;
296 	unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
297 			      SND_PCM_HW_PARBIT_RATE |
298 			      SND_PCM_HW_PARBIT_PERIOD_SIZE |
299 			      SND_PCM_HW_PARBIT_BUFFER_SIZE |
300 			      SND_PCM_HW_PARBIT_PERIODS |
301 			      SND_PCM_HW_PARBIT_PERIOD_TIME |
302 			      SND_PCM_HW_PARBIT_BUFFER_TIME |
303 			      SND_PCM_HW_PARBIT_TICK_TIME);
304 	err = _snd_pcm_hw_params_refine(params, links, sparams);
305 	if (err < 0)
306 		return err;
307 	return 0;
308 }
309 
snd_pcm_mulaw_hw_refine(snd_pcm_t * pcm,snd_pcm_hw_params_t * params)310 static int snd_pcm_mulaw_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
311 {
312 	return snd_pcm_hw_refine_slave(pcm, params,
313 				       snd_pcm_mulaw_hw_refine_cprepare,
314 				       snd_pcm_mulaw_hw_refine_cchange,
315 				       snd_pcm_mulaw_hw_refine_sprepare,
316 				       snd_pcm_mulaw_hw_refine_schange,
317 				       snd_pcm_generic_hw_refine);
318 }
319 
snd_pcm_mulaw_hw_params(snd_pcm_t * pcm,snd_pcm_hw_params_t * params)320 static int snd_pcm_mulaw_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
321 {
322 	snd_pcm_mulaw_t *mulaw = pcm->private_data;
323 	snd_pcm_format_t format;
324 	int err = snd_pcm_hw_params_slave(pcm, params,
325 					  snd_pcm_mulaw_hw_refine_cchange,
326 					  snd_pcm_mulaw_hw_refine_sprepare,
327 					  snd_pcm_mulaw_hw_refine_schange,
328 					  snd_pcm_generic_hw_params);
329 	if (err < 0)
330 		return err;
331 
332 	err = INTERNAL(snd_pcm_hw_params_get_format)(params, &format);
333 	if (err < 0)
334 		return err;
335 
336 	if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
337 		if (mulaw->sformat == SND_PCM_FORMAT_MU_LAW) {
338 			mulaw->getput_idx = snd_pcm_linear_get_index(format, SND_PCM_FORMAT_S16);
339 			mulaw->func = snd_pcm_mulaw_encode;
340 		} else {
341 			mulaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, mulaw->sformat);
342 			mulaw->func = snd_pcm_mulaw_decode;
343 		}
344 	} else {
345 		if (mulaw->sformat == SND_PCM_FORMAT_MU_LAW) {
346 			mulaw->getput_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, format);
347 			mulaw->func = snd_pcm_mulaw_decode;
348 		} else {
349 			mulaw->getput_idx = snd_pcm_linear_get_index(mulaw->sformat, SND_PCM_FORMAT_S16);
350 			mulaw->func = snd_pcm_mulaw_encode;
351 		}
352 	}
353 	return 0;
354 }
355 
356 static snd_pcm_uframes_t
snd_pcm_mulaw_write_areas(snd_pcm_t * pcm,const snd_pcm_channel_area_t * areas,snd_pcm_uframes_t offset,snd_pcm_uframes_t size,const snd_pcm_channel_area_t * slave_areas,snd_pcm_uframes_t slave_offset,snd_pcm_uframes_t * slave_sizep)357 snd_pcm_mulaw_write_areas(snd_pcm_t *pcm,
358 			  const snd_pcm_channel_area_t *areas,
359 			  snd_pcm_uframes_t offset,
360 			  snd_pcm_uframes_t size,
361 			  const snd_pcm_channel_area_t *slave_areas,
362 			  snd_pcm_uframes_t slave_offset,
363 			  snd_pcm_uframes_t *slave_sizep)
364 {
365 	snd_pcm_mulaw_t *mulaw = pcm->private_data;
366 	if (size > *slave_sizep)
367 		size = *slave_sizep;
368 	mulaw->func(slave_areas, slave_offset,
369 		    areas, offset,
370 		    pcm->channels, size,
371 		    mulaw->getput_idx);
372 	*slave_sizep = size;
373 	return size;
374 }
375 
376 static snd_pcm_uframes_t
snd_pcm_mulaw_read_areas(snd_pcm_t * pcm,const snd_pcm_channel_area_t * areas,snd_pcm_uframes_t offset,snd_pcm_uframes_t size,const snd_pcm_channel_area_t * slave_areas,snd_pcm_uframes_t slave_offset,snd_pcm_uframes_t * slave_sizep)377 snd_pcm_mulaw_read_areas(snd_pcm_t *pcm,
378 			 const snd_pcm_channel_area_t *areas,
379 			 snd_pcm_uframes_t offset,
380 			 snd_pcm_uframes_t size,
381 			 const snd_pcm_channel_area_t *slave_areas,
382 			 snd_pcm_uframes_t slave_offset,
383 			 snd_pcm_uframes_t *slave_sizep)
384 {
385 	snd_pcm_mulaw_t *mulaw = pcm->private_data;
386 	if (size > *slave_sizep)
387 		size = *slave_sizep;
388 	mulaw->func(areas, offset,
389 		    slave_areas, slave_offset,
390 		    pcm->channels, size,
391 		    mulaw->getput_idx);
392 	*slave_sizep = size;
393 	return size;
394 }
395 
snd_pcm_mulaw_dump(snd_pcm_t * pcm,snd_output_t * out)396 static void snd_pcm_mulaw_dump(snd_pcm_t *pcm, snd_output_t *out)
397 {
398 	snd_pcm_mulaw_t *mulaw = pcm->private_data;
399 	snd_output_printf(out, "Mu-Law conversion PCM (%s)\n",
400 		snd_pcm_format_name(mulaw->sformat));
401 	if (pcm->setup) {
402 		snd_output_printf(out, "Its setup is:\n");
403 		snd_pcm_dump_setup(pcm, out);
404 	}
405 	snd_output_printf(out, "Slave: ");
406 	snd_pcm_dump(mulaw->plug.gen.slave, out);
407 }
408 
409 static const snd_pcm_ops_t snd_pcm_mulaw_ops = {
410 	.close = snd_pcm_generic_close,
411 	.info = snd_pcm_generic_info,
412 	.hw_refine = snd_pcm_mulaw_hw_refine,
413 	.hw_params = snd_pcm_mulaw_hw_params,
414 	.hw_free = snd_pcm_generic_hw_free,
415 	.sw_params = snd_pcm_generic_sw_params,
416 	.channel_info = snd_pcm_generic_channel_info,
417 	.dump = snd_pcm_mulaw_dump,
418 	.nonblock = snd_pcm_generic_nonblock,
419 	.async = snd_pcm_generic_async,
420 	.mmap = snd_pcm_generic_mmap,
421 	.munmap = snd_pcm_generic_munmap,
422 	.get_chmap = snd_pcm_generic_get_chmap,
423 	.set_chmap = snd_pcm_generic_set_chmap,
424 };
425 
426 /**
427  * \brief Creates a new Mu-Law conversion PCM
428  * \param pcmp Returns created PCM handle
429  * \param name Name of PCM
430  * \param sformat Slave (destination) format
431  * \param slave Slave PCM handle
432  * \param close_slave When set, the slave PCM handle is closed with copy PCM
433  * \retval zero on success otherwise a negative error code
434  * \warning Using of this function might be dangerous in the sense
435  *          of compatibility reasons. The prototype might be freely
436  *          changed in future.
437  */
snd_pcm_mulaw_open(snd_pcm_t ** pcmp,const char * name,snd_pcm_format_t sformat,snd_pcm_t * slave,int close_slave)438 int snd_pcm_mulaw_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, snd_pcm_t *slave, int close_slave)
439 {
440 	snd_pcm_t *pcm;
441 	snd_pcm_mulaw_t *mulaw;
442 	int err;
443 	assert(pcmp && slave);
444 	if (snd_pcm_format_linear(sformat) != 1 &&
445 	    sformat != SND_PCM_FORMAT_MU_LAW)
446 		return -EINVAL;
447 	mulaw = calloc(1, sizeof(snd_pcm_mulaw_t));
448 	if (!mulaw) {
449 		return -ENOMEM;
450 	}
451 	snd_pcm_plugin_init(&mulaw->plug);
452 	mulaw->sformat = sformat;
453 	mulaw->plug.read = snd_pcm_mulaw_read_areas;
454 	mulaw->plug.write = snd_pcm_mulaw_write_areas;
455 	mulaw->plug.undo_read = snd_pcm_plugin_undo_read_generic;
456 	mulaw->plug.undo_write = snd_pcm_plugin_undo_write_generic;
457 	mulaw->plug.gen.slave = slave;
458 	mulaw->plug.gen.close_slave = close_slave;
459 
460 	err = snd_pcm_new(&pcm, SND_PCM_TYPE_MULAW, name, slave->stream, slave->mode);
461 	if (err < 0) {
462 		free(mulaw);
463 		return err;
464 	}
465 	pcm->ops = &snd_pcm_mulaw_ops;
466 	pcm->fast_ops = &snd_pcm_plugin_fast_ops;
467 	pcm->private_data = mulaw;
468 	pcm->poll_fd = slave->poll_fd;
469 	pcm->poll_events = slave->poll_events;
470 	pcm->tstamp_type = slave->tstamp_type;
471 	snd_pcm_set_hw_ptr(pcm, &mulaw->plug.hw_ptr, -1, 0);
472 	snd_pcm_set_appl_ptr(pcm, &mulaw->plug.appl_ptr, -1, 0);
473 	*pcmp = pcm;
474 
475 	return 0;
476 }
477 
478 /*! \page pcm_plugins
479 
480 \section pcm_plugins_mulaw Plugin: Mu-Law
481 
482 This plugin converts Mu-Law samples to linear or linear to Mu-Law samples
483 from master Mu-Law conversion PCM to given slave PCM. The channel count,
484 format and rate must match for both of them.
485 
486 \code
487 pcm.name {
488         type mulaw              # Mu-Law conversion PCM
489         slave STR               # Slave name
490         # or
491         slave {                 # Slave definition
492                 pcm STR         # Slave PCM name
493                 # or
494                 pcm { }         # Slave PCM definition
495                 format STR      # Slave format
496         }
497 }
498 \endcode
499 
500 \subsection pcm_plugins_mulaw_funcref Function reference
501 
502 <UL>
503   <LI>snd_pcm_mulaw_open()
504   <LI>_snd_pcm_mulaw_open()
505 </UL>
506 
507 */
508 
509 /**
510  * \brief Creates a new Mu-Law conversion PCM
511  * \param pcmp Returns created PCM handle
512  * \param name Name of PCM
513  * \param root Root configuration node
514  * \param conf Configuration node with copy PCM description
515  * \param stream Stream type
516  * \param mode Stream mode
517  * \retval zero on success otherwise a negative error code
518  * \warning Using of this function might be dangerous in the sense
519  *          of compatibility reasons. The prototype might be freely
520  *          changed in future.
521  */
_snd_pcm_mulaw_open(snd_pcm_t ** pcmp,const char * name,snd_config_t * root,snd_config_t * conf,snd_pcm_stream_t stream,int mode)522 int _snd_pcm_mulaw_open(snd_pcm_t **pcmp, const char *name,
523 			snd_config_t *root, snd_config_t *conf,
524 			snd_pcm_stream_t stream, int mode)
525 {
526 	snd_config_iterator_t i, next;
527 	int err;
528 	snd_pcm_t *spcm;
529 	snd_config_t *slave = NULL, *sconf;
530 	snd_pcm_format_t sformat;
531 	snd_config_for_each(i, next, conf) {
532 		snd_config_t *n = snd_config_iterator_entry(i);
533 		const char *id;
534 		if (snd_config_get_id(n, &id) < 0)
535 			continue;
536 		if (snd_pcm_conf_generic_id(id))
537 			continue;
538 		if (strcmp(id, "slave") == 0) {
539 			slave = n;
540 			continue;
541 		}
542 		SNDERR("Unknown field %s", id);
543 		return -EINVAL;
544 	}
545 	if (!slave) {
546 		SNDERR("slave is not defined");
547 		return -EINVAL;
548 	}
549 	err = snd_pcm_slave_conf(root, slave, &sconf, 1,
550 				 SND_PCM_HW_PARAM_FORMAT, SCONF_MANDATORY, &sformat);
551 	if (err < 0)
552 		return err;
553 	if (snd_pcm_format_linear(sformat) != 1 &&
554 	    sformat != SND_PCM_FORMAT_MU_LAW) {
555 		snd_config_delete(sconf);
556 		SNDERR("invalid slave format");
557 		return -EINVAL;
558 	}
559 	err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode, conf);
560 	snd_config_delete(sconf);
561 	if (err < 0)
562 		return err;
563 	err = snd_pcm_mulaw_open(pcmp, name, sformat, spcm, 1);
564 	if (err < 0)
565 		snd_pcm_close(spcm);
566 	return err;
567 }
568 #ifndef DOC_HIDDEN
569 SND_DLSYM_BUILD_VERSION(_snd_pcm_mulaw_open, SND_PCM_DLSYM_VERSION);
570 #endif
571