summaryrefslogtreecommitdiffstats
path: root/noatun/library/noatun/vequalizer.h
blob: 5e49447203000cb7a6baa41c6c68b5f3e6cb07a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 * Copyright (c) 2003 Charles Samuels <charles@kde.org>
 *
 * This file is hereby licensed under the GNU General Public License version
 * 2 or later at your option.
 *
 * This file is licensed under the TQt Public License version 1 with the
 * condition that the licensed will be governed under the Laws of California
 * (USA) instead of Norway.  Disputes will be settled in Santa Clara county
 * courts.
 *
 * This file is licensed under the following additional license.  Be aware
 * that it is identical to the BSD license, except for the added clause 3:

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. By integrating this software into any other software codebase, you waive
    all rights to any patents associated with the stated codebase.

 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef NOATUN_VETQUALIZER_H
#define NOATUN_VETQUALIZER_H

#include <tqptrlist.h>
#include <tqobject.h>
#include <kurl.h>
#include <kdemacros.h>
class VBand;

/**
 * Abstract base class for VInterpolation and VEqualizer
 **/
class VBandsInterface
{
	friend class VBand;

public:
	VBandsInterface();
	virtual ~VBandsInterface();

	virtual int bands() const=0;
	virtual VBand band(int num)=0;
	/**
	 * same as @ref band(num)
	 **/
	VBand operator [] (int num);

private:
	virtual int level(int index) const=0;
	virtual void setLevel(int index, int level)=0;
};


class VInterpolation;
class VEqualizer;

/**
 * Represents a single band in a vequalizer
 * @author Charles Samuels
 **/
class KDE_EXPORT VBand
{
	friend class VInterpolation;
	friend class VEqualizer;

	struct Private;
	VBand::Private *d;

private:
	VBand(VBandsInterface *bands, int index, int start, int end);

public:
	~VBand();

	VBand(const VBand &copy);
	VBand & operator =(const VBand &copy);

	/**
	* the intensity of the change.
	* it's logarithmic.  0 is no change
	* negative numbers are loss in intensity
	* positive numbers are a gain
	* And +-100 is the most you'd need to go
	**/
	int level() const;
	void setLevel(int l);

	int start() const;
	int end() const;

	/**
	* the middle between start and end
	**/
	int center() const;

	TQString formatStart(bool withHz=true) const;
	TQString formatEnd(bool withHz=true) const;
	/**
	* return the format for center()
	**/
	TQString format(bool withHz=true) const;
};


/**
 * This class is an interpolated representation to the Eq data.
 * This means that no matter how many bands
 * the real equalizer has, your interpolated
 * version appears like it has only as many
 * bands as you asked for. You can continue
 * to interact with all interpolations and
 * the true equalizer as normal. They even
 * modify eachother according to a Spline
 * interpolation.
 *
 * @short interpolated representation of Eq data
 * @author Charles Samuels
 **/
class KDE_EXPORT VInterpolation : public TQObject, public VBandsInterface
{
	Q_OBJECT
  TQ_OBJECT
	struct Private;
	Private *d;
	friend class VBand;

public:
	/**
	* create an interpolation with the one and only
	* Noatun equalizer, and @p bands
	**/
	VInterpolation(int bands);
	virtual ~VInterpolation();

	virtual int bands() const;
	virtual VBand band(int num);
	virtual int level(int index) const;
	virtual void setLevel(int index, int level);

signals:
	void changed();

private:
	void refresh();
	void getFrequencies(int num, int *high, int *low) const;
	/**
	 * where on the spline is my own interpolation's bandNum
	 * @returns an x value on the spline
	 **/
	double onSpline(int bandNum) const;
};

class VPreset;

/**
 * @short Noatun EQ
 * @author Charles Samuels
 **/
class KDE_EXPORT VEqualizer : public TQObject, public VBandsInterface
{
	Q_OBJECT
  TQ_OBJECT
	friend class VBand;
	friend class VPreset;
	friend class Engine;
	friend class NoatunApp;
	friend class VInterpolation;

	struct Private;
	Private *d;

	VEqualizer();
	~VEqualizer();
	void init();

public:

	/**
	 * @return the frequencies to use with @p num many
	 * bands.  This is a list of the upper frequency
	 * of each band, for example: { 40, 60, 100 } if
	 * you hear up to 40hz
	 **/
	static TQValueList<int> frequencies(int num);

	/**
	 * @return number of bands I have, which may be different
	 * than what setBands was called with
	 * @sa setBands
	 **/
	int bands() const;

	/**
	 * @return first frequency I operate on, currently 20
	 *
	 * does not apply to an interpolation
	 **/
	static int start();
	/**
	 * @return last frequency I operate on, currently 20000
	 */
	static int end();

	/**
	 * @return maximum amount of bands I may have
	 **/
	int maxBands() const;

	/**
	 * the minimum number of bands I may have (2)
	 **/
	int minBands() const;

