comparison audioio/PlaySpeedRangeMapper.cpp @ 59:1016a8ceceda

* Introduce PlaySpeedRangeMapper for playback speed dial
author Chris Cannam
date Tue, 17 Oct 2006 11:42:14 +0000
parents
children 06b3c3f437e6
comparison
equal deleted inserted replaced
58:f7cb156508cc 59:1016a8ceceda
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 Chris Cannam.
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
20
21 PlaySpeedRangeMapper::PlaySpeedRangeMapper(int minpos, int maxpos) :
22 m_minpos(minpos),
23 m_maxpos(maxpos)
24 {
25 }
26
27 int
28 PlaySpeedRangeMapper::getPositionForValue(float value) const
29 {
30 // value is percent
31
32 float factor = getFactorForValue(value);
33
34 bool slow = (factor > 1.0);
35
36 if (!slow) factor = 1.0 / factor;
37
38 int half = (m_maxpos + m_minpos) / 2;
39
40 factor = sqrtf((factor - 1.0) * 1000.f);
41 int position = lrintf(((factor * (half - m_minpos)) / 100.0) + m_minpos);
42
43 if (slow) {
44 position = half - position;
45 } else {
46 position = position + half;
47 }
48
49 // std::cerr << "value = " << value << " slow = " << slow << " factor = " << factor << " position = " << position << std::endl;
50
51 return position;
52 }
53
54 float
55 PlaySpeedRangeMapper::getValueForPosition(int position) const
56 {
57 float factor = getFactorForPosition(position);
58 float pc;
59 if (factor < 1.0) pc = ((1.0 / factor) - 1.0) * 100.0;
60 else pc = (1.0 - factor) * 100.0;
61 // std::cerr << "position = " << position << " percent = " << pc << std::endl;
62 return pc;
63 }
64
65 float
66 PlaySpeedRangeMapper::getFactorForValue(float value) const
67 {
68 // value is percent
69
70 float factor;
71
72 if (value <= 0) {
73 factor = 1.0 - (value / 100.0);
74 } else {
75 factor = 1.0 / (1.0 + (value / 100.0));
76 }
77
78 // std::cerr << "value = " << value << " factor = " << factor << std::endl;
79 return factor;
80 }
81
82 float
83 PlaySpeedRangeMapper::getFactorForPosition(int position) const
84 {
85 bool slow = false;
86
87 if (position < m_minpos) position = m_minpos;
88 if (position > m_maxpos) position = m_maxpos;
89
90 int half = (m_maxpos + m_minpos) / 2;
91
92 if (position < half) {
93 slow = true;
94 position = half - position;
95 } else {
96 position = position - half;
97 }
98
99 // position is between min and half (inclusive)
100
101 float factor;
102
103 if (position == m_minpos) {
104 factor = 1.0;
105 } else {
106 factor = ((position - m_minpos) * 100.0) / (half - m_minpos);
107 factor = 1.0 + (factor * factor) / 1000.f;
108 }
109
110 if (!slow) factor = 1.0 / factor;
111
112 // std::cerr << "position = " << position << " slow = " << slow << " factor = " << factor << std::endl;
113
114 return factor;
115 }
116
117 QString
118 PlaySpeedRangeMapper::getUnit() const
119 {
120 return "%";
121 }