npv:_reasonable_ "pedanticage" of the code
[nyanmp.git] / npv / audio / filt / local / code.frag.c
blobd361d3eab357d4596ef87216b95d6e83f45012e4
1 STATIC void fatal(u8 *fmt, ...)
3 va_list ap;
5 npv_perr("audio:filter:");
6 va_start(ap, fmt);
7 npv_vfatal(fmt, ap);
8 va_end(ap);
10 STATIC void warning(u8 *fmt, ...)
12 va_list ap;
14 npv_perr("audio:filter:");
15 va_start(ap, fmt);
16 npv_vwarning(fmt, ap);
17 va_end(ap);
19 STATIC void pout(u8 *fmt, ...)
21 va_list ap;
23 npv_pout("audio:filter:");
24 va_start(ap, fmt);
25 npv_vpout(fmt, ap);
26 va_end(ap);
28 STATIC void abufsrc_cfg(enum avutil_audio_fr_fmt_t fmt, int rate, int chans_n,
29 uint64_t chans_layout, bool print_info)
31 int r;
32 avutil_rational_t time_base;
33 u8 chans_layout_str[STR_SZ]; /* should be overkill */
35 abufsrc_l = avfilter_get_by_name("abuffer");
36 if (abufsrc_l == 0)
37 fatal("audio buffer source:could not find the filter\n");
38 abufsrc_ctx_l = avfilter_graph_alloc_filt(graph_l, abufsrc_l,
39 "src_abuf");
40 if (abufsrc_ctx_l == 0)
41 fatal("audio buffer source context:could not allocate the instance in the filter graph\n");
42 r = avutil_opt_set(abufsrc_ctx_l, "sample_fmt",
43 avutil_get_audio_fr_fmt_name(fmt),
44 AVUTIL_OPT_SEARCH_CHILDREN);
45 if (r < 0)
46 fatal("audio buffer source context:unable to set the decoder frame format option\n");
47 r = avutil_opt_set_int(abufsrc_ctx_l, "sample_rate",
48 rate, AVUTIL_OPT_SEARCH_CHILDREN);
49 if (r < 0)
50 fatal("audio buffer source context:unable to set the decoder rate option\n");
52 * XXX: at the time of coding, bug for 1 chans layout... or I did miss
53 * some valuable information
55 avutil_get_chans_layout_str(chans_layout_str, sizeof(chans_layout_str),
56 chans_n, chans_layout);
57 if (print_info)
58 pout("audio buffer source context:using channels layout \"%s\" (%d pcm channels)\n", chans_layout_str, chans_n);
59 r = avutil_opt_set(abufsrc_ctx_l, "channel_layout", chans_layout_str,
60 AVUTIL_OPT_SEARCH_CHILDREN);
61 if (r < 0)
62 fatal("audio buffer source context:unable to set the decoder channel layout option\n");
63 r = avfilter_init_str(abufsrc_ctx_l, 0);
64 if (r < 0)
65 fatal("audio buffer source context:unable to initialize\n");
67 STATIC void vol_cfg(bool muted, double vol_cfg)
69 double vol;
70 u8 vol_l10n_str[sizeof("xxx.xx")]; /* should be overkill */
71 int r;
73 vol_l = avfilter_get_by_name("volume");
74 if (vol_l == 0)
75 fatal("volume:could not find the filter\n");
76 vol_ctx_l = avfilter_graph_alloc_filt(graph_l, vol_l, "vol");
77 if (vol_ctx_l == 0)
78 fatal("volume context:could not allocate the instance in the filter graph\n");
79 if (muted)
80 vol = 0.0;
81 else
82 vol = vol_cfg;
83 /* yeah the radix is localized, can be '.', ','... */
84 snprintf(vol_l10n_str, sizeof(vol_l10n_str), "%f", vol);
85 r = avutil_opt_set(vol_ctx_l, "volume", vol_l10n_str,
86 AVUTIL_OPT_SEARCH_CHILDREN);
87 if (r < 0)
88 fatal("volume context:unable to set the volume option\n");
89 r = avfilter_init_str(vol_ctx_l, 0);
90 if (r < 0)
91 fatal("volume buffer context:unable to initialize\n");
93 STATIC void afmt_cfg(enum avutil_audio_fr_fmt_t fmt, int rate, int chans_n,
94 uint64_t chans_layout, bool print_info)
96 int r;
97 u8 rate_str[sizeof("dddddd")];
98 u8 chans_layout_str[STR_SZ]; /* should be overkill */
100 afmt_l = avfilter_get_by_name("aformat");
101 if (afmt_l == 0)
102 fatal("audio format:could not find the filter");
103 afmt_ctx_l = avfilter_graph_alloc_filt(graph_l, afmt_l, "afmt");
104 if (afmt_ctx_l == 0)
105 fatal("audio format:could not allocate the instance in the filter graph\n");
106 r = avutil_opt_set(afmt_ctx_l, "sample_fmts",
107 avutil_get_audio_fr_fmt_name(fmt), AVUTIL_OPT_SEARCH_CHILDREN);
108 if (r < 0)
109 fatal("audio format context:could to set the pcm sample format\n");
110 snprintf(rate_str, sizeof(rate_str), "%d", rate);
111 r = avutil_opt_set(afmt_ctx_l, "sample_rates", rate_str,
112 AVUTIL_OPT_SEARCH_CHILDREN);
113 if (r < 0)
114 fatal("audio format context:could not set the pcm rate\n");
115 avutil_get_chans_layout_str(chans_layout_str, sizeof(chans_layout_str),
116 chans_n, chans_layout);
117 r = avutil_opt_set(afmt_ctx_l, "channel_layouts", chans_layout_str,
118 AVUTIL_OPT_SEARCH_CHILDREN);
119 if (r < 0)
120 fatal("audio format context:could not set the layout of channels\n");
121 if (print_info)
122 pout("audio format context:channel layout is \"%s\"\n", chans_layout_str);
123 r = avfilter_init_str(afmt_ctx_l, 0);
124 if (r < 0)
125 fatal("audio format context:unable to initialize\n");
127 STATIC void abufsink_cfg(void)
129 int r;
131 abufsink_l = avfilter_get_by_name("abuffersink");
132 if (abufsink_l == 0)
133 fatal("audio buffer sink:could not find the filter\n");
134 abufsink_ctx_l = avfilter_graph_alloc_filt(graph_l, abufsink_l,
135 "sink_abuf");
136 if (abufsink_ctx_l == 0)
137 fatal("audio buffer sink context:could not allocate the instance in the filter graph\n");
138 r = avfilter_init_str(abufsink_ctx_l, 0);
139 if (r < 0)
140 fatal("audio buffer sink context:unable to initialize\n");
142 STATIC void init_once_local(void)
144 graph_l = 0;
145 abufsrc_ctx_l = 0;
146 abufsrc_l = 0;
147 vol_ctx_l = 0;
148 vol_l = 0;
149 afmt_ctx_l = 0;
150 afmt_l = 0;
151 abufsink_ctx_l = 0;
152 abufsink_l = 0;
153 /* floating point strs are localized... */
154 snprintf(double_zero_l10n_str_l, sizeof(double_zero_l10n_str_l),
155 "%f", 0.);
157 STATIC void init_once_public(double initial_vol)
159 filt_p.set = avutil_audio_set_ref_alloc();
160 if (filt_p.set == 0)
161 fatal("ffmpeg:unable to allocate a reference on set of frames for the public part of the interactive latency filter\n");
162 filt_p.pcm_written_ufrs_n = 0;
163 filt_p.vol = initial_vol;
164 filt_p.muted = false;