hydrogen 1.2.3
AutomationPath.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
24#include <core/Basics/Song.h>
25#include <core/Hydrogen.h>
26
27namespace H2Core
28{
29
30AutomationPath::AutomationPath(float min, float max, float def)
31 : Object(),
32 _min(min),
33 _max(max),
34 _def(def)
35{
36}
37
38
45float AutomationPath::get_value(float x) const noexcept
46{
47 if (_points.empty()) {
48 return _def;
49 }
50
51 auto f = _points.begin();
52 if(x <= f->first) {
53 return f->second;
54 }
55
56 auto l = _points.rbegin();
57 if(x >= l->first) {
58 return l->second;
59 }
60
61 auto i = _points.lower_bound(x);
62 auto p1 = *i;
63 auto p0 = *(--i);
64 float x1 = p0.first;
65 float y1 = p0.second;
66 float x2 = p1.first;
67 float y2 = p1.second;
68
69 float d = (x-x1)/(x2 - x1);
70
71 return y1 + (y2-y1)*d;
72}
73
74
80void AutomationPath::add_point(float x, float y)
81{
82 _points[x] = y;
84}
85
86
93bool operator==(const AutomationPath &lhs, const AutomationPath &rhs)
94{
95 return lhs._min == rhs._min
96 && lhs._max == rhs._max
97 && lhs._def == rhs._def
98 && lhs._points == rhs._points;
99}
100
101
102bool operator!=(const AutomationPath &lhs, const AutomationPath &rhs)
103{
104 return !(lhs==rhs);
105}
106
107QString AutomationPath::toQString( const QString& sPrefix, bool bShort ) const {
108 QString s = Base::sPrintIndention;
109 QString sOutput;
110 if ( ! bShort ) {
111 sOutput = QString( "%1[AutomationPath]\n" ).arg( sPrefix )
112 .append( QString( "%1%2min: %3\n" ).arg( sPrefix ).arg( s ).arg( _min ) )
113 .append( QString( "%1%2max: %3\n" ).arg( sPrefix ).arg( s ).arg( _max ) )
114 .append( QString( "%1%2def: %3\n" ).arg( sPrefix ).arg( s ).arg( _def ) )
115 .append( QString( "%1%2points:\n" ).arg( sPrefix ).arg( s ) );
116 for ( auto pp : _points ) {
117 sOutput.append( QString( "%1%2%3 : %4\n" ).arg( sPrefix ).arg( s ).arg( pp.first ).arg( pp.second ) );
118 }
119 } else {
120
121 sOutput = QString( "[AutomationPath]" )
122 .append( QString( " min: %1" ).arg( _min ) )
123 .append( QString( ", max: %1" ).arg( _max ) )
124 .append( QString( ", def: %1" ).arg( _def ) )
125 .append( QString( ", [points: " ) );
126 for ( auto pp : _points ) {
127 sOutput.append( QString( "(%1: %4) " ).arg( pp.first ).arg( pp.second ) );
128 }
129 sOutput.append( "]" );
130 }
131
132 return sOutput;
133}
134
142{
143 const float limit = 0.5f;
144
145 if (_points.empty()) {
146 return _points.end();
147 }
148
149 auto i = _points.lower_bound(x);
150
151 if (i != _points.end()) {
152 if( i->first - x <= limit) {
153 return i;
154 }
155 }
156
157 /* If there is a point before, check whether
158 * it is a close match */
159 if (i != _points.begin()) {
160 --i;
161 if( x - i->first <= limit) {
162 return i;
163 }
164 }
165
166 return _points.end();
167}
168
169
177{
178 _points.erase(in);
179 auto rv = _points.insert(std::make_pair(x,y));
181 return rv.first;
182}
183
184
190{
191 auto it = find(x);
192 if (it != _points.end()) {
193 _points.erase(it);
194 }
196}
197
198} //namespace H2Core
std::map< float, float >::iterator iterator
AutomationPath(float min, float max, float def)
std::map< float, float > _points
float get_value(float x) const noexcept
Get value at given location.
QString toQString(const QString &sPrefix="", bool bShort=true) const override
Formatted string version for debugging purposes.
void remove_point(float x)
Remove point from path.
iterator move(iterator &in, float x, float y)
Move point to other location.
iterator find(float x)
Find point near specific location.
void add_point(float x, float y)
Add a point to path.
static QString sPrintIndention
String used to format the debugging string output of some core classes.
Definition Object.h:127
static Hydrogen * get_instance()
Returns the current Hydrogen instance __instance.
Definition Hydrogen.h:83
void setIsModified(bool bIsModified)
Wrapper around Song::setIsModified() that checks whether a song is set.
bool operator==(const AutomationPath &lhs, const AutomationPath &rhs)
Compare two paths.
bool operator!=(const AutomationPath &lhs, const AutomationPath &rhs)