Chris@59: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@59: Chris@59: /* Chris@59: Sonic Visualiser Chris@59: An audio file viewer and annotation editor. Chris@59: Centre for Digital Music, Queen Mary, University of London. Chris@59: This file copyright 2006 Chris Cannam. Chris@59: Chris@59: This program is free software; you can redistribute it and/or Chris@59: modify it under the terms of the GNU General Public License as Chris@59: published by the Free Software Foundation; either version 2 of the Chris@59: License, or (at your option) any later version. See the file Chris@59: COPYING included with this distribution for more information. Chris@59: */ Chris@59: Chris@59: #include "PlaySpeedRangeMapper.h" Chris@59: Chris@59: #include Chris@59: Chris@59: Chris@59: PlaySpeedRangeMapper::PlaySpeedRangeMapper(int minpos, int maxpos) : Chris@59: m_minpos(minpos), Chris@59: m_maxpos(maxpos) Chris@59: { Chris@59: } Chris@59: Chris@59: int Chris@59: PlaySpeedRangeMapper::getPositionForValue(float value) const Chris@59: { Chris@59: // value is percent Chris@60: float factor = getFactorForValue(value); Chris@60: int position = getPositionForFactor(factor); Chris@60: return position; Chris@60: } Chris@59: Chris@60: int Chris@60: PlaySpeedRangeMapper::getPositionForFactor(float factor) const Chris@60: { Chris@59: bool slow = (factor > 1.0); Chris@59: Chris@59: if (!slow) factor = 1.0 / factor; Chris@59: Chris@59: int half = (m_maxpos + m_minpos) / 2; Chris@59: Chris@59: factor = sqrtf((factor - 1.0) * 1000.f); Chris@59: int position = lrintf(((factor * (half - m_minpos)) / 100.0) + m_minpos); Chris@59: Chris@59: if (slow) { Chris@59: position = half - position; Chris@59: } else { Chris@59: position = position + half; Chris@59: } Chris@59: Chris@59: // std::cerr << "value = " << value << " slow = " << slow << " factor = " << factor << " position = " << position << std::endl; Chris@59: Chris@59: return position; Chris@59: } Chris@59: Chris@59: float Chris@59: PlaySpeedRangeMapper::getValueForPosition(int position) const Chris@59: { Chris@59: float factor = getFactorForPosition(position); Chris@60: float pc = getValueForFactor(factor); Chris@60: return pc; Chris@60: } Chris@60: Chris@60: float Chris@60: PlaySpeedRangeMapper::getValueForFactor(float factor) const Chris@60: { Chris@59: float pc; Chris@59: if (factor < 1.0) pc = ((1.0 / factor) - 1.0) * 100.0; Chris@59: else pc = (1.0 - factor) * 100.0; Chris@59: // std::cerr << "position = " << position << " percent = " << pc << std::endl; Chris@59: return pc; Chris@59: } Chris@59: Chris@59: float Chris@59: PlaySpeedRangeMapper::getFactorForValue(float value) const Chris@59: { Chris@59: // value is percent Chris@59: Chris@59: float factor; Chris@59: Chris@59: if (value <= 0) { Chris@59: factor = 1.0 - (value / 100.0); Chris@59: } else { Chris@59: factor = 1.0 / (1.0 + (value / 100.0)); Chris@59: } Chris@59: Chris@59: // std::cerr << "value = " << value << " factor = " << factor << std::endl; Chris@59: return factor; Chris@59: } Chris@59: Chris@59: float Chris@59: PlaySpeedRangeMapper::getFactorForPosition(int position) const Chris@59: { Chris@59: bool slow = false; Chris@59: Chris@59: if (position < m_minpos) position = m_minpos; Chris@59: if (position > m_maxpos) position = m_maxpos; Chris@59: Chris@59: int half = (m_maxpos + m_minpos) / 2; Chris@59: Chris@59: if (position < half) { Chris@59: slow = true; Chris@59: position = half - position; Chris@59: } else { Chris@59: position = position - half; Chris@59: } Chris@59: Chris@59: // position is between min and half (inclusive) Chris@59: Chris@59: float factor; Chris@59: Chris@59: if (position == m_minpos) { Chris@59: factor = 1.0; Chris@59: } else { Chris@59: factor = ((position - m_minpos) * 100.0) / (half - m_minpos); Chris@59: factor = 1.0 + (factor * factor) / 1000.f; Chris@59: } Chris@59: Chris@59: if (!slow) factor = 1.0 / factor; Chris@59: Chris@59: // std::cerr << "position = " << position << " slow = " << slow << " factor = " << factor << std::endl; Chris@59: Chris@59: return factor; Chris@59: } Chris@59: Chris@59: QString Chris@59: PlaySpeedRangeMapper::getUnit() const Chris@59: { Chris@59: return "%"; Chris@59: }