annotate audioio/PlaySpeedRangeMapper.cpp @ 141:9a8c73ffdce0

* Make it possible to import an entire session from an RDF document. However, at the moment the timings of events appear to be constrained by how far the audio decoder has got through its audio file at the time the event is queried -- need to investigate.
author Chris Cannam
date Fri, 21 Nov 2008 18:03:14 +0000
parents 3c5756fb6a68
children 068235cf5bf7
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@43 37 PlaySpeedRangeMapper::getPositionForFactor(float factor) const
Chris@43 38 {
Chris@43 39 bool slow = (factor > 1.0);
Chris@43 40
Chris@43 41 if (!slow) factor = 1.0 / factor;
Chris@43 42
Chris@43 43 int half = (m_maxpos + m_minpos) / 2;
Chris@43 44
Chris@43 45 factor = sqrtf((factor - 1.0) * 1000.f);
Chris@43 46 int position = lrintf(((factor * (half - m_minpos)) / 100.0) + m_minpos);
Chris@43 47
Chris@43 48 if (slow) {
Chris@43 49 position = half - position;
Chris@43 50 } else {
Chris@43 51 position = position + half;
Chris@43 52 }
Chris@43 53
Chris@43 54 // std::cerr << "value = " << value << " slow = " << slow << " factor = " << factor << " position = " << position << std::endl;
Chris@43 55
Chris@43 56 return position;
Chris@43 57 }
Chris@43 58
Chris@43 59 float
Chris@43 60 PlaySpeedRangeMapper::getValueForPosition(int position) const
Chris@43 61 {
Chris@43 62 float factor = getFactorForPosition(position);
Chris@43 63 float pc = getValueForFactor(factor);
Chris@43 64 return pc;
Chris@43 65 }
Chris@43 66
Chris@43 67 float
Chris@43 68 PlaySpeedRangeMapper::getValueForFactor(float factor) const
Chris@43 69 {
Chris@43 70 float pc;
Chris@43 71 if (factor < 1.0) pc = ((1.0 / factor) - 1.0) * 100.0;
Chris@43 72 else pc = (1.0 - factor) * 100.0;
Chris@43 73 // std::cerr << "position = " << position << " percent = " << pc << std::endl;
Chris@43 74 return pc;
Chris@43 75 }
Chris@43 76
Chris@43 77 float
Chris@43 78 PlaySpeedRangeMapper::getFactorForValue(float value) const
Chris@43 79 {
Chris@43 80 // value is percent
Chris@43 81
Chris@43 82 float factor;
Chris@43 83
Chris@43 84 if (value <= 0) {
Chris@43 85 factor = 1.0 - (value / 100.0);
Chris@43 86 } else {
Chris@43 87 factor = 1.0 / (1.0 + (value / 100.0));
Chris@43 88 }
Chris@43 89
Chris@43 90 // std::cerr << "value = " << value << " factor = " << factor << std::endl;
Chris@43 91 return factor;
Chris@43 92 }
Chris@43 93
Chris@43 94 float
Chris@43 95 PlaySpeedRangeMapper::getFactorForPosition(int position) const
Chris@43 96 {
Chris@43 97 bool slow = false;
Chris@43 98
Chris@43 99 if (position < m_minpos) position = m_minpos;
Chris@43 100 if (position > m_maxpos) position = m_maxpos;
Chris@43 101
Chris@43 102 int half = (m_maxpos + m_minpos) / 2;
Chris@43 103
Chris@43 104 if (position < half) {
Chris@43 105 slow = true;
Chris@43 106 position = half - position;
Chris@43 107 } else {
Chris@43 108 position = position - half;
Chris@43 109 }
Chris@43 110
Chris@43 111 // position is between min and half (inclusive)
Chris@43 112
Chris@43 113 float factor;
Chris@43 114
Chris@43 115 if (position == m_minpos) {
Chris@43 116 factor = 1.0;
Chris@43 117 } else {
Chris@43 118 factor = ((position - m_minpos) * 100.0) / (half - m_minpos);
Chris@43 119 factor = 1.0 + (factor * factor) / 1000.f;
Chris@43 120 }
Chris@43 121
Chris@43 122 if (!slow) factor = 1.0 / factor;
Chris@43 123
Chris@43 124 // std::cerr << "position = " << position << " slow = " << slow << " factor = " << factor << std::endl;
Chris@43 125
Chris@43 126 return factor;
Chris@43 127 }
Chris@43 128
Chris@43 129 QString
Chris@43 130 PlaySpeedRangeMapper::getUnit() const
Chris@43 131 {
Chris@43 132 return "%";
Chris@43 133 }