Audacity  3.0.3
AudioIOBase.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 AudioIOBase.h
6 
7 Paul Licameli split from AudioIO.h
8 
9 **********************************************************************/
10 
11 #ifndef __AUDACITY_AUDIO_IO_BASE__
12 #define __AUDACITY_AUDIO_IO_BASE__
13 
14 
15 
16 
17 #include <cfloat>
18 #include <functional>
19 #include <vector>
20 #include <wx/string.h>
21 #include "MemoryX.h"
22 
23 struct PaDeviceInfo;
24 typedef void PaStream;
25 
26 #if USE_PORTMIXER
27 typedef void PxMixer;
28 #endif
29 
30 class AudioIOBase;
31 
32 class AudacityProject;
33 class AudioIOListener;
34 class BoundedEnvelope;
35 class Meter;
36 using PRCrossfadeData = std::vector< std::vector < float > >;
37 
38 #define BAD_STREAM_TIME (-DBL_MAX)
39 
40 class PlaybackPolicy;
41 
42 // To avoid growing the argument list of StartStream, add fields here
44 {
45  explicit
47  const std::shared_ptr<AudacityProject> &pProject, double rate_)
48  : pProject{ pProject }
49  , envelope(nullptr)
50  , rate(rate_)
51  , pStartTime(NULL)
52  , preRoll(0.0)
53  {}
54 
55  std::shared_ptr<AudacityProject> pProject;
56  std::weak_ptr<Meter> captureMeter, playbackMeter;
57  const BoundedEnvelope *envelope; // for time warping
58  std::shared_ptr< AudioIOListener > listener;
59  double rate;
60  double * pStartTime;
61  double preRoll;
62 
63  bool playNonWaveTracks{ true };
64 
65  // contents may get swapped with empty vector
67 
68  // An unfortunate thing needed just to make scrubbing work on Linux when
69  // we can't use a separate polling thread.
70  // The return value is a number of milliseconds to sleep before calling again
71  std::function< unsigned long() > playbackStreamPrimer;
72 
73  using PolicyFactory = std::function< std::unique_ptr<PlaybackPolicy>() >;
75 };
76 
78  wxString filename; // For crash report bundle
79  wxString text; // One big string, may be localized
80  wxString description; // Non-localized short description
81 };
82 
84 class AUDIO_DEVICES_API AudioIOExtBase
85 {
86 public:
87  virtual ~AudioIOExtBase();
88 
89  // Formerly in AudioIOBase
90  virtual bool IsOtherStreamActive() const = 0;
91 
93  virtual AudioIODiagnostics Dump() const = 0;
94 };
95 
98 class AUDIO_DEVICES_API AudioIOBase /* not final */
99  : public NonInterferingBase
100 {
101 public:
102  static AudioIOBase *Get();
103 
105  virtual ~AudioIOBase();
106 
107  AudioIOBase(const AudioIOBase &) = delete;
108  AudioIOBase &operator=(const AudioIOBase &) = delete;
109 
110  void SetCaptureMeter(
111  const std::shared_ptr<AudacityProject> &project, const std::weak_ptr<Meter> &meter);
112  void SetPlaybackMeter(
113  const std::shared_ptr<AudacityProject> &project, const std::weak_ptr<Meter> &meter);
114 
123  void HandleDeviceChange();
124 
137  static std::vector<long> GetSupportedPlaybackRates(int DevIndex = -1,
138  double rate = 0.0);
139 
152  static std::vector<long> GetSupportedCaptureRates(int devIndex = -1,
153  double rate = 0.0);
154 
169  static std::vector<long> GetSupportedSampleRates(int playDevice = -1,
170  int recDevice = -1,
171  double rate = 0.0);
172 
181  static int GetOptimalSupportedSampleRate();
182 
187  static const int StandardRates[];
189  static const int NumStandardRates;
190 
194  wxString GetDeviceInfo() const;
195 
197  std::vector<AudioIODiagnostics> GetAllDeviceInfo();
198 
200  bool IsPaused() const;
201 
202  virtual void StopStream() = 0;
203 
208  bool IsBusy() const;
209 
216  bool IsStreamActive() const;
217  bool IsStreamActive(int token) const;
218 
224  bool IsAudioTokenActive(int token) const;
225 
228  bool IsMonitoring() const;
229 
230  /* Mixer services are always available. If no stream is running, these
231  * methods use whatever device is specified by the preferences. If a
232  * stream *is* running, naturally they manipulate the mixer associated
233  * with that stream. If no mixer is available, output is emulated and
234  * input is stuck at 1.0f (a gain is applied to output samples).
235  */
236  void SetMixer(int inputSource);
237 
238 protected:
239  static std::unique_ptr<AudioIOBase> ugAudioIO;
240  static wxString DeviceName(const PaDeviceInfo* info);
241  static wxString HostName(const PaDeviceInfo* info);
242 
243  std::weak_ptr<AudacityProject> mOwningProject;
244 
246  bool mPaused;
247 
248  volatile int mStreamToken;
249 
251  double mRate;
252 
254 
255  std::weak_ptr<Meter> mInputMeter{};
256  std::weak_ptr<Meter> mOutputMeter{};
257 
258  #if USE_PORTMIXER
259  PxMixer *mPortMixer;
260  float mPreviousHWPlaythrough;
261  #endif /* USE_PORTMIXER */
262 
271 
272  // For cacheing supported sample rates
274  static std::vector<long> mCachedPlaybackRates;
276  static std::vector<long> mCachedCaptureRates;
277  static std::vector<long> mCachedSampleRates;
278  static double mCachedBestRateIn;
279 
280 protected:
288  static int getRecordDevIndex(const wxString &devName = {});
289 
294 #if USE_PORTMIXER
295  static int getRecordSourceIndex(PxMixer *portMixer);
296 #endif
297 
305  static int getPlayDevIndex(const wxString &devName = {});
306 
311  static const int RatesToTry[];
313  static const int NumRatesToTry;
314 
318  std::vector<std::unique_ptr<AudioIOExtBase>> mAudioIOExt;
319 };
320 
321 #include "Prefs.h"
322 
323 extern AUDIO_DEVICES_API StringSetting AudioIOHost;
324 extern AUDIO_DEVICES_API DoubleSetting AudioIOLatencyCorrection;
325 extern AUDIO_DEVICES_API DoubleSetting AudioIOLatencyDuration;
326 extern AUDIO_DEVICES_API StringSetting AudioIOPlaybackDevice;
327 extern AUDIO_DEVICES_API DoubleSetting AudioIOPlaybackVolume;
328 extern AUDIO_DEVICES_API IntSetting AudioIORecordChannels;
329 extern AUDIO_DEVICES_API StringSetting AudioIORecordingDevice;
330 extern AUDIO_DEVICES_API StringSetting AudioIORecordingSource;
331 extern AUDIO_DEVICES_API IntSetting AudioIORecordingSourceIndex;
332 
333 #endif
AudioIOListener
Monitors record play start/stop and new sample blocks. Has callbacks for these events.
Definition: AudioIOListener.h:22
AudioIORecordingSource
AUDIO_DEVICES_API StringSetting AudioIORecordingSource
Definition: AudioIOBase.cpp:947
AudioIOStartStreamOptions::playbackMeter
std::weak_ptr< Meter > playbackMeter
Definition: AudioIOBase.h:56
AudioIOStartStreamOptions::rate
double rate
Definition: AudioIOBase.h:59
AudioIOStartStreamOptions::PolicyFactory
std::function< std::unique_ptr< PlaybackPolicy >() > PolicyFactory
Definition: AudioIOBase.h:73
AudioIOStartStreamOptions::listener
std::shared_ptr< AudioIOListener > listener
Definition: AudioIOBase.h:58
AudioIOLatencyCorrection
AUDIO_DEVICES_API DoubleSetting AudioIOLatencyCorrection
Definition: AudioIOBase.cpp:935
IntSetting
Specialization of Setting for int.
Definition: Prefs.h:214
AudioIOBase::mPortStreamV19
PaStream * mPortStreamV19
Definition: AudioIOBase.h:253
PaStream
void PaStream
Definition: AudioIOBase.h:23
AudioIOBase::mInputMixerWorks
bool mInputMixerWorks
Can we control the hardware input level?
Definition: AudioIOBase.h:270
AudioIODiagnostics::text
wxString text
Definition: AudioIOBase.h:79
AudioIOBase::operator=
AudioIOBase & operator=(const AudioIOBase &)=delete
AudioIODiagnostics::filename
wxString filename
Definition: AudioIOBase.h:78
Meter
AudioIO uses this to send sample buffers for real-time display updates.
Definition: Meter.h:16
AudioIOExtBase::Dump
virtual AudioIODiagnostics Dump() const =0
Get diagnostic information for audio devices and also for extensions.
AudioIOStartStreamOptions::envelope
const BoundedEnvelope * envelope
Definition: AudioIOBase.h:57
AudioIOStartStreamOptions::policyFactory
PolicyFactory policyFactory
Definition: AudioIOBase.h:74
AudioIOStartStreamOptions::preRoll
double preRoll
Definition: AudioIOBase.h:61
AudioIOBase::NumStandardRates
static const int NumStandardRates
How many standard sample rates there are.
Definition: AudioIOBase.h:189
AudioIOStartStreamOptions::captureMeter
std::weak_ptr< Meter > captureMeter
Definition: AudioIOBase.h:56
PRCrossfadeData
std::vector< std::vector< float > > PRCrossfadeData
Definition: PlaybackSchedule.h:22
AudioIOExtBase::IsOtherStreamActive
virtual bool IsOtherStreamActive() const =0
AudioIOBase::ugAudioIO
static std::unique_ptr< AudioIOBase > ugAudioIO
Definition: AudioIOBase.h:239
AudioIOExtBase::~AudioIOExtBase
virtual ~AudioIOExtBase()
AudioIOExtBase
Abstract interface to alternative, concurrent playback with the main audio (such as MIDI events)
Definition: AudioIOBase.h:85
AudioIOBase::mCachedSampleRates
static std::vector< long > mCachedSampleRates
Definition: AudioIOBase.h:277
AudioIODiagnostics::description
wxString description
Definition: AudioIOBase.h:80
AudioIOPlaybackDevice
AUDIO_DEVICES_API StringSetting AudioIOPlaybackDevice
Definition: AudioIOBase.cpp:939
StringSetting
Specialization of Setting for strings.
Definition: Prefs.h:228
AudioIOBase::NumRatesToTry
static const int NumRatesToTry
How many sample rates to try.
Definition: AudioIOBase.h:313
BoundedEnvelope
Definition: Envelope.h:284
AudioIOBase::mOwningProject
std::weak_ptr< AudacityProject > mOwningProject
Definition: AudioIOBase.h:243
AudioIOBase::mPaused
bool mPaused
True if audio playback is paused.
Definition: AudioIOBase.h:246
AudioIOBase::AudioIOBase
AudioIOBase(const AudioIOBase &)=delete
AudioIOBase::mCachedPlaybackRates
static std::vector< long > mCachedPlaybackRates
Definition: AudioIOBase.h:274
BasicUI::Get
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:26
AudioIOBase::AudioIOBase
AudioIOBase()
AudioIOBase::mCachedPlaybackIndex
static int mCachedPlaybackIndex
Definition: AudioIOBase.h:273
AudioIODiagnostics
Definition: AudioIOBase.h:77
AudioIOBase::mRate
double mRate
Audio playback rate in samples per second.
Definition: AudioIOBase.h:251
AudioIORecordChannels
AUDIO_DEVICES_API IntSetting AudioIORecordChannels
Definition: AudioIOBase.cpp:943
AudioIOStartStreamOptions
struct holding stream options, including a pointer to the time warp info and AudioIOListener and whet...
Definition: AudioIOBase.h:44
AudioIOLatencyDuration
AUDIO_DEVICES_API DoubleSetting AudioIOLatencyDuration
Definition: AudioIOBase.cpp:937
AudioIOBase::mCachedCaptureIndex
static int mCachedCaptureIndex
Definition: AudioIOBase.h:275
AudioIOStartStreamOptions::AudioIOStartStreamOptions
AudioIOStartStreamOptions(const std::shared_ptr< AudacityProject > &pProject, double rate_)
Definition: AudioIOBase.h:46
AudioIOStartStreamOptions::playNonWaveTracks
bool playNonWaveTracks
Definition: AudioIOBase.h:63
AudioIOPlaybackVolume
AUDIO_DEVICES_API DoubleSetting AudioIOPlaybackVolume
Definition: AudioIOBase.cpp:941
AudioIOStartStreamOptions::pStartTime
double * pStartTime
Definition: AudioIOBase.h:60
AudioIOHost
AUDIO_DEVICES_API StringSetting AudioIOHost
Definition: AudioIOBase.cpp:933
AudioIOStartStreamOptions::pCrossfadeData
PRCrossfadeData * pCrossfadeData
Definition: AudioIOBase.h:66
MemoryX.h
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:92
AudioIORecordingDevice
AUDIO_DEVICES_API StringSetting AudioIORecordingDevice
Definition: AudioIOBase.cpp:945
AudioIORecordingSourceIndex
AUDIO_DEVICES_API IntSetting AudioIORecordingSourceIndex
Definition: AudioIOBase.cpp:949
AudioIOBase::StopStream
virtual void StopStream()=0
AudioIOBase::mCachedBestRateIn
static double mCachedBestRateIn
Definition: AudioIOBase.h:278
AudioIOBase::mStreamToken
volatile int mStreamToken
Definition: AudioIOBase.h:248
Prefs.h
AudioIOStartStreamOptions::playbackStreamPrimer
std::function< unsigned long() > playbackStreamPrimer
Definition: AudioIOBase.h:71
AudioIOBase
A singleton object supporting queries of the state of any active audio streams, and audio device capa...
Definition: AudioIOBase.h:100
AudioIOBase::mCachedCaptureRates
static std::vector< long > mCachedCaptureRates
Definition: AudioIOBase.h:276
DoubleSetting
Specialization of Setting for double.
Definition: Prefs.h:221
PlaybackPolicy
Directs which parts of tracks to fetch for playback.
Definition: PlaybackSchedule.h:173
AudioIOBase::~AudioIOBase
virtual ~AudioIOBase()
AudioIOStartStreamOptions::pProject
std::shared_ptr< AudacityProject > pProject
Definition: AudioIOBase.h:55