Mixxx

/home/maxime/Projets/Mixxx/1.10/mixxx/src/softtakeover.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           softtakeover.cpp  -  description
00003                           ----------------
00004     begin                : Sat Mar 26 2011
00005     copyright            : (C) 2011 by Sean M. Pappalardo
00006     email                : spappalardo@mixxx.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "softtakeover.h"
00019 #include <math.h>  // for fabs()
00020 #include <qdatetime.h>
00021 
00022 // HACK: remove this after Control 2.0 is here
00023 #include "controlpotmeter.h"
00024 
00025 // qint64 currentTimeMsecs() {
00026 uint SoftTakeover::currentTimeMsecs() {
00027     QDateTime currDT = QDateTime::currentDateTime();
00028 
00029     // toMSecsSinceEpoch() is preferred since it's only one QDateTime call but
00030     //  it requires Qt 4.7. Use it instead when something more substantial
00031     //  elsewhere in Mixxx also requires Qt 4.7.
00032     //qint64 t = dt.toMSecsSinceEpoch();  // Requires Qt 4.7
00033     uint t = currDT.toTime_t()*1000+currDT.toString("zzz").toUInt();
00034     return t;
00035 }
00036 
00037 // For legacy Controls
00038 void SoftTakeover::enable(QString group, QString name) {
00039     MixxxControl mixxxControl = MixxxControl(group,name);
00040     enable(mixxxControl);
00041 }
00042 
00043 void SoftTakeover::enable(MixxxControl control) {
00044     if (!m_times.contains(control)) m_times.insert(control,currentTimeMsecs());
00045 }
00046 
00047 // For legacy Controls
00048 void SoftTakeover::disable(QString group, QString name) {
00049     MixxxControl mixxxControl = MixxxControl(group,name);
00050     disable(mixxxControl);
00051 }
00052 
00053 void SoftTakeover::disable(MixxxControl control) {
00054     m_times.remove(control);
00055 }
00056 
00057 // For legacy Controls
00058 bool SoftTakeover::ignore(QString group, QString name, float newValue) {
00059     MixxxControl mixxxControl = MixxxControl(group,name);
00060     return ignore(mixxxControl,newValue);
00061 }
00062 
00063 bool SoftTakeover::ignore(MixxxControl mc, float newValue, bool midiVal) {
00064     bool ignore = false;
00065     QString message;
00066     if (m_times.contains(mc)) {
00067         // We only want to ignore the MIDI controller when all of the following are true:
00068         //  - its new value is far away from the MixxxControl
00069         //  - it's been awhile since the last MIDI message for this control affected it
00070         
00071         // 3/128 units away from the current is enough to catch fast non-sequential moves
00072         //  but not cause an audially noticeable jump.
00073         float threshold = 3;
00074         
00075         ControlObject* temp = ControlObject::getControl(
00076             ConfigKey(mc.getControlObjectGroup(), mc.getControlObjectValue()));
00077 
00078         if (temp == NULL) return ignore;
00079         
00080         if (!midiVal) {
00081             // These defaults will effectively disable soft-takeover for this pass
00082             //  (causing the control to jump to the new value regardless)
00083             //  if there's a problem with the below CO being NULL
00084             double maxValue=10000000;   // Anything, just higher than any CO can go
00085             double minValue=0;
00086             
00087             // HACK until we have Control 2.0. It can't come soon enough...
00088             ControlPotmeter* cpo = dynamic_cast<ControlPotmeter*>(temp);    // for getMax/getMin
00089             if (cpo != NULL) {
00090                 maxValue = cpo->getMax();
00091                 minValue = cpo->getMin();
00092             }
00093             // End hack
00094             
00095             double scaleFactor = maxValue-minValue;
00096             threshold = scaleFactor*(threshold/128);
00097         }
00098         
00099         double oldValue;
00100         if (midiVal) oldValue = temp->GetMidiValue();
00101         else oldValue = temp->get();
00102         double difference = oldValue - newValue;
00103         
00104         uint currentTime = currentTimeMsecs();
00105         if (fabs(difference)>threshold
00106             && (currentTime - m_times.value(mc)) > SUBSEQUENT_VALUE_OVERRIDE_TIME) {
00107             ignore = true;
00108         }
00109         if (!ignore) {
00110             //  Update the time only if the value is not ignored
00111             m_times.insert(mc,currentTime); // Replaces any previous value for this MixxxControl
00112         }
00113     }
00114     return ignore;
00115 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines