route.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Route Plug-In
  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. #include <linux/time.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include "pcm_plugin.h"
  25. static void zero_areas(struct snd_pcm_plugin_channel *dvp, int ndsts,
  26. snd_pcm_uframes_t frames, snd_pcm_format_t format)
  27. {
  28. int dst = 0;
  29. for (; dst < ndsts; ++dst) {
  30. if (dvp->wanted)
  31. snd_pcm_area_silence(&dvp->area, 0, frames, format);
  32. dvp->enabled = 0;
  33. dvp++;
  34. }
  35. }
  36. static inline void copy_area(const struct snd_pcm_plugin_channel *src_channel,
  37. struct snd_pcm_plugin_channel *dst_channel,
  38. snd_pcm_uframes_t frames, snd_pcm_format_t format)
  39. {
  40. dst_channel->enabled = 1;
  41. snd_pcm_area_copy(&src_channel->area, 0, &dst_channel->area, 0, frames, format);
  42. }
  43. static snd_pcm_sframes_t route_transfer(struct snd_pcm_plugin *plugin,
  44. const struct snd_pcm_plugin_channel *src_channels,
  45. struct snd_pcm_plugin_channel *dst_channels,
  46. snd_pcm_uframes_t frames)
  47. {
  48. int nsrcs, ndsts, dst;
  49. struct snd_pcm_plugin_channel *dvp;
  50. snd_pcm_format_t format;
  51. if (snd_BUG_ON(!plugin || !src_channels || !dst_channels))
  52. return -ENXIO;
  53. if (frames == 0)
  54. return 0;
  55. if (frames > dst_channels[0].frames)
  56. frames = dst_channels[0].frames;
  57. nsrcs = plugin->src_format.channels;
  58. ndsts = plugin->dst_format.channels;
  59. format = plugin->dst_format.format;
  60. dvp = dst_channels;
  61. if (nsrcs <= 1) {
  62. /* expand to all channels */
  63. for (dst = 0; dst < ndsts; ++dst) {
  64. copy_area(src_channels, dvp, frames, format);
  65. dvp++;
  66. }
  67. return frames;
  68. }
  69. for (dst = 0; dst < ndsts && dst < nsrcs; ++dst) {
  70. copy_area(src_channels, dvp, frames, format);
  71. dvp++;
  72. src_channels++;
  73. }
  74. if (dst < ndsts)
  75. zero_areas(dvp, ndsts - dst, frames, format);
  76. return frames;
  77. }
  78. int snd_pcm_plugin_build_route(struct snd_pcm_substream *plug,
  79. struct snd_pcm_plugin_format *src_format,
  80. struct snd_pcm_plugin_format *dst_format,
  81. struct snd_pcm_plugin **r_plugin)
  82. {
  83. struct snd_pcm_plugin *plugin;
  84. int err;
  85. if (snd_BUG_ON(!r_plugin))
  86. return -ENXIO;
  87. *r_plugin = NULL;
  88. if (snd_BUG_ON(src_format->rate != dst_format->rate))
  89. return -ENXIO;
  90. if (snd_BUG_ON(src_format->format != dst_format->format))
  91. return -ENXIO;
  92. err = snd_pcm_plugin_build(plug, "route conversion",
  93. src_format, dst_format, 0, &plugin);
  94. if (err < 0)
  95. return err;
  96. plugin->transfer = route_transfer;
  97. *r_plugin = plugin;
  98. return 0;
  99. }