Debian Codegraph @

src/libsyn123/volume.c

raw
1 /*
2 volume: libsyn123 audio volume handling
3
4 copyright 2018 by the mpg123 project
5 licensed under the terms of the LGPL 2.1
6 see COPYING and AUTHORS files in distribution or http://mpg123.org
7
8 initially written by Thomas Orgis
9 */
10
11 #define NO_GROW_BUF
12 #define NO_SMAX
13 #include "syn123_int.h"
14 #include "../common/debug.h"
15 // Avoid conflict with pragmas in isnan() and friends.
16 #undef warning
17
18 static const double db_min = -SYN123_DB_LIMIT;
19 static const double db_max = SYN123_DB_LIMIT;
20
21 double attribute_align_arg syn123_db2lin(double db)
22 {
23 if(isnan(db) || isless(db, db_min))
24 db = db_min;
25 else if(isgreater(db, db_max))
26 db = db_max;
27 return pow(10, db/20);
28 }
29
30 double attribute_align_arg syn123_lin2db(double volume)
31 {
32 double db;
33 if(isnan(volume) || islessequal(volume, 0.))
34 db = db_min;
35 else
36 {
37 db = 20*log10(volume);
38 if(isgreater(db, db_min))
39 {
40 if(isgreater(db, db_max))
41 db = db_max;
42 }
43 else
44 db = db_min;
45 }
46 return db;
47 }
48
49
50 #define CONVAMP(fXX) \
51 err = syn123_conv( \
52 sh->workbuf.fXX, mixenc, sizeof(sh->workbuf.fXX) \
53 , cbuf, encoding, inframe*block \
54 , NULL, NULL, NULL ); \
55 if(!err) \
56 { \
57 err = syn123_amp( sh->workbuf.fXX, mixenc, block \
58 , volume, offset, NULL, NULL ); \
59 if(err) \
60 return err; \
61 err = syn123_conv( \
62 cbuf, encoding, inframe*block \
63 , sh->workbuf.fXX, mixenc, mixframe*block \
64 , NULL, &clips_block, NULL ); \
65 clips += clips_block; \
66 }
67
68
69 int attribute_align_arg
70 syn123_amp( void* buf, int encoding, size_t samples
71 , double volume, double offset, size_t *clipped, syn123_handle *sh )
72 {
73 size_t clips = 0;
74 int err = 0;
75 if(!buf)
76 {
77 err = SYN123_BAD_BUF;
78 goto amp_end;
79 }
80 switch(encoding)
81 {
82 // This is close to FMA, but only that. It's FAM.
83 #define AMP_LOOP(type) \
84 for(size_t i=0; i<samples; ++i) \
85 ((type*)buf)[i] = (type)volume * (((type*)buf)[i] + (type)offset);
86 case MPG123_ENC_FLOAT_32:
87 AMP_LOOP(float)
88 goto amp_end;
89 case MPG123_ENC_FLOAT_64:
90 AMP_LOOP(double)
91 goto amp_end;
92 #undef AMP_LOOP
93 }
94 if(!sh)
95 {
96 err = SYN123_BAD_ENC;
97 goto amp_end;
98 }
99 else
100 {
101 char *cbuf = buf;
102 int mixenc = syn123_mixenc(encoding, encoding);
103 int mixframe = MPG123_SAMPLESIZE(mixenc);
104 int inframe = MPG123_SAMPLESIZE(encoding);
105 if(!mixenc || !mixframe || !inframe)
106 {
107 err = SYN123_BAD_CONV;
108 goto amp_end;
109 }
110 // Use the whole workbuf, both halves.
111 int mbufblock = 2*bufblock*sizeof(double)/mixframe;
112 mdebug("mbufblock=%i (enc %i)", mbufblock, mixenc);
113 while(samples)
114 {
115 int block = (int)smin(samples, mbufblock);
116 size_t clips_block = 0;
117 int err = 0;
118 if(mixenc == MPG123_ENC_FLOAT_64)
119 {
120 CONVAMP(f64)
121 } else
122 {
123 CONVAMP(f32)
124 }
125 if(err)
126 {
127 mdebug("conv error: %i", err);
128 err = SYN123_BAD_CONV; // SYN123_WEIRD also an option
129 goto amp_end;
130 }
131 cbuf += block*inframe;
132 samples -= block;
133 }
134 }
135 amp_end:
136 if(clipped)
137 *clipped = clips;
138 return err;
139 }