External Plugin: Filter-Type Plugin

The filter-type plugin is a plugin to convert the PCM signals from the input and feeds to the output. Thus, this plugin always needs a slave PCM as its output.

The plugin can modify the format and the channels of the input/output PCM. It can not modify the sample rate (because of simplicity reason).

The following fields have to be filled in extplug record before calling snd_pcm_extplug_create() : version, name, callback. Otherfields are optional and should be initialized with zero.

The constant SND_PCM_EXTPLUG_VERSION must be passed to the version field for the version check in alsa-lib. A non-NULL ASCII string has to be passed to the name field. The callback field contains the table of callback functions for this plugin (defined as snd_pcm_extplug_callback_t).

The driver can set an arbitrary value (pointer) to private_data field to refer its own data in the callbacks.

The rest fields are filled by snd_pcm_extplug_create(). The pcm field is the resultant PCM handle. The others are the current status of the PCM.

The callback functions in snd_pcm_extplug_callback_t define the real behavior of the driver. At least, transfer callback must be given. This callback is called at each time certain size of data block is transfered to the slave PCM. Other callbacks are optional. 
The close callback is called when the PCM is closed. If the plugin allocates private resources, this is the place to release them again. The hw_params and hw_free callbacks are called at snd_pcm_hw_params() and snd_pcm_hw_free() API calls, respectively. The last, dump callback, is called for printing the information of the given plugin.

The init callback is called when the PCM is at prepare state or any initialization is issued. Use this callback to reset the PCM instance to a sane initial state.

The hw_params constraints can be defined via either snd_pcm_extplug_set_param_minmax() and snd_pcm_extplug_set_param_list() functions after callingsnd_pcm_extplug_create(). The former defines the minimal and maximal acceptable values for the given hw_params parameter (SND_PCM_EXTPLUG_HW_XXX). This function can't be used for the format parameter. The latter function specifies the available parameter values as the list. As mentioned above, the rate can't be changed. Only changeable parameters are sample format and channels.

To define the constraints of the slave PCM configuration, use either snd_pcm_extplug_set_slave_param_minmax() and snd_pcm_extplug_set_slave_param_list(). The arguments are as same as former functions.

To clear the parameter constraints, call snd_pcm_extplug_params_reset() function.

When using snd_pcm_extplug_set_param_*() or snd_pcm_extplug_set_slave_param_*() for any parameter. This parameter is no longer linked between the client and slave PCM. Therefore it could differ and the extplug has to support conversion between all valid parameter configurations. To keep the client and slave parameter linkedsnd_pcm_extplug_set_param_link() can be used for the corresponding parameter. For example if the extplug does not support channel nor format conversion the supported client parameters can be limited with snd_pcm_extplug_set_param_*() and afterwards #snd_pcm_extplug_set_param_link(ext, SND_PCM_EXTPLUG_HW_FORMAT, 1) and #snd_pcm_extplug_set_param_link(ext, SND_PCM_EXTPLUG_HW_CHANNELS, 1) should be called to keep the client and slave parameters the same.

