+float
+AudioPlot::y_for_linear (float p, Metrics const & metrics) const
+{
+ if (p < 1e-4) {
+ p = 1e-4;
+ }
+
+ return metrics.height - (20 * log10(p) - _minimum) * metrics.y_scale - metrics.y_origin;
+}
+
+void
+AudioPlot::plot_peak (wxGraphicsPath& path, int channel, Metrics const & metrics) const
+{
+ if (_analysis->points (channel) == 0) {
+ return;
+ }
+
+ path.MoveToPoint (metrics.db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::PEAK], metrics));
+
+ float peak = 0;
+ int const N = _analysis->points(channel);
+ for (int i = 0; i < N; ++i) {
+ float const p = _analysis->get_point(channel, i)[AudioPoint::PEAK];
+ peak -= 0.01f * (1 - log10 (_smoothing) / log10 (max_smoothing));
+ if (p > peak) {
+ peak = p;
+ } else if (peak < 0) {
+ peak = 0;
+ }
+
+ path.AddLineToPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (peak, metrics));
+ }
+}
+
+void
+AudioPlot::plot_rms (wxGraphicsPath& path, int channel, Metrics const & metrics) const
+{
+ if (_analysis->points (channel) == 0) {
+ return;
+ }
+
+ path.MoveToPoint (metrics.db_label_width, y_for_linear (_analysis->get_point(channel, 0)[AudioPoint::RMS], metrics));
+
+ list<float> smoothing;
+
+ int const N = _analysis->points(channel);
+
+ float const first = _analysis->get_point(channel, 0)[AudioPoint::RMS];
+ float const last = _analysis->get_point(channel, N - 1)[AudioPoint::RMS];
+
+ int const before = _smoothing / 2;
+ int const after = _smoothing - before;
+
+ /* Pre-load the smoothing list */
+ for (int i = 0; i < before; ++i) {
+ smoothing.push_back (first);
+ }
+ for (int i = 0; i < after; ++i) {
+ if (i < N) {
+ smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
+ } else {
+ smoothing.push_back (last);
+ }
+ }
+
+ for (int i = 0; i < N; ++i) {
+
+ int const next_for_window = i + after;
+
+ if (next_for_window < N) {
+ smoothing.push_back (_analysis->get_point(channel, i)[AudioPoint::RMS]);
+ } else {
+ smoothing.push_back (last);
+ }
+
+ smoothing.pop_front ();
+
+ float p = 0;
+ for (list<float>::const_iterator j = smoothing.begin(); j != smoothing.end(); ++j) {
+ p += pow (*j, 2);
+ }
+
+ if (!smoothing.empty ()) {
+ p = sqrt (p / smoothing.size ());
+ }
+
+ path.AddLineToPoint (metrics.db_label_width + i * metrics.x_scale, y_for_linear (p, metrics));
+ }
+}
+