export-source: add float format

This commit is contained in:
Wim Taymans 2018-03-05 12:40:29 +01:00
parent 2ed627f3b1
commit 2660b5c4f5

View file

@ -203,7 +203,9 @@ static int port_enum_formats(struct spa_node *node,
d->t->param.idEnumFormat, d->t->spa_format, d->t->param.idEnumFormat, d->t->spa_format,
"I", d->type.media_type.audio, "I", d->type.media_type.audio,
"I", d->type.media_subtype.raw, "I", d->type.media_subtype.raw,
":", d->type.format_audio.format, "I", d->type.audio_format.S16, ":", d->type.format_audio.format, "Ieu", d->type.audio_format.S16,
SPA_POD_PROP_ENUM(2, d->type.audio_format.S16,
d->type.audio_format.F32),
":", d->type.format_audio.channels, "iru", 2, ":", d->type.format_audio.channels, "iru", 2,
SPA_POD_PROP_MIN_MAX(1, INT32_MAX), SPA_POD_PROP_MIN_MAX(1, INT32_MAX),
":", d->type.format_audio.rate, "iru", 44100, ":", d->type.format_audio.rate, "iru", 44100,
@ -353,7 +355,8 @@ static int port_set_format(struct spa_node *node,
if (spa_format_audio_raw_parse(format, &d->format, &d->type.format_audio) < 0) if (spa_format_audio_raw_parse(format, &d->format, &d->type.format_audio) < 0)
return -EINVAL; return -EINVAL;
if (d->format.format != d->type.audio_format.S16) if (d->format.format != d->type.audio_format.S16 &&
d->format.format != d->type.audio_format.F32)
return -EINVAL; return -EINVAL;
return 0; return 0;
@ -424,12 +427,51 @@ static int impl_port_reuse_buffer(struct spa_node *node, uint32_t port_id, uint3
return 0; return 0;
} }
static void fill_f32(struct data *d, void *dest, int avail)
{
float *dst = dest;
int n_samples = avail / (sizeof(float) * d->format.channels);
int i, c;
for (i = 0; i < n_samples; i++) {
float val;
d->accumulator += M_PI_M2 * 440 / d->format.rate;
if (d->accumulator >= M_PI_M2)
d->accumulator -= M_PI_M2;
val = sin(d->accumulator);
for (c = 0; c < d->format.channels; c++)
*dst++ = val;
}
}
static void fill_s16(struct data *d, void *dest, int avail)
{
int16_t *dst = dest;
int n_samples = avail / (sizeof(int16_t) * d->format.channels);
int i, c;
for (i = 0; i < n_samples; i++) {
int16_t val;
d->accumulator += M_PI_M2 * 440 / d->format.rate;
if (d->accumulator >= M_PI_M2)
d->accumulator -= M_PI_M2;
val = (int16_t) (sin(d->accumulator) * 32767.0);
for (c = 0; c < d->format.channels; c++)
*dst++ = val;
}
}
static int impl_node_process_output(struct spa_node *node) static int impl_node_process_output(struct spa_node *node)
{ {
struct data *d = SPA_CONTAINER_OF(node, struct data, impl_node); struct data *d = SPA_CONTAINER_OF(node, struct data, impl_node);
struct buffer *b; struct buffer *b;
int i, c, n_samples, avail; int avail;
int16_t *dst;
struct spa_io_buffers *io = d->io; struct spa_io_buffers *io = d->io;
uint32_t maxsize, index = 0; uint32_t maxsize, index = 0;
uint32_t filled, offset; uint32_t filled, offset;
@ -458,21 +500,10 @@ static int impl_node_process_output(struct spa_node *node)
if (offset + avail > maxsize) if (offset + avail > maxsize)
avail = maxsize - offset; avail = maxsize - offset;
dst = SPA_MEMBER(b->ptr, offset, void); if (d->format.format == d->type.audio_format.S16)
n_samples = avail / (sizeof(int16_t) * d->format.channels); fill_s16(d, SPA_MEMBER(b->ptr, offset, void), avail);
else if (d->format.format == d->type.audio_format.F32)
for (i = 0; i < n_samples; i++) { fill_f32(d, SPA_MEMBER(b->ptr, offset, void), avail);
int16_t val;
d->accumulator += M_PI_M2 * 440 / d->format.rate;
if (d->accumulator >= M_PI_M2)
d->accumulator -= M_PI_M2;
val = (int16_t) (sin(d->accumulator) * 32767.0);
for (c = 0; c < d->format.channels; c++)
*dst++ = val;
}
od[0].chunk->offset = 0; od[0].chunk->offset = 0;
od[0].chunk->size = avail; od[0].chunk->size = avail;