162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci *  Route Plug-In
362306a36Sopenharmony_ci *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci *   This library is free software; you can redistribute it and/or modify
762306a36Sopenharmony_ci *   it under the terms of the GNU Library General Public License as
862306a36Sopenharmony_ci *   published by the Free Software Foundation; either version 2 of
962306a36Sopenharmony_ci *   the License, or (at your option) any later version.
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci *   This program is distributed in the hope that it will be useful,
1262306a36Sopenharmony_ci *   but WITHOUT ANY WARRANTY; without even the implied warranty of
1362306a36Sopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1462306a36Sopenharmony_ci *   GNU Library General Public License for more details.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *   You should have received a copy of the GNU Library General Public
1762306a36Sopenharmony_ci *   License along with this library; if not, write to the Free Software
1862306a36Sopenharmony_ci *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci */
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#include <linux/time.h>
2362306a36Sopenharmony_ci#include <sound/core.h>
2462306a36Sopenharmony_ci#include <sound/pcm.h>
2562306a36Sopenharmony_ci#include "pcm_plugin.h"
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic void zero_areas(struct snd_pcm_plugin_channel *dvp, int ndsts,
2862306a36Sopenharmony_ci		       snd_pcm_uframes_t frames, snd_pcm_format_t format)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	int dst = 0;
3162306a36Sopenharmony_ci	for (; dst < ndsts; ++dst) {
3262306a36Sopenharmony_ci		if (dvp->wanted)
3362306a36Sopenharmony_ci			snd_pcm_area_silence(&dvp->area, 0, frames, format);
3462306a36Sopenharmony_ci		dvp->enabled = 0;
3562306a36Sopenharmony_ci		dvp++;
3662306a36Sopenharmony_ci	}
3762306a36Sopenharmony_ci}
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cistatic inline void copy_area(const struct snd_pcm_plugin_channel *src_channel,
4062306a36Sopenharmony_ci			     struct snd_pcm_plugin_channel *dst_channel,
4162306a36Sopenharmony_ci			     snd_pcm_uframes_t frames, snd_pcm_format_t format)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	dst_channel->enabled = 1;
4462306a36Sopenharmony_ci	snd_pcm_area_copy(&src_channel->area, 0, &dst_channel->area, 0, frames, format);
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistatic snd_pcm_sframes_t route_transfer(struct snd_pcm_plugin *plugin,
4862306a36Sopenharmony_ci					const struct snd_pcm_plugin_channel *src_channels,
4962306a36Sopenharmony_ci					struct snd_pcm_plugin_channel *dst_channels,
5062306a36Sopenharmony_ci					snd_pcm_uframes_t frames)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	int nsrcs, ndsts, dst;
5362306a36Sopenharmony_ci	struct snd_pcm_plugin_channel *dvp;
5462306a36Sopenharmony_ci	snd_pcm_format_t format;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	if (snd_BUG_ON(!plugin || !src_channels || !dst_channels))
5762306a36Sopenharmony_ci		return -ENXIO;
5862306a36Sopenharmony_ci	if (frames == 0)
5962306a36Sopenharmony_ci		return 0;
6062306a36Sopenharmony_ci	if (frames > dst_channels[0].frames)
6162306a36Sopenharmony_ci		frames = dst_channels[0].frames;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	nsrcs = plugin->src_format.channels;
6462306a36Sopenharmony_ci	ndsts = plugin->dst_format.channels;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	format = plugin->dst_format.format;
6762306a36Sopenharmony_ci	dvp = dst_channels;
6862306a36Sopenharmony_ci	if (nsrcs <= 1) {
6962306a36Sopenharmony_ci		/* expand to all channels */
7062306a36Sopenharmony_ci		for (dst = 0; dst < ndsts; ++dst) {
7162306a36Sopenharmony_ci			copy_area(src_channels, dvp, frames, format);
7262306a36Sopenharmony_ci			dvp++;
7362306a36Sopenharmony_ci		}
7462306a36Sopenharmony_ci		return frames;
7562306a36Sopenharmony_ci	}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	for (dst = 0; dst < ndsts && dst < nsrcs; ++dst) {
7862306a36Sopenharmony_ci		copy_area(src_channels, dvp, frames, format);
7962306a36Sopenharmony_ci		dvp++;
8062306a36Sopenharmony_ci		src_channels++;
8162306a36Sopenharmony_ci	}
8262306a36Sopenharmony_ci	if (dst < ndsts)
8362306a36Sopenharmony_ci		zero_areas(dvp, ndsts - dst, frames, format);
8462306a36Sopenharmony_ci	return frames;
8562306a36Sopenharmony_ci}
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciint snd_pcm_plugin_build_route(struct snd_pcm_substream *plug,
8862306a36Sopenharmony_ci			       struct snd_pcm_plugin_format *src_format,
8962306a36Sopenharmony_ci			       struct snd_pcm_plugin_format *dst_format,
9062306a36Sopenharmony_ci			       struct snd_pcm_plugin **r_plugin)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	struct snd_pcm_plugin *plugin;
9362306a36Sopenharmony_ci	int err;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	if (snd_BUG_ON(!r_plugin))
9662306a36Sopenharmony_ci		return -ENXIO;
9762306a36Sopenharmony_ci	*r_plugin = NULL;
9862306a36Sopenharmony_ci	if (snd_BUG_ON(src_format->rate != dst_format->rate))
9962306a36Sopenharmony_ci		return -ENXIO;
10062306a36Sopenharmony_ci	if (snd_BUG_ON(src_format->format != dst_format->format))
10162306a36Sopenharmony_ci		return -ENXIO;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	err = snd_pcm_plugin_build(plug, "route conversion",
10462306a36Sopenharmony_ci				   src_format, dst_format, 0, &plugin);
10562306a36Sopenharmony_ci	if (err < 0)
10662306a36Sopenharmony_ci		return err;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	plugin->transfer = route_transfer;
10962306a36Sopenharmony_ci	*r_plugin = plugin;
11062306a36Sopenharmony_ci	return 0;
11162306a36Sopenharmony_ci}
112