+/** Find areas of `silence' within a region.
+ *
+ * @param threshold Threshold below which signal is considered silence (as a sample value)
+ * @param min_length Minimum length of silent period to be reported.
+ * @return Silent periods; first of pair is the offset within the region, second is the length of the period
+ */
+
+std::list<std::pair<frameoffset_t, framecnt_t> >
+AudioRegion::find_silence (Sample threshold, framecnt_t min_length, InterThreadInfo& itt) const
+{
+ framecnt_t const block_size = 64 * 1024;
+ Sample loudest[block_size];
+ Sample buf[block_size];
+
+ framepos_t pos = _start;
+ framepos_t const end = _start + _length - 1;
+
+ std::list<std::pair<frameoffset_t, framecnt_t> > silent_periods;
+
+ bool in_silence = false;
+ frameoffset_t silence_start = 0;
+ bool silence;
+
+ while (pos < end && !itt.cancel) {
+
+ /* fill `loudest' with the loudest absolute sample at each instant, across all channels */
+ memset (loudest, 0, sizeof (Sample) * block_size);
+ for (uint32_t n = 0; n < n_channels(); ++n) {
+
+ read_raw_internal (buf, pos, block_size, n);
+ for (framecnt_t i = 0; i < block_size; ++i) {
+ loudest[i] = max (loudest[i], abs (buf[i]));
+ }
+ }
+
+ /* now look for silence */
+ for (framecnt_t i = 0; i < block_size; ++i) {
+ silence = abs (loudest[i]) < threshold;
+ if (silence && !in_silence) {
+ /* non-silence to silence */
+ in_silence = true;
+ silence_start = pos + i;
+ } else if (!silence && in_silence) {
+ /* silence to non-silence */
+ in_silence = false;
+ if (pos + i - 1 - silence_start >= min_length) {
+ silent_periods.push_back (std::make_pair (silence_start, pos + i - 1));
+ }
+ }
+ }
+
+ pos += block_size;
+ itt.progress = (end-pos)/(double)_length;
+ }
+
+ if (in_silence && end - 1 - silence_start >= min_length) {
+ /* last block was silent, so finish off the last period */
+ silent_periods.push_back (std::make_pair (silence_start, end));
+ }
+
+ itt.done = true;
+
+ return silent_periods;
+}
+
+
+