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