1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Rosegarden
5     A sequencer and musical notation editor.
6     Copyright 2000-2021 the Rosegarden development team.
7     See the AUTHORS file for more details.
8 
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.  See the file
13     COPYING included with this distribution for more information.
14 */
15 
16 #ifndef RG_AUDIO_LEVEL_H
17 #define RG_AUDIO_LEVEL_H
18 
19 #include "base/MidiProgram.h"  // for MidiByte
20 
21 namespace Rosegarden {
22 
23 
24 /// Audio math utility class.
25 /**
26  * We need to represent audio levels in three different ways: as dB
27  * values; as a floating-point multiplier for gain; and as an integer
28  * on a scale for fader position and vu level.  This class does the
29  * necessary conversions.
30  */
31 class AudioLevel
32 {
33 public:
34 
35     static constexpr float DB_FLOOR = -1000.0;
36 
37     enum FaderType {
38              ShortFader = 0, // -40 -> +6  dB
39               LongFader = 1, // -70 -> +10 dB
40             IEC268Meter = 2, // -70 ->  0  dB
41         IEC268LongMeter = 3, // -70 -> +10 dB (0dB aligns with LongFader)
42            PreviewLevel = 4
43     };
44 
45     static float multiplier_to_dB(float multiplier);
46     static float dB_to_multiplier(float dB);
47 
48     static float fader_to_dB(int level, int maxLevel, FaderType type);
49     static int   dB_to_fader(float dB, int maxFaderLevel, FaderType type);
50 
51     static float fader_to_multiplier(int level, int maxLevel, FaderType type);
52     static int   multiplier_to_fader(float multiplier, int maxFaderLevel,
53                                      FaderType type);
54 
55     // fast if "levels" doesn't change often -- for audio segment previews
56     static int   multiplier_to_preview(float multiplier, int levels);
57     static float preview_to_multiplier(int level, int levels);
58 
59     // Set or retrieve the number of the pan law.
setPanLaw(int panLaw)60     static void setPanLaw(int panLaw) { m_panLaw = panLaw; }
getPanLaw()61     static int getPanLaw() { return m_panLaw; }
62 
63     /// Apply pan law.  Assumes pan range 0 - 100 - 200.
64     static float panGainLeft(float pan);
65     static float panGainRight(float pan);
66 
67     /// Convert MIDI pan (0 - 64 - 127) to Audio pan (0 - 100 - 200).
68     /**
69      * You may need to subtract 100 from this as some parts of the system deal
70      * in -100 - 100 pan.  E.g. the pan knobs in the UI.
71      */
72     static double AudioPanD(MidiByte midiPan);
73     /// Integer version of AudioPanD().
74     static int AudioPanI(MidiByte midiPan);
75 
76     /// Convert Audio pan (0 - 100 - 200) to MIDI pan (0 - 64 - 127).
77     static MidiByte MIDIPanI(int audioPan);
78 
79 private:
80 
81     static int m_panLaw;  // number of pan law currently in use
82 };
83 
84 
85 }
86 
87 #endif
88 
89 
90