参考pcm_speex.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <alsa/asoundlib.h>
#include <alsa/pcm_external.h> struct ctx_parms {
int frames;
int enable_dump;
FILE *dump_fp;
float gain;
}; typedef struct {
snd_pcm_extplug_t ext;
struct ctx_parms parms;
short *buf;
short *outbuf;
unsigned int filled;
unsigned int processed;
}snd_pcm_fellowext_t; static inline void *area_addr(const snd_pcm_channel_area_t *area, snd_pcm_uframes_t offset)
{
unsigned int bitofs = area->first + area->step * offset;
return (char *)area->addr + bitofs / ;
} static void process(snd_pcm_fellowext_t *ctx)
{
int frames = ctx->parms.frames;
short *inbuf = ctx->buf;
short *outbuf= ctx->outbuf;
int channels= ctx->ext.channels;
int ch_idx = , frame_idx = ;
for (frame_idx = ; frame_idx < frames; frame_idx++)
{
for (ch_idx = ; ch_idx < channels; ch_idx++)
{
outbuf[frame_idx * channels + ch_idx] = (short)((float)inbuf[frame_idx * channels + ch_idx] * ctx->parms.gain);
}
}
} static snd_pcm_sframes_t fellowext_transfer(snd_pcm_extplug_t *ext, const snd_pcm_channel_area_t *dst_areas, snd_pcm_uframes_t dst_offset, const snd_pcm_channel_area_t *src_areas, snd_pcm_uframes_t src_offset, snd_pcm_uframes_t size)
{
snd_pcm_fellowext_t *ctx = (snd_pcm_fellowext_t *)ext;
short *src = area_addr(src_areas, src_offset);
short *dst = area_addr(dst_areas, dst_offset);
unsigned int count = size;
int channels = ctx->ext.channels;
int bytes_per_frame = * channels; while (count > ) {
unsigned int chunk;
if (ctx->filled + count > ctx->parms.frames)
chunk = ctx->parms.frames - ctx->filled;
else
chunk = count;
if (ctx->processed)
memcpy(dst, ctx->outbuf + ctx->filled * channels, chunk * bytes_per_frame);
else
memset(dst, , chunk * bytes_per_frame);
if (ctx->parms.enable_dump)
fwrite(dst, , chunk * bytes_per_frame, ctx->parms.dump_fp);
dst += chunk * channels; memcpy(ctx->buf + ctx->filled * channels, src, chunk * bytes_per_frame);
ctx->filled += chunk;
if (ctx->filled == ctx->parms.frames) {
process(ctx);
ctx->processed = ;
ctx->filled = 0;
}
src += chunk * channels;
count -= chunk;
}
return size;
} static int fellowext_init(snd_pcm_extplug_t *ext)
{
snd_pcm_fellowext_t *ctx = (snd_pcm_fellowext_t *)ext;
int channels = ctx->ext.channels; ctx->filled = ;
ctx->processed = ;
if (!ctx->buf) {
ctx->buf = malloc(ctx->parms.frames * * channels);
if (!ctx->buf)
return -ENOMEM;
}
memset (ctx->buf, , ctx->parms.frames * * channels);
if (!ctx->outbuf) {
ctx->outbuf = malloc(ctx->parms.frames * * channels);
if (!ctx->outbuf)
return -ENOMEM;
}
memset (ctx->outbuf, , ctx->parms.frames * * channels);
return ;
} static int fellowext_close(snd_pcm_extplug_t *ext)
{
snd_pcm_fellowext_t *ctx = (snd_pcm_fellowext_t *)ext;
if (ctx->parms.enable_dump)
fclose(ctx->parms.dump_fp);
if (ctx->buf) {
free(ctx->buf);
ctx->buf = NULL;
}
if (ctx->outbuf) {
free(ctx->outbuf);
ctx->outbuf = NULL;
}
return ;
} static const snd_pcm_extplug_callback_t fellowext_callback = {
.transfer = fellowext_transfer,
.init = fellowext_init,
.close = fellowext_close,
}; static int get_bool_parm(snd_config_t *n, const char *id, const char *str, int *val_ret)
{
int val;
if (strcmp(id, str))
return ;
val = snd_config_get_bool(n);
if (val < ) {
SNDERR("Invalid value for %s", id );
return val;
}
*val_ret = val;
return ;
} static int get_int_parm(snd_config_t *n, const char *id, const char *str, int *val_ret)
{
long val;
int err;
if (strcmp(id, str))
return ;
err = snd_config_get_integer(n, &val);
if (err < ) {
SNDERR("Invalid value for %s", id );
return err;
}
*val_ret = val;
return ;
} static int get_float_parm(snd_config_t *n, const char *id, const char *str, float *val_ret)
{
double val;
int err;
if (strcmp(id, str))
return ;
err = snd_config_get_ireal(n, &val);
if (err < ) {
SNDERR("Invalid value for %s", id );
return err;
}
*val_ret = val;
return ;
}
SND_PCM_PLUGIN_DEFINE_FUNC(fellowext)
{
snd_config_iterator_t i, next;
snd_pcm_fellowext_t *ctx;
snd_config_t *sconf = NULL;
int err;
struct ctx_parms parms = {
.frames = ,
.enable_dump = ,
.dump_fp = NULL,
.gain = 0.5,
}; snd_config_for_each(i, next, conf) {
snd_config_t *n = snd_config_iterator_entry(i);
const char *id;
if (snd_config_get_id(n, &id) < )
continue;
if (strcmp(id, "comment") == || strcmp(id, "type") == )
continue;
if (strcmp(id, "slave") == ) {
sconf = n;
continue;
}
err = get_int_parm(n, id, "frames", &parms.frames);
if (err)
goto ok;
err = get_bool_parm(n, id, "enable_dump", &parms.enable_dump);
if (err)
goto ok;
err = get_float_parm(n, id, "gain", &parms.gain);
if (err)
goto ok; SNDERR("Unknown field %s", id);
return -EINVAL;
ok:
if (err < )
return err;
}
if (!sconf) {
SNDERR("No slave configuration for fellowext pcm");
return -EINVAL;
}
if (parms.enable_dump)
parms.dump_fp = fopen("extplug.pcm", "wb");
ctx = calloc(, sizeof(*ctx));
if (!ctx)
return -ENOMEM;
ctx->ext.version = SND_PCM_EXTPLUG_VERSION;
ctx->ext.name = "Fellow Ext Plugin";
ctx->ext.callback = &fellowext_callback;
ctx->ext.private_data = ctx;
ctx->parms = parms;
err = snd_pcm_extplug_create(&ctx->ext, name, root, sconf, stream, mode);
if (err < ) {
free(ctx);
return err;
}
snd_pcm_extplug_set_param(&ctx->ext, SND_PCM_EXTPLUG_HW_CHANNELS, );
snd_pcm_extplug_set_slave_param(&ctx->ext, SND_PCM_EXTPLUG_HW_CHANNELS, );
snd_pcm_extplug_set_param(&ctx->ext, SND_PCM_EXTPLUG_HW_FORMAT, SND_PCM_FORMAT_S16);
snd_pcm_extplug_set_slave_param(&ctx->ext, SND_PCM_EXTPLUG_HW_FORMAT, SND_PCM_FORMAT_S16);
*pcmp = ctx->ext.pcm;
return ;
}
SND_PCM_PLUGIN_SYMBOL(fellowext);

