comparison layer/SpectrogramLayer.cpp @ 482:7f1ed4bfea1e

* throw (but do not yet catch!) exception when running out of disc space in coded audio file * simpler (faster?) cache-to-cache move operation in spectrogram, and some other minor fixes
author Chris Cannam
date Wed, 04 Feb 2009 12:13:16 +0000
parents 74a7729e3653
children 18f38f564d7c
comparison
equal deleted inserted replaced
481:74a7729e3653 482:7f1ed4bfea1e
1811 std::cerr << "xPixelRatio = " << xPixelRatio << std::endl; 1811 std::cerr << "xPixelRatio = " << xPixelRatio << std::endl;
1812 if (xPixelRatio < 1.f) xPixelRatio = 1.f; 1812 if (xPixelRatio < 1.f) xPixelRatio = 1.f;
1813 */ 1813 */
1814 if (cache.validArea.width() > 0) { 1814 if (cache.validArea.width() > 0) {
1815 1815
1816 int cw = cache.image.width();
1817 int ch = cache.image.height();
1818
1816 if (int(cache.zoomLevel) == zoomLevel && 1819 if (int(cache.zoomLevel) == zoomLevel &&
1817 cache.image.width() == v->width() && 1820 cw == v->width() &&
1818 cache.image.height() == v->height()) { 1821 ch == v->height()) {
1819 1822
1820 if (v->getXForFrame(cache.startFrame) == 1823 if (v->getXForFrame(cache.startFrame) ==
1821 v->getXForFrame(startFrame) && 1824 v->getXForFrame(startFrame) &&
1822 cache.validArea.x() <= x0 && 1825 cache.validArea.x() <= x0 &&
1823 cache.validArea.x() + cache.validArea.width() >= x1) { 1826 cache.validArea.x() + cache.validArea.width() >= x1) {
1844 1847
1845 int dx = v->getXForFrame(cache.startFrame) - 1848 int dx = v->getXForFrame(cache.startFrame) -
1846 v->getXForFrame(startFrame); 1849 v->getXForFrame(startFrame);
1847 1850
1848 #ifdef DEBUG_SPECTROGRAM_REPAINT 1851 #ifdef DEBUG_SPECTROGRAM_REPAINT
1849 std::cerr << "SpectrogramLayer: dx = " << dx << " (image cache " << cache.image.width() << "x" << cache.image.height() << ")" << std::endl; 1852 std::cerr << "SpectrogramLayer: dx = " << dx << " (image cache " << cw << "x" << ch << ")" << std::endl;
1850 #endif 1853 #endif
1851 1854
1852 if (dx != 0 && 1855 if (dx != 0 &&
1853 dx > -cache.image.width() && 1856 dx > -cw &&
1854 dx < cache.image.width()) { 1857 dx < cw) {
1855 1858
1856 QImage tmp = cache.image; 1859 int dxp = dx;
1857 QPainter cachePainter(&cache.image); 1860 if (dxp < 0) dxp = -dxp;
1858 if (dx < 0) { 1861 int copy = (cw - dxp) * sizeof(QRgb);
1859 cachePainter.drawImage 1862 for (int y = 0; y < ch; ++y) {
1860 (QRect(0, 0, 1863 QRgb *line = (QRgb *)cache.image.scanLine(y);
1861 cache.image.width() + dx, 1864 if (dx < 0) {
1862 cache.image.height()), 1865 memmove(line, line + dxp, copy);
1863 tmp, 1866 } else {
1864 QRect(-dx, 0, 1867 memmove(line + dxp, line, copy);
1865 cache.image.width() + dx, 1868 }
1866 cache.image.height()));
1867 } else {
1868 cachePainter.drawImage
1869 (QRect(dx, 0,
1870 cache.image.width() - dx,
1871 cache.image.height()),
1872 tmp,
1873 QRect(0, 0,
1874 cache.image.width() - dx,
1875 cache.image.height()));
1876 } 1869 }
1877 1870
1878 int px = cache.validArea.x(); 1871 int px = cache.validArea.x();
1879 int pw = cache.validArea.width(); 1872 int pw = cache.validArea.width();
1880 1873
1881 if (dx < 0) { 1874 if (dx < 0) {
1882 x0 = cache.image.width() + dx; 1875 x0 = cw + dx;
1883 x1 = cache.image.width(); 1876 x1 = cw;
1884 px += dx; 1877 px += dx;
1885 if (px < 0) { 1878 if (px < 0) {
1886 pw += px; 1879 pw += px;
1887 px = 0; 1880 px = 0;
1888 if (pw < 0) pw = 0; 1881 if (pw < 0) pw = 0;
1889 } 1882 }
1890 } else { 1883 } else {
1891 x0 = 0; 1884 x0 = 0;
1892 x1 = dx; 1885 x1 = dx;
1893 px += dx; 1886 px += dx;
1894 if (px + pw > cache.image.width()) { 1887 if (px + pw > cw) {
1895 pw = int(cache.image.width()) - px; 1888 pw = int(cw) - px;
1896 if (pw < 0) pw = 0; 1889 if (pw < 0) pw = 0;
1897 } 1890 }
1898 } 1891 }
1899 1892
1900 cache.validArea = 1893 cache.validArea =
1910 /* 1903 /*
1911 paint.drawImage(rect & cache.validArea, 1904 paint.drawImage(rect & cache.validArea,
1912 cache.image, 1905 cache.image,
1913 rect & cache.validArea); 1906 rect & cache.validArea);
1914 */ 1907 */
1915
1916 } else if (dx != 0) { 1908 } else if (dx != 0) {
1917 1909
1918 // we scrolled too far to be of use 1910 // we scrolled too far to be of use
1919 1911
1920 #ifdef DEBUG_SPECTROGRAM_REPAINT 1912 #ifdef DEBUG_SPECTROGRAM_REPAINT
1930 std::cerr << "SpectrogramLayer: image cache useless" << std::endl; 1922 std::cerr << "SpectrogramLayer: image cache useless" << std::endl;
1931 if (int(cache.zoomLevel) != zoomLevel) { 1923 if (int(cache.zoomLevel) != zoomLevel) {
1932 std::cerr << "(cache zoomLevel " << cache.zoomLevel 1924 std::cerr << "(cache zoomLevel " << cache.zoomLevel
1933 << " != " << zoomLevel << ")" << std::endl; 1925 << " != " << zoomLevel << ")" << std::endl;
1934 } 1926 }
1935 if (cache.image.width() != v->width()) { 1927 if (cw != v->width()) {
1936 std::cerr << "(cache width " << cache.image.width() 1928 std::cerr << "(cache width " << cw
1937 << " != " << v->width(); 1929 << " != " << v->width();
1938 } 1930 }
1939 if (cache.image.height() != v->height()) { 1931 if (ch != v->height()) {
1940 std::cerr << "(cache height " << cache.image.height() 1932 std::cerr << "(cache height " << ch
1941 << " != " << v->height(); 1933 << " != " << v->height();
1942 } 1934 }
1943 #endif 1935 #endif
1944 cache.validArea = QRect(); 1936 cache.validArea = QRect();
1945 // recreateWholeImageCache = true; 1937 // recreateWholeImageCache = true;
2190 2182
2191 int bufwid; 2183 int bufwid;
2192 2184
2193 if (bufferBinResolution) { 2185 if (bufferBinResolution) {
2194 2186
2195 for (int x = x0 - 1; ; --x) { 2187 for (int x = x0; ; --x) {
2196 long f = v->getFrameForX(x); 2188 long f = v->getFrameForX(x);
2197 if ((f / increment) * increment == f) { 2189 if ((f / increment) * increment == f) {
2198 if (leftCropFrame == -1) leftCropFrame = f; 2190 if (leftCropFrame == -1) leftCropFrame = f;
2199 else if (x < x0 - 3) { leftBoundaryFrame = f; break; } 2191 else if (x < x0 - 2) { leftBoundaryFrame = f; break; }
2200 } 2192 }
2201 } 2193 }
2202 for (int x = x0 + w + 1; ; ++x) { 2194 for (int x = x0 + w; ; ++x) {
2203 long f = v->getFrameForX(x); 2195 long f = v->getFrameForX(x);
2204 if ((f / increment) * increment == f) { 2196 if ((f / increment) * increment == f) {
2205 if (rightCropFrame == -1) rightCropFrame = f; 2197 if (rightCropFrame == -1) rightCropFrame = f;
2206 else if (x > x0 + w + 3) { rightBoundaryFrame = f; break; } 2198 else if (x > x0 + w + 2) { rightBoundaryFrame = f; break; }
2207 } 2199 }
2208 } 2200 }
2209 cerr << "Left: crop: " << leftCropFrame << " (bin " << leftCropFrame/increment << "); boundary: " << leftBoundaryFrame << " (bin " << leftBoundaryFrame/increment << ")" << endl; 2201 cerr << "Left: crop: " << leftCropFrame << " (bin " << leftCropFrame/increment << "); boundary: " << leftBoundaryFrame << " (bin " << leftBoundaryFrame/increment << ")" << endl;
2210 cerr << "Right: crop: " << rightCropFrame << " (bin " << rightCropFrame/increment << "); boundary: " << rightBoundaryFrame << " (bin " << rightBoundaryFrame/increment << ")" << endl; 2202 cerr << "Right: crop: " << rightCropFrame << " (bin " << rightCropFrame/increment << "); boundary: " << rightBoundaryFrame << " (bin " << rightBoundaryFrame/increment << ")" << endl;
2211 2203
2220 int binfory[h]; 2212 int binfory[h];
2221 2213
2222 if (bufferBinResolution) { 2214 if (bufferBinResolution) {
2223 for (int x = 0; x < bufwid; ++x) { 2215 for (int x = 0; x < bufwid; ++x) {
2224 binforx[x] = (leftBoundaryFrame / increment) + x; 2216 binforx[x] = (leftBoundaryFrame / increment) + x;
2225 cerr << "binforx[" << x << "] = " << binforx[x] << endl; 2217 // cerr << "binforx[" << x << "] = " << binforx[x] << endl;
2226 } 2218 }
2227 m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8); 2219 m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8);
2228 } else { 2220 } else {
2229 for (int x = 0; x < bufwid; ++x) { 2221 for (int x = 0; x < bufwid; ++x) {
2230 float s0 = 0, s1 = 0; 2222 float s0 = 0, s1 = 0;
2430 int psx = -1; 2422 int psx = -1;
2431 float values[maxbin - minbin + 1]; 2423 float values[maxbin - minbin + 1];
2432 2424
2433 for (int x = 0; x < w; ++x) { 2425 for (int x = 0; x < w; ++x) {
2434 2426
2427 if (binforx[x] < 0) continue;
2428
2435 for (int y = 0; y < h; ++y) { 2429 for (int y = 0; y < h; ++y) {
2436 2430
2437 unsigned char peakpix = 0; 2431 unsigned char peakpix = 0;
2438 2432
2439 int sx0 = binforx[x]; 2433 int sx0 = binforx[x];