UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
silence.c
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2023 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <assert.h>
39#include <stddef.h>
40#include <stdlib.h>
41#include <string.h>
42
43#include "audio/audio_filter.h"
44#include "audio/types.h"
45#include "lib_common.h"
46#include "utils/color_out.h"
47
48enum {
50};
51
57
58static void
60{
61 color_printf("Audio capture " TBOLD(
62 "silence") " allows muting individual channels.\n\n");
63 color_printf("Usage:\n");
64 color_printf("\t" TBOLD("--audio-filter silence[:<idx1>,<idx2>]\n\n"));
65 color_printf("(indexed from zero)\n");
66 color_printf("If no index is given, all channels will be muted.\n");
67}
68
69static enum af_result_code
70init(struct module *parent, const char *cfg, void **state)
71{
72 (void) parent;
73 if (strcmp(cfg, "help") == 0) {
74 usage();
75 return AF_HELP_SHOWN;
76 }
77 struct state_silence *s = calloc(1, sizeof *s);
78
79 const size_t len = strlen(cfg) + 1;
80 char fmt[len];
81 strncpy(fmt, cfg, len);
82 char *tmp = fmt;
83 char *item = NULL;
84 char *end_ptr = NULL;
85 while ((item = strtok_r(tmp, ",", &end_ptr)) != NULL) {
86 assert(s->silence_channels_count < MAX_CHANNELS - 1);
88 strtol(item, NULL, 10);
89 tmp = NULL;
90 }
91 *state = s;
92 return AF_OK;
93}
94
95static enum af_result_code
96configure(void *state, int in_bps, int in_ch_count, int in_sample_rate)
97{
98 struct state_silence *s = state;
99
100 s->desc.bps = in_bps;
101 s->desc.ch_count = in_ch_count;
102 s->desc.sample_rate = in_sample_rate;
103 return AF_OK;
104}
105
106static void
108{
109 free(state);
110}
111
112static void
113get_configured_desc(void *state, int *bps, int *ch_count, int *sample_rate)
114{
115 struct state_silence *s = state;
116
117 *bps = s->desc.bps;
118 *ch_count = s->desc.ch_count;
120}
121
122static enum af_result_code
124{
125 struct state_silence *s = state;
126
127 if (s->silence_channels_count == 0) {
128 memset((*frame)->data, 0, (*frame)->data_len);
129 return AF_OK;
130 }
131
132 const int frame_size = s->desc.bps * s->desc.ch_count;
133 for (int i = 0; i < s->silence_channels_count; ++i) {
134 if (s->silence_channels[i] >= (size_t) (*frame)->ch_count) {
135 continue;
136 }
137 char *ptr =
138 (*frame)->data + s->silence_channels[i] * (*frame)->bps;
139 for (int j = 0; j < (*frame)->data_len; j += frame_size) {
140 memset(ptr, 0, (*frame)->bps);
141 ptr += frame_size;
142 }
143 }
144
145 return AF_OK;
146}
147
149 .name = "silence",
150 .init = init,
151 .done = done,
152 .configure = configure,
153 .get_configured_in = get_configured_desc,
154 .get_configured_out = get_configured_desc,
155 .filter = filter,
156};
157
void ** state
int int int sample_rate
struct video_frame * frame
const char * fmt
int void size_t * len
This file contains definition of some common audio types.
#define AUDIO_FILTER_ABI_VERSION
af_result_code
@ AF_HELP_SHOWN
@ AF_OK
int color_printf(const char *format,...)
#define TBOLD(x)
Definition color_out.h:70
@ LIBRARY_CLASS_AUDIO_FILTER
Definition lib_common.h:85
#define REGISTER_MODULE(name, info, lclass, abi)
Registers module to global modules' registry.
Definition lib_common.h:144
const char * parent
Definition settings.cpp:198
static void get_configured_desc(void *state, int *bps, int *ch_count, int *sample_rate)
Definition silence.c:113
static enum af_result_code configure(void *state, int in_bps, int in_ch_count, int in_sample_rate)
Definition silence.c:96
static enum af_result_code init(struct module *parent, const char *cfg, void **state)
Definition silence.c:70
static void usage()
Definition silence.c:59
@ MAX_CHANNELS
Definition silence.c:49
static const struct audio_filter_info audio_filter_silence
Definition silence.c:148
static enum af_result_code filter(void *state, struct audio_frame **frame)
Definition silence.c:123
static void done(void *state)
Definition silence.c:107
int sample_rate
Definition types.h:78
int ch_count
Definition types.h:79
int bps
Definition types.h:77
struct used to comunicate with audio capture/playback device
Definition types.h:99
Definition hd-rum.c:27
size_t silence_channels[MAX_CHANNELS]
Definition silence.c:54
struct audio_desc desc
Definition silence.c:53
int silence_channels_count
Definition silence.c:55