annotate audioio/PlaySpeedRangeMapper.cpp @ 423:f32a64149602 alignment_view

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