* add µlaw/alaw support

* abstracted resampler API
* add integer-only resampler ("trivial")
* show used resampler wherever useful
* add mixing/volume adjusting for float32ne and u8


git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@294 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2004-11-20 16:23:53 +00:00
parent 8641af3c6d
commit 5f647c8fef
28 changed files with 3237 additions and 192 deletions

View file

@ -8,8 +8,6 @@
- make most buffer sizes dependant on the sample type
- limit all resources
- commenting
- non-fp mixing
- non-fp resampling
- module-tunnel: use latency interpolation
- polish for starting polypaudio as root/system-wide instance

View file

@ -210,7 +210,8 @@ polypaudio_SOURCES = idxset.c idxset.h \
conf-parser.h conf-parser.c \
caps.h caps.c \
props.h props.c \
mcalign.c mcalign.h
mcalign.c mcalign.h \
g711.c g711.h
polypaudio_CFLAGS = $(AM_CFLAGS) $(LIBSAMPLERATE_CFLAGS) $(LIBSNDFILE_CFLAGS)
polypaudio_INCLUDES = $(INCLTDL)

View file

@ -162,15 +162,21 @@ char *pa_source_output_list_to_string(struct pa_core *c) {
for (o = pa_idxset_first(c->source_outputs, &index); o; o = pa_idxset_next(c->source_outputs, &index)) {
char ss[PA_SAMPLE_SPEC_SNPRINT_MAX];
const char *rm;
pa_sample_spec_snprint(ss, sizeof(ss), &o->sample_spec);
assert(o->source);
if (!(rm = pa_resample_method_to_string(pa_source_output_get_resample_method(o))))
rm = "invalid";
pa_strbuf_printf(
s, " index: %u\n\tname: '%s'\n\tstate: %s\n\tsource: <%u> '%s'\n\tsample_spec: <%s>\n",
s, " index: %u\n\tname: '%s'\n\tstate: %s\n\tsource: <%u> '%s'\n\tsample_spec: <%s>\n\tresample method: %s\n",
o->index,
o->name,
state_table[o->state],
o->source->index, o->source->name,
ss);
ss,
rm);
if (o->owner)
pa_strbuf_printf(s, "\towner module: <%u>\n", o->owner->index);
if (o->client)
@ -198,10 +204,15 @@ char *pa_sink_input_list_to_string(struct pa_core *c) {
for (i = pa_idxset_first(c->sink_inputs, &index); i; i = pa_idxset_next(c->sink_inputs, &index)) {
char ss[PA_SAMPLE_SPEC_SNPRINT_MAX];
const char *rm;
if (!(rm = pa_resample_method_to_string(pa_sink_input_get_resample_method(i))))
rm = "invalid";
pa_sample_spec_snprint(ss, sizeof(ss), &i->sample_spec);
assert(i->sink);
pa_strbuf_printf(
s, " index: %u\n\tname: <%s>\n\tstate: %s\n\tsink: <%u> '%s'\n\tvolume: <0x%04x> (%0.2fdB)\n\tlatency: <%0.0f usec>\n\tsample_spec: <%s>\n",
s, " index: %u\n\tname: <%s>\n\tstate: %s\n\tsink: <%u> '%s'\n\tvolume: <0x%04x> (%0.2fdB)\n\tlatency: <%0.0f usec>\n\tsample_spec: <%s>\n\tresample method: %s\n",
i->index,
i->name,
state_table[i->state],
@ -209,7 +220,8 @@ char *pa_sink_input_list_to_string(struct pa_core *c) {
(unsigned) i->volume,
pa_volume_to_dB(i->volume),
(float) pa_sink_input_get_latency(i),
ss);
ss,
rm);
if (i->owner)
pa_strbuf_printf(s, "\towner module: <%u>\n", i->owner->index);

View file

@ -27,7 +27,6 @@
#include <assert.h>
#include <stdio.h>
#include <signal.h>
#include <samplerate.h>
#include "core.h"
#include "module.h"
@ -82,7 +81,7 @@ struct pa_core* pa_core_new(struct pa_mainloop_api *m) {
c->module_idle_time = 20;
c->scache_idle_time = 20;
c->resample_method = SRC_SINC_FASTEST;
c->resample_method = PA_RESAMPLER_SRC_SINC_FASTEST;
pa_property_init(c);

View file

@ -27,6 +27,7 @@
#include "mainloop-api.h"
#include "sample.h"
#include "memblock.h"
#include "resampler.h"
struct pa_core {
struct pa_mainloop_api *mainloop;
@ -54,7 +55,7 @@ struct pa_core {
struct pa_time_event *scache_auto_unload_event;
int resample_method;
enum pa_resample_method resample_method;
};
struct pa_core* pa_core_new(struct pa_mainloop_api *m);

View file

@ -28,13 +28,13 @@
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <samplerate.h>
#include "daemon-conf.h"
#include "util.h"
#include "xmalloc.h"
#include "strbuf.h"
#include "conf-parser.h"
#include "resampler.h"
#ifndef DEFAULT_CONFIG_DIR
#define DEFAULT_CONFIG_DIR "/etc/polypaudio"
@ -64,7 +64,7 @@ static const struct pa_daemon_conf default_conf = {
.dl_search_path = NULL,
.default_script_file = NULL,
.log_target = PA_LOG_SYSLOG,
.resample_method = SRC_SINC_FASTEST,
.resample_method = PA_RESAMPLER_SRC_SINC_FASTEST,
.config_file = NULL,
};
@ -214,14 +214,6 @@ int pa_daemon_conf_env(struct pa_daemon_conf *c) {
char *pa_daemon_conf_dump(struct pa_daemon_conf *c) {
struct pa_strbuf *s = pa_strbuf_new();
static const char const* resample_methods[] = {
"sinc-best-quality",
"sinc-medium-quality",
"sinc-fastest",
"zero-order-hold",
"linear"
};
if (c->config_file)
pa_strbuf_printf(s, "### Read from configuration file: %s ###\n", c->config_file);
@ -238,7 +230,7 @@ char *pa_daemon_conf_dump(struct pa_daemon_conf *c) {
pa_strbuf_printf(s, "log-target = %s\n", c->auto_log_target ? "auto" : (c->log_target == PA_LOG_SYSLOG ? "syslog" : "stderr"));
assert(c->resample_method <= 4 && c->resample_method >= 0);
pa_strbuf_printf(s, "resample-method = %s\n", resample_methods[c->resample_method]);
pa_strbuf_printf(s, "resample-method = %s\n", pa_resample_method_to_string(c->resample_method));
return pa_strbuf_tostring_free(s);
}

2531
polyp/g711.c Normal file

File diff suppressed because it is too large Load diff

40
polyp/g711.h Normal file
View file

@ -0,0 +1,40 @@
#ifndef foog711hfoo
#define foog711hfoo
/* g711.h - include for G711 u-law and a-law conversion routines
**
** Copyright (C) 2001 Chris Bagwell
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*/
/** Copied from sox -- Lennart Poettring*/
#include <inttypes.h>
#ifdef FAST_ALAW_CONVERSION
extern uint8_t _st_13linear2alaw[0x2000];
extern int16_t _st_alaw2linear16[256];
#define st_13linear2alaw(sw) (_st_13linear2alaw[(sw + 0x1000)])
#define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
#else
unsigned char st_13linear2alaw(int16_t pcm_val);
int16_t st_alaw2linear16(unsigned char);
#endif
#ifdef FAST_ULAW_CONVERSION
extern uint8_t _st_14linear2ulaw[0x4000];
extern int16_t _st_ulaw2linear16[256];
#define st_14linear2ulaw(sw) (_st_14linear2ulaw[(sw + 0x2000)])
#define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
#else
unsigned char st_14linear2ulaw(int16_t pcm_val);
int16_t st_ulaw2linear16(unsigned char);
#endif
#endif

View file

@ -324,7 +324,8 @@ static void get_sink_input_info_callback(struct pa_context *c, const struct pa_s
"Sample Specification: %s\n"
"Volume: 0x%03x (%0.2f dB)\n"
"Buffer Latency: %0.0f usec\n"
"Sink Latency: %0.0f usec\n",
"Sink Latency: %0.0f usec\n"
"Resample method: %s\n",
i->index,
i->name,
i->owner_module != PA_INVALID_INDEX ? t : "n/a",
@ -333,7 +334,8 @@ static void get_sink_input_info_callback(struct pa_context *c, const struct pa_s
s,
i->volume, pa_volume_to_dB(i->volume),
(double) i->buffer_usec,
(double) i->sink_usec);
(double) i->sink_usec,
i->resample_method ? i->resample_method : "n/a");
}
static void get_source_output_info_callback(struct pa_context *c, const struct pa_source_output_info *i, int is_last, void *userdata) {
@ -367,7 +369,8 @@ static void get_source_output_info_callback(struct pa_context *c, const struct p
"Source: %u\n"
"Sample Specification: %s\n"
"Buffer Latency: %0.0f usec\n"
"Source Latency: %0.0f usec\n",
"Source Latency: %0.0f usec\n"
"Resample method: %s\n",
i->index,
i->name,
i->owner_module != PA_INVALID_INDEX ? t : "n/a",
@ -375,7 +378,8 @@ static void get_source_output_info_callback(struct pa_context *c, const struct p
i->source,
s,
(double) i->buffer_usec,
(double) i->source_usec);
(double) i->source_usec,
i->resample_method ? i->resample_method : "n/a");
}
static void get_sample_info_callback(struct pa_context *c, const struct pa_sample_info *i, int is_last, void *userdata) {

View file

@ -449,7 +449,8 @@ static void context_get_sink_input_info_callback(struct pa_pdispatch *pd, uint32
pa_tagstruct_get_sample_spec(t, &i.sample_spec) < 0 ||
pa_tagstruct_getu32(t, &i.volume) < 0 ||
pa_tagstruct_get_usec(t, &i.buffer_usec) < 0 ||
pa_tagstruct_get_usec(t, &i.sink_usec) < 0) {
pa_tagstruct_get_usec(t, &i.sink_usec) < 0 ||
pa_tagstruct_gets(t, &i.resample_method) < 0) {
pa_context_fail(o->context, PA_ERROR_PROTOCOL);
goto finish;
}
@ -519,7 +520,8 @@ static void context_get_source_output_info_callback(struct pa_pdispatch *pd, uin
pa_tagstruct_getu32(t, &i.source) < 0 ||
pa_tagstruct_get_sample_spec(t, &i.sample_spec) < 0 ||
pa_tagstruct_get_usec(t, &i.buffer_usec) < 0 ||
pa_tagstruct_get_usec(t, &i.source_usec) < 0) {
pa_tagstruct_get_usec(t, &i.source_usec) < 0 ||
pa_tagstruct_gets(t, &i.resample_method) < 0) {
pa_context_fail(o->context, PA_ERROR_PROTOCOL);
goto finish;
}

View file

@ -143,6 +143,7 @@ struct pa_sink_input_info {
pa_volume_t volume; /**< The volume of this sink input */
pa_usec_t buffer_usec; /**< Latency due to buffering in sink input, see pa_latency_info for details */
pa_usec_t sink_usec; /**< Latency of the sink device, see pa_latency_info for details */
const char *resample_method; /**< Thre resampling method used by this sink input. \since 0.7 */
};
/** Get some information about a sink input by its index */
@ -161,6 +162,7 @@ struct pa_source_output_info {
struct pa_sample_spec sample_spec; /**< The sample specification of the source output */
pa_usec_t buffer_usec; /**< Latency due to buffering in the source output, see pa_latency_info for details. \since 0.5 */
pa_usec_t source_usec; /**< Latency of the source device, see pa_latency_info for details. \since 0.5 */
const char *resample_method; /**< Thre resampling method used by this source output. \since 0.7 */
};
/** Get information about a source output by its index */

View file

@ -1160,6 +1160,7 @@ static void sink_input_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink_inp
pa_tagstruct_putu32(t, s->volume);
pa_tagstruct_put_usec(t, pa_sink_input_get_latency(s));
pa_tagstruct_put_usec(t, pa_sink_get_latency(s->sink));
pa_tagstruct_puts(t, pa_resample_method_to_string(pa_sink_input_get_resample_method(s)));
}
static void source_output_fill_tagstruct(struct pa_tagstruct *t, struct pa_source_output *s) {
@ -1172,6 +1173,7 @@ static void source_output_fill_tagstruct(struct pa_tagstruct *t, struct pa_sourc
pa_tagstruct_put_sample_spec(t, &s->sample_spec);
pa_tagstruct_put_usec(t, pa_source_output_get_latency(s));
pa_tagstruct_put_usec(t, pa_source_get_latency(s->source));
pa_tagstruct_puts(t, pa_resample_method_to_string(pa_source_output_get_resample_method(s)));
}
static void scache_fill_tagstruct(struct pa_tagstruct *t, struct pa_scache_entry *e) {

View file

@ -32,7 +32,8 @@ void pa_pstream_send_tagstruct(struct pa_pstream *p, struct pa_tagstruct *t) {
size_t length;
uint8_t *data;
struct pa_packet *packet;
assert(p && t);
assert(p);
assert(t);
data = pa_tagstruct_free_data(t, &length);
assert(data && length);

View file

@ -24,6 +24,7 @@
#endif
#include <assert.h>
#include <string.h>
#include <samplerate.h>
@ -34,55 +35,74 @@
struct pa_resampler {
struct pa_sample_spec i_ss, o_ss;
float* i_buf, *o_buf;
unsigned i_alloc, o_alloc;
size_t i_sz, o_sz;
int channels;
pa_convert_to_float32_func_t to_float32_func;
pa_convert_from_float32_func_t from_float32_func;
SRC_STATE *src_state;
size_t i_fz, o_fz;
struct pa_memblock_stat *memblock_stat;
void *impl_data;
int channels;
enum pa_resample_method resample_method;
void (*impl_free)(struct pa_resampler *r);
void (*impl_set_input_rate)(struct pa_resampler *r, uint32_t rate);
void (*impl_run)(struct pa_resampler *r, const struct pa_memchunk *in, struct pa_memchunk *out);
};
struct pa_resampler* pa_resampler_new(const struct pa_sample_spec *a, const struct pa_sample_spec *b, struct pa_memblock_stat *s, int resample_method) {
struct impl_libsamplerate {
float* i_buf, *o_buf;
unsigned i_alloc, o_alloc;
pa_convert_to_float32ne_func_t to_float32ne_func;
pa_convert_from_float32ne_func_t from_float32ne_func;
SRC_STATE *src_state;
};
struct impl_trivial {
unsigned o_counter;
unsigned i_counter;
};
static int libsamplerate_init(struct pa_resampler*r);
static int trivial_init(struct pa_resampler*r);
struct pa_resampler* pa_resampler_new(const struct pa_sample_spec *a, const struct pa_sample_spec *b, struct pa_memblock_stat *s, enum pa_resample_method resample_method) {
struct pa_resampler *r = NULL;
int err;
assert(a && b && pa_sample_spec_valid(a) && pa_sample_spec_valid(b));
assert(a && b && pa_sample_spec_valid(a) && pa_sample_spec_valid(b) && resample_method != PA_RESAMPLER_INVALID);
if (a->channels != b->channels && a->channels != 1 && b->channels != 1)
goto fail;
if (a->format == PA_SAMPLE_ALAW || a->format == PA_SAMPLE_ULAW || b->format == PA_SAMPLE_ALAW || b->format == PA_SAMPLE_ULAW)
goto fail;
r = pa_xmalloc(sizeof(struct pa_resampler));
r->impl_data = NULL;
r->memblock_stat = s;
r->resample_method = resample_method;
r->impl_free = NULL;
r->impl_set_input_rate = NULL;
r->impl_run = NULL;
/* Fill sample specs */
r->i_ss = *a;
r->o_ss = *b;
r->i_fz = pa_frame_size(a);
r->o_fz = pa_frame_size(b);
r->channels = a->channels;
if (b->channels < r->channels)
r->channels = b->channels;
r->i_buf = r->o_buf = NULL;
r->i_alloc = r->o_alloc = 0;
/* Choose implementation */
if (a->channels != b->channels || a->format != b->format || resample_method != PA_RESAMPLER_TRIVIAL) {
/* Use the libsamplerate based resampler for the complicated cases */
if (resample_method == PA_RESAMPLER_TRIVIAL)
r->resample_method = PA_RESAMPLER_SRC_ZERO_ORDER_HOLD;
r->src_state = src_new(resample_method, r->channels, &err);
if (err != 0 || !r->src_state)
if (libsamplerate_init(r) < 0)
goto fail;
r->i_ss = *a;
r->o_ss = *b;
r->i_sz = pa_frame_size(a);
r->o_sz = pa_frame_size(b);
r->to_float32_func = pa_get_convert_to_float32_function(a->format);
r->from_float32_func = pa_get_convert_from_float32_function(b->format);
assert(r->to_float32_func && r->from_float32_func);
r->memblock_stat = s;
} else {
/* Use our own simple non-fp resampler for the trivial cases and when the user selects it */
if (trivial_init(r) < 0)
goto fail;
}
return r;
@ -95,32 +115,86 @@ fail:
void pa_resampler_free(struct pa_resampler *r) {
assert(r);
if (r->src_state)
src_delete(r->src_state);
pa_xfree(r->i_buf);
pa_xfree(r->o_buf);
if (r->impl_free)
r->impl_free(r);
pa_xfree(r);
}
size_t pa_resampler_request(struct pa_resampler *r, size_t out_length) {
assert(r && (out_length % r->o_sz) == 0);
void pa_resampler_set_input_rate(struct pa_resampler *r, uint32_t rate) {
assert(r && rate);
return (((out_length / r->o_sz)*r->i_ss.rate)/r->o_ss.rate) * r->i_sz;
r->i_ss.rate = rate;
if (r->impl_set_input_rate)
r->impl_set_input_rate(r, rate);
}
void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, struct pa_memchunk *out) {
assert(r && in && out && r->impl_run);
r->impl_run(r, in, out);
}
size_t pa_resampler_request(struct pa_resampler *r, size_t out_length) {
assert(r && (out_length % r->o_fz) == 0);
return (((out_length / r->o_fz)*r->i_ss.rate)/r->o_ss.rate) * r->i_fz;
}
enum pa_resample_method pa_resampler_get_method(struct pa_resampler *r) {
assert(r);
return r->resample_method;
}
/* Parse a libsamplrate compatible resampling implementation */
enum pa_resample_method pa_parse_resample_method(const char *string) {
assert(string);
if (!strcmp(string, "src-sinc-best-quality"))
return PA_RESAMPLER_SRC_SINC_BEST_QUALITY;
else if (!strcmp(string, "src-sinc-medium-quality"))
return PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY;
else if (!strcmp(string, "src-sinc-fastest"))
return PA_RESAMPLER_SRC_SINC_FASTEST;
else if (!strcmp(string, "src-zero-order-hold"))
return PA_RESAMPLER_SRC_ZERO_ORDER_HOLD;
else if (!strcmp(string, "src-linear"))
return PA_RESAMPLER_SRC_LINEAR;
else if (!strcmp(string, "trivial"))
return PA_RESAMPLER_TRIVIAL;
else
return PA_RESAMPLER_INVALID;
}
/*** libsamplerate based implementation ***/
static void libsamplerate_free(struct pa_resampler *r) {
struct impl_libsamplerate *i;
assert(r && r->impl_data);
i = r->impl_data;
if (i->src_state)
src_delete(i->src_state);
pa_xfree(i->i_buf);
pa_xfree(i->o_buf);
pa_xfree(i);
}
static void libsamplerate_run(struct pa_resampler *r, const struct pa_memchunk *in, struct pa_memchunk *out) {
unsigned i_nchannels, o_nchannels, ins, ons, eff_ins, eff_ons;
float *cbuf;
assert(r && in && out && in->length && in->memblock && (in->length % r->i_sz) == 0);
struct impl_libsamplerate *i;
assert(r && in && out && in->length && in->memblock && (in->length % r->i_fz) == 0 && r->impl_data);
i = r->impl_data;
/* How many input samples? */
ins = in->length/r->i_sz;
ins = in->length/r->i_fz;
/* pa_log("%u / %u = %u\n", in->length, r->i_sz, ins); */
/* pa_log("%u / %u = %u\n", in->length, r->i_fz, ins); */
/* How much space for output samples? */
if (r->src_state)
if (i->src_state)
ons = (ins*r->o_ss.rate/r->i_ss.rate)+1024;
else
ons = ins;
@ -140,40 +214,40 @@ void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, stru
/* pa_log("eff_ins = %u \n", eff_ins); */
out->memblock = pa_memblock_new(out->length = (ons*r->o_sz), r->memblock_stat);
out->memblock = pa_memblock_new(out->length = (ons*r->o_fz), r->memblock_stat);
out->index = 0;
assert(out->memblock);
if (r->i_alloc < eff_ins)
r->i_buf = pa_xrealloc(r->i_buf, sizeof(float) * (r->i_alloc = eff_ins));
assert(r->i_buf);
if (i->i_alloc < eff_ins)
i->i_buf = pa_xrealloc(i->i_buf, sizeof(float) * (i->i_alloc = eff_ins));
assert(i->i_buf);
/* pa_log("eff_ins = %u \n", eff_ins); */
r->to_float32_func(eff_ins, (uint8_t*) in->memblock->data+in->index, i_nchannels, r->i_buf);
i->to_float32ne_func(eff_ins, (uint8_t*) in->memblock->data+in->index, i_nchannels, i->i_buf);
if (r->src_state) {
if (i->src_state) {
int ret;
SRC_DATA data;
if (r->o_alloc < eff_ons)
r->o_buf = pa_xrealloc(r->o_buf, sizeof(float) * (r->o_alloc = eff_ons));
assert(r->o_buf);
if (i->o_alloc < eff_ons)
i->o_buf = pa_xrealloc(i->o_buf, sizeof(float) * (i->o_alloc = eff_ons));
assert(i->o_buf);
data.data_in = r->i_buf;
data.data_in = i->i_buf;
data.input_frames = ins;
data.data_out = r->o_buf;
data.data_out = i->o_buf;
data.output_frames = ons;
data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
data.end_of_input = 0;
ret = src_process(r->src_state, &data);
ret = src_process(i->src_state, &data);
assert(ret == 0);
assert((unsigned) data.input_frames_used == ins);
cbuf = r->o_buf;
cbuf = i->o_buf;
ons = data.output_frames_gen;
if (r->i_ss.channels == r->o_ss.channels)
@ -181,12 +255,11 @@ void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, stru
else
eff_ons = ons;
} else
cbuf = r->i_buf;
cbuf = i->i_buf;
if (eff_ons)
r->from_float32_func(eff_ons, cbuf, (uint8_t*)out->memblock->data+out->index, o_nchannels);
out->length = ons*r->o_sz;
i->from_float32ne_func(eff_ons, cbuf, (uint8_t*)out->memblock->data+out->index, o_nchannels);
out->length = ons*r->o_fz;
if (!out->length) {
pa_memblock_unref(out->memblock);
@ -194,11 +267,147 @@ void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, stru
}
}
void pa_resampler_set_input_rate(struct pa_resampler *r, uint32_t rate) {
static void libsamplerate_set_input_rate(struct pa_resampler *r, uint32_t rate) {
int ret;
assert(r);
struct impl_libsamplerate *i;
assert(r && rate > 0 && r->impl_data);
i = r->impl_data;
r->i_ss.rate = rate;
ret = src_set_ratio(r->src_state, (double) r->o_ss.rate / r->i_ss.rate);
ret = src_set_ratio(i->src_state, (double) r->o_ss.rate / r->i_ss.rate);
assert(ret == 0);
}
static int libsamplerate_init(struct pa_resampler *r) {
struct impl_libsamplerate *i = NULL;
int err;
r->impl_data = i = pa_xmalloc(sizeof(struct impl_libsamplerate));
i->to_float32ne_func = pa_get_convert_to_float32ne_function(r->i_ss.format);
i->from_float32ne_func = pa_get_convert_from_float32ne_function(r->o_ss.format);
if (!i->to_float32ne_func || !i->from_float32ne_func)
goto fail;
if (!(i->src_state = src_new(r->resample_method, r->channels, &err)) || !i->src_state)
goto fail;
i->i_buf = i->o_buf = NULL;
i->i_alloc = i->o_alloc = 0;
r->impl_free = libsamplerate_free;
r->impl_set_input_rate = libsamplerate_set_input_rate;
r->impl_run = libsamplerate_run;
return 0;
fail:
pa_xfree(i);
return -1;
}
/* Trivial implementation */
static void trivial_run(struct pa_resampler *r, const struct pa_memchunk *in, struct pa_memchunk *out) {
size_t fz;
unsigned nsamples;
struct impl_trivial *i;
assert(r && in && out && r->impl_data);
i = r->impl_data;
fz = r->i_fz;
assert(fz == r->o_fz);
nsamples = in->length/fz;
if (r->i_ss.rate == r->o_ss.rate) {
/* In case there's no diefference in sample types, do nothing */
*out = *in;
pa_memblock_ref(in->memblock);
i->o_counter += nsamples;
} else {
/* Do real resampling */
size_t l;
unsigned o_index;
/* The length of the new memory block rounded up */
l = ((nsamples * r->o_ss.rate + r->i_ss.rate - 1) / r->i_ss.rate) * fz;
out->index = 0;
out->memblock = pa_memblock_new(l, r->memblock_stat);
for (o_index = 0;; o_index++, i->o_counter++) {
unsigned j;
j = (i->o_counter * r->i_ss.rate / r->o_ss.rate);
assert(j >= i->i_counter);
j = j - i->i_counter;
if (j >= nsamples)
break;
assert(o_index*fz < out->memblock->length);
memcpy((uint8_t*) out->memblock->data + fz*o_index,
(uint8_t*) in->memblock->data + fz*j, fz);
}
out->length = o_index*fz;
}
/* Normalize the output counter */
while (i->o_counter >= r->o_ss.rate)
i->o_counter -= r->o_ss.rate;
i->i_counter += nsamples;
while (i->i_counter >= r->i_ss.rate)
i->i_counter -= r->i_ss.rate;
}
static void trivial_free(struct pa_resampler *r) {
assert(r);
pa_xfree(r->impl_data);
}
static void trivial_set_input_rate(struct pa_resampler *r, uint32_t rate) {
struct impl_trivial *i;
assert(r && rate > 0 && r->impl_data);
i = r->impl_data;
i->i_counter = 0;
i->o_counter = 0;
}
static int trivial_init(struct pa_resampler*r) {
struct impl_trivial *i;
assert(r && r->i_ss.format == r->o_ss.format && r->i_ss.channels == r->o_ss.channels);
r->impl_data = i = pa_xmalloc(sizeof(struct impl_trivial));
i->o_counter = i->i_counter = 0;
r->impl_run = trivial_run;
r->impl_free = trivial_free;
r->impl_set_input_rate = trivial_set_input_rate;
return 0;
}
const char *pa_resample_method_to_string(enum pa_resample_method m) {
static const char const* resample_methods[] = {
"src-sinc-best-quality",
"src-sinc-medium-quality",
"src-sinc-fastest",
"src-zero-order-hold",
"src-linear",
"trivial"
};
if (m < 0 || m >= PA_RESAMPLER_MAX)
return NULL;
return resample_methods[m];
}

View file

@ -22,18 +22,44 @@
USA.
***/
#include <samplerate.h>
#include "sample.h"
#include "memblock.h"
#include "memchunk.h"
struct pa_resampler;
enum pa_resample_method {
PA_RESAMPLER_INVALID = -1,
PA_RESAMPLER_SRC_SINC_BEST_QUALITY = SRC_SINC_BEST_QUALITY,
PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY = SRC_SINC_MEDIUM_QUALITY,
PA_RESAMPLER_SRC_SINC_FASTEST = SRC_SINC_FASTEST,
PA_RESAMPLER_SRC_ZERO_ORDER_HOLD = SRC_ZERO_ORDER_HOLD,
PA_RESAMPLER_SRC_LINEAR = SRC_LINEAR,
PA_RESAMPLER_TRIVIAL,
PA_RESAMPLER_MAX
};
struct pa_resampler* pa_resampler_new(const struct pa_sample_spec *a, const struct pa_sample_spec *b, struct pa_memblock_stat *s, int resample_method);
void pa_resampler_free(struct pa_resampler *r);
/* Returns the size of an input memory block which is required to return the specified amount of output data */
size_t pa_resampler_request(struct pa_resampler *r, size_t out_length);
/* Pass the specified memory chunk to the resampler and return the newly resampled data */
void pa_resampler_run(struct pa_resampler *r, const struct pa_memchunk *in, struct pa_memchunk *out);
/* Change the input rate of the resampler object */
void pa_resampler_set_input_rate(struct pa_resampler *r, uint32_t rate);
/* Return the resampling method of the resampler object */
enum pa_resample_method pa_resampler_get_method(struct pa_resampler *r);
/* Try to parse the resampler method */
enum pa_resample_method pa_parse_resample_method(const char *string);
/* return a human readable string for the specified resampling method. Inverse of pa_parse_resample_method() */
const char *pa_resample_method_to_string(enum pa_resample_method m);
#endif

View file

@ -26,6 +26,7 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include "sample-util.h"
@ -41,12 +42,12 @@ void pa_silence_memchunk(struct pa_memchunk *c, const struct pa_sample_spec *spe
}
void pa_silence_memory(void *p, size_t length, const struct pa_sample_spec *spec) {
char c = 0;
uint8_t c = 0;
assert(p && length && spec);
switch (spec->format) {
case PA_SAMPLE_U8:
c = 127;
c = 0x80;
break;
case PA_SAMPLE_S16LE:
case PA_SAMPLE_S16BE:
@ -65,11 +66,13 @@ void pa_silence_memory(void *p, size_t length, const struct pa_sample_spec *spec
}
size_t pa_mix(struct pa_mix_info channels[], unsigned nchannels, void *data, size_t length, const struct pa_sample_spec *spec, pa_volume_t volume) {
unsigned c, d;
assert(channels && data && length && spec);
assert(spec->format == PA_SAMPLE_S16NE);
if (spec->format == PA_SAMPLE_S16NE) {
size_t d;
for (d = 0;; d += sizeof(int16_t)) {
unsigned c;
int32_t sum = 0;
if (d >= length)
@ -77,18 +80,107 @@ size_t pa_mix(struct pa_mix_info channels[], unsigned nchannels, void *data, siz
for (c = 0; c < nchannels; c++) {
int32_t v;
uint32_t volume = channels[c].volume;
pa_volume_t cvolume = channels[c].volume;
if (d >= channels[c].chunk.length)
return d;
if (volume == PA_VOLUME_MUTED)
if (cvolume == PA_VOLUME_MUTED)
v = 0;
else {
v = *((int16_t*) ((uint8_t*) channels[c].chunk.memblock->data + channels[c].chunk.index + d));
if (volume != PA_VOLUME_NORM)
v = (int32_t) ((float)v*volume/PA_VOLUME_NORM);
if (cvolume != PA_VOLUME_NORM) {
v *= cvolume;
v /= PA_VOLUME_NORM;
}
}
sum += v;
}
if (volume == PA_VOLUME_MUTED)
sum = 0;
else if (volume != PA_VOLUME_NORM) {
sum *= volume;
sum /= PA_VOLUME_NORM;
}
if (sum < -0x8000) sum = -0x8000;
if (sum > 0x7FFF) sum = 0x7FFF;
*((int16_t*) data) = sum;
data = (uint8_t*) data + sizeof(int16_t);
}
} else if (spec->format == PA_SAMPLE_U8) {
size_t d;
for (d = 0;; d ++) {
int32_t sum = 0;
unsigned c;
if (d >= length)
return d;
for (c = 0; c < nchannels; c++) {
int32_t v;
pa_volume_t cvolume = channels[c].volume;
if (d >= channels[c].chunk.length)
return d;
if (cvolume == PA_VOLUME_MUTED)
v = 0;
else {
v = (int32_t) *((uint8_t*) channels[c].chunk.memblock->data + channels[c].chunk.index + d) - 0x80;
if (cvolume != PA_VOLUME_NORM) {
v *= cvolume;
v /= PA_VOLUME_NORM;
}
}
sum += v;
}
if (volume == PA_VOLUME_MUTED)
sum = 0;
else if (volume != PA_VOLUME_NORM) {
sum *= volume;
sum /= PA_VOLUME_NORM;
}
if (sum < -0x80) sum = -0x80;
if (sum > 0x7F) sum = 0x7F;
*((uint8_t*) data) = (uint8_t) (sum + 0x80);
data = (uint8_t*) data + 1;
}
} else if (spec->format == PA_SAMPLE_FLOAT32NE) {
size_t d;
for (d = 0;; d += sizeof(float)) {
float_t sum = 0;
unsigned c;
if (d >= length)
return d;
for (c = 0; c < nchannels; c++) {
float v;
pa_volume_t cvolume = channels[c].volume;
if (d >= channels[c].chunk.length)
return d;
if (cvolume == PA_VOLUME_MUTED)
v = 0;
else {
v = *((float*) ((uint8_t*) channels[c].chunk.memblock->data + channels[c].chunk.index + d));
if (cvolume != PA_VOLUME_NORM)
v = v*cvolume/PA_VOLUME_NORM;
}
sum += v;
@ -97,22 +189,22 @@ size_t pa_mix(struct pa_mix_info channels[], unsigned nchannels, void *data, siz
if (volume == PA_VOLUME_MUTED)
sum = 0;
else if (volume != PA_VOLUME_NORM)
sum = (int32_t) ((float) sum*volume/PA_VOLUME_NORM);
sum = sum*volume/PA_VOLUME_NORM;
if (sum < -0x8000) sum = -0x8000;
if (sum > 0x7FFF) sum = 0x7FFF;
if (sum < -1) sum = -1;
if (sum > 1) sum = 1;
*((int16_t*) data) = sum;
data = (uint8_t*) data + sizeof(int16_t);
*((float*) data) = sum;
data = (uint8_t*) data + sizeof(float);
}
} else {
abort();
}
}
void pa_volume_memchunk(struct pa_memchunk*c, const struct pa_sample_spec *spec, pa_volume_t volume) {
int16_t *d;
size_t n;
assert(c && spec && (c->length % pa_frame_size(spec) == 0));
assert(spec->format == PA_SAMPLE_S16NE);
if (volume == PA_VOLUME_NORM)
return;
@ -122,6 +214,10 @@ void pa_volume_memchunk(struct pa_memchunk*c, const struct pa_sample_spec *spec,
return;
}
if (spec->format == PA_SAMPLE_S16NE) {
int16_t *d;
size_t n;
for (d = (int16_t*) ((uint8_t*) c->memblock->data+c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
int32_t t = (int32_t)(*d);
@ -133,5 +229,40 @@ void pa_volume_memchunk(struct pa_memchunk*c, const struct pa_sample_spec *spec,
*d = (int16_t) t;
}
} else if (spec->format == PA_SAMPLE_U8) {
uint8_t *d;
size_t n;
for (d = (uint8_t*) c->memblock->data + c->index, n = c->length; n > 0; d++, n--) {
int32_t t = (int32_t) *d - 0x80;
t *= volume;
t /= PA_VOLUME_NORM;
if (t < -0x80) t = -0x80;
if (t > 0x7F) t = 0x7F;
*d = (uint8_t) (t + 0x80);
}
} else if (spec->format == PA_SAMPLE_FLOAT32NE) {
float *d;
size_t n;
for (d = (float*) ((uint8_t*) c->memblock->data+c->index), n = c->length/sizeof(float); n > 0; d++, n--) {
float t = *d;
t *= volume;
t /= PA_VOLUME_NORM;
if (t < -1) t = -1;
if (t > 1) t = 1;
*d = t;
}
} else {
abort();
}
}

View file

@ -28,7 +28,7 @@
#define INT16_FROM INT16_FROM_BE
#define INT16_TO INT16_TO_BE
#define pa_sconv_s16le_to_float32 pa_sconv_s16be_to_float32
#define pa_sconv_s16le_from_float32 pa_sconv_s16be_from_float32
#define pa_sconv_s16le_to_float32ne pa_sconv_s16be_to_float32ne
#define pa_sconv_s16le_from_float32ne pa_sconv_s16be_from_float32ne
#include "sconv-s16le.c"

View file

@ -22,7 +22,7 @@
USA.
***/
void pa_sconv_s16be_to_float32(unsigned n, const void *a, unsigned an, float *b);
void pa_sconv_s16be_from_float32(unsigned n, const float *a, void *b, unsigned bn);
void pa_sconv_s16be_to_float32ne(unsigned n, const void *a, unsigned an, float *b);
void pa_sconv_s16be_from_float32ne(unsigned n, const float *a, void *b, unsigned bn);
#endif

View file

@ -38,7 +38,7 @@
#define INT16_TO INT16_TO_LE
#endif
void pa_sconv_s16le_to_float32(unsigned n, const void *a, unsigned an, float *b) {
void pa_sconv_s16le_to_float32ne(unsigned n, const void *a, unsigned an, float *b) {
const int16_t *ca = a;
assert(n && a && an && b);
@ -60,7 +60,7 @@ void pa_sconv_s16le_to_float32(unsigned n, const void *a, unsigned an, float *b)
}
}
void pa_sconv_s16le_from_float32(unsigned n, const float *a, void *b, unsigned bn) {
void pa_sconv_s16le_from_float32ne(unsigned n, const float *a, void *b, unsigned bn) {
int16_t *cb = b;
/* pa_log("%u %p %p %u\n", n, a, b, bn); */

View file

@ -22,7 +22,7 @@
USA.
***/
void pa_sconv_s16le_to_float32(unsigned n, const void *a, unsigned an, float *b);
void pa_sconv_s16le_from_float32(unsigned n, const float *a, void *b, unsigned bn);
void pa_sconv_s16le_to_float32ne(unsigned n, const void *a, unsigned an, float *b);
void pa_sconv_s16le_from_float32ne(unsigned n, const float *a, void *b, unsigned bn);
#endif

View file

@ -28,11 +28,12 @@
#include <assert.h>
#include "endianmacros.h"
#include "sconv.h"
#include "g711.h"
#include "sconv-s16le.h"
#include "sconv-s16be.h"
static void u8_to_float32(unsigned n, const void *a, unsigned an, float *b) {
static void u8_to_float32ne(unsigned n, const void *a, unsigned an, float *b) {
unsigned i;
const uint8_t *ca = a;
assert(n && a && an && b);
@ -42,7 +43,7 @@ static void u8_to_float32(unsigned n, const void *a, unsigned an, float *b) {
for (i = 0; i < an; i++) {
uint8_t v = *(ca++);
sum += (((float) v)-127)/127;
sum += (((float) v)-128)/127;
}
if (sum > 1)
@ -54,7 +55,7 @@ static void u8_to_float32(unsigned n, const void *a, unsigned an, float *b) {
}
}
static void u8_from_float32(unsigned n, const float *a, void *b, unsigned bn) {
static void u8_from_float32ne(unsigned n, const float *a, void *b, unsigned bn) {
unsigned i;
uint8_t *cb = b;
@ -69,14 +70,14 @@ static void u8_from_float32(unsigned n, const float *a, void *b, unsigned bn) {
if (v < -1)
v = -1;
u = (uint8_t) (v*127+127);
u = (uint8_t) (v*127+128);
for (i = 0; i < bn; i++)
*(cb++) = u;
}
}
static void float32_to_float32(unsigned n, const void *a, unsigned an, float *b) {
static void float32ne_to_float32ne(unsigned n, const void *a, unsigned an, float *b) {
unsigned i;
const float *ca = a;
assert(n && a && an && b);
@ -95,7 +96,7 @@ static void float32_to_float32(unsigned n, const void *a, unsigned an, float *b)
}
}
static void float32_from_float32(unsigned n, const float *a, void *b, unsigned bn) {
static void float32ne_from_float32ne(unsigned n, const float *a, void *b, unsigned bn) {
unsigned i;
float *cb = b;
assert(n && a && b && bn);
@ -106,31 +107,122 @@ static void float32_from_float32(unsigned n, const float *a, void *b, unsigned b
}
}
pa_convert_to_float32_func_t pa_get_convert_to_float32_function(enum pa_sample_format f) {
static void ulaw_to_float32ne(unsigned n, const void *a, unsigned an, float *b) {
unsigned i;
const uint8_t *ca = a;
assert(n && a && an && b);
for (; n > 0; n--) {
float sum = 0;
for (i = 0; i < an; i++)
sum += (float) st_ulaw2linear16(*ca++) / 0x7FFF;
if (sum > 1)
sum = 1;
if (sum < -1)
sum = -1;
*(b++) = sum;
}
}
static void ulaw_from_float32ne(unsigned n, const float *a, void *b, unsigned bn) {
unsigned i;
uint8_t *cb = b;
assert(n && a && b && bn);
for (; n > 0; n--) {
float v = *(a++);
uint8_t u;
if (v > 1)
v = 1;
if (v < -1)
v = -1;
u = st_14linear2ulaw((int16_t) (v * 0x1FFF));
for (i = 0; i < bn; i++)
*(cb++) = u;
}
}
static void alaw_to_float32ne(unsigned n, const void *a, unsigned an, float *b) {
unsigned i;
const uint8_t *ca = a;
assert(n && a && an && b);
for (; n > 0; n--) {
float sum = 0;
for (i = 0; i < an; i++)
sum += (float) st_alaw2linear16(*ca++) / 0x7FFF;
if (sum > 1)
sum = 1;
if (sum < -1)
sum = -1;
*(b++) = sum;
}
}
static void alaw_from_float32ne(unsigned n, const float *a, void *b, unsigned bn) {
unsigned i;
uint8_t *cb = b;
assert(n && a && b && bn);
for (; n > 0; n--) {
float v = *(a++);
uint8_t u;
if (v > 1)
v = 1;
if (v < -1)
v = -1;
u = st_13linear2alaw((int16_t) (v * 0xFFF));
for (i = 0; i < bn; i++)
*(cb++) = u;
}
}
pa_convert_to_float32ne_func_t pa_get_convert_to_float32ne_function(enum pa_sample_format f) {
switch(f) {
case PA_SAMPLE_U8:
return u8_to_float32;
return u8_to_float32ne;
case PA_SAMPLE_S16LE:
return pa_sconv_s16le_to_float32;
return pa_sconv_s16le_to_float32ne;
case PA_SAMPLE_S16BE:
return pa_sconv_s16be_to_float32;
case PA_SAMPLE_FLOAT32:
return float32_to_float32;
return pa_sconv_s16be_to_float32ne;
case PA_SAMPLE_FLOAT32NE:
return float32ne_to_float32ne;
case PA_SAMPLE_ALAW:
return alaw_to_float32ne;
case PA_SAMPLE_ULAW:
return ulaw_to_float32ne;
default:
return NULL;
}
}
pa_convert_from_float32_func_t pa_get_convert_from_float32_function(enum pa_sample_format f) {
pa_convert_from_float32ne_func_t pa_get_convert_from_float32ne_function(enum pa_sample_format f) {
switch(f) {
case PA_SAMPLE_U8:
return u8_from_float32;
return u8_from_float32ne;
case PA_SAMPLE_S16LE:
return pa_sconv_s16le_from_float32;
return pa_sconv_s16le_from_float32ne;
case PA_SAMPLE_S16BE:
return pa_sconv_s16be_from_float32;
case PA_SAMPLE_FLOAT32:
return float32_from_float32;
return pa_sconv_s16be_from_float32ne;
case PA_SAMPLE_FLOAT32NE:
return float32ne_from_float32ne;
case PA_SAMPLE_ALAW:
return alaw_from_float32ne;
case PA_SAMPLE_ULAW:
return ulaw_from_float32ne;
default:
return NULL;
}

View file

@ -24,10 +24,10 @@
#include "sample.h"
typedef void (*pa_convert_to_float32_func_t)(unsigned n, const void *a, unsigned an, float *b);
typedef void (*pa_convert_from_float32_func_t)(unsigned n, const float *a, void *b, unsigned bn);
typedef void (*pa_convert_to_float32ne_func_t)(unsigned n, const void *a, unsigned an, float *b);
typedef void (*pa_convert_from_float32ne_func_t)(unsigned n, const float *a, void *b, unsigned bn);
pa_convert_to_float32_func_t pa_get_convert_to_float32_function(enum pa_sample_format f);
pa_convert_from_float32_func_t pa_get_convert_from_float32_function(enum pa_sample_format f);
pa_convert_to_float32ne_func_t pa_get_convert_to_float32ne_function(enum pa_sample_format f);
pa_convert_from_float32ne_func_t pa_get_convert_from_float32ne_function(enum pa_sample_format f);
#endif

View file

@ -48,7 +48,7 @@ struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, con
return NULL;
}
if (resample_method < 0)
if (resample_method == PA_RESAMPLER_INVALID)
resample_method = s->core->resample_method;
if (variable_rate || !pa_sample_spec_equal(spec, &s->sample_spec))
@ -272,3 +272,12 @@ void pa_sink_input_set_name(struct pa_sink_input *i, const char *name) {
pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
}
enum pa_resample_method pa_sink_input_get_resample_method(struct pa_sink_input *i) {
assert(i && i->ref >= 1);
if (!i->resampler)
return PA_RESAMPLER_INVALID;
return pa_resampler_get_method(i->resampler);
}

View file

@ -87,4 +87,6 @@ void pa_sink_input_set_rate(struct pa_sink_input *i, uint32_t rate);
void pa_sink_input_set_name(struct pa_sink_input *i, const char *name);
enum pa_resample_method pa_sink_input_get_resample_method(struct pa_sink_input *i);
#endif

View file

@ -44,7 +44,7 @@ struct pa_source_output* pa_source_output_new(struct pa_source *s, const char *n
return NULL;
}
if (resample_method < 0)
if (resample_method == PA_RESAMPLER_INVALID)
resample_method = s->core->resample_method;
if (!pa_sample_spec_equal(&s->sample_spec, spec))
@ -175,3 +175,12 @@ void pa_source_output_cork(struct pa_source_output *o, int b) {
o->state = b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING;
}
enum pa_resample_method pa_source_output_get_resample_method(struct pa_source_output *o) {
assert(o && o->ref >= 1);
if (!o->resampler)
return PA_RESAMPLER_INVALID;
return pa_resampler_get_method(o->resampler);
}

View file

@ -76,5 +76,6 @@ pa_usec_t pa_source_output_get_latency(struct pa_source_output *i);
void pa_source_output_cork(struct pa_source_output *i, int b);
enum pa_resample_method pa_source_output_get_resample_method(struct pa_source_output *o);
#endif

View file

@ -530,23 +530,6 @@ const char *pa_strsignal(int sig) {
}
}
/* Parse a libsamplrate compatible resampling implementation */
int pa_parse_resample_method(const char *string) {
assert(string);
if (!strcmp(string, "sinc-best-quality"))
return SRC_SINC_BEST_QUALITY;
else if (!strcmp(string, "sinc-medium-quality"))
return SRC_SINC_MEDIUM_QUALITY;
else if (!strcmp(string, "sinc-fastest"))
return SRC_SINC_FASTEST;
else if (!strcmp(string, "zero-order-hold"))
return SRC_ZERO_ORDER_HOLD;
else if (!strcmp(string, "linear"))
return SRC_LINEAR;
else
return -1;
}
/* Check whether the specified GID and the group name match */
static int is_group(gid_t gid, const char *name) {

View file

@ -72,8 +72,6 @@ char *pa_strip_nl(char *s);
const char *pa_strsignal(int sig);
int pa_parse_resample_method(const char *string);
int pa_uid_in_group(const char *name, gid_t *gid);
int pa_lock_fd(int fd, int b);