comparison audioio/PlaySpeedRangeMapper.cpp @ 43:3c5756fb6a68

* Move some things around to facilitate plundering libraries for other applications without needing to duplicate so much code. sv/osc -> data/osc sv/audioio -> audioio sv/transform -> plugin/transform sv/document -> document (will rename to framework in next commit)
author Chris Cannam
date Wed, 24 Oct 2007 16:34:31 +0000
parents
children 068235cf5bf7
comparison
equal deleted inserted replaced
42:0619006a1ee3 43:3c5756fb6a68
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::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 float factor = getFactorForValue(value);
32 int position = getPositionForFactor(factor);
33 return position;
34 }
35
36 int
37 PlaySpeedRangeMapper::getPositionForFactor(float factor) const
38 {
39 bool slow = (factor > 1.0);
40
41 if (!slow) factor = 1.0 / factor;
42
43 int half = (m_maxpos + m_minpos) / 2;
44
45 factor = sqrtf((factor - 1.0) * 1000.f);
46 int position = lrintf(((factor * (half - m_minpos)) / 100.0) + m_minpos);
47
48 if (slow) {
49 position = half - position;
50 } else {
51 position = position + half;
52 }
53
54 // std::cerr << "value = " << value << " slow = " << slow << " factor = " << factor << " position = " << position << std::endl;
55
56 return position;
57 }
58
59 float
60 PlaySpeedRangeMapper::getValueForPosition(int position) const
61 {
62 float factor = getFactorForPosition(position);
63 float pc = getValueForFactor(factor);
64 return pc;
65 }
66
67 float
68 PlaySpeedRangeMapper::getValueForFactor(float factor) const
69 {
70 float pc;
71 if (factor < 1.0) pc = ((1.0 / factor) - 1.0) * 100.0;
72 else pc = (1.0 - factor) * 100.0;
73 // std::cerr << "position = " << position << " percent = " << pc << std::endl;
74 return pc;
75 }
76
77 float
78 PlaySpeedRangeMapper::getFactorForValue(float value) const
79 {
80 // value is percent
81
82 float factor;
83
84 if (value <= 0) {
85 factor = 1.0 - (value / 100.0);
86 } else {
87 factor = 1.0 / (1.0 + (value / 100.0));
88 }
89
90 // std::cerr << "value = " << value << " factor = " << factor << std::endl;
91 return factor;
92 }
93
94 float
95 PlaySpeedRangeMapper::getFactorForPosition(int position) const
96 {
97 bool slow = false;
98
99 if (position < m_minpos) position = m_minpos;
100 if (position > m_maxpos) position = m_maxpos;
101
102 int half = (m_maxpos + m_minpos) / 2;
103
104 if (position < half) {
105 slow = true;
106 position = half - position;
107 } else {
108 position = position - half;
109 }
110
111 // position is between min and half (inclusive)
112
113 float factor;
114
115 if (position == m_minpos) {
116 factor = 1.0;
117 } else {
118 factor = ((position - m_minpos) * 100.0) / (half - m_minpos);
119 factor = 1.0 + (factor * factor) / 1000.f;
120 }
121
122 if (!slow) factor = 1.0 / factor;
123
124 // std::cerr << "position = " << position << " slow = " << slow << " factor = " << factor << std::endl;
125
126 return factor;
127 }
128
129 QString
130 PlaySpeedRangeMapper::getUnit() const
131 {
132 return "%";
133 }