comparison foosdk/sdk/foobar2000/SDK/replaygain_info.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "foobar2000-sdk-pch.h"
2
3 #ifdef _MSC_VER
4 #include <pfc/fpu.h>
5 #define RG_FPU() fpu_control_roundnearest bah;
6 #else
7 #define RG_FPU()
8 #endif
9
10 bool replaygain_info::g_format_gain(float p_value,char p_buffer[text_buffer_size])
11 {
12 RG_FPU();
13 if (p_value == gain_invalid)
14 {
15 p_buffer[0] = 0;
16 return false;
17 }
18 else
19 {
20 pfc::float_to_string(p_buffer,text_buffer_size - 4,p_value,2,true);
21 #ifdef _MSC_VER
22 strcat_s(p_buffer, text_buffer_size, " dB");
23 #else
24 strcat(p_buffer, " dB");
25 #endif
26 return true;
27 }
28 }
29
30 bool replaygain_info::g_format_peak_db(float p_value, char p_buffer[text_buffer_size]) {
31 const float lo = 1.0 / (float)(1 << 24);
32 if ( p_value == peak_invalid || p_value < lo ) return false;
33 return g_format_gain((float)audio_math::scale_to_gain(p_value), p_buffer);
34
35 }
36
37 bool replaygain_info::g_format_peak(float p_value,char p_buffer[text_buffer_size])
38 {
39 RG_FPU();
40 if (p_value == peak_invalid)
41 {
42 p_buffer[0] = 0;
43 return false;
44 }
45 else
46 {
47 pfc::float_to_string(p_buffer,text_buffer_size,p_value,6,false);
48 return true;
49 }
50 }
51
52 void replaygain_info::reset()
53 {
54 *this = replaygain_info();
55 }
56
57 #define meta_album_gain "replaygain_album_gain"
58 #define meta_album_peak "replaygain_album_peak"
59 #define meta_track_gain "replaygain_track_gain"
60 #define meta_track_peak "replaygain_track_peak"
61
62 bool replaygain_info::g_is_meta_replaygain(const char * p_name,t_size p_name_len)
63 {
64 return
65 stricmp_utf8_ex(p_name,p_name_len,meta_album_gain,SIZE_MAX) == 0 ||
66 stricmp_utf8_ex(p_name,p_name_len,meta_album_peak,SIZE_MAX) == 0 ||
67 stricmp_utf8_ex(p_name,p_name_len,meta_track_gain,SIZE_MAX) == 0 ||
68 stricmp_utf8_ex(p_name,p_name_len,meta_track_peak,SIZE_MAX) == 0;
69 }
70
71 bool replaygain_info::set_from_meta_ex(const char * p_name,t_size p_name_len,const char * p_value,t_size p_value_len)
72 {
73 RG_FPU();
74 if (stricmp_utf8_ex(p_name,p_name_len,meta_album_gain,SIZE_MAX) == 0)
75 {
76 m_album_gain = (float)pfc::string_to_float(p_value,p_value_len);
77 return true;
78 }
79 else if (stricmp_utf8_ex(p_name,p_name_len,meta_album_peak,SIZE_MAX) == 0)
80 {
81 m_album_peak = (float)pfc::string_to_float(p_value,p_value_len);
82 if (m_album_peak < 0) m_album_peak = 0;
83 return true;
84 }
85 else if (stricmp_utf8_ex(p_name,p_name_len,meta_track_gain,SIZE_MAX) == 0)
86 {
87 m_track_gain = (float)pfc::string_to_float(p_value,p_value_len);
88 return true;
89 }
90 else if (stricmp_utf8_ex(p_name,p_name_len,meta_track_peak,SIZE_MAX) == 0)
91 {
92 m_track_peak = (float)pfc::string_to_float(p_value,p_value_len);
93 if (m_track_peak < 0) m_track_peak = 0;
94 return true;
95 }
96 else return false;
97 }
98
99
100 t_size replaygain_info::get_value_count()
101 {
102 t_size ret = 0;
103 if (is_album_gain_present()) ret++;
104 if (is_album_peak_present()) ret++;
105 if (is_track_gain_present()) ret++;
106 if (is_track_peak_present()) ret++;
107 return ret;
108 }
109
110 float replaygain_info::anyGain(bool bPreferAlbum) const {
111 if ( bPreferAlbum ) {
112 if ( this->is_album_gain_present() ) return this->m_album_gain;
113 return this->m_track_gain;
114 } else {
115 if ( this->is_track_gain_present() ) return this->m_track_gain;
116 return this->m_album_gain;
117 }
118 }
119
120 float replaygain_info::g_parse_gain_text(const char * p_text, t_size p_text_len) {
121 RG_FPU();
122 if (p_text != 0 && p_text_len > 0 && *p_text != 0)
123 return (float)pfc::string_to_float(p_text, p_text_len);
124 else
125 return gain_invalid;
126 }
127
128 void replaygain_info::set_album_gain_text(const char * p_text,t_size p_text_len) {
129 m_album_gain = g_parse_gain_text(p_text, p_text_len);
130 }
131
132 void replaygain_info::set_track_gain_text(const char * p_text,t_size p_text_len)
133 {
134 m_track_gain = g_parse_gain_text(p_text, p_text_len);
135 }
136
137 void replaygain_info::set_album_peak_text(const char * p_text,t_size p_text_len)
138 {
139 RG_FPU();
140 if (p_text != 0 && p_text_len > 0 && *p_text != 0)
141 m_album_peak = (float)pfc::string_to_float(p_text,p_text_len);
142 else
143 remove_album_peak();
144 }
145
146 void replaygain_info::set_track_peak_text(const char * p_text,t_size p_text_len)
147 {
148 RG_FPU();
149 if (p_text != 0 && p_text_len > 0 && *p_text != 0)
150 m_track_peak = (float)pfc::string_to_float(p_text,p_text_len);
151 else
152 remove_track_peak();
153 }
154
155 replaygain_info replaygain_info::g_merge(replaygain_info r1,replaygain_info r2)
156 {
157 replaygain_info ret = r1;
158 if (!ret.is_album_gain_present()) ret.m_album_gain = r2.m_album_gain;
159 if (!ret.is_album_peak_present()) ret.m_album_peak = r2.m_album_peak;
160 if (!ret.is_track_gain_present()) ret.m_track_gain = r2.m_track_gain;
161 if (!ret.is_track_peak_present()) ret.m_track_peak = r2.m_track_peak;
162 return ret;
163 }
164
165
166 void replaygain_info::for_each(for_each_t f) const {
167 t_text_buffer buffer;
168 if (format_track_gain(buffer)) f(meta_track_gain, buffer);
169 if (format_track_peak(buffer)) f(meta_track_peak, buffer);
170 if (format_album_gain(buffer)) f(meta_album_gain, buffer);
171 if (format_album_peak(buffer)) f(meta_album_peak, buffer);
172 }