Mercurial > hg > svgui
comparison layer/SpectrogramLayer.cpp @ 1031:55ac6ac1982e spectrogram-minor-refactor
Further fixes to the scrollable cache logic
author | Chris Cannam |
---|---|
date | Fri, 29 Jan 2016 18:51:05 +0000 |
parents | 0be17aafa935 |
children | 6f7a471826d1 |
comparison
equal
deleted
inserted
replaced
1030:0be17aafa935 | 1031:55ac6ac1982e |
---|---|
1672 } | 1672 } |
1673 } | 1673 } |
1674 | 1674 |
1675 if (cache.getZoomLevel() != zoomLevel || | 1675 if (cache.getZoomLevel() != zoomLevel || |
1676 cache.getSize() != v->getPaintSize()) { | 1676 cache.getSize() != v->getPaintSize()) { |
1677 #ifdef DEBUG_SPECTROGRAM_REPAINT | |
1678 cerr << "SpectrogramLayer: resizing image cache from " | |
1679 << cache.getSize().width() << "x" << cache.getSize().height() | |
1680 << " to " | |
1681 << v->getPaintSize().width() << "x" << v->getPaintSize().height() | |
1682 << " and updating zoom level from " << cache.getZoomLevel() | |
1683 << " to " << zoomLevel | |
1684 << endl; | |
1685 #endif | |
1677 cache.resize(v->getPaintSize()); | 1686 cache.resize(v->getPaintSize()); |
1687 cache.setZoomLevel(zoomLevel); | |
1688 cache.setStartFrame(startFrame); | |
1678 } | 1689 } |
1679 | 1690 |
1680 if (cache.isValid()) { | 1691 if (cache.isValid()) { |
1681 | 1692 |
1682 if (v->getXForFrame(cache.getStartFrame()) == | 1693 if (v->getXForFrame(cache.getStartFrame()) == |
1704 #endif | 1715 #endif |
1705 | 1716 |
1706 cache.scrollTo(startFrame); | 1717 cache.scrollTo(startFrame); |
1707 | 1718 |
1708 #ifdef DEBUG_SPECTROGRAM_REPAINT | 1719 #ifdef DEBUG_SPECTROGRAM_REPAINT |
1709 cerr << "SpectrogramLayer: cache valid now from " | 1720 cerr << "SpectrogramLayer: after scrolling, cache valid from " |
1710 << cache.getValidLeft() << " width " << cache.getValidWidth() | 1721 << cache.getValidLeft() << " width " << cache.getValidWidth() |
1711 << endl; | 1722 << endl; |
1712 #endif | 1723 #endif |
1713 } | 1724 } |
1714 } | 1725 } |
1733 // the view -- if we lose the partly-built cache every time | 1744 // the view -- if we lose the partly-built cache every time |
1734 // the user does that, we'll never finish building it. | 1745 // the user does that, we'll never finish building it. |
1735 int left = x0; | 1746 int left = x0; |
1736 int width = x1 - x0; | 1747 int width = x1 - x0; |
1737 bool isLeftOfValidArea = false; | 1748 bool isLeftOfValidArea = false; |
1738 cache.resizeToTouchValidArea(left, width, isLeftOfValidArea); | 1749 cache.adjustToTouchValidArea(left, width, isLeftOfValidArea); |
1739 x0 = left; | 1750 x0 = left; |
1740 x1 = x0 + width; | 1751 x1 = x0 + width; |
1741 | 1752 |
1742 // That call also told us whether we should be painting | 1753 // That call also told us whether we should be painting |
1743 // sub-regions of our target region in right-to-left order in | 1754 // sub-regions of our target region in right-to-left order in |
1893 binforx[x] = int(s0 + 0.0001); | 1904 binforx[x] = int(s0 + 0.0001); |
1894 } else { | 1905 } else { |
1895 binforx[x] = -1; //??? | 1906 binforx[x] = -1; //??? |
1896 } | 1907 } |
1897 } | 1908 } |
1898 if (m_drawBuffer.width() < bufwid || m_drawBuffer.height() < h) { | 1909 if (m_drawBuffer.width() < bufwid || m_drawBuffer.height() != h) { |
1899 m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8); | 1910 m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8); |
1900 } | 1911 } |
1901 usePeaksCache = (increment * 8) < zoomLevel; | 1912 usePeaksCache = (increment * 8) < zoomLevel; |
1902 if (m_colourScale == PhaseColourScale) usePeaksCache = false; | 1913 if (m_colourScale == PhaseColourScale) usePeaksCache = false; |
1903 } | 1914 } |
1937 overallMag, overallMagChanged, | 1948 overallMag, overallMagChanged, |
1938 rightToLeft); | 1949 rightToLeft); |
1939 } | 1950 } |
1940 | 1951 |
1941 int failedToRepaint = bufwid - attainedBufwid; | 1952 int failedToRepaint = bufwid - attainedBufwid; |
1953 | |
1954 int paintedLeft = x0; | |
1955 int paintedWidth = x1 - x0; | |
1956 | |
1942 if (failedToRepaint > 0) { | 1957 if (failedToRepaint > 0) { |
1958 | |
1943 #ifdef DEBUG_SPECTROGRAM_REPAINT | 1959 #ifdef DEBUG_SPECTROGRAM_REPAINT |
1944 cerr << "SpectrogramLayer::paint(): Failed to repaint " << failedToRepaint << " of " << bufwid | 1960 cerr << "SpectrogramLayer::paint(): Failed to repaint " << failedToRepaint << " of " << bufwid |
1945 << " columns in time (so managed to repaint " << bufwid - failedToRepaint << ")" << endl; | 1961 << " columns in time (so managed to repaint " << bufwid - failedToRepaint << ")" << endl; |
1946 #endif | 1962 #endif |
1963 | |
1964 if (rightToLeft) { | |
1965 paintedLeft += failedToRepaint; | |
1966 } | |
1967 | |
1968 paintedWidth -= failedToRepaint; | |
1969 | |
1970 if (paintedWidth < 0) { | |
1971 paintedWidth = 0; | |
1972 } | |
1973 | |
1947 } else if (failedToRepaint < 0) { | 1974 } else if (failedToRepaint < 0) { |
1948 cerr << "WARNING: failedToRepaint < 0 (= " << failedToRepaint << ")" | 1975 cerr << "WARNING: failedToRepaint < 0 (= " << failedToRepaint << ")" |
1949 << endl; | 1976 << endl; |
1950 failedToRepaint = 0; | 1977 failedToRepaint = 0; |
1951 } | 1978 } |
1952 | 1979 |
1953 if (overallMagChanged) { | 1980 if (overallMagChanged) { |
1954 m_viewMags[v->getId()] = overallMag; | 1981 m_viewMags[v->getId()] = overallMag; |
1955 #ifdef DEBUG_SPECTROGRAM_REPAINT | 1982 #ifdef DEBUG_SPECTROGRAM_REPAINT |
1956 cerr << "SpectrogramLayer: Overall mag is now [" << m_viewMags[v->getId()].getMin() << "->" << m_viewMags[v->getId()].getMax() << "] - will be updating" << endl; | 1983 cerr << "SpectrogramLayer: Overall mag is now [" << m_viewMags[v->getId()].getMin() << "->" << m_viewMags[v->getId()].getMax() << "] - will be updating" << endl; |
1957 #endif | 1984 #endif |
1959 | 1986 |
1960 outerprof.end(); | 1987 outerprof.end(); |
1961 | 1988 |
1962 Profiler profiler2("SpectrogramLayer::paint: draw image"); | 1989 Profiler profiler2("SpectrogramLayer::paint: draw image"); |
1963 | 1990 |
1964 if (repaintWidth > 0) { | 1991 if (paintedWidth > 0) { |
1965 | 1992 |
1966 #ifdef DEBUG_SPECTROGRAM_REPAINT | 1993 #ifdef DEBUG_SPECTROGRAM_REPAINT |
1967 cerr << "SpectrogramLayer: Copying " << repaintWidth << "x" << h | 1994 cerr << "SpectrogramLayer: Copying " << paintedWidth << "x" << h |
1968 << " from draw buffer at " << 0 << "," << 0 | 1995 << " from draw buffer at " << paintedLeft - x0 << "," << 0 |
1969 << " to " << repaintWidth << "x" << h << " on cache at " | 1996 << " to " << paintedWidth << "x" << h << " on cache at " |
1970 << x0 << "," << 0 << endl; | 1997 << x0 << "," << 0 << endl; |
1971 #endif | 1998 #endif |
1972 | 1999 |
1973 if (bufferBinResolution) { | 2000 if (bufferBinResolution) { |
2001 | |
1974 int scaledLeft = v->getXForFrame(leftBoundaryFrame); | 2002 int scaledLeft = v->getXForFrame(leftBoundaryFrame); |
1975 int scaledRight = v->getXForFrame(rightBoundaryFrame); | 2003 int scaledRight = v->getXForFrame(rightBoundaryFrame); |
2004 | |
1976 #ifdef DEBUG_SPECTROGRAM_REPAINT | 2005 #ifdef DEBUG_SPECTROGRAM_REPAINT |
1977 cerr << "SpectrogramLayer: Rescaling image from " << bufwid | 2006 cerr << "SpectrogramLayer: Rescaling image from " << bufwid |
1978 << "x" << h << " to " | 2007 << "x" << h << " to " |
1979 << scaledRight-scaledLeft << "x" << h << endl; | 2008 << scaledRight-scaledLeft << "x" << h << endl; |
1980 #endif | 2009 #endif |
2010 | |
1981 Preferences::SpectrogramXSmoothing xsmoothing = | 2011 Preferences::SpectrogramXSmoothing xsmoothing = |
1982 Preferences::getInstance()->getSpectrogramXSmoothing(); | 2012 Preferences::getInstance()->getSpectrogramXSmoothing(); |
1983 | 2013 |
1984 QImage scaled = m_drawBuffer.scaled | 2014 QImage scaled = m_drawBuffer.scaled |
1985 (scaledRight - scaledLeft, h, | 2015 (scaledRight - scaledLeft, h, |
1987 ((xsmoothing == Preferences::SpectrogramXInterpolated) ? | 2017 ((xsmoothing == Preferences::SpectrogramXInterpolated) ? |
1988 Qt::SmoothTransformation : Qt::FastTransformation)); | 2018 Qt::SmoothTransformation : Qt::FastTransformation)); |
1989 | 2019 |
1990 int scaledLeftCrop = v->getXForFrame(leftCropFrame); | 2020 int scaledLeftCrop = v->getXForFrame(leftCropFrame); |
1991 int scaledRightCrop = v->getXForFrame(rightCropFrame); | 2021 int scaledRightCrop = v->getXForFrame(rightCropFrame); |
2022 | |
1992 #ifdef DEBUG_SPECTROGRAM_REPAINT | 2023 #ifdef DEBUG_SPECTROGRAM_REPAINT |
1993 cerr << "SpectrogramLayer: Drawing image region of width " << scaledRightCrop - scaledLeftCrop << " to " | 2024 cerr << "SpectrogramLayer: Drawing image region of width " << scaledRightCrop - scaledLeftCrop << " to " |
1994 << scaledLeftCrop << " from " << scaledLeftCrop - scaledLeft << endl; | 2025 << scaledLeftCrop << " from " << scaledLeftCrop - scaledLeft << endl; |
1995 #endif | 2026 #endif |
1996 | 2027 |
2028 //!!! Update this for failedToRepaint logic | |
2029 | |
1997 cache.drawImage | 2030 cache.drawImage |
1998 (scaledLeftCrop, | 2031 (scaledLeftCrop, |
1999 scaledRightCrop - scaledLeftCrop, | 2032 scaledRightCrop - scaledLeftCrop, |
2000 scaled, | 2033 scaled, |
2001 scaledLeftCrop - scaledLeft, | 2034 scaledLeftCrop - scaledLeft, |
2002 scaledRightCrop - scaledLeftCrop); | 2035 scaledRightCrop - scaledLeftCrop); |
2003 | 2036 |
2004 } else { | 2037 } else { |
2005 | 2038 |
2006 cache.drawImage(x0, repaintWidth, | 2039 cache.drawImage(paintedLeft, paintedWidth, |
2007 m_drawBuffer, | 2040 m_drawBuffer, |
2008 0, repaintWidth); | 2041 paintedLeft - x0, paintedWidth); |
2009 } | 2042 } |
2010 } | 2043 } |
2011 | 2044 |
2012 #ifdef DEBUG_SPECTROGRAM_REPAINT | 2045 #ifdef DEBUG_SPECTROGRAM_REPAINT |
2013 cerr << "SpectrogramLayer: Cache valid area now from " << cache.getValidLeft() | 2046 cerr << "SpectrogramLayer: Cache valid area now from " << cache.getValidLeft() |
2027 pr.x(), pr.y(), pr.width(), pr.height()); | 2060 pr.x(), pr.y(), pr.width(), pr.height()); |
2028 | 2061 |
2029 if (!m_synchronous) { | 2062 if (!m_synchronous) { |
2030 | 2063 |
2031 if ((m_normalization != NormalizeVisibleArea) || !overallMagChanged) { | 2064 if ((m_normalization != NormalizeVisibleArea) || !overallMagChanged) { |
2032 | 2065 |
2033 if (cache.getValidLeft() > 0) { | 2066 QRect areaLeft(0, 0, cache.getValidLeft(), h); |
2034 #ifdef DEBUG_SPECTROGRAM_REPAINT | 2067 QRect areaRight(cache.getValidRight(), 0, |
2035 cerr << "SpectrogramLayer::paint() updating left (0, " | 2068 cache.getSize().width() - cache.getValidRight(), h); |
2036 << cache.getValidLeft() << ")" << endl; | 2069 |
2037 #endif | 2070 bool haveSpaceLeft = (areaLeft.width() > 0); |
2038 v->updatePaintRect(QRect(0, 0, cache.getValidLeft(), h)); | 2071 bool haveSpaceRight = (areaRight.width() > 0); |
2072 | |
2073 bool updateLeft = haveSpaceLeft; | |
2074 bool updateRight = haveSpaceRight; | |
2075 | |
2076 if (updateLeft && updateRight) { | |
2077 if (rightToLeft) { | |
2078 // we just did something adjoining the cache on | |
2079 // its left side, so now do something on its right | |
2080 updateLeft = false; | |
2081 } else { | |
2082 updateRight = false; | |
2083 } | |
2039 } | 2084 } |
2040 | 2085 |
2041 if (cache.getValidRight() < | 2086 if (updateLeft) { |
2042 cache.getSize().width()) { | 2087 #ifdef DEBUG_SPECTROGRAM_REPAINT |
2088 cerr << "SpectrogramLayer::paint() updating left (" | |
2089 << areaLeft.x() << ", " | |
2090 << areaLeft.width() << ")" << endl; | |
2091 #endif | |
2092 v->updatePaintRect(areaLeft); | |
2093 } | |
2094 | |
2095 if (updateRight) { | |
2043 #ifdef DEBUG_SPECTROGRAM_REPAINT | 2096 #ifdef DEBUG_SPECTROGRAM_REPAINT |
2044 cerr << "SpectrogramLayer::paint() updating right (" | 2097 cerr << "SpectrogramLayer::paint() updating right (" |
2045 << cache.getValidRight() | 2098 << areaRight.x() << ", " |
2046 << ", " | 2099 << areaRight.width() << ")" << endl; |
2047 << cache.getSize().width() - cache.getValidRight() | 2100 #endif |
2048 << ")" << endl; | 2101 v->updatePaintRect(areaRight); |
2049 #endif | |
2050 v->updatePaintRect | |
2051 (QRect(cache.getValidRight(), | |
2052 0, | |
2053 cache.getSize().width() - cache.getValidRight(), | |
2054 h)); | |
2055 } | 2102 } |
2103 | |
2056 } else { | 2104 } else { |
2057 // overallMagChanged | 2105 // overallMagChanged |
2058 cerr << "\noverallMagChanged - updating all\n" << endl; | 2106 cerr << "\noverallMagChanged - updating all\n" << endl; |
2059 cache.invalidate(); | 2107 cache.invalidate(); |
2060 v->updatePaintRect(v->getPaintRect()); | 2108 v->updatePaintRect(v->getPaintRect()); |
2207 auto t = chrono::steady_clock::now(); | 2255 auto t = chrono::steady_clock::now(); |
2208 double diff = chrono::duration<double>(t - startTime).count(); | 2256 double diff = chrono::duration<double>(t - startTime).count(); |
2209 if (diff > maxTime) { | 2257 if (diff > maxTime) { |
2210 #ifdef DEBUG_SPECTROGRAM_REPAINT | 2258 #ifdef DEBUG_SPECTROGRAM_REPAINT |
2211 cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: Max time " << maxTime << " sec exceeded after " | 2259 cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: Max time " << maxTime << " sec exceeded after " |
2212 << x << " columns with time " << diff << endl; | 2260 << columnCount << " columns with time " << diff << endl; |
2213 #endif | 2261 #endif |
2214 return columnCount; | 2262 return columnCount; |
2215 } | 2263 } |
2216 } | 2264 } |
2217 } | 2265 } |
2476 auto t = chrono::steady_clock::now(); | 2524 auto t = chrono::steady_clock::now(); |
2477 double diff = chrono::duration<double>(t - startTime).count(); | 2525 double diff = chrono::duration<double>(t - startTime).count(); |
2478 if (diff > maxTime) { | 2526 if (diff > maxTime) { |
2479 #ifdef DEBUG_SPECTROGRAM_REPAINT | 2527 #ifdef DEBUG_SPECTROGRAM_REPAINT |
2480 cerr << "SpectrogramLayer::paintDrawBuffer: Max time " << maxTime << " sec exceeded after " | 2528 cerr << "SpectrogramLayer::paintDrawBuffer: Max time " << maxTime << " sec exceeded after " |
2481 << x << " columns with time " << diff << endl; | 2529 << columnCount << " columns with time " << diff << endl; |
2482 #endif | 2530 #endif |
2483 return columnCount; | 2531 return columnCount; |
2484 } | 2532 } |
2485 } | 2533 } |
2486 } | 2534 } |