svapp  1.9
PlaySpeedRangeMapper.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 QMUL.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "PlaySpeedRangeMapper.h"
00017 
00018 #include <iostream>
00019 #include <cmath>
00020 
00021 PlaySpeedRangeMapper::PlaySpeedRangeMapper(int minpos, int maxpos) :
00022     m_minpos(minpos),
00023     m_maxpos(maxpos)
00024 {
00025 }
00026 
00027 int
00028 PlaySpeedRangeMapper::getPositionForValue(float value) const
00029 {
00030     // value is percent
00031     float factor = getFactorForValue(value);
00032     int position = getPositionForFactor(factor);
00033     return position;
00034 }
00035 
00036 int
00037 PlaySpeedRangeMapper::getPositionForValueUnclamped(float value) const
00038 {
00039     // We don't really provide this
00040     return getPositionForValue(value);
00041 }
00042 
00043 int
00044 PlaySpeedRangeMapper::getPositionForFactor(float factor) const
00045 {
00046     bool slow = (factor > 1.0);
00047 
00048     if (!slow) factor = 1.0 / factor;
00049     
00050     int half = (m_maxpos + m_minpos) / 2;
00051 
00052     factor = sqrtf((factor - 1.0) * 1000.f);
00053     int position = lrintf(((factor * (half - m_minpos)) / 100.0) + m_minpos);
00054 
00055     if (slow) {
00056         position = half - position;
00057     } else {
00058         position = position + half;
00059     }
00060 
00061 //    cerr << "value = " << value << " slow = " << slow << " factor = " << factor << " position = " << position << endl;
00062 
00063     return position;
00064 }
00065 
00066 float
00067 PlaySpeedRangeMapper::getValueForPosition(int position) const
00068 {
00069     float factor = getFactorForPosition(position);
00070     float pc = getValueForFactor(factor);
00071     return pc;
00072 }
00073 
00074 float
00075 PlaySpeedRangeMapper::getValueForPositionUnclamped(int position) const
00076 {
00077     // We don't really provide this
00078     return getValueForPosition(position);
00079 }
00080 
00081 float
00082 PlaySpeedRangeMapper::getValueForFactor(float factor) const
00083 {
00084     float pc;
00085     if (factor < 1.0) pc = ((1.0 / factor) - 1.0) * 100.0;
00086     else pc = (1.0 - factor) * 100.0;
00087 //    cerr << "position = " << position << " percent = " << pc << endl;
00088     return pc;
00089 }
00090 
00091 float
00092 PlaySpeedRangeMapper::getFactorForValue(float value) const
00093 {
00094     // value is percent
00095     
00096     float factor;
00097 
00098     if (value <= 0) {
00099         factor = 1.0 - (value / 100.0);
00100     } else {
00101         factor = 1.0 / (1.0 + (value / 100.0));
00102     }
00103 
00104 //    cerr << "value = " << value << " factor = " << factor << endl;
00105     return factor;
00106 }
00107 
00108 float
00109 PlaySpeedRangeMapper::getFactorForPosition(int position) const
00110 {
00111     bool slow = false;
00112 
00113     if (position < m_minpos) position = m_minpos;
00114     if (position > m_maxpos) position = m_maxpos;
00115 
00116     int half = (m_maxpos + m_minpos) / 2;
00117 
00118     if (position < half) {
00119         slow = true;
00120         position = half - position;
00121     } else {
00122         position = position - half;
00123     }
00124 
00125     // position is between min and half (inclusive)
00126 
00127     float factor;
00128 
00129     if (position == m_minpos) {
00130         factor = 1.0;
00131     } else {
00132         factor = ((position - m_minpos) * 100.0) / (half - m_minpos);
00133         factor = 1.0 + (factor * factor) / 1000.f;
00134     }
00135 
00136     if (!slow) factor = 1.0 / factor;
00137 
00138 //    cerr << "position = " << position << " slow = " << slow << " factor = " << factor << endl;
00139 
00140     return factor;
00141 }
00142 
00143 QString
00144 PlaySpeedRangeMapper::getUnit() const
00145 {
00146     return "%";
00147 }