~shanx-shashank/mixxx/effects_ladspa

« back to all changes in this revision

Viewing changes to mixxx/src/vinylcontrol/steadypitch.cpp

  • Committer: shanxS
  • Date: 2013-06-16 07:42:19 UTC
  • Revision ID: shanx.shashank@gmail.com-20130616074219-wszmk0slwfa1z61q
Init Repository.
Starting with GUI of lp:~shanx-shashank/mixxx/effects_parametricEq as base

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          steadypitch.cpp
 
3
                             -------------------
 
4
    begin                : Halloween, 2011
 
5
    copyright            : (C) 2011 Owen Williams
 
6
    email                : owilliams@mixxx.org
 
7
***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
*                                                                         *
 
11
*   This program is free software; you can redistribute it and/or modify  *
 
12
*   it under the terms of the GNU General Public License as published by  *
 
13
*   the Free Software Foundation; either version 2 of the License, or     *
 
14
*   (at your option) any later version.                                   *
 
15
*                                                                         *
 
16
***************************************************************************/
 
17
 
 
18
#include <QtDebug>
 
19
#include <limits.h>
 
20
#include "vinylcontrolxwax.h"
 
21
#include "steadypitch.h"
 
22
#include <math.h>
 
23
 
 
24
SteadyPitch::SteadyPitch(double threshold)
 
25
{
 
26
    m_dPitchThreshold = threshold; //variation above which we say we aren't steady
 
27
    m_dOldSteadyPitch = 1.0f;  //last-known steady pitch value
 
28
    m_dSteadyPitchTime = 0.0f; //last track location we had a steady pitch
 
29
    m_dLastSteadyDur = 0.0f;   //last known duration of steadiness
 
30
    m_dLastTime = 0.0f;        //track location of previous call
 
31
    m_iPlayDirection = 1;      //1=forward, -1=backward
 
32
}
 
33
 
 
34
 
 
35
void SteadyPitch::reset(double pitch, double time)
 
36
{
 
37
    m_dSteadyPitch = pitch;
 
38
    m_dSteadyPitchTime = time;
 
39
    m_dLastTime = time;
 
40
    if (m_dSteadyPitch >= 0) {
 
41
        m_iPlayDirection = 1;
 
42
    } else {
 
43
        m_iPlayDirection = -1;
 
44
    }
 
45
    m_dLastSteadyDur = 0.0;
 
46
}
 
47
 
 
48
bool SteadyPitch::directionChanged(double pitch)
 
49
{
 
50
    if (pitch >= 0) {
 
51
        return m_iPlayDirection != 1;
 
52
    } else {
 
53
        return m_iPlayDirection != -1;
 
54
    }
 
55
}
 
56
 
 
57
bool SteadyPitch::resyncDetected(double new_time)
 
58
{
 
59
    //did track location jump opposite to the play direction?
 
60
    if (m_iPlayDirection >= 0)
 
61
    {
 
62
        return new_time < m_dLastTime;
 
63
    }
 
64
    else
 
65
    {
 
66
        return new_time > m_dLastTime;
 
67
    }
 
68
}
 
69
 
 
70
double SteadyPitch::check(double pitch, double time, bool looping=false)
 
71
{
 
72
    //return length of time pitch has been steady, 0 if not steady
 
73
    if (directionChanged(pitch))
 
74
    {
 
75
        //we've reversed direction, reset
 
76
        reset(pitch, time);
 
77
        return 0.0;
 
78
    }
 
79
    
 
80
    if (resyncDetected(time))
 
81
    {
 
82
        m_dLastTime = time;
 
83
        if (looping)
 
84
        {
 
85
            //if looping, rereport the last value since we don't know where the 
 
86
            //loop actually is
 
87
            if (fabs(pitch - m_dSteadyPitch) < m_dPitchThreshold)
 
88
            {
 
89
                //fabricate an old time by take current time and applying
 
90
                //last known duration
 
91
                m_dSteadyPitchTime = time - (m_dLastSteadyDur * m_iPlayDirection);
 
92
                return m_dLastSteadyDur;
 
93
            }
 
94
        }
 
95
        reset(pitch, time);
 
96
        return 0.0;
 
97
    }
 
98
    m_dLastTime = time;
 
99
 
 
100
    if (fabs(pitch - m_dSteadyPitch) < m_dPitchThreshold)
 
101
    {
 
102
        if (fabs(time - m_dSteadyPitchTime) > 2.0) //fabs for both directions
 
103
        {
 
104
            m_dSteadyPitch = pitch;
 
105
            m_dOldSteadyPitch = m_dSteadyPitch; //this was a known-good value
 
106
            //update steady pitch time so it's between 1 and 2 seconds ago
 
107
            //(or ahead, if moving backward)
 
108
            m_dSteadyPitchTime += 1.0 * m_iPlayDirection;
 
109
        }
 
110
        m_dLastSteadyDur = fabs(time - m_dSteadyPitchTime);
 
111
        return m_dLastSteadyDur;
 
112
    }
 
113
 
 
114
    //else
 
115
    reset(pitch, time);
 
116
    return 0.0;
 
117
}
 
118
 
 
119
double SteadyPitch::steadyValue(void)
 
120
{
 
121
    return m_dOldSteadyPitch;
 
122
}