13 years C/C++/C# programing, focus on embedded and mobile device development.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

一个非常简单的、调节音量的filter,非线性调节:

#include <obs-module.h>
#include <math.h>

struct volume_data {
    obs_source_t *context;
    size_t channels;
    float factor;
};

static const char *volume_name(void *unused)
{
    UNUSED_PARAMETER(unused);
    return obs_module_text("Volume");
}

static void volume_destroy(void *data)
{
    struct volume_data *gf = data;
    bfree(gf);
}

static void volume_update(void *data, obs_data_t *s)
{
    struct volume_data *gf = data;
    double val = obs_data_get_double(s, "volume");
    gf->channels = audio_output_get_channels(obs_get_audio());
    gf->factor = (float)val;
}

static void *volume_create(obs_data_t *settings, obs_source_t *filter)
{
    struct volume_data *gf = bzalloc(sizeof(*gf));
    gf->context = filter;
    volume_update(gf, settings);
    return gf;
}

static struct obs_audio_data *volume_filter_audio(void *data, struct obs_audio_data *audio)
{
    struct volume_data *gf = data;
    const size_t channels = gf->channels;
    float **adata = (float**)audio->data;
    const float factor = gf->factor;

    for (size_t c = 0; c < channels; c++) {
        if (audio->data[c]) {
            for (size_t i = 0; i < audio->frames; i++) {
                adata[c][i] *= factor;
            }
        }
    }

    return audio;
}

static void volume_defaults(obs_data_t *s)
{
    obs_data_set_default_double(s, "volume", 1.0f);
}

static obs_properties_t *volume_properties(void *data)
{
    obs_properties_t *ppts = obs_properties_create();

    obs_properties_add_float_slider(ppts, "volume", "Volume", 0, 1.0, 0.1);

    UNUSED_PARAMETER(data);
    return ppts;
}

struct obs_source_info volume_filter = {
    .id = "volume_filter",
    .type = OBS_SOURCE_TYPE_FILTER,
    .output_flags = OBS_SOURCE_AUDIO,
    .get_name = volume_name,
    .create = volume_create,
    .destroy = volume_destroy,
    .update = volume_update,
    .filter_audio = volume_filter_audio,
    .get_defaults = volume_defaults,
    .get_properties = volume_properties,
};

posted on 2017-12-26 11:27  woaiusd  阅读(2703)  评论(0编辑  收藏  举报