audioconvert: add an option generate silence noise

Add an empty.noise option that specifies the number of bits to
use for noise when the input signal is pure silence.

Some amplifiers can go into suspend mode pretty easily when they
get pure silence. With empty.noise = 1, audioconvert will now generate
a bitpattern that can keep those amplifiers alive, together with
disabling suspend in the session manager.

Fixes #705
This commit is contained in:
Wim Taymans 2022-06-24 13:41:02 +02:00
parent 9430df0ba6
commit 1c6cb049ce
4 changed files with 182 additions and 29 deletions

View file

@ -49,6 +49,7 @@
#include "fmt-ops.h"
#include "channelmix-ops.h"
#include "resample.h"
#include "noise-ops.h"
#undef SPA_LOG_TOPIC_DEFAULT
#define SPA_LOG_TOPIC_DEFAULT log_topic
@ -89,9 +90,10 @@ struct props {
struct volumes monitor;
unsigned int have_soft_volume:1;
unsigned int mix_disabled:1;
double rate;
unsigned int resample_quality;
unsigned int resample_disabled:1;
double rate;
uint32_t empty_noise;
};
static void props_reset(struct props *props)
@ -108,6 +110,7 @@ static void props_reset(struct props *props)
props->rate = 1.0;
props->resample_quality = RESAMPLE_DEFAULT_QUALITY;
props->resample_disabled = false;
props->empty_noise = 0;
}
struct buffer {
@ -209,6 +212,7 @@ struct impl {
struct channelmix mix;
struct resample resample;
struct volume volume;
struct noise noise;
double rate_scale;
uint32_t in_offset;
@ -605,6 +609,14 @@ static int impl_node_enum_params(void *object, int seq,
SPA_PROP_INFO_type, SPA_POD_CHOICE_Bool(p->resample_disabled),
SPA_PROP_INFO_params, SPA_POD_Bool(true));
break;
case 21:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_PropInfo, id,
SPA_PROP_INFO_name, SPA_POD_String("empty.noise"),
SPA_PROP_INFO_description, SPA_POD_String("Fill empty buffers with noise"),
SPA_PROP_INFO_type, SPA_POD_CHOICE_RANGE_Int(p->empty_noise, 0, 16),
SPA_PROP_INFO_params, SPA_POD_Bool(true));
break;
default:
return 0;
}
@ -671,6 +683,8 @@ static int impl_node_enum_params(void *object, int seq,
spa_pod_builder_int(&b, p->resample_quality);
spa_pod_builder_string(&b, "resample.disable");
spa_pod_builder_bool(&b, p->resample_disabled);
spa_pod_builder_string(&b, "empty.noise");
spa_pod_builder_int(&b, p->empty_noise);
spa_pod_builder_pop(&b, &f[1]);
param = spa_pod_builder_pop(&b, &f[0]);
break;
@ -740,6 +754,8 @@ static int audioconvert_set_param(struct impl *this, const char *k, const char *
this->props.resample_quality = atoi(s);
else if (spa_streq(k, "resample.disable"))
this->props.resample_disabled = spa_atob(s);
else if (spa_streq(k, "empty.noise"))
spa_atou32(s, &this->props.empty_noise, 0);
else
return 0;
return 1;
@ -1302,6 +1318,33 @@ static int setup_resample(struct impl *this)
return res;
}
static int calc_width(struct spa_audio_info *info)
{
switch (info->info.raw.format) {
case SPA_AUDIO_FORMAT_U8:
case SPA_AUDIO_FORMAT_U8P:
case SPA_AUDIO_FORMAT_S8:
case SPA_AUDIO_FORMAT_S8P:
case SPA_AUDIO_FORMAT_ULAW:
case SPA_AUDIO_FORMAT_ALAW:
return 1;
case SPA_AUDIO_FORMAT_S16P:
case SPA_AUDIO_FORMAT_S16:
case SPA_AUDIO_FORMAT_S16_OE:
return 2;
case SPA_AUDIO_FORMAT_S24P:
case SPA_AUDIO_FORMAT_S24:
case SPA_AUDIO_FORMAT_S24_OE:
return 3;
case SPA_AUDIO_FORMAT_F64P:
case SPA_AUDIO_FORMAT_F64:
case SPA_AUDIO_FORMAT_F64_OE:
return 8;
default:
return 4;
}
}
static int setup_out_convert(struct impl *this)
{
uint32_t i, j;
@ -1352,6 +1395,17 @@ static int setup_out_convert(struct impl *this)
spa_log_debug(this->log, "%p: got converter features %08x:%08x passthrough:%d", this,
this->cpu_flags, out->conv.cpu_flags, out->conv.is_passthrough);
this->noise.intensity = (calc_width(&dst_info) * 8) - 1;
this->noise.intensity -= SPA_MIN(this->noise.intensity, this->props.empty_noise);
this->noise.n_channels = dst_info.info.raw.channels;
this->noise.cpu_flags = this->cpu_flags;
if ((res = noise_init(&this->noise)) < 0)
return res;
spa_log_debug(this->log, "%p: empty noise:%d intensity:%d", this,
this->props.empty_noise, this->noise.intensity);
return 0;
}
@ -1688,33 +1742,6 @@ static int clear_buffers(struct impl *this, struct port *port)
return 0;
}
static int calc_width(struct spa_audio_info *info)
{
switch (info->info.raw.format) {
case SPA_AUDIO_FORMAT_U8:
case SPA_AUDIO_FORMAT_U8P:
case SPA_AUDIO_FORMAT_S8:
case SPA_AUDIO_FORMAT_S8P:
case SPA_AUDIO_FORMAT_ULAW:
case SPA_AUDIO_FORMAT_ALAW:
return 1;
case SPA_AUDIO_FORMAT_S16P:
case SPA_AUDIO_FORMAT_S16:
case SPA_AUDIO_FORMAT_S16_OE:
return 2;
case SPA_AUDIO_FORMAT_S24P:
case SPA_AUDIO_FORMAT_S24:
case SPA_AUDIO_FORMAT_S24_OE:
return 3;
case SPA_AUDIO_FORMAT_F64P:
case SPA_AUDIO_FORMAT_F64:
case SPA_AUDIO_FORMAT_F64_OE:
return 8;
default:
return 4;
}
}
static int port_set_latency(void *object,
enum spa_direction direction,
uint32_t port_id,
@ -2445,6 +2472,12 @@ static int impl_node_process(void *object)
}
this->out_offset += n_samples;
if (in_empty && this->props.empty_noise > 0) {
if (out_passthrough)
out_datas = dst_datas;
noise_process(&this->noise, out_datas, n_samples);
in_empty = false;
}
if (!out_passthrough) {
in_datas = (const void**)out_datas;
spa_log_trace_fp(this->log, "%p: convert %d", this, n_samples);

View file

@ -94,7 +94,9 @@ audioconvert_lib = static_library('audioconvert',
'resample-peaks.c',
'fmt-ops-c.c',
'volume-ops.c',
'volume-ops-c.c' ],
'volume-ops-c.c',
'noise-ops.c',
'noise-ops-c.c' ],
c_args : [ simd_cargs, '-O3'],
link_with : simd_dependencies,
include_directories : [configinc],

