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::getPositionForFactor(float factor) const
00038 {
00039     bool slow = (factor > 1.0);
00040 
00041     if (!slow) factor = 1.0 / factor;
00042     
00043     int half = (m_maxpos + m_minpos) / 2;
00044 
00045     factor = sqrtf((factor - 1.0) * 1000.f);
00046     int position = lrintf(((factor * (half - m_minpos)) / 100.0) + m_minpos);
00047 
00048     if (slow) {
00049         position = half - position;
00050     } else {
00051         position = position + half;
00052     }
00053 
00054 //    std::cerr << "value = " << value << " slow = " << slow << " factor = " << factor << " position = " << position << std::endl;
00055 
00056     return position;
00057 }
00058 
00059 float
00060 PlaySpeedRangeMapper::getValueForPosition(int position) const
00061 {
00062     float factor = getFactorForPosition(position);
00063     float pc = getValueForFactor(factor);
00064     return pc;
00065 }
00066 
00067 float
00068 PlaySpeedRangeMapper::getValueForFactor(float factor) const
00069 {
00070     float pc;
00071     if (factor < 1.0) pc = ((1.0 / factor) - 1.0) * 100.0;
00072     else pc = (1.0 - factor) * 100.0;
00073 //    std::cerr << "position = " << position << " percent = " << pc << std::endl;
00074     return pc;
00075 }
00076 
00077 float
00078 PlaySpeedRangeMapper::getFactorForValue(float value) const
00079 {
00080     // value is percent
00081     
00082     float factor;
00083 
00084     if (value <= 0) {
00085         factor = 1.0 - (value / 100.0);
00086     } else {
00087         factor = 1.0 / (1.0 + (value / 100.0));
00088     }
00089 
00090 //    std::cerr << "value = " << value << " factor = " << factor << std::endl;
00091     return factor;
00092 }
00093 
00094 float
00095 PlaySpeedRangeMapper::getFactorForPosition(int position) const
00096 {
00097     bool slow = false;
00098 
00099     if (position < m_minpos) position = m_minpos;
00100     if (position > m_maxpos) position = m_maxpos;
00101 
00102     int half = (m_maxpos + m_minpos) / 2;
00103 
00104     if (position < half) {
00105         slow = true;
00106         position = half - position;
00107     } else {
00108         position = position - half;
00109     }
00110 
00111     // position is between min and half (inclusive)
00112 
00113     float factor;
00114 
00115     if (position == m_minpos) {
00116         factor = 1.0;
00117     } else {
00118         factor = ((position - m_minpos) * 100.0) / (half - m_minpos);
00119         factor = 1.0 + (factor * factor) / 1000.f;
00120     }
00121 
00122     if (!slow) factor = 1.0 / factor;
00123 
00124 //    std::cerr << "position = " << position << " slow = " << slow << " factor = " << factor << std::endl;
00125 
00126     return factor;
00127 }
00128 
00129 QString
00130 PlaySpeedRangeMapper::getUnit() const
00131 {
00132     return "%";
00133 }

Generated on Wed Feb 20 15:45:27 2008 for SonicVisualiser by  doxygen 1.5.1