From 7a7c9abd9b3913f2d805f99d4fa1be7d4ee23b17 Mon Sep 17 00:00:00 2001 From: Vesa Date: Thu, 20 Mar 2014 12:19:55 +0200 Subject: [PATCH] GraphModel: fix/improve normalize() function - remove bias before maximizing, also add some new slots for future use (which I plan to use for something neat in the future ;) ) --- include/graph.h | 20 +++++++------- src/gui/widgets/graph.cpp | 55 +++++++++++++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/include/graph.h b/include/graph.h index 411224e1f..025e15a7b 100644 --- a/include/graph.h +++ b/include/graph.h @@ -3,7 +3,7 @@ * * Copyright (c) 2006-2007 Andreas Brandmaier * 2008 Paul Giblock - * + * * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net * * This program is free software; you can redistribute it and/or @@ -24,8 +24,8 @@ */ -#ifndef _GRAPH_H -#define _GRAPH_H +#ifndef GRAPH_H +#define GRAPH_H #include #include @@ -71,7 +71,7 @@ public: { return m_graphStyle; } - + inline void setGraphStyle( graphStyle _s ) { @@ -101,10 +101,10 @@ private: QPixmap m_foreground; QColor m_graphColor; - + graphModel * m_graphModel; graphStyle m_graphStyle; - + bool m_mouseDown; int m_lastCursorX; @@ -125,7 +125,7 @@ public: virtual ~graphModel(); // TODO: saveSettings, loadSettings? - + inline float minValue() const { return( m_minValue ); @@ -140,7 +140,7 @@ public: { return( m_samples.count() ); } - + inline const float * samples() const { return( m_samples.data() ); @@ -160,10 +160,12 @@ public slots: void setWaveToSquare(); void setWaveToNoise(); QString setWaveToUser( ); - + void smooth(); void smoothNonCyclic(); void normalize(); + void invert(); + void shiftPhase( int _deg ); signals: void lengthChanged(); diff --git a/src/gui/widgets/graph.cpp b/src/gui/widgets/graph.cpp index f336434fa..a7011537c 100644 --- a/src/gui/widgets/graph.cpp +++ b/src/gui/widgets/graph.cpp @@ -629,28 +629,61 @@ void graphModel::smoothNonCyclic() emit samplesChanged(0, length()-1); } + + void graphModel::normalize() { float max = 0.0001f; + float avg = 0.0f; + + // first correct dc offset by normalizing to average for( int i = 0; i < length(); i++ ) - { - if( fabsf(m_samples[i]) > max && m_samples[i] != 0.0f ) - { - max = fabs( m_samples[i] ); - } - } + avg += m_samples[i]; + avg /= length(); + for( int i = 0; i < length(); i++ ) + m_samples[i] -= avg; + + // then maximize + for( int i = 0; i < length(); i++ ) + max = qMax( max, qAbs( m_samples[i] ) ); for( int i = 0; i < length(); i++ ) - { - m_samples[i] /= max; - } + m_samples[i] = qBound( m_minValue, m_samples[i] / max, m_maxValue ); - if( max != 1.0f ) { + // signal changes if any + if( max != 1.0f || avg != 0.0f ) emit samplesChanged( 0, length()-1 ); - } } +void graphModel::invert() +{ + const float range = m_maxValue - m_minValue; + + for( int i = 0; i < length(); i++ ) + m_samples[i] = m_minValue + ( range - ( m_samples[i] - m_minValue ) ); + + emit samplesChanged( 0, length()-1 ); +} + + + +void graphModel::shiftPhase( int _deg ) +{ + // calculate offset in samples + int offset = ( _deg * length() ) / 360; //multiply first because integers + + // store values in temporary array + QVector temp = m_samples; + + // shift phase + for( int i = 0; i < length(); i++ ) + m_samples[i] = temp[ ( i + offset ) % length() ]; + + emit samplesChanged( 0, length()-1 ); +} + + #include "moc_graph.cxx"