View file

@ -0,0 +1,39 @@
/* Spa
*
* Copyright © 2022 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 "noise-ops.h"
void
noise_f32_c(struct noise *ns, void * SPA_RESTRICT dst[], uint32_t n_samples)
{
uint32_t i, n;
float **d = (float**)dst;
const float *t = ns->tab;
int tab_idx = ns->tab_idx;
for (i = 0; i < ns->n_channels; i++)
for (n = 0; n < n_samples; n++)
d[i][n] = t[tab_idx++ & NOISE_MOD];
ns->tab_idx = (tab_idx + 23) & NOISE_MOD;
}

View file

@ -0,0 +1,79 @@
/* Spa
*
* Copyright © 2022 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 <string.h>
#include <stdio.h>
#include <math.h>
#include <spa/param/audio/format-utils.h>
#include <spa/support/cpu.h>
#include <spa/support/log.h>
#include <spa/utils/defs.h>
#include "noise-ops.h"
typedef void (*noise_func_t) (struct noise *ns, void * SPA_RESTRICT dst[], uint32_t n_samples);
static const struct noise_info {
noise_func_t process;
uint32_t cpu_flags;
} noise_table[] =
{
{ noise_f32_c, 0 },
};
#define MATCH_CPU_FLAGS(a,b) ((a) == 0 || ((a) & (b)) == a)
static const struct noise_info *find_noise_info(uint32_t cpu_flags)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(noise_table); i++) {
if (!MATCH_CPU_FLAGS(noise_table[i].cpu_flags, cpu_flags))
continue;
return &noise_table[i];
}
return NULL;
}
static void impl_noise_free(struct noise *ns)
{
ns->process = NULL;
}
int noise_init(struct noise *ns)
{
const struct noise_info *info;
int i;
info = find_noise_info(ns->cpu_flags);
if (info == NULL)
return -ENOTSUP;
for (i = 0; i < NOISE_SIZE; i++)
ns->tab[i] = (drand48() - 0.5) / (1 << ns->intensity);
ns->free = impl_noise_free;
ns->process = info->process;
return 0;
}