Chris@43: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@43: Chris@43: /* Chris@43: Sonic Visualiser Chris@43: An audio file viewer and annotation editor. Chris@43: Centre for Digital Music, Queen Mary, University of London. Chris@43: This file copyright 2006 QMUL. Chris@43: Chris@43: This program is free software; you can redistribute it and/or Chris@43: modify it under the terms of the GNU General Public License as Chris@43: published by the Free Software Foundation; either version 2 of the Chris@43: License, or (at your option) any later version. See the file Chris@43: COPYING included with this distribution for more information. Chris@43: */ Chris@43: Chris@43: #include "PlaySpeedRangeMapper.h" Chris@43: Chris@43: #include Chris@43: #include Chris@43: Chris@466: // PlaySpeedRangeMapper maps a position in the range [0,120] on to a Chris@466: // play speed factor on a logarithmic scale in the range 0.125 -> Chris@466: // 8. This ensures that the desirable speed factors 0.25, 0.5, 1, 2, Chris@466: // and 4 are all mapped to exact positions (respectively 20, 40, 60, Chris@466: // 80, 100). Chris@466: Chris@466: // Note that the "factor" referred to below is a play speed factor Chris@466: // (higher = faster, 1.0 = normal speed), the "value" is a percentage Chris@466: // (higher = faster, 100 = normal speed), and the "position" is an Chris@466: // integer step on the dial's scale (0-120, 60 = centre). Chris@466: Chris@466: PlaySpeedRangeMapper::PlaySpeedRangeMapper() : Chris@466: m_minpos(0), Chris@466: m_maxpos(120) Chris@43: { Chris@43: } Chris@43: Chris@43: int Chris@434: PlaySpeedRangeMapper::getPositionForValue(double value) const Chris@43: { Chris@43: // value is percent Chris@434: double factor = getFactorForValue(value); Chris@43: int position = getPositionForFactor(factor); Chris@43: return position; Chris@43: } Chris@43: Chris@43: int Chris@434: PlaySpeedRangeMapper::getPositionForValueUnclamped(double value) const Chris@330: { Chris@330: // We don't really provide this Chris@330: return getPositionForValue(value); Chris@330: } Chris@330: Chris@434: double Chris@43: PlaySpeedRangeMapper::getValueForPosition(int position) const Chris@43: { Chris@434: double factor = getFactorForPosition(position); Chris@434: double pc = getValueForFactor(factor); Chris@43: return pc; Chris@43: } Chris@43: Chris@434: double Chris@330: PlaySpeedRangeMapper::getValueForPositionUnclamped(int position) const Chris@330: { Chris@330: // We don't really provide this Chris@330: return getValueForPosition(position); Chris@330: } Chris@330: Chris@434: double Chris@434: PlaySpeedRangeMapper::getValueForFactor(double factor) const Chris@43: { Chris@466: return factor * 100.0; Chris@43: } Chris@43: Chris@434: double Chris@434: PlaySpeedRangeMapper::getFactorForValue(double value) const Chris@43: { Chris@466: return value / 100.0; Chris@466: } Chris@43: Chris@466: int Chris@466: PlaySpeedRangeMapper::getPositionForFactor(double factor) const Chris@466: { Chris@466: if (factor == 0) return m_minpos; Chris@466: int pos = int(lrint((log2(factor) + 3.0) * 20.0)); Chris@466: if (pos < m_minpos) pos = m_minpos; Chris@466: if (pos > m_maxpos) pos = m_maxpos; Chris@466: return pos; Chris@43: } Chris@43: Chris@434: double Chris@43: PlaySpeedRangeMapper::getFactorForPosition(int position) const Chris@43: { Chris@466: return pow(2.0, double(position) * 0.05 - 3.0); Chris@43: } Chris@43: Chris@43: QString Chris@43: PlaySpeedRangeMapper::getUnit() const Chris@43: { Chris@43: return "%"; Chris@43: }