mirror of
https://github.com/alsa-project/alsa-lib.git
synced 2025-11-10 13:30:01 -05:00
PCM plugin patches made by Abramo Bagnara.
Added new plugin - voice & balance.
This commit is contained in:
parent
7167132eed
commit
78dc424b72
11 changed files with 580 additions and 260 deletions
|
|
@ -1,7 +1,7 @@
|
|||
EXTRA_LTLIBRARIES = libpcmplugin.la
|
||||
|
||||
libpcmplugin_la_SOURCES = block.c mmap.c stream.c linear.c interleave.c \
|
||||
mulaw.c alaw.c adpcm.c rate.c voices.c
|
||||
mulaw.c alaw.c adpcm.c rate.c voices.c volbal.c
|
||||
all: libpcmplugin.la
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -880,17 +880,25 @@ static ssize_t adpcm_dst_size(snd_pcm_plugin_t *plugin, size_t size)
|
|||
}
|
||||
}
|
||||
|
||||
int snd_pcm_plugin_build_adpcm(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin)
|
||||
int snd_pcm_plugin_build_adpcm(snd_pcm_format_t *src_format,
|
||||
snd_pcm_format_t *dst_format,
|
||||
snd_pcm_plugin_t **r_plugin)
|
||||
{
|
||||
struct adpcm_private_data *data;
|
||||
snd_pcm_plugin_t *plugin;
|
||||
combination_t cmd;
|
||||
|
||||
if (!r_plugin)
|
||||
if (!r_plugin || !src_format || !dst_format)
|
||||
return -EINVAL;
|
||||
*r_plugin = NULL;
|
||||
if (dst_format == SND_PCM_SFMT_IMA_ADPCM) {
|
||||
switch (src_format) {
|
||||
|
||||
if (src_format->interleave != dst_format->interleave ||
|
||||
src_format->voices != dst_format->voices ||
|
||||
src_format->rate != dst_format->rate)
|
||||
return -EINVAL;
|
||||
|
||||
if (dst_format->format == SND_PCM_SFMT_IMA_ADPCM) {
|
||||
switch (src_format->format) {
|
||||
case SND_PCM_SFMT_U8: cmd = _U8_ADPCM; break;
|
||||
case SND_PCM_SFMT_S8: cmd = _S8_ADPCM; break;
|
||||
case SND_PCM_SFMT_U16_LE: cmd = _U16LE_ADPCM; break;
|
||||
|
|
@ -900,8 +908,8 @@ int snd_pcm_plugin_build_adpcm(int src_format, int dst_format, snd_pcm_plugin_t
|
|||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
} else if (src_format == SND_PCM_SFMT_IMA_ADPCM) {
|
||||
switch (dst_format) {
|
||||
} else if (src_format->format == SND_PCM_SFMT_IMA_ADPCM) {
|
||||
switch (dst_format->format) {
|
||||
case SND_PCM_SFMT_U8: cmd = _ADPCM_U8; break;
|
||||
case SND_PCM_SFMT_S8: cmd = _ADPCM_S8; break;
|
||||
case SND_PCM_SFMT_U16_LE: cmd = _ADPCM_U16LE; break;
|
||||
|
|
|
|||
|
|
@ -414,7 +414,9 @@ static ssize_t alaw_dst_size(snd_pcm_plugin_t *plugin, size_t size)
|
|||
}
|
||||
}
|
||||
|
||||
int snd_pcm_plugin_build_alaw(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin)
|
||||
int snd_pcm_plugin_build_alaw(snd_pcm_format_t *src_format,
|
||||
snd_pcm_format_t *dst_format,
|
||||
snd_pcm_plugin_t **r_plugin)
|
||||
{
|
||||
struct alaw_private_data *data;
|
||||
snd_pcm_plugin_t *plugin;
|
||||
|
|
@ -423,8 +425,16 @@ int snd_pcm_plugin_build_alaw(int src_format, int dst_format, snd_pcm_plugin_t *
|
|||
if (!r_plugin)
|
||||
return -EINVAL;
|
||||
*r_plugin = NULL;
|
||||
if (dst_format == SND_PCM_SFMT_A_LAW) {
|
||||
switch (src_format) {
|
||||
|
||||
if (src_format->interleave != dst_format->interleave)
|
||||
return -EINVAL;
|
||||
if (src_format->rate != dst_format->rate)
|
||||
return -EINVAL;
|
||||
if (src_format->voices != dst_format->voices)
|
||||
return -EINVAL;
|
||||
|
||||
if (dst_format->format == SND_PCM_SFMT_A_LAW) {
|
||||
switch (src_format->format) {
|
||||
case SND_PCM_SFMT_U8: cmd = _U8_ALAW; break;
|
||||
case SND_PCM_SFMT_S8: cmd = _S8_ALAW; break;
|
||||
case SND_PCM_SFMT_U16_LE: cmd = _U16LE_ALAW; break;
|
||||
|
|
@ -434,8 +444,8 @@ int snd_pcm_plugin_build_alaw(int src_format, int dst_format, snd_pcm_plugin_t *
|
|||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
} else if (src_format == SND_PCM_SFMT_A_LAW) {
|
||||
switch (dst_format) {
|
||||
} else if (src_format->format == SND_PCM_SFMT_A_LAW) {
|
||||
switch (dst_format->format) {
|
||||
case SND_PCM_SFMT_U8: cmd = _ALAW_U8; break;
|
||||
case SND_PCM_SFMT_S8: cmd = _ALAW_S8; break;
|
||||
case SND_PCM_SFMT_U16_LE: cmd = _ALAW_U16LE; break;
|
||||
|
|
|
|||
|
|
@ -177,7 +177,9 @@ static ssize_t interleave_transfer(snd_pcm_plugin_t *plugin,
|
|||
return src_size;
|
||||
}
|
||||
|
||||
int snd_pcm_plugin_build_interleave(int src_interleave, int dst_interleave, int format, snd_pcm_plugin_t **r_plugin)
|
||||
int snd_pcm_plugin_build_interleave(snd_pcm_format_t *src_format,
|
||||
snd_pcm_format_t *dst_format,
|
||||
snd_pcm_plugin_t **r_plugin)
|
||||
{
|
||||
struct interleave_private_data *data;
|
||||
snd_pcm_plugin_t *plugin;
|
||||
|
|
@ -186,14 +188,21 @@ int snd_pcm_plugin_build_interleave(int src_interleave, int dst_interleave, int
|
|||
|
||||
if (!r_plugin)
|
||||
return -EINVAL;
|
||||
if (src_interleave && !dst_interleave) {
|
||||
if (src_format->interleave && !dst_format->interleave) {
|
||||
cmd = _INTERLEAVE_NON;
|
||||
} else if (!src_interleave && dst_interleave) {
|
||||
} else if (!src_format->interleave && dst_format->interleave) {
|
||||
cmd = _NON_INTERLEAVE;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
switch (format) {
|
||||
if (src_format->format != dst_format->format)
|
||||
return -EINVAL;
|
||||
if (src_format->rate != dst_format->rate)
|
||||
return -EINVAL;
|
||||
if (src_format->voices != dst_format->voices)
|
||||
return -EINVAL;
|
||||
|
||||
switch (dst_format->format) {
|
||||
case SND_PCM_SFMT_S8:
|
||||
case SND_PCM_SFMT_U8: size = 1; break;
|
||||
case SND_PCM_SFMT_S16_LE:
|
||||
|
|
|
|||
|
|
@ -310,7 +310,9 @@ static int linear_sign(int format)
|
|||
}
|
||||
}
|
||||
|
||||
int snd_pcm_plugin_build_linear(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin)
|
||||
int snd_pcm_plugin_build_linear(snd_pcm_format_t *src_format,
|
||||
snd_pcm_format_t *dst_format,
|
||||
snd_pcm_plugin_t **r_plugin)
|
||||
{
|
||||
struct linear_private_data *data;
|
||||
snd_pcm_plugin_t *plugin;
|
||||
|
|
@ -320,12 +322,20 @@ int snd_pcm_plugin_build_linear(int src_format, int dst_format, snd_pcm_plugin_t
|
|||
if (!r_plugin)
|
||||
return -EINVAL;
|
||||
*r_plugin = NULL;
|
||||
wide1 = linear_wide(src_format);
|
||||
endian1 = linear_endian(src_format);
|
||||
sign1 = linear_sign(src_format);
|
||||
wide2 = linear_wide(dst_format);
|
||||
endian2 = linear_endian(dst_format);
|
||||
sign2 = linear_sign(dst_format);
|
||||
|
||||
if (src_format->interleave != dst_format->interleave)
|
||||
return -EINVAL;
|
||||
if (src_format->rate != dst_format->rate)
|
||||
return -EINVAL;
|
||||
if (src_format->voices != dst_format->voices)
|
||||
return -EINVAL;
|
||||
|
||||
wide1 = linear_wide(src_format->format);
|
||||
endian1 = linear_endian(src_format->format);
|
||||
sign1 = linear_sign(src_format->format);
|
||||
wide2 = linear_wide(dst_format->format);
|
||||
endian2 = linear_endian(dst_format->format);
|
||||
sign2 = linear_sign(dst_format->format);
|
||||
if (wide1 < 0 || wide2 < 0 || endian1 < 0 || endian2 < 0 || sign1 < 0 || sign2 < 0)
|
||||
return -EINVAL;
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
|
|
|
|||
|
|
@ -424,7 +424,9 @@ static ssize_t mulaw_dst_size(snd_pcm_plugin_t *plugin, size_t size)
|
|||
}
|
||||
}
|
||||
|
||||
int snd_pcm_plugin_build_mulaw(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin)
|
||||
int snd_pcm_plugin_build_mulaw(snd_pcm_format_t *src_format,
|
||||
snd_pcm_format_t *dst_format,
|
||||
snd_pcm_plugin_t **r_plugin)
|
||||
{
|
||||
struct mulaw_private_data *data;
|
||||
snd_pcm_plugin_t *plugin;
|
||||
|
|
@ -433,8 +435,16 @@ int snd_pcm_plugin_build_mulaw(int src_format, int dst_format, snd_pcm_plugin_t
|
|||
if (!r_plugin)
|
||||
return -EINVAL;
|
||||
*r_plugin = NULL;
|
||||
if (dst_format == SND_PCM_SFMT_MU_LAW) {
|
||||
switch (src_format) {
|
||||
|
||||
if (src_format->interleave != dst_format->interleave)
|
||||
return -EINVAL;
|
||||
if (src_format->rate != dst_format->rate)
|
||||
return -EINVAL;
|
||||
if (src_format->voices != dst_format->voices)
|
||||
return -EINVAL;
|
||||
|
||||
if (dst_format->format == SND_PCM_SFMT_MU_LAW) {
|
||||
switch (src_format->format) {
|
||||
case SND_PCM_SFMT_U8: cmd = _U8_MULAW; break;
|
||||
case SND_PCM_SFMT_S8: cmd = _S8_MULAW; break;
|
||||
case SND_PCM_SFMT_U16_LE: cmd = _U16LE_MULAW; break;
|
||||
|
|
@ -444,8 +454,8 @@ int snd_pcm_plugin_build_mulaw(int src_format, int dst_format, snd_pcm_plugin_t
|
|||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
} else if (src_format == SND_PCM_SFMT_MU_LAW) {
|
||||
switch (dst_format) {
|
||||
} else if (src_format->format == SND_PCM_SFMT_MU_LAW) {
|
||||
switch (dst_format->format) {
|
||||
case SND_PCM_SFMT_U8: cmd = _MULAW_U8; break;
|
||||
case SND_PCM_SFMT_S8: cmd = _MULAW_S8; break;
|
||||
case SND_PCM_SFMT_U16_LE: cmd = _MULAW_U16LE; break;
|
||||
|
|
|
|||
|
|
@ -48,6 +48,43 @@ struct rate_private_data {
|
|||
ssize_t old_src_size, old_dst_size;
|
||||
};
|
||||
|
||||
static void mix_mono(struct rate_private_data *data,
|
||||
signed short *src_ptr, int src_size,
|
||||
signed short *dst_ptr, int dst_size)
|
||||
{
|
||||
unsigned int pos;
|
||||
signed int val;
|
||||
signed int L_S1, L_S2;
|
||||
|
||||
pos = data->pos;
|
||||
L_S1 = data->last_L_S1;
|
||||
L_S2 = data->last_L_S2;
|
||||
if (pos >> SHIFT) {
|
||||
src_ptr += ((pos >> SHIFT) - 1); pos &= MASK;
|
||||
L_S1 = L_S2;
|
||||
L_S2 = *src_ptr;
|
||||
}
|
||||
while (dst_size-- > 0) {
|
||||
if (pos >> SHIFT) {
|
||||
src_ptr += (pos >> SHIFT); pos &= MASK;
|
||||
L_S1 = L_S2;
|
||||
L_S2 = *src_ptr;
|
||||
}
|
||||
|
||||
val = L_S1 + ((L_S2 - L_S1) * (signed int)pos) / BITS;
|
||||
if (val < -32768)
|
||||
val = -32768;
|
||||
else if (val > 32767)
|
||||
val = 32767;
|
||||
*dst_ptr++ = val;
|
||||
|
||||
pos += data->pitch;
|
||||
}
|
||||
data->last_L_S1 = L_S1;
|
||||
data->last_L_S2 = L_S2;
|
||||
data->pos = pos;
|
||||
}
|
||||
|
||||
static void mix_stereo(struct rate_private_data *data,
|
||||
signed short *src_ptr, int src_size,
|
||||
signed short *dst_ptr, int dst_size)
|
||||
|
|
@ -115,7 +152,12 @@ static ssize_t rate_transfer(snd_pcm_plugin_t *plugin,
|
|||
data = (struct rate_private_data *)snd_pcm_plugin_extra_data(plugin);
|
||||
if (data == NULL)
|
||||
return -EINVAL;
|
||||
if (data->src_voices == 2) {
|
||||
if (data->src_voices == 1) {
|
||||
mix_mono(data, (signed short *)src_ptr, src_size / 2,
|
||||
(signed short *)dst_ptr, dst_size / 2);
|
||||
return (dst_size / 2) * 2;
|
||||
}
|
||||
else if (data->src_voices == 2) {
|
||||
mix_stereo(data, (signed short *)src_ptr, src_size / 4,
|
||||
(signed short *)dst_ptr, dst_size / 4);
|
||||
return (dst_size / 4) * 4;
|
||||
|
|
@ -152,7 +194,9 @@ static ssize_t rate_src_size(snd_pcm_plugin_t *plugin, size_t size)
|
|||
if (!plugin || size <= 0)
|
||||
return -EINVAL;
|
||||
data = (struct rate_private_data *)snd_pcm_plugin_extra_data(plugin);
|
||||
res = (((size * data->pitch) + (BITS/2)) >> SHIFT) & ~3;
|
||||
res = (((size * data->pitch) + (BITS/2)) >> SHIFT);
|
||||
res = res / (data->src_voices*2) * (data->src_voices*2);
|
||||
/* Why this? */
|
||||
if (size < 128*1024) {
|
||||
if (data->old_src_size == size)
|
||||
return data->old_dst_size;
|
||||
|
|
@ -170,7 +214,9 @@ static ssize_t rate_dst_size(snd_pcm_plugin_t *plugin, size_t size)
|
|||
if (!plugin || size <= 0)
|
||||
return -EINVAL;
|
||||
data = (struct rate_private_data *)snd_pcm_plugin_extra_data(plugin);
|
||||
res = (((size << SHIFT) + (data->pitch / 2)) / data->pitch) & ~3;
|
||||
res = (((size << SHIFT) + (data->pitch / 2)) / data->pitch);
|
||||
res = res / (data->src_voices*2) * (data->src_voices*2);
|
||||
/* Why this? */
|
||||
if (size < 128*1024) {
|
||||
if (data->old_dst_size == size)
|
||||
return data->old_src_size;
|
||||
|
|
@ -180,8 +226,8 @@ static ssize_t rate_dst_size(snd_pcm_plugin_t *plugin, size_t size)
|
|||
return res;
|
||||
}
|
||||
|
||||
int snd_pcm_plugin_build_rate(int src_format, int src_rate, int src_voices,
|
||||
int dst_format, int dst_rate, int dst_voices,
|
||||
int snd_pcm_plugin_build_rate(snd_pcm_format_t *src_format,
|
||||
snd_pcm_format_t *dst_format,
|
||||
snd_pcm_plugin_t **r_plugin)
|
||||
{
|
||||
struct rate_private_data *data;
|
||||
|
|
@ -190,23 +236,33 @@ int snd_pcm_plugin_build_rate(int src_format, int src_rate, int src_voices,
|
|||
if (!r_plugin)
|
||||
return -EINVAL;
|
||||
*r_plugin = NULL;
|
||||
if (src_voices != 2 || dst_voices != 2)
|
||||
|
||||
if (src_format->interleave != dst_format->interleave)
|
||||
return -EINVAL;
|
||||
if (src_format != SND_PCM_SFMT_S16_LE ||
|
||||
dst_format != SND_PCM_SFMT_S16_LE)
|
||||
if (src_format->format != dst_format->format)
|
||||
return -EINVAL;
|
||||
if (src_rate == dst_rate)
|
||||
if (!dst_format->interleave)
|
||||
return -EINVAL;
|
||||
if (src_format->voices != dst_format->voices)
|
||||
return -EINVAL;
|
||||
if (dst_format->voices != 1 && dst_format->voices != 2)
|
||||
return -EINVAL;
|
||||
|
||||
if (src_format->format != SND_PCM_SFMT_S16_LE ||
|
||||
dst_format->format != SND_PCM_SFMT_S16_LE)
|
||||
return -EINVAL;
|
||||
if (src_format->rate == dst_format->rate)
|
||||
return -EINVAL;
|
||||
plugin = snd_pcm_plugin_build("rate format conversion",
|
||||
sizeof(struct rate_private_data));
|
||||
if (plugin == NULL)
|
||||
return -ENOMEM;
|
||||
data = (struct rate_private_data *)snd_pcm_plugin_extra_data(plugin);
|
||||
data->src_voices = src_voices;
|
||||
data->dst_voices = dst_voices;
|
||||
data->src_rate = src_rate;
|
||||
data->dst_rate = dst_rate;
|
||||
data->pitch = ((src_rate << SHIFT) + (dst_rate >> 1)) / dst_rate;
|
||||
data->src_voices = src_format->voices;
|
||||
data->dst_voices = dst_format->voices;
|
||||
data->src_rate = src_format->rate;
|
||||
data->dst_rate = dst_format->rate;
|
||||
data->pitch = ((src_format->rate << SHIFT) + (dst_format->rate >> 1)) / dst_format->rate;
|
||||
data->pos = 0;
|
||||
data->last_L_S1 = data->last_R_S1 = 0;
|
||||
data->last_L_S2 = data->last_R_S2 = 0;
|
||||
|
|
|
|||
|
|
@ -168,8 +168,8 @@ static ssize_t voices_dst_size(snd_pcm_plugin_t *plugin, size_t size)
|
|||
return (size * data->dst_voices) / data->src_voices;
|
||||
}
|
||||
|
||||
int snd_pcm_plugin_build_voices(int src_format, int src_voices,
|
||||
int dst_format, int dst_voices,
|
||||
int snd_pcm_plugin_build_voices(snd_pcm_format_t *src_format,
|
||||
snd_pcm_format_t *dst_format,
|
||||
snd_pcm_plugin_t **r_plugin)
|
||||
{
|
||||
struct voices_private_data *data;
|
||||
|
|
@ -178,15 +178,22 @@ int snd_pcm_plugin_build_voices(int src_format, int src_voices,
|
|||
if (!r_plugin)
|
||||
return -EINVAL;
|
||||
*r_plugin = NULL;
|
||||
if (src_voices == dst_voices)
|
||||
|
||||
if (src_format->interleave != dst_format->interleave)
|
||||
return -EINVAL;
|
||||
if (src_voices < 1 || src_voices > 2 ||
|
||||
dst_voices < 1 || dst_voices > 2)
|
||||
if (!dst_format->interleave)
|
||||
return -EINVAL;
|
||||
if (src_format != dst_format)
|
||||
if (src_format->format != dst_format->format)
|
||||
return -EINVAL;
|
||||
if (src_format < SND_PCM_SFMT_S8 || src_format > SND_PCM_SFMT_U16_BE) {
|
||||
if (src_format != SND_PCM_SFMT_MU_LAW && src_format != SND_PCM_SFMT_A_LAW)
|
||||
if (src_format->rate != dst_format->rate)
|
||||
return -EINVAL;
|
||||
if (src_format->voices == dst_format->voices)
|
||||
return -EINVAL;
|
||||
if (src_format->voices < 1 || src_format->voices > 2 ||
|
||||
dst_format->voices < 1 || dst_format->voices > 2)
|
||||
return -EINVAL;
|
||||
if (src_format->format < SND_PCM_SFMT_S8 || src_format->format > SND_PCM_SFMT_U16_BE) {
|
||||
if (src_format->format != SND_PCM_SFMT_MU_LAW && src_format->format != SND_PCM_SFMT_A_LAW)
|
||||
return -EINVAL;
|
||||
}
|
||||
plugin = snd_pcm_plugin_build("voices conversion",
|
||||
|
|
@ -194,11 +201,11 @@ int snd_pcm_plugin_build_voices(int src_format, int src_voices,
|
|||
if (plugin == NULL)
|
||||
return -ENOMEM;
|
||||
data = (struct voices_private_data *)snd_pcm_plugin_extra_data(plugin);
|
||||
data->src_voices = src_voices;
|
||||
data->dst_voices = dst_voices;
|
||||
data->width = snd_pcm_format_width(src_format);
|
||||
data->flg_merge = src_voices > dst_voices;
|
||||
data->flg_signed = snd_pcm_format_signed(src_format);
|
||||
data->src_voices = src_format->voices;
|
||||
data->dst_voices = dst_format->voices;
|
||||
data->width = snd_pcm_format_width(src_format->format);
|
||||
data->flg_merge = src_format->voices > dst_format->voices;
|
||||
data->flg_signed = snd_pcm_format_signed(src_format->format);
|
||||
plugin->transfer = voices_transfer;
|
||||
plugin->src_size = voices_src_size;
|
||||
plugin->dst_size = voices_dst_size;
|
||||
|
|
|
|||
171
src/pcm/plugin/volbal.c
Normal file
171
src/pcm/plugin/volbal.c
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
/*
|
||||
* Volume and balance Plug-In
|
||||
* Copyright (c) 1999 by Abramo Bagnara <abbagnara@racine.ra.it>
|
||||
*
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Library General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <endian.h>
|
||||
#include <byteswap.h>
|
||||
#include "../pcm_local.h"
|
||||
|
||||
|
||||
#define VOLBAL_RESOLUTION 16
|
||||
|
||||
struct volbal_private_data {
|
||||
int src_voices;
|
||||
int noop;
|
||||
int ttable[0];
|
||||
};
|
||||
|
||||
static void volbal(int voices, int samples, int *ttable,
|
||||
signed short *src_ptr, signed short *dst_ptr)
|
||||
{
|
||||
while (samples-- > 0) {
|
||||
int dst_voice;
|
||||
int *t = ttable;
|
||||
for (dst_voice = 0; dst_voice < voices; ++dst_voice) {
|
||||
int v = 0;
|
||||
int src_voice;
|
||||
signed short *s = src_ptr;
|
||||
for (src_voice = 0; src_voice < voices; ++src_voice) {
|
||||
v += *s++ * *t++ / VOLBAL_RESOLUTION;
|
||||
}
|
||||
*dst_ptr++ = v;
|
||||
src_ptr += voices;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static ssize_t volbal_transfer(snd_pcm_plugin_t *plugin,
|
||||
char *src_ptr, size_t src_size,
|
||||
char *dst_ptr, size_t dst_size)
|
||||
{
|
||||
struct volbal_private_data *data;
|
||||
if (plugin == NULL || src_ptr == NULL || src_size < 0 ||
|
||||
dst_ptr == NULL || dst_size < 0)
|
||||
return -EINVAL;
|
||||
if (src_size == 0)
|
||||
return 0;
|
||||
data = (struct volbal_private_data *)snd_pcm_plugin_extra_data(plugin);
|
||||
if (data == NULL)
|
||||
return -EINVAL;
|
||||
if (data->noop)
|
||||
return 0;
|
||||
|
||||
volbal(data->src_voices, src_size / 2 / data->src_voices, data->ttable,
|
||||
(signed short *)src_ptr, (signed short *)dst_ptr);
|
||||
return src_size;
|
||||
}
|
||||
|
||||
static ssize_t volbal_src_size(snd_pcm_plugin_t *plugin, size_t size)
|
||||
{
|
||||
if (!plugin || size <= 0)
|
||||
return -EINVAL;
|
||||
return size;
|
||||
}
|
||||
|
||||
static ssize_t volbal_dst_size(snd_pcm_plugin_t *plugin, size_t size)
|
||||
{
|
||||
if (!plugin || size <= 0)
|
||||
return -EINVAL;
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
static int volbal_load_ttable(struct volbal_private_data *data,
|
||||
const int *src_ttable)
|
||||
{
|
||||
int src_voice, dst_voice;
|
||||
const int *sptr;
|
||||
int *dptr;
|
||||
data->noop = 1;
|
||||
if (src_ttable == NULL)
|
||||
return 0;
|
||||
sptr = src_ttable;
|
||||
dptr = data->ttable;
|
||||
for (dst_voice = 0; dst_voice < data->src_voices; ++dst_voice) {
|
||||
int t = 0;
|
||||
for (src_voice = 0; src_voice < data->src_voices; ++src_voice) {
|
||||
if (*sptr < 0 || *sptr > VOLBAL_RESOLUTION)
|
||||
return -EINVAL;
|
||||
if (src_voice == dst_voice) {
|
||||
if (*sptr != VOLBAL_RESOLUTION)
|
||||
data->noop = 0;
|
||||
}
|
||||
else {
|
||||
if (*sptr != 0)
|
||||
data->noop = 0;
|
||||
}
|
||||
t += *sptr;
|
||||
*dptr++ = *sptr++;
|
||||
}
|
||||
if (t > VOLBAL_RESOLUTION)
|
||||
return -EINVAL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int snd_pcm_plugin_build_volbal(snd_pcm_format_t *src_format,
|
||||
snd_pcm_format_t *dst_format,
|
||||
int *ttable,
|
||||
snd_pcm_plugin_t **r_plugin)
|
||||
{
|
||||
struct volbal_private_data *data;
|
||||
snd_pcm_plugin_t *plugin;
|
||||
int res;
|
||||
|
||||
if (!r_plugin)
|
||||
return -EINVAL;
|
||||
*r_plugin = NULL;
|
||||
if (src_format->interleave != dst_format->interleave)
|
||||
return -EINVAL;
|
||||
if (!dst_format->interleave)
|
||||
return -EINVAL;
|
||||
if (src_format->format != dst_format->format)
|
||||
return -EINVAL;
|
||||
if (src_format->rate != dst_format->rate)
|
||||
return -EINVAL;
|
||||
if (src_format->voices != dst_format->voices)
|
||||
return -EINVAL;
|
||||
|
||||
if (src_format->voices < 1)
|
||||
return -EINVAL;
|
||||
if (src_format->format != SND_PCM_SFMT_S16_LE)
|
||||
return -EINVAL;
|
||||
plugin = snd_pcm_plugin_build("Volume/balance conversion",
|
||||
sizeof(struct volbal_private_data) +
|
||||
sizeof(data->ttable[0]) * src_format->voices * src_format->voices);
|
||||
if (plugin == NULL)
|
||||
return -ENOMEM;
|
||||
data = (struct volbal_private_data *)snd_pcm_plugin_extra_data(plugin);
|
||||
data->src_voices = src_format->voices;
|
||||
if ((res = volbal_load_ttable(data, ttable)) < 0)
|
||||
return res;
|
||||
|
||||
plugin->transfer = volbal_transfer;
|
||||
plugin->src_size = volbal_src_size;
|
||||
plugin->dst_size = volbal_dst_size;
|
||||
*r_plugin = plugin;
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue