Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 2 | * Route Plug-In |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/time.h> |
| 24 | #include <sound/core.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include "pcm_plugin.h" |
| 27 | |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 28 | static void zero_areas(struct snd_pcm_plugin_channel *dvp, int ndsts, |
| 29 | snd_pcm_uframes_t frames, int format) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 31 | int dst = 0; |
| 32 | for (; dst < ndsts; ++dst) { |
| 33 | if (dvp->wanted) |
| 34 | snd_pcm_area_silence(&dvp->area, 0, frames, format); |
| 35 | dvp->enabled = 0; |
| 36 | dvp++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 40 | static inline void copy_area(const struct snd_pcm_plugin_channel *src_channel, |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 41 | struct snd_pcm_plugin_channel *dst_channel, |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 42 | snd_pcm_uframes_t frames, int format) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | dst_channel->enabled = 1; |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 45 | snd_pcm_area_copy(&src_channel->area, 0, &dst_channel->area, 0, frames, format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 48 | static snd_pcm_sframes_t route_transfer(struct snd_pcm_plugin *plugin, |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 49 | const struct snd_pcm_plugin_channel *src_channels, |
| 50 | struct snd_pcm_plugin_channel *dst_channels, |
| 51 | snd_pcm_uframes_t frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 53 | int nsrcs, ndsts, dst; |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 54 | struct snd_pcm_plugin_channel *dvp; |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 55 | int format; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 57 | if (snd_BUG_ON(!plugin || !src_channels || !dst_channels)) |
| 58 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | if (frames == 0) |
| 60 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 62 | nsrcs = plugin->src_format.channels; |
| 63 | ndsts = plugin->dst_format.channels; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 65 | format = plugin->dst_format.format; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | dvp = dst_channels; |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 67 | if (nsrcs <= 1) { |
| 68 | /* expand to all channels */ |
| 69 | for (dst = 0; dst < ndsts; ++dst) { |
| 70 | copy_area(src_channels, dvp, frames, format); |
| 71 | dvp++; |
| 72 | } |
| 73 | return frames; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 76 | for (dst = 0; dst < ndsts && dst < nsrcs; ++dst) { |
| 77 | copy_area(src_channels, dvp, frames, format); |
| 78 | dvp++; |
| 79 | src_channels++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 81 | if (dst < ndsts) |
| 82 | zero_areas(dvp, ndsts - dst, frames, format); |
| 83 | return frames; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 86 | int snd_pcm_plugin_build_route(struct snd_pcm_substream *plug, |
| 87 | struct snd_pcm_plugin_format *src_format, |
| 88 | struct snd_pcm_plugin_format *dst_format, |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 89 | struct snd_pcm_plugin **r_plugin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 91 | struct snd_pcm_plugin *plugin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | int err; |
| 93 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 94 | if (snd_BUG_ON(!r_plugin)) |
| 95 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | *r_plugin = NULL; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 97 | if (snd_BUG_ON(src_format->rate != dst_format->rate)) |
| 98 | return -ENXIO; |
| 99 | if (snd_BUG_ON(src_format->format != dst_format->format)) |
| 100 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 102 | err = snd_pcm_plugin_build(plug, "route conversion", |
| 103 | src_format, dst_format, 0, &plugin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | if (err < 0) |
| 105 | return err; |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | plugin->transfer = route_transfer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | *r_plugin = plugin; |
| 109 | return 0; |
| 110 | } |