1 //=========================================================
2 //  MusE
3 //  Linux Music Editor
4 //
5 //  latency_info.h
6 //  (C) Copyright 2019 Tim E. Real (terminator356 on users dot sourceforge dot net)
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU General Public License
10 //  as published by the Free Software Foundation; version 2 of
11 //  the License, or (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, write to the Free Software
20 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 //=========================================================
23 
24 #ifndef __LATENCY_INFO_H__
25 #define __LATENCY_INFO_H__
26 
27 namespace MusECore {
28 
29 // During latency computations each process cycle,
30 //  this holds cached computed latency values.
31 struct TrackLatencyInfo
32 {
33   // Whether the dominance information is valid (has already been
34   //  gathered in the in the dominance latency scan).
35   // This is reset near the beginning of the process handler.
36   bool _dominanceProcessed;
37   bool _dominanceInputProcessed;
38   bool _canDominateProcessed;
39   bool _canDominateInputProcessed;
40   // Whether the correction information is valid (has already been gathered
41   //  in the correction latency scan).
42   // This is reset near the beginning of the process handler.
43   bool _correctionProcessed;
44   bool _correctionInputProcessed;
45   // Whether the _worstEffRackLatency has been processed.
46   bool _worstPluginLatencyProcessed;
47   // The worst contribution to latency by the channels in the track's effect rack.
48   float _worstPluginLatency;
49   // Whether the _worstPortLatency has been processed.
50   bool _worstPortLatencyProcessed;
51   // The worst contribution to latency by any ports (for ex. audio input/output tracks).
52   float _worstPortLatency;
53   // Whether the final latency information is valid (has already been gathered
54   //  in the in the final latency scan).
55   // This is reset near the beginning of the process handler.
56   bool _processed;
57   bool _inputProcessed;
58   bool _worstSelfLatencyProcessed;
59   bool _worstSelfLatencyMidiProcessed;
60   // Contributions to latency from rack plugins and/or Jack ports etc.
61   // This value is the worst-case latency of all the channels in a track.
62   // See AudioTrack::trackLatency().
63   float _worstSelfLatency;
64   float _worstSelfLatencyMidi;
65   // The absolute latency of all signals leaving a track, relative to audio driver frame (transport, etc).
66   // This value is the cumulative value of all series routes connected to this track, plus some
67   //  adjustment for the track's own members' latency.
68   // The goal is to have equal latency output on all channels.
69   // Thus the value will be the WORST-CASE latency of any channel. All other channels are delayed to match it.
70   // For example, a Wave Track can use this total value to appropriately shift recordings of the signals
71   //  arriving at its inputs.
72   float _outputLatency;
73   float _inputLatency;
74 
75   // Whether any of the connected output routes are effectively connected.
76   // That means track is not off, track is monitored where applicable, etc,
77   //   ie. signal can actually flow.
78   bool _isLatencyInputTerminal;
79   bool _isLatencyOutputTerminal;
80   bool _isLatencyInputTerminalProcessed;
81   bool _isLatencyOutputTerminalProcessed;
82   // Whether this track (and the branch it is in) can force other parallel branches to
83   //  increase their latency compensation to match this one.
84   // If false, this branch will NOT disturb other parallel branches' compensation,
85   //  intead only allowing compensation UP TO the worst case in other branches.
86   bool _canDominateOutputLatency;
87   bool _canDominateInputLatency;
88   // Whether this track and its branch can correct for latency, not just compensate.
89   bool _canCorrectOutputLatency;
90   // For tracks which can correct for latency, this is the value that the track
91   //  must shift (ahead) to correct. It is essentially the programmed latency value
92   //  of the track's ultimate source (wave file etc.). Therefore it will be NEGATIVE
93   //  when requiring correction, but never POSITIVE (that would be unnecessary,
94   //  a subsequent compensator delay can do that).
95   float _sourceCorrectionValue;
96   // Balances end points (Audio Outputs or open branches) of parallel branches.
97   unsigned long int _compensatorWriteOffset;
98 
99   //--------------------------------------------------------
100   // Special for Midi Tracks: We don't have Midi Track to Midi Port routes yet
101   //  because we don't have multiple Midi Track outputs yet, only a single output port.
102   // So we must store this information here just for Midi Tracks.
103   //
104   // All other tracks store this information in the route itself.
105   //
106   // Temporary variables used during latency calculations:
107   // Holds the output latency of this node, so that it can be compared with others.
108   float _latencyOutMidiTrack;
109   //--------------------------------------------------------
110 
111   //--------------------------------------------------------
112   // Special for Metronome: We don't have Metronome routes.
113   // So we must store this information here just for the metronome.
114   // Temporary variables used during latency calculations:
115   // Holds the output latency of this node, so that it can be compared with others.
116   float _latencyOutMetronome;
117   //--------------------------------------------------------
118 
119 
120   // Initializes (resets) the structure to prepare for (re)computation.
121   void initialize();
122 };
123 
124 } // namespace MusECore
125 
126 #endif
127