	VBand band(int num);

	bool isEnabled() const;

	/**
	 * returns the level of preamp (-100 to 100 normally)
	 **/
	int preamp() const;

public: // serialization
	/**
	 * save the current levels
	 * all noatun equalizer files have the "*.noatunequalizer"
	 * pattern.  Nevertheless, the file can be identified
	 * by magic, so it's not required
	 **/
	bool save(const KURL &file, const TQString &friendly) const;

	/**
	 * restore the EQ settings from this file
	 **/
	bool load(const KURL &file);

	/**
	 * convert the current EQ settings to string form,
	 * suitable for storage, the given string is the title
	 *
	 * @see fromString
	 **/
	TQString toString(const TQString &name="stored") const;

	/**
	 * undo a toString, restoring the EQ
	 * to the settings in the given string,
	 * emitting the changed signals
	 **/
	bool fromString(const TQString &str);

public: // presets
	/**
	 * create a preset with such a name
	 * and remember that it'l return an invalied Preset
	 * if the name already exists
	 *
	 * If smart is true, append a number to the end
	 * of the name, if one already exists by the given
	 **/
	VPreset createPreset(const TQString &name, bool smart=true);

	/**
	 * return all the presets
	 * remember to setAutoDelete on this
	 **/
	TQValueList<VPreset> presets() const;

	/**
	 * @returns the preset with the given name
	 * or an invalid Preset if none exists
	 **/
	VPreset presetByName(const TQString &name);

	/**
	 * @returns the preset in the given file
	 * or an invalid Preset if none exists
	 **/
	VPreset presetByFile(const TQString &file);

	/**
	 * @returns whether a preset with the
	 * given name exists
	 **/
	bool presetExists(const TQString &name) const;

signals:
	/**
	 * emitted when the number of bands has changed (and hence
	 * all my associated Bands are useless
	 **/
	void changedBands();

	/**
	 * emitted when something changes.  Preamplication, band level,
	 * number of bands, enabled/disabled
	 **/
	void changed();
	/**
	 * emitted when the value of one or more of the bands
	 * has changed, but not immediately after
	 * a changedBands
	 **/
	void modified();

	void preampChanged();
	void preampChanged(int);

	void enabled();
	void disabled();

	void enabled(bool e);

	/**
	 * emitted when a preset is created
	 **/
	void created(VPreset preset);

	/**
	 * emitted when the given @p preset is
	 * selected
	 **/
	void selected(VPreset preset);

	/**
	 * when @p preset has been renamed
	 **/
	void renamed(VPreset preset);

	/**
	 * the given @p preset has been removed
	 **/
	void removed(VPreset preset);
public slots:
	/**
	 * set the preamplification
	 * it's logarithmic.  0 is no change
	 * negative numbers are loss in intensity
	 * positive numbers are a gain
	 * And +-100 is the most you'd need to go
	 **/
	void setPreamp(int p);
	/**
	 * turn on EQ
	 */
	void enable();
	/**
	 * turn off EQ
	 */
	void disable();
	/**
	 * turn on/off EQ depending on @p e
	 */
	void setEnabled(bool e);
	void setBands(int bands);
	void setBands(int bands, bool interpolate);

private:
	virtual int level(int index) const;
	virtual void setLevel(int index, int level);
	void setLevels(const TQValueList<int> &levels);

private:
	/**
	 * tell the filter to start using the new settings
	 * if @p full is false, it doesn't take the full
	 * update, but just the values (not the number of
	 * bands
	 **/
	void update(bool full=false);
};

/**
 * a preset stores the state of the equalizer
 *
 * VEqualizer provides a way to get a list of these
 * or access them by name
 *
 * this acts as a reference to the preset, so
 * it might be invalid in which case
 * isValid() is false, isNull() is true, and
 * operator bool() return false
 **/
class VPreset
{
	friend class VEqualizer;

	struct Private;
	Private *d;

	void *_bc;
	VPreset(const TQString &file);
public:
	VPreset();
	VPreset(const VPreset &copy);
	VPreset & operator=(const VPreset &copy);
	~VPreset();
	bool operator ==(const VPreset &other) const;

	/**
	 * @returns the name of the preset, which is user visible
	 **/
	TQString name() const;
	/**
	 * set the user-presentable name of the preset
	 *
	 * Equalizer will emit renamed
	 *
	 * @returns success.  If another preset is named
	 * this, it'l fail.
	 **/
	bool setName(const TQString &name);

	/**
	 * @returns the file that this preset is in
	 **/
	TQString file() const;

	bool isValid() const;
	bool isNull() const { return !isValid(); }
	operator bool() const { return isValid(); }

	/**
	 * save the state of the equalizer into this preset
	 **/
	void save();

	/**
	 * load the preset into the equalizer
	 **/
	void load() const;

	/**
	 * obliterate this preset!
	 **/
	void remove();
};

#endif