@@ -16,7 +16,7 @@ using namespace spl;
16
16
// ---------------------------------------------------------------------------
17
17
/* private */
18
18
19
- /* warning : This function is called from ISR */
19
+ /* WARNING : This function is called from ISR */
20
20
void meter::mic_data_ready (const int16_t *data, uint16_t data_len)
21
21
{
22
22
if (this ->dsp_buffer_ready )
@@ -53,19 +53,19 @@ meter::meter(hal::microphone µphone, const new_data_cb_t &new_data_cb) : mi
53
53
this ->new_spl_data_cb = new_data_cb;
54
54
this ->spl_data .spl_min = 120 ;
55
55
this ->spl_data .spl_max = 0 ;
56
+ this ->spl_data .spl = 0 ;
56
57
this ->spl_data_period = static_cast <float32_t >(this ->dsp_buffer .size ()) / this ->mic .get_sampling_frequency ();
57
58
58
59
this ->spl_data .weighting = weighting_t ::a;
59
- this ->weighting_filter = new a_weighting ();
60
+ this ->weighting_filter = std::make_unique<spl:: a_weighting> ();
60
61
61
62
this ->spl_data .averaging = averaging_t ::slow;
62
- this ->averaging_filter = new slow_averaging (this ->spl_data_period , this ->spl_data .spl );
63
+ this ->averaging_filter = std::make_unique<spl:: slow_averaging> (this ->spl_data_period , this ->spl_data .spl );
63
64
}
64
65
65
66
meter::~meter ()
66
67
{
67
- delete this ->weighting_filter ;
68
- delete this ->averaging_filter ;
68
+
69
69
}
70
70
71
71
void meter::enable (void )
@@ -77,47 +77,47 @@ void meter::enable(void)
77
77
void meter::process (void )
78
78
{
79
79
/* DSP processing takes around 2.68 msec at 48MHz */
80
- if (this ->dsp_buffer_ready )
81
- {
82
- auto ¤t_dsp_buffer = this ->dsp_buffer ;
80
+ if (!this ->dsp_buffer_ready )
81
+ return ;
83
82
84
- /* Apply weighting filter */
85
- this ->weighting_filter ->process (current_dsp_buffer, this ->aux_dsp_buffer );
86
- this ->dsp_buffer_ready = false ;
83
+ auto ¤t_dsp_buffer = this ->dsp_buffer ;
87
84
88
- current_dsp_buffer = this ->aux_dsp_buffer ;
85
+ /* Apply weighting filter */
86
+ this ->weighting_filter ->process (current_dsp_buffer, this ->aux_dsp_buffer );
87
+ this ->dsp_buffer_ready = false ;
89
88
90
- /* Delete offset */
91
- float32_t mean;
92
- arm_mean_f32 (current_dsp_buffer.data (), current_dsp_buffer.size (), &mean);
93
- arm_offset_f32 (current_dsp_buffer.data (), -mean, current_dsp_buffer.data (), current_dsp_buffer.size ());
89
+ current_dsp_buffer = this ->aux_dsp_buffer ;
94
90
95
- /* Calculate RMS value */
96
- float32_t rms;
97
- arm_rms_f32 (current_dsp_buffer.data (), current_dsp_buffer.size (), &rms);
91
+ /* Delete offset */
92
+ float32_t mean;
93
+ arm_mean_f32 (current_dsp_buffer.data (), current_dsp_buffer.size (), &mean);
94
+ arm_offset_f32 (current_dsp_buffer.data (), -mean, current_dsp_buffer.data (), current_dsp_buffer.size ());
98
95
99
- /* Normalize RMS value */
100
- rms /= INT16_MAX;
96
+ /* Calculate RMS value */
97
+ float32_t rms;
98
+ arm_rms_f32 (current_dsp_buffer.data (), current_dsp_buffer.size (), &rms);
101
99
102
- /* Calculate dB SPL */
103
- float32_t db_spl_raw = 94 - this -> mic . get_sensitivity () + 20 . 0f * log10f (rms) ;
100
+ /* Normalize RMS value */
101
+ rms /= INT16_MAX ;
104
102
105
- /* Apply averaging */
106
- float32_t db_spl = this ->averaging_filter -> process (db_spl_raw );
103
+ /* Calculate dB SPL */
104
+ float32_t db_spl_raw = 94 - this ->mic . get_sensitivity () + 20 . 0f * log10f (rms );
107
105
108
- const uint32_t averaging_period = this ->averaging_filter ->time_constant * 1000 ;
106
+ /* Apply averaging */
107
+ float32_t db_spl = this ->averaging_filter ->process (db_spl_raw);
109
108
110
- if (hal::system::clock::is_elapsed (this ->averaging_time_point , std::chrono::milliseconds (averaging_period)))
111
- {
112
- this ->averaging_time_point = hal::system::clock::now ();
109
+ const uint32_t averaging_period = this ->averaging_filter ->time_constant * 1000 ;
113
110
114
- this ->spl_data .spl_max = std::max (db_spl, this ->spl_data .spl_max );
115
- this ->spl_data .spl_min = std::min (db_spl, this ->spl_data .spl_min );
116
- this ->spl_data .spl = db_spl;
111
+ if (hal::system::clock::is_elapsed (this ->averaging_time_point , std::chrono::milliseconds (averaging_period)))
112
+ {
113
+ this ->averaging_time_point = hal::system::clock::now ();
114
+
115
+ this ->spl_data .spl_max = std::max (db_spl, this ->spl_data .spl_max );
116
+ this ->spl_data .spl_min = std::min (db_spl, this ->spl_data .spl_min );
117
+ this ->spl_data .spl = db_spl;
117
118
118
- if (this ->new_spl_data_cb != nullptr )
119
- this ->new_spl_data_cb (this ->spl_data );
120
- }
119
+ if (this ->new_spl_data_cb != nullptr )
120
+ this ->new_spl_data_cb (this ->spl_data );
121
121
}
122
122
}
123
123
@@ -146,16 +146,13 @@ void meter::set_weighting(weighting_t weighting)
146
146
switch (weighting)
147
147
{
148
148
case weighting_t ::a:
149
- delete this ->weighting_filter ;
150
- this ->weighting_filter = new a_weighting ();
149
+ this ->weighting_filter = std::make_unique<spl::a_weighting>();
151
150
break ;
152
151
case weighting_t ::c:
153
- delete this ->weighting_filter ;
154
- this ->weighting_filter = new c_weighting ();
152
+ this ->weighting_filter = std::make_unique<spl::c_weighting>();
155
153
break ;
156
154
case weighting_t ::z:
157
- delete this ->weighting_filter ;
158
- this ->weighting_filter = new z_weighting ();
155
+ this ->weighting_filter = std::make_unique<spl::z_weighting>();
159
156
break ;
160
157
default :
161
158
return ;
@@ -172,12 +169,11 @@ void meter::set_averaging(averaging_t averaging)
172
169
switch (averaging)
173
170
{
174
171
case averaging_t ::fast:
175
- delete this ->averaging_filter ;
176
- this -> averaging_filter = new fast_averaging ( this -> spl_data_period , this -> spl_data . spl );
172
+ this ->averaging_filter = std::make_unique<spl::fast_averaging>( this -> spl_data_period , this -> spl_data . spl ) ;
173
+
177
174
break ;
178
175
case averaging_t ::slow:
179
- delete this ->averaging_filter ;
180
- this ->averaging_filter = new slow_averaging (this ->spl_data_period , this ->spl_data .spl );
176
+ this ->averaging_filter = std::make_unique<spl::slow_averaging>(this ->spl_data_period , this ->spl_data .spl );
181
177
break ;
182
178
default :
183
179
return ;
0 commit comments