OSDN Git Service

Improved .asoundrc changes
[android-x86/external-alsa-lib.git] / src / pcm / pcm_rate.c
1 /*
2  *  PCM - Rate conversion
3  *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
4  *
5  *
6  *   This library is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU Library General Public License as
8  *   published by the Free Software Foundation; either version 2 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 Library General Public License for more details.
15  *
16  *   You should have received a copy of the GNU Library General Public
17  *   License along with this library; if not, write to the Free Software
18  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21   
22 #include <limits.h>
23 #include <byteswap.h>
24 #include "pcm_local.h"
25 #include "pcm_plugin.h"
26
27 #define DIV (1<<16)
28
29 typedef struct {
30         int16_t sample;
31         int sum;
32         unsigned int pos;
33 } snd_pcm_rate_state_t;
34  
35 typedef snd_pcm_uframes_t (*rate_f)(const snd_pcm_channel_area_t *dst_areas,
36                                     snd_pcm_uframes_t dst_offset,
37                                     snd_pcm_uframes_t *dst_framesp,
38                                     const snd_pcm_channel_area_t *src_areas,
39                                     snd_pcm_uframes_t src_offset,
40                                     snd_pcm_uframes_t src_frames,
41                                     unsigned int channels,
42                                     int getidx, int putidx,
43                                     unsigned int arg,
44                                     snd_pcm_rate_state_t *states);
45
46 typedef struct {
47         /* This field need to be the first */
48         snd_pcm_plugin_t plug;
49         int get_idx;
50         int put_idx;
51         unsigned int pitch;
52         rate_f func;
53         snd_pcm_format_t sformat;
54         int srate;
55         snd_pcm_rate_state_t *states;
56 } snd_pcm_rate_t;
57
58 snd_pcm_uframes_t snd_pcm_rate_expand(const snd_pcm_channel_area_t *dst_areas,
59                                       snd_pcm_uframes_t dst_offset, snd_pcm_uframes_t *dst_framesp,
60                                       const snd_pcm_channel_area_t *src_areas,
61                                       snd_pcm_uframes_t src_offset, snd_pcm_uframes_t src_frames,
62                                       unsigned int channels,
63                                       int getidx, int putidx,
64                                       unsigned int get_threshold,
65                                       snd_pcm_rate_state_t *states)
66 {
67 #define GET16_LABELS
68 #define PUT16_LABELS
69 #include "plugin_ops.h"
70 #undef GET16_LABELS
71 #undef PUT16_LABELS
72         void *get = get16_labels[getidx];
73         void *put = put16_labels[putidx];
74         unsigned int channel;
75         snd_pcm_uframes_t src_frames1 = 0;
76         snd_pcm_uframes_t dst_frames1 = 0;
77         snd_pcm_uframes_t dst_frames = *dst_framesp;
78         int16_t sample = 0;
79         
80         if (src_frames == 0 ||
81             dst_frames == 0)
82                 return 0;
83         for (channel = 0; channel < channels; ++channel) {
84                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
85                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
86                 const char *src;
87                 char *dst;
88                 int src_step, dst_step;
89                 int16_t old_sample = states->sample;
90                 unsigned int pos = states->pos;
91                 src = snd_pcm_channel_area_addr(src_area, src_offset);
92                 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
93                 src_step = snd_pcm_channel_area_step(src_area);
94                 dst_step = snd_pcm_channel_area_step(dst_area);
95                 src_frames1 = 0;
96                 dst_frames1 = 0;
97                 while (dst_frames1 < dst_frames) {
98                         if (pos >= get_threshold) {
99                                 int16_t new_sample;
100                                 if (src_frames1 == src_frames)
101                                         break;
102                                 pos -= get_threshold;
103                                 goto *get;
104 #define GET16_END after_get
105 #include "plugin_ops.h"
106 #undef GET16_END
107                         after_get:
108                                 src += src_step;
109                                 src_frames1++;
110                                 new_sample = sample;
111                                 sample = (old_sample * (DIV - pos) + new_sample * pos) / DIV;
112                                 old_sample = new_sample;
113                         } else
114                                 sample = old_sample;
115                         goto *put;
116 #define PUT16_END after_put
117 #include "plugin_ops.h"
118 #undef PUT16_END
119                 after_put:
120                         dst += dst_step;
121                         dst_frames1++;
122                         pos += DIV;
123                 }
124                 states->sample = old_sample;
125                 states->pos = pos;
126                 states++;
127         }
128         *dst_framesp = dst_frames1;
129         return src_frames1;
130 }
131
132 snd_pcm_uframes_t snd_pcm_rate_shrink(const snd_pcm_channel_area_t *dst_areas,
133                                       snd_pcm_uframes_t dst_offset, snd_pcm_uframes_t *dst_framesp,
134                                       const snd_pcm_channel_area_t *src_areas,
135                                       snd_pcm_uframes_t src_offset, snd_pcm_uframes_t src_frames,
136                                       unsigned int channels,
137                                       int getidx, int putidx,
138                                       unsigned int get_increment,
139                                       snd_pcm_rate_state_t *states)
140 {
141 #define GET16_LABELS
142 #define PUT16_LABELS
143 #include "plugin_ops.h"
144 #undef GET16_LABELS
145 #undef PUT16_LABELS
146         void *get = get16_labels[getidx];
147         void *put = put16_labels[putidx];
148         unsigned int channel;
149         snd_pcm_uframes_t src_frames1 = 0;
150         snd_pcm_uframes_t dst_frames1 = 0;
151         snd_pcm_uframes_t dst_frames = *dst_framesp;
152         int16_t sample = 0;
153
154         if (src_frames == 0 ||
155             dst_frames == 0)
156                 return 0;
157         for (channel = 0; channel < channels; ++channel) {
158                 const snd_pcm_channel_area_t *src_area = &src_areas[channel];
159                 const snd_pcm_channel_area_t *dst_area = &dst_areas[channel];
160                 unsigned int pos;
161                 int sum;
162                 const char *src;
163                 char *dst;
164                 int src_step, dst_step;
165                 sum = states->sum;
166                 pos = states->pos;
167                 src = snd_pcm_channel_area_addr(src_area, src_offset);
168                 dst = snd_pcm_channel_area_addr(dst_area, dst_offset);
169                 src_step = snd_pcm_channel_area_step(src_area);
170                 dst_step = snd_pcm_channel_area_step(dst_area);
171                 src_frames1 = 0;
172                 dst_frames1 = 0;
173                 while (src_frames1 < src_frames) {
174                         
175                         goto *get;
176 #define GET16_END after_get
177 #include "plugin_ops.h"
178 #undef GET16_END
179                 after_get:
180                         src += src_step;
181                         src_frames1++;
182                         pos += get_increment;
183                         if (pos >= DIV) {
184                                 int s = sample;
185                                 pos -= DIV;
186                                 sum += s * (get_increment - pos);
187                                 sum /= DIV;
188                                 sample = sum;
189                                 goto *put;
190 #define PUT16_END after_put
191 #include "plugin_ops.h"
192 #undef PUT16_END
193                         after_put:
194                                 dst += dst_step;
195                                 sum = s * pos;
196                                 dst_frames1++;
197                                 if (dst_frames1 == dst_frames)
198                                         break;
199                         } else
200                                 sum += sample * get_increment;
201                 }
202                 states->sum = sum;
203                 states->pos = pos;
204                 states++;
205         }
206         *dst_framesp = dst_frames1;
207         return src_frames1;
208 }
209
210 static int snd_pcm_rate_hw_refine_cprepare(snd_pcm_t *pcm ATTRIBUTE_UNUSED, snd_pcm_hw_params_t *params)
211 {
212         int err;
213         snd_pcm_access_mask_t access_mask = { SND_PCM_ACCBIT_PLUGIN };
214         snd_pcm_format_mask_t format_mask = { SND_PCM_FMTBIT_LINEAR };
215         err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
216                                          &access_mask);
217         if (err < 0)
218                 return err;
219         err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
220                                          &format_mask);
221         if (err < 0)
222                 return err;
223         err = _snd_pcm_hw_params_set_subformat(params, SND_PCM_SUBFORMAT_STD);
224         if (err < 0)
225                 return err;
226         err = _snd_pcm_hw_param_set_min(params,
227                                         SND_PCM_HW_PARAM_RATE, RATE_MIN, 0);
228         if (err < 0)
229                 return err;
230         err = _snd_pcm_hw_param_set_max(params,
231                                         SND_PCM_HW_PARAM_RATE, RATE_MAX, 0);
232         if (err < 0)
233                 return err;
234         params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
235         return 0;
236 }
237
238 static int snd_pcm_rate_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
239 {
240         snd_pcm_rate_t *rate = pcm->private_data;
241         snd_pcm_access_mask_t saccess_mask = { SND_PCM_ACCBIT_MMAP };
242         _snd_pcm_hw_params_any(sparams);
243         _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
244                                    &saccess_mask);
245         if (rate->sformat != SND_PCM_FORMAT_UNKNOWN) {
246                 _snd_pcm_hw_params_set_format(sparams, rate->sformat);
247                 _snd_pcm_hw_params_set_subformat(sparams, SND_PCM_SUBFORMAT_STD);
248         }
249         _snd_pcm_hw_param_set_minmax(sparams, SND_PCM_HW_PARAM_RATE,
250                                      rate->srate, 0, rate->srate + 1, -1);
251         return 0;
252 }
253
254 static int snd_pcm_rate_hw_refine_schange(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
255                                           snd_pcm_hw_params_t *sparams)
256 {
257         snd_pcm_rate_t *rate = pcm->private_data;
258         snd_interval_t t, buffer_size;
259         const snd_interval_t *srate, *crate;
260         int err;
261         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
262                               SND_PCM_HW_PARBIT_PERIOD_TIME |
263                               SND_PCM_HW_PARBIT_TICK_TIME);
264         if (rate->sformat == SND_PCM_FORMAT_UNKNOWN)
265                 links |= (SND_PCM_HW_PARBIT_FORMAT |
266                           SND_PCM_HW_PARBIT_SUBFORMAT |
267                           SND_PCM_HW_PARBIT_SAMPLE_BITS |
268                           SND_PCM_HW_PARBIT_FRAME_BITS);
269         snd_interval_copy(&buffer_size, snd_pcm_hw_param_get_interval(params, SND_PCM_HW_PARAM_BUFFER_SIZE));
270         snd_interval_unfloor(&buffer_size);
271         crate = snd_pcm_hw_param_get_interval(params, SND_PCM_HW_PARAM_RATE);
272         srate = snd_pcm_hw_param_get_interval(sparams, SND_PCM_HW_PARAM_RATE);
273         snd_interval_muldiv(&buffer_size, srate, crate, &t);
274         err = _snd_pcm_hw_param_set_interval(sparams, SND_PCM_HW_PARAM_BUFFER_SIZE, &t);
275         if (err < 0)
276                 return err;
277         err = _snd_pcm_hw_params_refine(sparams, links, params);
278         if (err < 0)
279                 return err;
280         return 0;
281 }
282         
283 static int snd_pcm_rate_hw_refine_cchange(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
284                                           snd_pcm_hw_params_t *sparams)
285 {
286         snd_pcm_rate_t *rate = pcm->private_data;
287         snd_interval_t t;
288         const snd_interval_t *sbuffer_size;
289         const snd_interval_t *srate, *crate;
290         int err;
291         unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
292                               SND_PCM_HW_PARBIT_PERIOD_TIME |
293                               SND_PCM_HW_PARBIT_TICK_TIME);
294         if (rate->sformat == SND_PCM_FORMAT_UNKNOWN)
295                 links |= (SND_PCM_HW_PARBIT_FORMAT |
296                           SND_PCM_HW_PARBIT_SUBFORMAT |
297                           SND_PCM_HW_PARBIT_SAMPLE_BITS |
298                           SND_PCM_HW_PARBIT_FRAME_BITS);
299         sbuffer_size = snd_pcm_hw_param_get_interval(sparams, SND_PCM_HW_PARAM_BUFFER_SIZE);
300         crate = snd_pcm_hw_param_get_interval(params, SND_PCM_HW_PARAM_RATE);
301         srate = snd_pcm_hw_param_get_interval(sparams, SND_PCM_HW_PARAM_RATE);
302         snd_interval_muldiv(sbuffer_size, crate, srate, &t);
303         snd_interval_floor(&t);
304         err = _snd_pcm_hw_param_set_interval(params, SND_PCM_HW_PARAM_BUFFER_SIZE, &t);
305         if (err < 0)
306                 return err;
307         err = _snd_pcm_hw_params_refine(params, links, sparams);
308         if (err < 0)
309                 return err;
310         return 0;
311 }
312
313 static int snd_pcm_rate_hw_refine(snd_pcm_t *pcm, 
314                                   snd_pcm_hw_params_t *params)
315 {
316         return snd_pcm_hw_refine_slave(pcm, params,
317                                        snd_pcm_rate_hw_refine_cprepare,
318                                        snd_pcm_rate_hw_refine_cchange,
319                                        snd_pcm_rate_hw_refine_sprepare,
320                                        snd_pcm_rate_hw_refine_schange,
321                                        snd_pcm_plugin_hw_refine_slave);
322 }
323
324 static int snd_pcm_rate_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
325 {
326         snd_pcm_rate_t *rate = pcm->private_data;
327         snd_pcm_t *slave = rate->plug.slave;
328         snd_pcm_format_t src_format, dst_format;
329         unsigned int src_rate, dst_rate;
330         int err = snd_pcm_hw_params_slave(pcm, params,
331                                           snd_pcm_rate_hw_refine_cchange,
332                                           snd_pcm_rate_hw_refine_sprepare,
333                                           snd_pcm_rate_hw_refine_schange,
334                                           snd_pcm_plugin_hw_params_slave);
335         if (err < 0)
336                 return err;
337
338         if (pcm->stream == SND_PCM_STREAM_PLAYBACK) {
339                 src_format = snd_pcm_hw_params_get_format(params);
340                 dst_format = slave->format;
341                 src_rate = snd_pcm_hw_params_get_rate(params, 0);
342                 dst_rate = slave->rate;
343         } else {
344                 src_format = slave->format;
345                 dst_format = snd_pcm_hw_params_get_format(params);
346                 src_rate = slave->rate;
347                 dst_rate = snd_pcm_hw_params_get_rate(params, 0);
348         }
349         rate->get_idx = snd_pcm_linear_get_index(src_format, SND_PCM_FORMAT_S16);
350         rate->put_idx = snd_pcm_linear_put_index(SND_PCM_FORMAT_S16, dst_format);
351         if (src_rate < dst_rate) {
352                 rate->func = snd_pcm_rate_expand;
353                 /* pitch is get_threshold */
354         } else {
355                 rate->func = snd_pcm_rate_shrink;
356                 /* pitch is get_increment */
357         }
358         rate->pitch = (((u_int64_t)dst_rate * DIV) + src_rate / 2) / src_rate;
359         assert(!rate->states);
360         rate->states = malloc(snd_pcm_hw_param_get(params, SND_PCM_HW_PARAM_CHANNELS, 0) * sizeof(*rate->states));
361         return 0;
362 }
363
364 static int snd_pcm_rate_hw_free(snd_pcm_t *pcm)
365 {
366         snd_pcm_rate_t *rate = pcm->private_data;
367         if (rate->states) {
368                 free(rate->states);
369                 rate->states = 0;
370         }
371         return snd_pcm_hw_free(rate->plug.slave);
372 }
373
374 static int snd_pcm_rate_sw_params(snd_pcm_t *pcm, snd_pcm_sw_params_t * params)
375 {
376         snd_pcm_rate_t *rate = pcm->private_data;
377         snd_pcm_t *slave = rate->plug.slave;
378         snd_pcm_sw_params_t sparams;
379         sparams = *params;
380         sparams.avail_min = muldiv_near(sparams.avail_min, slave->rate, pcm->rate);
381         sparams.xfer_align = muldiv_near(sparams.xfer_align, slave->rate, pcm->rate);
382         sparams.silence_threshold = muldiv_near(sparams.silence_threshold, slave->rate, pcm->rate);
383         sparams.silence_size = muldiv_near(sparams.silence_size, slave->rate, pcm->rate);
384         return snd_pcm_sw_params(slave, &sparams);
385 }
386
387 static int snd_pcm_rate_init(snd_pcm_t *pcm)
388 {
389         snd_pcm_rate_t *rate = pcm->private_data;
390         unsigned int k;
391         for (k = 0; k < pcm->channels; ++k) {
392                 rate->states[k].sum = 0;
393                 rate->states[k].sample = 0;
394                 if (rate->func == snd_pcm_rate_expand) {
395                         /* Get a sample on entry */
396                         rate->states[k].pos = rate->pitch + DIV;
397                 } else {
398                         rate->states[k].pos = 0;
399                 }
400         }
401         return 0;
402 }
403
404 static snd_pcm_uframes_t
405 snd_pcm_rate_write_areas(snd_pcm_t *pcm,
406                          const snd_pcm_channel_area_t *areas,
407                          snd_pcm_uframes_t offset,
408                          snd_pcm_uframes_t size,
409                          const snd_pcm_channel_area_t *slave_areas,
410                          snd_pcm_uframes_t slave_offset,
411                          snd_pcm_uframes_t *slave_sizep)
412 {
413         snd_pcm_rate_t *rate = pcm->private_data;
414         return rate->func(slave_areas, slave_offset, slave_sizep, 
415                           areas, offset, size,
416                           pcm->channels,
417                           rate->get_idx, rate->put_idx,
418                           rate->pitch, rate->states);
419 }
420
421 static snd_pcm_uframes_t
422 snd_pcm_rate_read_areas(snd_pcm_t *pcm,
423                          const snd_pcm_channel_area_t *areas,
424                          snd_pcm_uframes_t offset,
425                          snd_pcm_uframes_t size,
426                          const snd_pcm_channel_area_t *slave_areas,
427                          snd_pcm_uframes_t slave_offset,
428                          snd_pcm_uframes_t *slave_sizep)
429 {
430         snd_pcm_rate_t *rate = pcm->private_data;
431         *slave_sizep = rate->func(areas, offset, &size,
432                                   slave_areas, slave_offset, *slave_sizep,
433                                   pcm->channels,
434                                   rate->get_idx, rate->put_idx,
435                                   rate->pitch, rate->states);
436         return size;
437 }
438
439 snd_pcm_sframes_t snd_pcm_rate_client_frames(snd_pcm_t *pcm, snd_pcm_sframes_t frames)
440 {
441         snd_pcm_rate_t *rate = pcm->private_data;
442         /* Round toward zero */
443         if (pcm->stream == SND_PCM_STREAM_PLAYBACK)
444                 return muldiv_down(frames, DIV, rate->pitch);
445         else
446                 return muldiv_down(frames, rate->pitch, DIV);
447 }
448
449 snd_pcm_sframes_t snd_pcm_rate_slave_frames(snd_pcm_t *pcm, snd_pcm_sframes_t frames)
450 {
451         snd_pcm_rate_t *rate = pcm->private_data;
452         /* Round toward zero */
453         if (pcm->stream == SND_PCM_STREAM_PLAYBACK)
454                 return muldiv_down(frames, rate->pitch, DIV);
455         else
456                 return muldiv_down(frames, DIV, rate->pitch);
457 }
458
459 static void snd_pcm_rate_dump(snd_pcm_t *pcm, snd_output_t *out)
460 {
461         snd_pcm_rate_t *rate = pcm->private_data;
462         if (rate->sformat == SND_PCM_FORMAT_UNKNOWN)
463                 snd_output_printf(out, "Rate conversion PCM (%d)\n", 
464                         rate->srate);
465         else
466                 snd_output_printf(out, "Rate conversion PCM (%d, sformat=%s)\n", 
467                         rate->srate,
468                         snd_pcm_format_name(rate->sformat));
469         if (pcm->setup) {
470                 snd_output_printf(out, "Its setup is:\n");
471                 snd_pcm_dump_setup(pcm, out);
472         }
473         snd_output_printf(out, "Slave: ");
474         snd_pcm_dump(rate->plug.slave, out);
475 }
476
477 snd_pcm_ops_t snd_pcm_rate_ops = {
478         close: snd_pcm_plugin_close,
479         info: snd_pcm_plugin_info,
480         hw_refine: snd_pcm_rate_hw_refine,
481         hw_params: snd_pcm_rate_hw_params,
482         hw_free: snd_pcm_rate_hw_free,
483         sw_params: snd_pcm_rate_sw_params,
484         channel_info: snd_pcm_plugin_channel_info,
485         dump: snd_pcm_rate_dump,
486         nonblock: snd_pcm_plugin_nonblock,
487         async: snd_pcm_plugin_async,
488         mmap: snd_pcm_plugin_mmap,
489         munmap: snd_pcm_plugin_munmap,
490 };
491
492 int snd_pcm_rate_open(snd_pcm_t **pcmp, const char *name, snd_pcm_format_t sformat, int srate, snd_pcm_t *slave, int close_slave)
493 {
494         snd_pcm_t *pcm;
495         snd_pcm_rate_t *rate;
496         assert(pcmp && slave);
497         if (sformat != SND_PCM_FORMAT_UNKNOWN &&
498             snd_pcm_format_linear(sformat) != 1)
499                 return -EINVAL;
500         rate = calloc(1, sizeof(snd_pcm_rate_t));
501         if (!rate) {
502                 return -ENOMEM;
503         }
504         rate->srate = srate;
505         rate->sformat = sformat;
506         rate->plug.read = snd_pcm_rate_read_areas;
507         rate->plug.write = snd_pcm_rate_write_areas;
508         rate->plug.client_frames = snd_pcm_rate_client_frames;
509         rate->plug.slave_frames = snd_pcm_rate_slave_frames;
510         rate->plug.init = snd_pcm_rate_init;
511         rate->plug.slave = slave;
512         rate->plug.close_slave = close_slave;
513
514         pcm = calloc(1, sizeof(snd_pcm_t));
515         if (!pcm) {
516                 free(rate);
517                 return -ENOMEM;
518         }
519         if (name)
520                 pcm->name = strdup(name);
521         pcm->type = SND_PCM_TYPE_RATE;
522         pcm->stream = slave->stream;
523         pcm->mode = slave->mode;
524         pcm->ops = &snd_pcm_rate_ops;
525         pcm->op_arg = pcm;
526         pcm->fast_ops = &snd_pcm_plugin_fast_ops;
527         pcm->fast_op_arg = pcm;
528         pcm->private_data = rate;
529         pcm->poll_fd = slave->poll_fd;
530         pcm->hw_ptr = &rate->plug.hw_ptr;
531         pcm->appl_ptr = &rate->plug.appl_ptr;
532         *pcmp = pcm;
533
534         return 0;
535 }
536
537 int _snd_pcm_rate_open(snd_pcm_t **pcmp, const char *name,
538                          snd_config_t *conf, 
539                          snd_pcm_stream_t stream, int mode)
540 {
541         snd_config_iterator_t i, next;
542         const char *sname = NULL;
543         int err;
544         snd_pcm_t *spcm;
545         snd_config_t *slave = NULL;
546         snd_pcm_format_t sformat = SND_PCM_FORMAT_UNKNOWN;
547         int srate = -1;
548         snd_config_for_each(i, next, conf) {
549                 snd_config_t *n = snd_config_iterator_entry(i);
550                 const char *id = snd_config_get_id(n);
551                 if (strcmp(id, "comment") == 0)
552                         continue;
553                 if (strcmp(id, "type") == 0)
554                         continue;
555                 if (strcmp(id, "slave") == 0) {
556                         slave = n;
557                         continue;
558                 }
559                 SNDERR("Unknown field %s", id);
560                 return -EINVAL;
561         }
562         if (!slave) {
563                 SNDERR("slave is not defined");
564                 return -EINVAL;
565         }
566         err = snd_pcm_slave_conf(slave, &sname, 2,
567                                  SND_PCM_HW_PARAM_FORMAT, 0, &sformat,
568                                  SND_PCM_HW_PARAM_RATE, 1, &srate);
569         if (err < 0)
570                 return err;
571         if (sformat != SND_PCM_FORMAT_UNKNOWN &&
572             snd_pcm_format_linear(sformat) != 1) {
573                 SNDERR("slave format is not linear");
574                 return -EINVAL;
575         }
576         /* This is needed cause snd_config_update may destroy config */
577         sname = strdup(sname);
578         if (!sname)
579                 return  -ENOMEM;
580         err = snd_pcm_open(&spcm, sname, stream, mode);
581         free((void *) sname);
582         if (err < 0)
583                 return err;
584         err = snd_pcm_rate_open(pcmp, name, sformat, srate, spcm, 1);
585         if (err < 0)
586                 snd_pcm_close(spcm);
587         return err;
588 }
589                                 
590