view layer/ScrollableMagRangeCache.cpp @ 1127:9fb8dfd7ce4c spectrogram-minor-refactor

Fix threshold in spectrogram -- it wasn't working in the last release. There is a new protocol for this. Formerly the threshold parameter had a range from -50dB to 0 with the default at -50, and -50 treated internally as "no threshold". However, there was a hardcoded, hidden internal threshold for spectrogram colour mapping at -80dB with anything below this being rounded to zero. Now the threshold parameter has range -81 to -1 with the default at -80, -81 is treated internally as "no threshold", and there is no hidden internal threshold. So the default behaviour is the same as before, an effective -80dB threshold, but it is now possible to change this in both directions. Sessions reloaded from prior versions may look slightly different because, if the session says there should be no threshold, there will now actually be no threshold instead of having the hidden internal one. Still need to do something in the UI to make it apparent that the -81dB setting removes the threshold entirely. This is at least no worse than the previous, also obscured, magic -50dB setting.
author Chris Cannam
date Mon, 01 Aug 2016 16:21:01 +0100
parents 94370157b265
children 745a868f0abf
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "ScrollableMagRangeCache.h"

#include <iostream>
using namespace std;

#define DEBUG_SCROLLABLE_MAG_RANGE_CACHE 1

void
ScrollableMagRangeCache::scrollTo(const LayerGeometryProvider *v,
				  sv_frame_t newStartFrame)
{	
    int dx = (v->getXForFrame(m_startFrame) -
	      v->getXForFrame(newStartFrame));

#ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE
    cerr << "ScrollableMagRangeCache::scrollTo: start frame " << m_startFrame
	 << " -> " << newStartFrame << ", dx = " << dx << endl;
#endif

    if (m_startFrame == newStartFrame) {
	// haven't moved
        return;
    }
    
    m_startFrame = newStartFrame;

    if (dx == 0) {
	// haven't moved visibly (even though start frame may have changed)
	return;
    }
	
    int w = int(m_ranges.size());

    if (dx <= -w || dx >= w) {
	// scrolled entirely off
	invalidate();
	return;
    }
	
    // dx is in range, cache is scrollable

    if (dx < 0) {
	// The new start frame is to the left of the old start
	// frame. We need to add some empty ranges at the left (start)
	// end and clip the right end. Assemble -dx new values, then
	// w+dx old values starting at index 0.

	auto newRanges = vector<MagnitudeRange>(-dx);
	newRanges.insert(newRanges.end(),
			 m_ranges.begin(), m_ranges.begin() + (w + dx));
	m_ranges = newRanges;
	
    } else {
	// The new start frame is to the right of the old start
	// frame. We want to clip the left (start) end and add some
	// empty ranges at the right end. Assemble w-dx old values
	// starting at index dx, then dx new values.

	auto newRanges = vector<MagnitudeRange>(dx);
	newRanges.insert(newRanges.begin(),
			 m_ranges.begin() + dx, m_ranges.end());
	m_ranges = newRanges;
    }

    cerr << "maxes now: ";
    for (int i = 0; in_range_for(m_ranges, i); ++i) {
	cerr << m_ranges[i].getMax() << " ";
    }
    cerr << endl;
}

MagnitudeRange
ScrollableMagRangeCache::getRange(int x, int count) const
{
    MagnitudeRange r;
#ifdef DEBUG_SCROLLABLE_MAG_RANGE_CACHE
    cerr << "ScrollableMagRangeCache::getRange(" << x << ", " << count << ")" << endl;
#endif
    for (int i = 0; i < count; ++i) {
	r.sample(m_ranges.at(x + i));
    }
    return r;
}

void
ScrollableMagRangeCache::sampleColumn(int column, const MagnitudeRange &r)
{
    if (!in_range_for(m_ranges, column)) {
	cerr << "ERROR: ScrollableMagRangeCache::sampleColumn: column " << column
	     << " is out of range for cache of width " << m_ranges.size()
	     << " (with start frame " << m_startFrame << ")" << endl;
	throw logic_error("column out of range");
    } else {
	m_ranges[column].sample(r);
    }
}

//!!! unneeded?
void
ScrollableMagRangeCache::sampleColumn(const LayerGeometryProvider *v,
				      sv_frame_t frame,
				      const MagnitudeRange &r)
{
    int x = (v->getXForFrame(frame) -
	     v->getXForFrame(m_startFrame));

    if (!in_range_for(m_ranges, x)) {
	cerr << "WARNING: ScrollableMagRangeCache::sampleColumn: column " << x
	     << " arising from frame " << frame << " is out of range for cache "
	     << "of width " << m_ranges.size()
	     << " (with start frame " << m_startFrame << ")" << endl;
	throw logic_error("column out of range");
    } else {
	sampleColumn(x, r);
    }
}