resample: add peaks resampler

This commit is contained in:
Wim Taymans 2018-12-05 15:58:55 +01:00
parent e7462c470e
commit 5fa7e0f3eb
4 changed files with 236 additions and 27 deletions

View file

@ -0,0 +1,101 @@
/* Spa
*
* Copyright © 2018 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <math.h>
struct peaks_data {
unsigned o_count;
unsigned i_count;
float max_f[0];
};
static void impl_peaks_free(struct resample *r)
{
if (r->data)
free(r->data);
r->data = NULL;
}
static void impl_peaks_update_rate(struct resample *r, double rate)
{
}
static void impl_peaks_process(struct resample *r, int channel,
void *src, uint32_t *in_len, void *dst, uint32_t *out_len)
{
struct peaks_data *pd = r->data;
float *s = src, *d = dst;
int i, o, end;
i = ((uint64_t) pd->o_count * r->i_rate) / r->o_rate;
i = i > pd->i_count ? i - pd->i_count : 0;
o = 0;
while (i < *in_len && o < *out_len) {
end = ((uint64_t) (pd->o_count + 1) * r->i_rate) / r->o_rate;
end = end > pd->i_count ? end - pd->i_count : 0;
for (; i < end && i < *in_len; i++) {
float n = fabsf(s[i]);
if (n > pd->max_f[channel])
pd->max_f[channel] = n;
}
if (i == end) {
d[o++] = pd->max_f[channel];
pd->max_f[channel] = 0.0f;
pd->o_count++;
}
}
*out_len = o;
*in_len = i;
pd->i_count += i;
while (pd->i_count >= r->i_rate) {
pd->i_count -= r->i_rate;
pd->o_count -= r->o_rate;
}
}
static void impl_peaks_reset (struct resample *r)
{
struct peaks_data *d = r->data;
d->i_count = d->o_count = 0;
}
static int impl_peaks_init(struct resample *r)
{
struct peaks_data *d;
r->free = impl_peaks_free;
r->update_rate = impl_peaks_update_rate;
r->process = impl_peaks_process;
r->reset = impl_peaks_reset;
d = r->data = calloc(1, sizeof(struct peaks_data) * sizeof(float) * r->channels);
if (r->data == NULL)
return -ENOMEM;
d->i_count = d->o_count = 0;
return 0;
}

View file

@ -0,0 +1,66 @@
/* Spa
*
* Copyright © 2018 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <speex/speex_resampler.h>
static void impl_speex_free(struct resample *r)
{
if (r->data)
speex_resampler_destroy(r->data);
r->data = NULL;
}
static void impl_speex_update_rate(struct resample *r, double rate)
{
speex_resampler_set_rate_frac(r->data,
r->i_rate * rate, r->o_rate, r->i_rate, r->o_rate);
}
static void impl_speex_process(struct resample *r, int channel,
void *src, uint32_t *in_len, void *dst, uint32_t *out_len)
{
speex_resampler_process_float(r->data, channel, src, in_len, dst, out_len);
}
static void impl_speex_reset (struct resample *r)
{
}
static int impl_speex_init(struct resample *r)
{
int err;
r->free = impl_speex_free;
r->update_rate = impl_speex_update_rate;
r->process = impl_speex_process;
r->reset = impl_speex_reset;
r->data = speex_resampler_init_frac(r->channels,
r->i_rate, r->o_rate, r->i_rate, r->o_rate,
SPEEX_RESAMPLER_QUALITY_DEFAULT, &err);
if (r->data == NULL) {
return -ENOMEM;
}
return 0;
}

View file

@ -26,8 +26,6 @@
#include <string.h>
#include <stdio.h>
#include <speex/speex_resampler.h>
#include <spa/support/log.h>
#include <spa/utils/list.h>
#include <spa/node/node.h>
@ -37,6 +35,11 @@
#include <spa/pod/filter.h>
#include <spa/debug/types.h>
#include "resample.h"
#include "resample-speex.h"
#include "resample-peaks.h"
#define NAME "resample"
#define DEFAULT_RATE 44100
@ -99,8 +102,9 @@ struct impl {
struct port out_port;
bool started;
bool monitor;
SpeexResamplerState *state;
struct resample resample;
};
#define CHECK_PORT(this,d,id) (id == 0)
@ -134,20 +138,18 @@ static int setup_convert(struct impl *this,
if (src_info->info.raw.channels != dst_info->info.raw.channels)
return -EINVAL;
if (this->state)
speex_resampler_destroy(this->state);
resample_free(&this->resample);
this->state = speex_resampler_init_frac(src_info->info.raw.channels,
src_info->info.raw.rate,
dst_info->info.raw.rate,
src_info->info.raw.rate,
dst_info->info.raw.rate,
SPEEX_RESAMPLER_QUALITY_DEFAULT,
&err);
if (this->state == NULL)
return -ENOMEM;
this->resample.channels = src_info->info.raw.channels;
this->resample.i_rate = src_info->info.raw.rate;
this->resample.o_rate = dst_info->info.raw.rate;
return 0;
if (this->monitor)
err = impl_peaks_init(&this->resample);
else
err = impl_speex_init(&this->resample);
return err;
}
static int impl_node_enum_params(struct spa_node *node,
@ -168,15 +170,9 @@ static int apply_props(struct impl *this, const struct spa_pod *param)
SPA_POD_OBJECT_FOREACH(obj, prop) {
switch (prop->key) {
case SPA_PROP_rate:
{
spx_uint32_t in_rate, out_rate;
spa_pod_get_double(&prop->value, &p->rate);
speex_resampler_get_rate(this->state, &in_rate, &out_rate);
speex_resampler_set_rate_frac(this->state,
in_rate * p->rate, out_rate, in_rate, out_rate);
resample_update_rate(&this->resample, p->rate);
break;
}
default:
break;
}
@ -812,12 +808,14 @@ static int impl_node_process(struct spa_node *node)
pout_len = out_len = (maxsize - outport->offset) / sizeof(float);
for (i = 0; i < sb->n_datas; i++) {
void *src, *dst;
in_len = pin_len;
out_len = pout_len;
src = SPA_MEMBER(sb->datas[i].data, inport->offset, void);
dst = SPA_MEMBER(db->datas[i].data, outport->offset, void);
speex_resampler_process_float(this->state, i,
SPA_MEMBER(sb->datas[i].data, inport->offset, void), &in_len,
SPA_MEMBER(db->datas[i].data, outport->offset, void), &out_len);
resample_process(&this->resample, i, src, &in_len, dst, &out_len);
spa_log_trace(this->log, NAME " %p: in %d/%ld %d out %d/%ld %d",
this, in_len, size / sizeof(float), inport->offset,
@ -893,8 +891,7 @@ static int impl_clear(struct spa_handle *handle)
this = (struct impl *) handle;
if (this->state)
speex_resampler_destroy(this->state);
resample_free(&this->resample);
return 0;
}
@ -915,6 +912,7 @@ impl_init(const struct spa_handle_factory *factory,
struct impl *this;
struct port *port;
uint32_t i;
const char *str;
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(handle != NULL, -EINVAL);
@ -929,6 +927,9 @@ impl_init(const struct spa_handle_factory *factory,
this->log = support[i].data;
}
if (info != NULL && (str = spa_dict_lookup(info, "resample.peaks")) != NULL)
this->monitor = atoi(str);
this->node = impl_node;
port = GET_OUT_PORT(this, 0);

View file

@ -0,0 +1,41 @@
/* Spa
*
* Copyright © 2018 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
struct resample {
uint32_t channels;
uint32_t i_rate;
uint32_t o_rate;
void (*free) (struct resample *r);
void (*update_rate) (struct resample *r, double rate);
void (*process) (struct resample *r, int channel,
void *src, uint32_t *in_len, void *dst, uint32_t *out_len);
void (*reset) (struct resample *r);
void *data;
};
#define resample_free(r) (r)->free(r)
#define resample_update_rate(r,...) (r)->update_rate(r,__VA_ARGS__)
#define resample_process(r,...) (r)->process(r,__VA_ARGS__)
#define resample_reset(r) (r)->reset(r)