blob: c8171f5783c873c49eaa86d61420b99e30adfac8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Takashi Iwai0534ab42006-01-13 12:09:12 +01002 * Route Plug-In
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/time.h>
23#include <sound/core.h>
24#include <sound/pcm.h>
25#include "pcm_plugin.h"
26
Takashi Iwai0534ab42006-01-13 12:09:12 +010027static void zero_areas(struct snd_pcm_plugin_channel *dvp, int ndsts,
Clemens Ladischfea952e2011-02-14 11:00:47 +010028 snd_pcm_uframes_t frames, snd_pcm_format_t format)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Takashi Iwai0534ab42006-01-13 12:09:12 +010030 int dst = 0;
31 for (; dst < ndsts; ++dst) {
32 if (dvp->wanted)
33 snd_pcm_area_silence(&dvp->area, 0, frames, format);
34 dvp->enabled = 0;
35 dvp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 }
37}
38
Takashi Iwai0534ab42006-01-13 12:09:12 +010039static inline void copy_area(const struct snd_pcm_plugin_channel *src_channel,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010040 struct snd_pcm_plugin_channel *dst_channel,
Clemens Ladischfea952e2011-02-14 11:00:47 +010041 snd_pcm_uframes_t frames, snd_pcm_format_t format)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 dst_channel->enabled = 1;
Takashi Iwai0534ab42006-01-13 12:09:12 +010044 snd_pcm_area_copy(&src_channel->area, 0, &dst_channel->area, 0, frames, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010047static snd_pcm_sframes_t route_transfer(struct snd_pcm_plugin *plugin,
Takashi Iwai0534ab42006-01-13 12:09:12 +010048 const struct snd_pcm_plugin_channel *src_channels,
49 struct snd_pcm_plugin_channel *dst_channels,
50 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Takashi Iwai0534ab42006-01-13 12:09:12 +010052 int nsrcs, ndsts, dst;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010053 struct snd_pcm_plugin_channel *dvp;
Clemens Ladischfea952e2011-02-14 11:00:47 +010054 snd_pcm_format_t format;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Takashi Iwai7eaa9432008-08-08 17:09:09 +020056 if (snd_BUG_ON(!plugin || !src_channels || !dst_channels))
57 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (frames == 0)
59 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Takashi Iwai0534ab42006-01-13 12:09:12 +010061 nsrcs = plugin->src_format.channels;
62 ndsts = plugin->dst_format.channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Takashi Iwai0534ab42006-01-13 12:09:12 +010064 format = plugin->dst_format.format;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 dvp = dst_channels;
Takashi Iwai0534ab42006-01-13 12:09:12 +010066 if (nsrcs <= 1) {
67 /* expand to all channels */
68 for (dst = 0; dst < ndsts; ++dst) {
69 copy_area(src_channels, dvp, frames, format);
70 dvp++;
71 }
72 return frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Takashi Iwai0534ab42006-01-13 12:09:12 +010075 for (dst = 0; dst < ndsts && dst < nsrcs; ++dst) {
76 copy_area(src_channels, dvp, frames, format);
77 dvp++;
78 src_channels++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
Takashi Iwai0534ab42006-01-13 12:09:12 +010080 if (dst < ndsts)
81 zero_areas(dvp, ndsts - dst, frames, format);
82 return frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010085int snd_pcm_plugin_build_route(struct snd_pcm_substream *plug,
86 struct snd_pcm_plugin_format *src_format,
87 struct snd_pcm_plugin_format *dst_format,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010088 struct snd_pcm_plugin **r_plugin)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010090 struct snd_pcm_plugin *plugin;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 int err;
92
Takashi Iwai7eaa9432008-08-08 17:09:09 +020093 if (snd_BUG_ON(!r_plugin))
94 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *r_plugin = NULL;
Takashi Iwai7eaa9432008-08-08 17:09:09 +020096 if (snd_BUG_ON(src_format->rate != dst_format->rate))
97 return -ENXIO;
98 if (snd_BUG_ON(src_format->format != dst_format->format))
99 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Takashi Iwai0534ab42006-01-13 12:09:12 +0100101 err = snd_pcm_plugin_build(plug, "route conversion",
102 src_format, dst_format, 0, &plugin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (err < 0)
104 return err;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 plugin->transfer = route_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *r_plugin = plugin;
108 return 0;
109}