Skip to content

Commit

Permalink
findLastSoundInChunk() returns samples.size() if no sample is found
Browse files Browse the repository at this point in the history
  • Loading branch information
daschuer committed Dec 13, 2022
1 parent 96ed941 commit 9f99531
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/analyzer/analyzersilence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ SINT AnalyzerSilence::findFirstSoundInChunk(std::span<const CSAMPLE> samples) {
}

// static
/// returns a std::reverse_iterator
SINT AnalyzerSilence::findLastSoundInChunk(std::span<const CSAMPLE> samples) {
// -1 is required, because the distance from the fist sample index (0) to crend() is 1,
return std::distance(first_sound(samples.rbegin(), samples.rend()), samples.rend()) - 1;
SINT ret = std::distance(first_sound(samples.rbegin(), samples.rend()), samples.rend()) - 1;
if (ret == -1) {
ret = samples.size();
}
return ret;
}

// static
Expand All @@ -84,14 +87,13 @@ bool AnalyzerSilence::processSamples(const CSAMPLE* pIn, SINT iLen) {
std::span<const CSAMPLE> samples = mixxx::spanutil::spanFromPtrLen(pIn, iLen);
if (m_iSignalStart < 0) {
const SINT firstSoundSample = findFirstSoundInChunk(samples);
if (firstSoundSample < static_cast<SINT>(samples.size())) {
if (firstSoundSample < iLen) {
m_iSignalStart = m_iFramesProcessed + firstSoundSample / mixxx::kAnalysisChannels;
}
}
if (m_iSignalStart >= 0) {
const SINT lastSoundSample = findLastSoundInChunk(samples);
if (lastSoundSample > -1 && // only silence
lastSoundSample < iLen - 1) { // only sound
if (lastSoundSample < iLen - 1) { // not only sound or silence
m_iSignalEnd = m_iFramesProcessed + lastSoundSample / mixxx::kAnalysisChannels + 1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/analyzersilence.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class AnalyzerSilence : public Analyzer {
void cleanup() override;

/// returns the index of the first sample in the buffer that is above -60 dB
/// or iLen if no sample is found
/// or samples.size() if no sample is found
static SINT findFirstSoundInChunk(std::span<const CSAMPLE> samples);

/// returns the index of the last sample in the buffer that is above -60 dB
/// or -1 if no sample is found.
/// or samples.size() if no sample is found
static SINT findLastSoundInChunk(std::span<const CSAMPLE> samples);

/// Returns true if the first sound if found at the given frame and logs a
Expand Down

0 comments on commit 9f99531

Please sign in to comment.