annotate data/model/PowerOfSqrtTwoZoomConstraint.cpp @ 1513:75d92155fa20 import-audio-data

Add normalisation option
author Chris Cannam
date Fri, 07 Sep 2018 16:13:21 +0100
parents 48e9f538e6e9
children 710e6250a401 a7485c1bdba5
rev   line source
Chris@147 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@147 2
Chris@147 3 /*
Chris@147 4 Sonic Visualiser
Chris@147 5 An audio file viewer and annotation editor.
Chris@147 6 Centre for Digital Music, Queen Mary, University of London.
Chris@147 7 This file copyright 2006 Chris Cannam.
Chris@147 8
Chris@147 9 This program is free software; you can redistribute it and/or
Chris@147 10 modify it under the terms of the GNU General Public License as
Chris@147 11 published by the Free Software Foundation; either version 2 of the
Chris@147 12 License, or (at your option) any later version. See the file
Chris@147 13 COPYING included with this distribution for more information.
Chris@147 14 */
Chris@147 15
Chris@147 16 #include "PowerOfSqrtTwoZoomConstraint.h"
Chris@147 17
Chris@147 18 #include <iostream>
Chris@147 19 #include <cmath>
Chris@147 20
Chris@147 21
Chris@929 22 int
Chris@929 23 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize,
Chris@1429 24 RoundingDirection dir) const
Chris@147 25 {
Chris@147 26 int type, power;
Chris@929 27 int rv = getNearestBlockSize(blockSize, type, power, dir);
Chris@147 28 return rv;
Chris@147 29 }
Chris@147 30
Chris@929 31 int
Chris@929 32 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize,
Chris@1429 33 int &type,
Chris@1429 34 int &power,
Chris@1429 35 RoundingDirection dir) const
Chris@147 36 {
Chris@843 37 // cerr << "given " << blockSize << endl;
Chris@147 38
Chris@929 39 int minCachePower = getMinCachePower();
Chris@147 40
Chris@929 41 if (blockSize < (1 << minCachePower)) {
Chris@1429 42 type = -1;
Chris@1429 43 power = 0;
Chris@1429 44 float val = 1.0, prevVal = 1.0;
Chris@1429 45 while (val + 0.01 < blockSize) {
Chris@1429 46 prevVal = val;
Chris@1429 47 val *= sqrtf(2.f);
Chris@1429 48 }
Chris@1429 49 int rval;
Chris@1429 50 if (dir == RoundUp) rval = int(val + 0.01f);
Chris@1429 51 else if (dir == RoundDown) rval = int(prevVal + 0.01f);
Chris@1429 52 else if (val - float(blockSize) <
Chris@1038 53 float(blockSize) - prevVal) rval = int(val + 0.01f);
Chris@1429 54 else rval = int(prevVal + 0.01);
Chris@1429 55 // SVDEBUG << "returning " << rval << endl;
Chris@1429 56 return rval;
Chris@147 57 }
Chris@147 58
Chris@929 59 int prevBase = (1 << minCachePower);
Chris@929 60 int prevPower = minCachePower;
Chris@929 61 int prevType = 0;
Chris@147 62
Chris@929 63 int result = 0;
Chris@147 64
Chris@147 65 for (unsigned int i = 0; ; ++i) {
Chris@147 66
Chris@1429 67 power = minCachePower + i/2;
Chris@1429 68 type = i % 2;
Chris@147 69
Chris@1429 70 int base;
Chris@1429 71 if (type == 0) {
Chris@1429 72 base = (1 << power);
Chris@1429 73 } else {
Chris@1429 74 base = (((unsigned int)((1 << minCachePower) * sqrt(2.) + 0.01))
Chris@1429 75 << (power - minCachePower));
Chris@1429 76 }
Chris@147 77
Chris@1429 78 // SVDEBUG << "Testing base " << base << endl;
Chris@377 79
Chris@377 80 if (base == blockSize) {
Chris@377 81 result = base;
Chris@377 82 break;
Chris@377 83 }
Chris@377 84
Chris@1429 85 if (base > blockSize) {
Chris@1429 86 if (dir == RoundNearest) {
Chris@1429 87 if (base - blockSize < blockSize - prevBase) {
Chris@1429 88 dir = RoundUp;
Chris@1429 89 } else {
Chris@1429 90 dir = RoundDown;
Chris@1429 91 }
Chris@1429 92 }
Chris@1429 93 if (dir == RoundUp) {
Chris@1429 94 result = base;
Chris@1429 95 break;
Chris@1429 96 } else {
Chris@1429 97 type = prevType;
Chris@1429 98 power = prevPower;
Chris@1429 99 result = prevBase;
Chris@1429 100 break;
Chris@1429 101 }
Chris@1429 102 }
Chris@147 103
Chris@1429 104 prevType = type;
Chris@1429 105 prevPower = power;
Chris@1429 106 prevBase = base;
Chris@147 107 }
Chris@147 108
Chris@147 109 if (result > getMaxZoomLevel()) result = getMaxZoomLevel();
Chris@147 110 return result;
Chris@147 111 }