blob: 46917dc0196b3bae00de5f08350219f3e9a24d1f [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
22#include <sound/driver.h>
Jaroslav Kysela21a34792006-01-13 09:12:11 +010023
24#ifdef CONFIG_SND_PCM_OSS_PLUGINS
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/slab.h>
27#include <linux/time.h>
28#include <sound/core.h>
29#include <sound/pcm.h>
30#include "pcm_plugin.h"
31
Takashi Iwai0534ab42006-01-13 12:09:12 +010032static void zero_areas(struct snd_pcm_plugin_channel *dvp, int ndsts,
33 snd_pcm_uframes_t frames, int format)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Takashi Iwai0534ab42006-01-13 12:09:12 +010035 int dst = 0;
36 for (; dst < ndsts; ++dst) {
37 if (dvp->wanted)
38 snd_pcm_area_silence(&dvp->area, 0, frames, format);
39 dvp->enabled = 0;
40 dvp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 }
42}
43
Takashi Iwai0534ab42006-01-13 12:09:12 +010044static inline void copy_area(const struct snd_pcm_plugin_channel *src_channel,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010045 struct snd_pcm_plugin_channel *dst_channel,
Takashi Iwai0534ab42006-01-13 12:09:12 +010046 snd_pcm_uframes_t frames, int format)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 dst_channel->enabled = 1;
Takashi Iwai0534ab42006-01-13 12:09:12 +010049 snd_pcm_area_copy(&src_channel->area, 0, &dst_channel->area, 0, frames, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010052static snd_pcm_sframes_t route_transfer(struct snd_pcm_plugin *plugin,
Takashi Iwai0534ab42006-01-13 12:09:12 +010053 const struct snd_pcm_plugin_channel *src_channels,
54 struct snd_pcm_plugin_channel *dst_channels,
55 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Takashi Iwai0534ab42006-01-13 12:09:12 +010057 int nsrcs, ndsts, dst;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010058 struct snd_pcm_plugin_channel *dvp;
Takashi Iwai0534ab42006-01-13 12:09:12 +010059 int format;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
62 if (frames == 0)
63 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Takashi Iwai0534ab42006-01-13 12:09:12 +010065 nsrcs = plugin->src_format.channels;
66 ndsts = plugin->dst_format.channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Takashi Iwai0534ab42006-01-13 12:09:12 +010068 format = plugin->dst_format.format;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 dvp = dst_channels;
Takashi Iwai0534ab42006-01-13 12:09:12 +010070 if (nsrcs <= 1) {
71 /* expand to all channels */
72 for (dst = 0; dst < ndsts; ++dst) {
73 copy_area(src_channels, dvp, frames, format);
74 dvp++;
75 }
76 return frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Takashi Iwai0534ab42006-01-13 12:09:12 +010079 for (dst = 0; dst < ndsts && dst < nsrcs; ++dst) {
80 copy_area(src_channels, dvp, frames, format);
81 dvp++;
82 src_channels++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
Takashi Iwai0534ab42006-01-13 12:09:12 +010084 if (dst < ndsts)
85 zero_areas(dvp, ndsts - dst, frames, format);
86 return frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010089int snd_pcm_plugin_build_route(struct snd_pcm_substream *plug,
90 struct snd_pcm_plugin_format *src_format,
91 struct snd_pcm_plugin_format *dst_format,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010092 struct snd_pcm_plugin **r_plugin)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010094 struct snd_pcm_plugin *plugin;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int err;
96
97 snd_assert(r_plugin != NULL, return -ENXIO);
98 *r_plugin = NULL;
99 snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
Takashi Iwai0534ab42006-01-13 12:09:12 +0100100 snd_assert(src_format->format == dst_format->format, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Takashi Iwai0534ab42006-01-13 12:09:12 +0100102 err = snd_pcm_plugin_build(plug, "route conversion",
103 src_format, dst_format, 0, &plugin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (err < 0)
105 return err;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 plugin->transfer = route_transfer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *r_plugin = plugin;
109 return 0;
110}
Jaroslav Kysela21a34792006-01-13 09:12:11 +0100111
112#endif