Makefile:

ALSA_INC_PATH=/home/fellow/alsa-lib-1.2./output/usr/include
ALSA_LIB_PATH=/usr/lib/i386-linux-gnu export LD_LIBRARY_PATH=${ALSA_LIB_PATH}:$LD_LIBRARY_PATH
export CC=gcc
export CFLAGS=-I${ALSA_INC_PATH}
export LDFLAGS=-L{ALSA_LIB_PATH} -lasound
SOURCE=pcm_fellowext.c
TARGET=libasound_module_pcm_fellowext.so
all:
${CC} ${SOURCE} ${CFLAGS} ${LDFLAGS} -shared -fPIC -DPIC -Wall -Werror -o ${TARGET}

将libasound_module_pcm_fellowext.so copy到/usr/lib/alsa-lib

在home目录下新建.asoundrc

pcm.myout {
type fellow
slavepcm "default"
dumpfile "fellowdump.pcm"
} pcm.out1 {
type plug
slave.pcm myout
} pcm.out {
type plug
slave.pcm "default"
} pcm.myext {
type fellowext
enable_dump
slave {
pcm out1
}
}

执行命令:aplay -D myext xxx.wav

ALSA lib-ext plugin的更多相关文章

  1. jmeter的dubbo压测,依赖jar包要放到执行机的lib/ext下

    对于jmeter的dubbo压测场景的master-slave结构: 即master的jmeter进行任务的下发和报告的生成,slave进行任务的执行 因为dubbo压测需要依赖很多三方jar包,那么 ...

  2. ALSA lib编译

    http://blog.sina.com.cn/s/blog_7d7e9d0f0101lqlp.html alsa  lib: #!bin/sh rm -rf ./output/* mkdir -p ...

  3. ALSA lib基本概念

    1.channel 通道,即我们熟知的声道数.左/右声道,5.1channel等等 2.sample A sample is a single value that describes the amp ...

  4. ALSA lib调用实例

    1. Display Some PCM Types and Formats 2. Opening PCM Device and Setting Parameters /* This example o ...

  5. ALSA 学习小记

    对于playback snd_pcm_begin snd_pcm_commit, 貌似 commit给的frame才会使得alsa去把数据填充 转自 http://magodo.github.io/ ...

  6. Jmeter plugin jp@gc - PerfMon Metrics Collector

    Jmeter由于是开源工具,所以目前有很多插件可以供使用,最简单的方法是先把Plugin Manager安装了 下载地址:https://jmeter-plugins.org/wiki/Plugins ...

  7. 转:JMeter监控内存及CPU ——plugin插件监控被测系统资源方法

    JMeter监控内存及CPU ——plugin插件监控被测系统资源方法 jmeter中也可以监控服务器的CPU和内存使用情况,但是需要安装一些插件还需要在被监测服务器上开启服务. 1.需要的插件准备 ...

  8. Spring整合JUnit4进行AOP单元测试的时候,报:"C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\lib\idea_rt.jar=64

    错误代码 "C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" -ea -Didea.test.cyclic.buffer.size= ...

  9. Jmeter-12-如何使用Plugin Manager

    1. 搜索 Jmeter plugin 并找到plugin manager 下载jar文件 2. 放到jmeter/lib/ext下面, 重启jmeter 3. 找到选项-> Plugin ma ...

随机推荐

  1. CDQ分治笔记+例题

    CDQ分治是一种离线分治算法,它基于时间顺序对操作序列进行分治. 看这样一个问题: 在一个三维坐标系中,有若干个点,每个点都有对应的坐标 \((X_i , Y_i , Z_i)\) ,我们要对于每个点 ...

  2. SpringBoot整合NoSql--(三)Redis集群

    (1)集群原理 在Redis集群中,所有的Redis节点彼此互联,节点内部使用二进制协议优化传输速度和带宽. 当一个节点挂掉后,集群中超过半数的节点检测失效时才认为该节点已失效.不同于Tomcat集群 ...

  3. windows本地安装Oracle数据库

    一.下载Oracle 11g R2 for Windows. 官方网站: https://www.oracle.com/database/technologies/oracle-database-so ...

  4. kaks calculator批量计算多个基因的选择压力kaks值

    欢迎来到"bio生物信息"的世界 今天给大家带来"批量计算kaks值"的技能. 关于kaks的背景知识我就不介绍了,感兴趣的自行搜索,这里直接开始讲怎么批量计算 ...

  5. USB-Blaster CPLD FPGA Intel 驱动安装不上的问题,文件的哈希值不在指定的目录文件中,的解决办法,其实很简单

    intel的官网的驱动安装文档: https://www.intel.com/content/www/us/en/programmable/support/support-resources/down ...

  6. java new一个对象的过程中发生了什么?

    java在new一个对象的时候,会先查看对象所属的类有没有被加载到内存,如果没有的话,就会先通过类的全限定名来加载.加载并初始化类完成后,再进行对象的创建工作. 我们先假设是第一次使用该类,这样的话n ...

  7. 自定义配置JNOJ

    OJ 全名 online judge 在线判题系统,对于从事编程竞赛的人来说一点都不陌生,今天我们讨论的是怎么样自定义搭建 推荐的开源的OJ有hustOJ,JNOJ 因为hustOJ 是一键安装脚本, ...

  8. 剑指offer-面试题36-二叉搜索树与双向链表-中序遍历

    /* 题目: 将二叉搜索树转化为排序的双向链表,不能创建新的节点, 只能调整节点的指向,返回双向链表的头节点. */ /* 思路: 递归. 二叉搜索树的中序遍历得到的序列是递增序列. 左子树left& ...

  9. Mac上的屏幕截图不起作用该如何修复?

    屏幕截图是Mac提供的内置功能,很少有它不起作用.但是由于某些意外的设置或硬件问题,Mac上的屏幕截图有时无法正常工作,这里提供的是Mac上的屏幕截图不起作用该如何修复? 1.在Mac上启用屏幕快照快 ...

  10. js阻止事件冒泡(phpcms,浮窗第一次10秒弹出后每30秒弹出,动态更换日期)

    /* v9_date_list 日期表 tiptime 考试日期(数据类型为日期) 如果要实现浮窗淡入淡出用jquery的("#main0").fadeIn(3500);淡出(&q ...