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
|
Chris@59
|
21 PlaySpeedRangeMapper::PlaySpeedRangeMapper(int minpos, int maxpos) :
|
Chris@59
|
22 m_minpos(minpos),
|
Chris@59
|
23 m_maxpos(maxpos)
|
Chris@59
|
24 {
|
Chris@59
|
25 }
|
Chris@59
|
26
|
Chris@59
|
27 int
|
Chris@59
|
28 PlaySpeedRangeMapper::getPositionForValue(float value) const
|
Chris@59
|
29 {
|
Chris@59
|
30 // value is percent
|
Chris@60
|
31 float factor = getFactorForValue(value);
|
Chris@60
|
32 int position = getPositionForFactor(factor);
|
Chris@60
|
33 return position;
|
Chris@60
|
34 }
|
Chris@59
|
35
|
Chris@60
|
36 int
|
Chris@60
|
37 PlaySpeedRangeMapper::getPositionForFactor(float factor) const
|
Chris@60
|
38 {
|
Chris@59
|
39 bool slow = (factor > 1.0);
|
Chris@59
|
40
|
Chris@59
|
41 if (!slow) factor = 1.0 / factor;
|
Chris@59
|
42
|
Chris@59
|
43 int half = (m_maxpos + m_minpos) / 2;
|
Chris@59
|
44
|
Chris@59
|
45 factor = sqrtf((factor - 1.0) * 1000.f);
|
Chris@59
|
46 int position = lrintf(((factor * (half - m_minpos)) / 100.0) + m_minpos);
|
Chris@59
|
47
|
Chris@59
|
48 if (slow) {
|
Chris@59
|
49 position = half - position;
|
Chris@59
|
50 } else {
|
Chris@59
|
51 position = position + half;
|
Chris@59
|
52 }
|
Chris@59
|
53
|
Chris@59
|
54 // std::cerr << "value = " << value << " slow = " << slow << " factor = " << factor << " position = " << position << std::endl;
|
Chris@59
|
55
|
Chris@59
|
56 return position;
|
Chris@59
|
57 }
|
Chris@59
|
58
|
Chris@59
|
59 float
|
Chris@59
|
60 PlaySpeedRangeMapper::getValueForPosition(int position) const
|
Chris@59
|
61 {
|
Chris@59
|
62 float factor = getFactorForPosition(position);
|
Chris@60
|
63 float pc = getValueForFactor(factor);
|
Chris@60
|
64 return pc;
|
Chris@60
|
65 }
|
Chris@60
|
66
|
Chris@60
|
67 float
|
Chris@60
|
68 PlaySpeedRangeMapper::getValueForFactor(float factor) const
|
Chris@60
|
69 {
|
Chris@59
|
70 float pc;
|
Chris@59
|
71 if (factor < 1.0) pc = ((1.0 / factor) - 1.0) * 100.0;
|
Chris@59
|
72 else pc = (1.0 - factor) * 100.0;
|
Chris@59
|
73 // std::cerr << "position = " << position << " percent = " << pc << std::endl;
|
Chris@59
|
74 return pc;
|
Chris@59
|
75 }
|
Chris@59
|
76
|
Chris@59
|
77 float
|
Chris@59
|
78 PlaySpeedRangeMapper::getFactorForValue(float value) const
|
Chris@59
|
79 {
|
Chris@59
|
80 // value is percent
|
Chris@59
|
81
|
Chris@59
|
82 float factor;
|
Chris@59
|
83
|
Chris@59
|
84 if (value <= 0) {
|
Chris@59
|
85 factor = 1.0 - (value / 100.0);
|
Chris@59
|
86 } else {
|
Chris@59
|
87 factor = 1.0 / (1.0 + (value / 100.0));
|
Chris@59
|
88 }
|
Chris@59
|
89
|
Chris@59
|
90 // std::cerr << "value = " << value << " factor = " << factor << std::endl;
|
Chris@59
|
91 return factor;
|
Chris@59
|
92 }
|
Chris@59
|
93
|
Chris@59
|
94 float
|
Chris@59
|
95 PlaySpeedRangeMapper::getFactorForPosition(int position) const
|
Chris@59
|
96 {
|
Chris@59
|
97 bool slow = false;
|
Chris@59
|
98
|
Chris@59
|
99 if (position < m_minpos) position = m_minpos;
|
Chris@59
|
100 if (position > m_maxpos) position = m_maxpos;
|
Chris@59
|
101
|
Chris@59
|
102 int half = (m_maxpos + m_minpos) / 2;
|
Chris@59
|
103
|
Chris@59
|
104 if (position < half) {
|
Chris@59
|
105 slow = true;
|
Chris@59
|
106 position = half - position;
|
Chris@59
|
107 } else {
|
Chris@59
|
108 position = position - half;
|
Chris@59
|
109 }
|
Chris@59
|
110
|
Chris@59
|
111 // position is between min and half (inclusive)
|
Chris@59
|
112
|
Chris@59
|
113 float factor;
|
Chris@59
|
114
|
Chris@59
|
115 if (position == m_minpos) {
|
Chris@59
|
116 factor = 1.0;
|
Chris@59
|
117 } else {
|
Chris@59
|
118 factor = ((position - m_minpos) * 100.0) / (half - m_minpos);
|
Chris@59
|
119 factor = 1.0 + (factor * factor) / 1000.f;
|
Chris@59
|
120 }
|
Chris@59
|
121
|
Chris@59
|
122 if (!slow) factor = 1.0 / factor;
|
Chris@59
|
123
|
Chris@59
|
124 // std::cerr << "position = " << position << " slow = " << slow << " factor = " << factor << std::endl;
|
Chris@59
|
125
|
Chris@59
|
126 return factor;
|
Chris@59
|
127 }
|
Chris@59
|
128
|
Chris@59
|
129 QString
|
Chris@59
|
130 PlaySpeedRangeMapper::getUnit() const
|
Chris@59
|
131 {
|
Chris@59
|
132 return "%";
|
Chris@59
|
133 }
|