1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
/* -*- 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 file copyright 2006 QMUL.
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 "PlaySpeedRangeMapper.h"
#include <iostream>
#include <cmath>
// PlaySpeedRangeMapper maps a position in the range [0,120] on to a
// play speed factor on a logarithmic scale in the range 0.125 ->
// 8. This ensures that the desirable speed factors 0.25, 0.5, 1, 2,
// and 4 are all mapped to exact positions (respectively 20, 40, 60,
// 80, 100).
// Note that the "factor" referred to below is a play speed factor
// (higher = faster, 1.0 = normal speed), the "value" is a percentage
// (higher = faster, 100 = normal speed), and the "position" is an
// integer step on the dial's scale (0-120, 60 = centre).
namespace sv {
PlaySpeedRangeMapper::PlaySpeedRangeMapper() :
m_minpos(0),
m_maxpos(120)
{
}
int
PlaySpeedRangeMapper::getPositionForValue(double value) const
{
// value is percent
double factor = getFactorForValue(value);
int position = getPositionForFactor(factor);
return position;
}
int
PlaySpeedRangeMapper::getPositionForValueUnclamped(double value) const
{
// We don't really provide this
return getPositionForValue(value);
}
double
PlaySpeedRangeMapper::getValueForPosition(int position) const
{
double factor = getFactorForPosition(position);
double pc = getValueForFactor(factor);
return pc;
}
double
PlaySpeedRangeMapper::getValueForPositionUnclamped(int position) const
{
// We don't really provide this
return getValueForPosition(position);
}
double
PlaySpeedRangeMapper::getValueForFactor(double factor) const
{
return factor * 100.0;
}
double
PlaySpeedRangeMapper::getFactorForValue(double value) const
{
return value / 100.0;
}
int
PlaySpeedRangeMapper::getPositionForFactor(double factor) const
{
if (factor == 0) return m_minpos;
int pos = int(lrint((log2(factor) + 3.0) * 20.0));
if (pos < m_minpos) pos = m_minpos;
if (pos > m_maxpos) pos = m_maxpos;
return pos;
}
double
PlaySpeedRangeMapper::getFactorForPosition(int position) const
{
return pow(2.0, double(position) * 0.05 - 3.0);
}
QString
PlaySpeedRangeMapper::getUnit() const
{
return "%";
}
} // end namespace sv
|