Chris@59
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@59
|
2
|
Chris@59
|
3 /*
|
Chris@59
|
4 Sonic Visualiser
|
Chris@59
|
5 An audio file viewer and annotation editor.
|
Chris@59
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@59
|
7 This file copyright 2006 Chris Cannam.
|
Chris@59
|
8
|
Chris@59
|
9 This program is free software; you can redistribute it and/or
|
Chris@59
|
10 modify it under the terms of the GNU General Public License as
|
Chris@59
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@59
|
12 License, or (at your option) any later version. See the file
|
Chris@59
|
13 COPYING included with this distribution for more information.
|
Chris@59
|
14 */
|
Chris@59
|
15
|
Chris@59
|
16 #include "PlaySpeedRangeMapper.h"
|
Chris@59
|
17
|
Chris@59
|
18 #include <iostream>
|
Chris@59
|
19
|
Chris@59
|
20
|
Martin@68
|
21 #include <math.h>
|
Martin@68
|
22
|
Chris@59
|
23 PlaySpeedRangeMapper::PlaySpeedRangeMapper(int minpos, int maxpos) :
|
Chris@59
|
24 m_minpos(minpos),
|
Chris@59
|
25 m_maxpos(maxpos)
|
Chris@59
|
26 {
|
Chris@59
|
27 }
|
Chris@59
|
28
|
Chris@59
|
29 int
|
Chris@59
|
30 PlaySpeedRangeMapper::getPositionForValue(float value) const
|
Chris@59
|
31 {
|
Chris@59
|
32 // value is percent
|
Chris@60
|
33 float factor = getFactorForValue(value);
|
Chris@60
|
34 int position = getPositionForFactor(factor);
|
Chris@60
|
35 return position;
|
Chris@60
|
36 }
|
Chris@59
|
37
|
Chris@60
|
38 int
|
Chris@60
|
39 PlaySpeedRangeMapper::getPositionForFactor(float factor) const
|
Chris@60
|
40 {
|
Chris@59
|
41 bool slow = (factor > 1.0);
|
Chris@59
|
42
|
Chris@59
|
43 if (!slow) factor = 1.0 / factor;
|
Chris@59
|
44
|
Chris@59
|
45 int half = (m_maxpos + m_minpos) / 2;
|
Chris@59
|
46
|
Chris@59
|
47 factor = sqrtf((factor - 1.0) * 1000.f);
|
Chris@59
|
48 int position = lrintf(((factor * (half - m_minpos)) / 100.0) + m_minpos);
|
Chris@59
|
49
|
Chris@59
|
50 if (slow) {
|
Chris@59
|
51 position = half - position;
|
Chris@59
|
52 } else {
|
Chris@59
|
53 position = position + half;
|
Chris@59
|
54 }
|
Chris@59
|
55
|
Chris@59
|
56 // std::cerr << "value = " << value << " slow = " << slow << " factor = " << factor << " position = " << position << std::endl;
|
Chris@59
|
57
|
Chris@59
|
58 return position;
|
Chris@59
|
59 }
|
Chris@59
|
60
|
Chris@59
|
61 float
|
Chris@59
|
62 PlaySpeedRangeMapper::getValueForPosition(int position) const
|
Chris@59
|
63 {
|
Chris@59
|
64 float factor = getFactorForPosition(position);
|
Chris@60
|
65 float pc = getValueForFactor(factor);
|
Chris@60
|
66 return pc;
|
Chris@60
|
67 }
|
Chris@60
|
68
|
Chris@60
|
69 float
|
Chris@60
|
70 PlaySpeedRangeMapper::getValueForFactor(float factor) const
|
Chris@60
|
71 {
|
Chris@59
|
72 float pc;
|
Chris@59
|
73 if (factor < 1.0) pc = ((1.0 / factor) - 1.0) * 100.0;
|
Chris@59
|
74 else pc = (1.0 - factor) * 100.0;
|
Chris@59
|
75 // std::cerr << "position = " << position << " percent = " << pc << std::endl;
|
Chris@59
|
76 return pc;
|
Chris@59
|
77 }
|
Chris@59
|
78
|
Chris@59
|
79 float
|
Chris@59
|
80 PlaySpeedRangeMapper::getFactorForValue(float value) const
|
Chris@59
|
81 {
|
Chris@59
|
82 // value is percent
|
Chris@59
|
83
|
Chris@59
|
84 float factor;
|
Chris@59
|
85
|
Chris@59
|
86 if (value <= 0) {
|
Chris@59
|
87 factor = 1.0 - (value / 100.0);
|
Chris@59
|
88 } else {
|
Chris@59
|
89 factor = 1.0 / (1.0 + (value / 100.0));
|
Chris@59
|
90 }
|
Chris@59
|
91
|
Chris@59
|
92 // std::cerr << "value = " << value << " factor = " << factor << std::endl;
|
Chris@59
|
93 return factor;
|
Chris@59
|
94 }
|
Chris@59
|
95
|
Chris@59
|
96 float
|
Chris@59
|
97 PlaySpeedRangeMapper::getFactorForPosition(int position) const
|
Chris@59
|
98 {
|
Chris@59
|
99 bool slow = false;
|
Chris@59
|
100
|
Chris@59
|
101 if (position < m_minpos) position = m_minpos;
|
Chris@59
|
102 if (position > m_maxpos) position = m_maxpos;
|
Chris@59
|
103
|
Chris@59
|
104 int half = (m_maxpos + m_minpos) / 2;
|
Chris@59
|
105
|
Chris@59
|
106 if (position < half) {
|
Chris@59
|
107 slow = true;
|
Chris@59
|
108 position = half - position;
|
Chris@59
|
109 } else {
|
Chris@59
|
110 position = position - half;
|
Chris@59
|
111 }
|
Chris@59
|
112
|
Chris@59
|
113 // position is between min and half (inclusive)
|
Chris@59
|
114
|
Chris@59
|
115 float factor;
|
Chris@59
|
116
|
Chris@59
|
117 if (position == m_minpos) {
|
Chris@59
|
118 factor = 1.0;
|
Chris@59
|
119 } else {
|
Chris@59
|
120 factor = ((position - m_minpos) * 100.0) / (half - m_minpos);
|
Chris@59
|
121 factor = 1.0 + (factor * factor) / 1000.f;
|
Chris@59
|
122 }
|
Chris@59
|
123
|
Chris@59
|
124 if (!slow) factor = 1.0 / factor;
|
Chris@59
|
125
|
Chris@59
|
126 // std::cerr << "position = " << position << " slow = " << slow << " factor = " << factor << std::endl;
|
Chris@59
|
127
|
Chris@59
|
128 return factor;
|
Chris@59
|
129 }
|
Chris@59
|
130
|
Chris@59
|
131 QString
|
Chris@59
|
132 PlaySpeedRangeMapper::getUnit() const
|
Chris@59
|
133 {
|
Chris@59
|
134 return "%";
|
Chris@59
|
135 }
|