1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Sonic Visualiser
5     An audio file viewer and annotation editor.
6     Centre for Digital Music, Queen Mary, University of London.
7     This file copyright 2006 QMUL.
8 
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.  See the file
13     COPYING included with this distribution for more information.
14 */
15 
16 #include "PlaySpeedRangeMapper.h"
17 
18 #include <iostream>
19 #include <cmath>
20 
21 // PlaySpeedRangeMapper maps a position in the range [0,120] on to a
22 // play speed factor on a logarithmic scale in the range 0.125 ->
23 // 8. This ensures that the desirable speed factors 0.25, 0.5, 1, 2,
24 // and 4 are all mapped to exact positions (respectively 20, 40, 60,
25 // 80, 100).
26 
27 // Note that the "factor" referred to below is a play speed factor
28 // (higher = faster, 1.0 = normal speed), the "value" is a percentage
29 // (higher = faster, 100 = normal speed), and the "position" is an
30 // integer step on the dial's scale (0-120, 60 = centre).
31 
PlaySpeedRangeMapper()32 PlaySpeedRangeMapper::PlaySpeedRangeMapper() :
33     m_minpos(0),
34     m_maxpos(120)
35 {
36 }
37 
38 int
getPositionForValue(double value) const39 PlaySpeedRangeMapper::getPositionForValue(double value) const
40 {
41     // value is percent
42     double factor = getFactorForValue(value);
43     int position = getPositionForFactor(factor);
44     return position;
45 }
46 
47 int
getPositionForValueUnclamped(double value) const48 PlaySpeedRangeMapper::getPositionForValueUnclamped(double value) const
49 {
50     // We don't really provide this
51     return getPositionForValue(value);
52 }
53 
54 double
getValueForPosition(int position) const55 PlaySpeedRangeMapper::getValueForPosition(int position) const
56 {
57     double factor = getFactorForPosition(position);
58     double pc = getValueForFactor(factor);
59     return pc;
60 }
61 
62 double
getValueForPositionUnclamped(int position) const63 PlaySpeedRangeMapper::getValueForPositionUnclamped(int position) const
64 {
65     // We don't really provide this
66     return getValueForPosition(position);
67 }
68 
69 double
getValueForFactor(double factor) const70 PlaySpeedRangeMapper::getValueForFactor(double factor) const
71 {
72     return factor * 100.0;
73 }
74 
75 double
getFactorForValue(double value) const76 PlaySpeedRangeMapper::getFactorForValue(double value) const
77 {
78     return value / 100.0;
79 }
80 
81 int
getPositionForFactor(double factor) const82 PlaySpeedRangeMapper::getPositionForFactor(double factor) const
83 {
84     if (factor == 0) return m_minpos;
85     int pos = int(lrint((log2(factor) + 3.0) * 20.0));
86     if (pos < m_minpos) pos = m_minpos;
87     if (pos > m_maxpos) pos = m_maxpos;
88     return pos;
89 }
90 
91 double
getFactorForPosition(int position) const92 PlaySpeedRangeMapper::getFactorForPosition(int position) const
93 {
94     return pow(2.0, double(position) * 0.05 - 3.0);
95 }
96 
97 QString
getUnit() const98 PlaySpeedRangeMapper::getUnit() const
99 {
100     return "%";
101 }
102