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@43: PlaySpeedRangeMapper::PlaySpeedRangeMapper(int minpos, int maxpos) : Chris@43: m_minpos(minpos), Chris@43: m_maxpos(maxpos) Chris@43: { Chris@43: } Chris@43: Chris@43: int Chris@43: PlaySpeedRangeMapper::getPositionForValue(float value) const Chris@43: { Chris@43: // value is percent Chris@43: float factor = getFactorForValue(value); Chris@43: int position = getPositionForFactor(factor); Chris@43: return position; Chris@43: } Chris@43: Chris@43: int Chris@43: PlaySpeedRangeMapper::getPositionForFactor(float factor) const Chris@43: { Chris@43: bool slow = (factor > 1.0); Chris@43: Chris@43: if (!slow) factor = 1.0 / factor; Chris@43: Chris@43: int half = (m_maxpos + m_minpos) / 2; Chris@43: Chris@43: factor = sqrtf((factor - 1.0) * 1000.f); Chris@43: int position = lrintf(((factor * (half - m_minpos)) / 100.0) + m_minpos); Chris@43: Chris@43: if (slow) { Chris@43: position = half - position; Chris@43: } else { Chris@43: position = position + half; Chris@43: } Chris@43: Chris@43: // std::cerr << "value = " << value << " slow = " << slow << " factor = " << factor << " position = " << position << std::endl; Chris@43: Chris@43: return position; Chris@43: } Chris@43: Chris@43: float Chris@43: PlaySpeedRangeMapper::getValueForPosition(int position) const Chris@43: { Chris@43: float factor = getFactorForPosition(position); Chris@43: float pc = getValueForFactor(factor); Chris@43: return pc; Chris@43: } Chris@43: Chris@43: float Chris@43: PlaySpeedRangeMapper::getValueForFactor(float factor) const Chris@43: { Chris@43: float pc; Chris@43: if (factor < 1.0) pc = ((1.0 / factor) - 1.0) * 100.0; Chris@43: else pc = (1.0 - factor) * 100.0; Chris@43: // std::cerr << "position = " << position << " percent = " << pc << std::endl; Chris@43: return pc; Chris@43: } Chris@43: Chris@43: float Chris@43: PlaySpeedRangeMapper::getFactorForValue(float value) const Chris@43: { Chris@43: // value is percent Chris@43: Chris@43: float factor; Chris@43: Chris@43: if (value <= 0) { Chris@43: factor = 1.0 - (value / 100.0); Chris@43: } else { Chris@43: factor = 1.0 / (1.0 + (value / 100.0)); Chris@43: } Chris@43: Chris@43: // std::cerr << "value = " << value << " factor = " << factor << std::endl; Chris@43: return factor; Chris@43: } Chris@43: Chris@43: float Chris@43: PlaySpeedRangeMapper::getFactorForPosition(int position) const Chris@43: { Chris@43: bool slow = false; Chris@43: Chris@43: if (position < m_minpos) position = m_minpos; Chris@43: if (position > m_maxpos) position = m_maxpos; Chris@43: Chris@43: int half = (m_maxpos + m_minpos) / 2; Chris@43: Chris@43: if (position < half) { Chris@43: slow = true; Chris@43: position = half - position; Chris@43: } else { Chris@43: position = position - half; Chris@43: } Chris@43: Chris@43: // position is between min and half (inclusive) Chris@43: Chris@43: float factor; Chris@43: Chris@43: if (position == m_minpos) { Chris@43: factor = 1.0; Chris@43: } else { Chris@43: factor = ((position - m_minpos) * 100.0) / (half - m_minpos); Chris@43: factor = 1.0 + (factor * factor) / 1000.f; Chris@43: } Chris@43: Chris@43: if (!slow) factor = 1.0 / factor; Chris@43: Chris@43: // std::cerr << "position = " << position << " slow = " << slow << " factor = " << factor << std::endl; Chris@43: Chris@43: return factor; Chris@43: } Chris@43: Chris@43: QString Chris@43: PlaySpeedRangeMapper::getUnit() const Chris@43: { Chris@43: return "%"; Chris@43: }