Audacity 3.2.0
AudioIOBase.h
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5AudioIOBase.h
6
7Paul Licameli split from AudioIO.h
8
9**********************************************************************/
10
11#ifndef __AUDACITY_AUDIO_IO_BASE__
12#define __AUDACITY_AUDIO_IO_BASE__
13
14#include <atomic>
15#include <cfloat>
16#include <chrono>
17#include <functional>
18#include <optional>
19#include <vector>
20#include <wx/string.h>
21#include "MemoryX.h"
22
23struct PaDeviceInfo;
24typedef void PaStream;
25
26#if USE_PORTMIXER
27typedef void PxMixer;
28#endif
29
30class AudioIOBase;
31
32class AudacityProject;
33class AudioIOListener;
34class BoundedEnvelope;
35class Meter;
36using PRCrossfadeData = std::vector< std::vector < float > >;
37
38#define BAD_STREAM_TIME (-DBL_MAX)
39
40class 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_)
49 , envelope(nullptr)
50 , rate(rate_)
51 , preRoll(0.0)
52 {}
53
54 std::shared_ptr<AudacityProject> pProject;
55 std::weak_ptr<Meter> captureMeter, playbackMeter;
56 const BoundedEnvelope *envelope; // for time warping
57 std::shared_ptr< AudioIOListener > listener;
58 double rate;
59 mutable std::optional<double> pStartTime;
60 double preRoll;
61
62 bool playNonWaveTracks{ true };
63
64 // contents may get swapped with empty vector
66
67 // An unfortunate thing needed just to make scrubbing work on Linux when
68 // we can't use a separate polling thread.
69 // The return value is duration to sleep before calling again
70 std::function< std::chrono::milliseconds() > playbackStreamPrimer;
71
72 using PolicyFactory = std::function<
73 std::unique_ptr<PlaybackPolicy>(const AudioIOStartStreamOptions&) >;
75
76 bool loopEnabled{ false };
77 bool variableSpeed{ false };
78};
79
81 wxString filename; // For crash report bundle
82 wxString text; // One big string, may be localized
83 wxString description; // Non-localized short description
84};
85
87class AUDIO_DEVICES_API AudioIOExtBase
88{
89public:
90 virtual ~AudioIOExtBase();
91
92 // Formerly in AudioIOBase
93 virtual bool IsOtherStreamActive() const = 0;
94
96 virtual AudioIODiagnostics Dump() const = 0;
97};
98
101class AUDIO_DEVICES_API AudioIOBase /* not final */
102 : public NonInterferingBase
103{
104public:
105 static AudioIOBase *Get();
106
108 virtual ~AudioIOBase();
109
110 AudioIOBase(const AudioIOBase &) = delete;
112
113 void SetCaptureMeter(
114 const std::shared_ptr<AudacityProject> &project, const std::weak_ptr<Meter> &meter);
115 void SetPlaybackMeter(
116 const std::shared_ptr<AudacityProject> &project, const std::weak_ptr<Meter> &meter);
117
126 void HandleDeviceChange();
127
140 static std::vector<long> GetSupportedPlaybackRates(int DevIndex = -1,
141 double rate = 0.0);
142
155 static std::vector<long> GetSupportedCaptureRates(int devIndex = -1,
156 double rate = 0.0);
157
172 static std::vector<long> GetSupportedSampleRates(int playDevice = -1,
173 int recDevice = -1,
174 double rate = 0.0);
175
184 static int GetOptimalSupportedSampleRate();
185
190 static const int StandardRates[];
192 static const int NumStandardRates;
193
197 wxString GetDeviceInfo() const;
198
200 std::vector<AudioIODiagnostics> GetAllDeviceInfo();
201
203 bool IsPaused() const;
204
205 virtual void StopStream() = 0;
206
211 bool IsBusy() const;
212
219 bool IsStreamActive() const;
220 bool IsStreamActive(int token) const;
221
227 bool IsAudioTokenActive(int token) const;
228
231 bool IsMonitoring() const;
232
233 /* Mixer services are always available. If no stream is running, these
234 * methods use whatever device is specified by the preferences. If a
235 * stream *is* running, naturally they manipulate the mixer associated
236 * with that stream. If no mixer is available, output is emulated and
237 * input is stuck at 1.0f (a gain is applied to output samples).
238 */
239 void SetMixer(int inputSource);
240
241protected:
242 static std::unique_ptr<AudioIOBase> ugAudioIO;
243 static wxString DeviceName(const PaDeviceInfo* info);
244 static wxString HostName(const PaDeviceInfo* info);
245
246 std::weak_ptr<AudacityProject> mOwningProject;
247
249 std::atomic<bool> mPaused{ false };
250
252 int mStreamToken{ 0 };
253
255
256 double mRate;
257
259
260 std::weak_ptr<Meter> mInputMeter{};
261 std::weak_ptr<Meter> mOutputMeter{};
262
263 #if USE_PORTMIXER
264 PxMixer *mPortMixer;
265 float mPreviousHWPlaythrough;
266 #endif /* USE_PORTMIXER */
267
276
277 // For cacheing supported sample rates
279 static std::vector<long> mCachedPlaybackRates;
281 static std::vector<long> mCachedCaptureRates;
282 static std::vector<long> mCachedSampleRates;
283 static double mCachedBestRateIn;
284
285protected:
293 static int getRecordDevIndex(const wxString &devName = {});
294
299#if USE_PORTMIXER
300 static int getRecordSourceIndex(PxMixer *portMixer);
301#endif
302
310 static int getPlayDevIndex(const wxString &devName = {});
311
316 static const int RatesToTry[];
318 static const int NumRatesToTry;
319
323 std::vector<std::unique_ptr<AudioIOExtBase>> mAudioIOExt;
324};
325
326#include "Prefs.h"
327
328extern AUDIO_DEVICES_API StringSetting AudioIOHost;
329extern AUDIO_DEVICES_API DoubleSetting AudioIOLatencyCorrection;
330extern AUDIO_DEVICES_API DoubleSetting AudioIOLatencyDuration;
331extern AUDIO_DEVICES_API StringSetting AudioIOPlaybackDevice;
332extern AUDIO_DEVICES_API StringSetting AudioIOPlaybackSource;
333extern AUDIO_DEVICES_API DoubleSetting AudioIOPlaybackVolume;
334extern AUDIO_DEVICES_API IntSetting AudioIORecordChannels;
335extern AUDIO_DEVICES_API StringSetting AudioIORecordingDevice;
336extern AUDIO_DEVICES_API StringSetting AudioIORecordingSource;
337extern AUDIO_DEVICES_API IntSetting AudioIORecordingSourceIndex;
338
339#endif
void PaStream
Definition: AudioIOBase.h:23
AUDIO_DEVICES_API IntSetting AudioIORecordChannels
AUDIO_DEVICES_API DoubleSetting AudioIOLatencyCorrection
AUDIO_DEVICES_API StringSetting AudioIOHost
AUDIO_DEVICES_API StringSetting AudioIORecordingSource
AUDIO_DEVICES_API StringSetting AudioIOPlaybackSource
AUDIO_DEVICES_API DoubleSetting AudioIOLatencyDuration
AUDIO_DEVICES_API StringSetting AudioIOPlaybackDevice
AUDIO_DEVICES_API DoubleSetting AudioIOPlaybackVolume
AUDIO_DEVICES_API StringSetting AudioIORecordingDevice
AUDIO_DEVICES_API IntSetting AudioIORecordingSourceIndex
std::vector< std::vector< float > > PRCrossfadeData
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:89
A singleton object supporting queries of the state of any active audio streams, and audio device capa...
Definition: AudioIOBase.h:103
PaStream * mPortStreamV19
Definition: AudioIOBase.h:258
static std::vector< long > mCachedSampleRates
Definition: AudioIOBase.h:282
double mRate
Audio playback rate in samples per second.
Definition: AudioIOBase.h:256
static int mCachedCaptureIndex
Definition: AudioIOBase.h:280
AudioIOBase & operator=(const AudioIOBase &)=delete
virtual ~AudioIOBase()
static std::unique_ptr< AudioIOBase > ugAudioIO
Definition: AudioIOBase.h:242
std::vector< std::unique_ptr< AudioIOExtBase > > mAudioIOExt
Definition: AudioIOBase.h:323
static double mCachedBestRateIn
Definition: AudioIOBase.h:283
virtual void StopStream()=0
AudioIOBase(const AudioIOBase &)=delete
static std::vector< long > mCachedCaptureRates
Definition: AudioIOBase.h:281
std::weak_ptr< AudacityProject > mOwningProject
Definition: AudioIOBase.h:246
bool mInputMixerWorks
Can we control the hardware input level?
Definition: AudioIOBase.h:275
static const int NumRatesToTry
How many sample rates to try.
Definition: AudioIOBase.h:318
static const int NumStandardRates
How many standard sample rates there are.
Definition: AudioIOBase.h:192
static std::vector< long > mCachedPlaybackRates
Definition: AudioIOBase.h:279
static int mCachedPlaybackIndex
Definition: AudioIOBase.h:278
Abstract interface to alternative, concurrent playback with the main audio (such as MIDI events)
Definition: AudioIOBase.h:88
virtual bool IsOtherStreamActive() const =0
virtual AudioIODiagnostics Dump() const =0
Get diagnostic information for audio devices and also for extensions.
virtual ~AudioIOExtBase()
Monitors record play start/stop and new sample blocks. Has callbacks for these events.
Specialization of Setting for double.
Definition: Prefs.h:356
Specialization of Setting for int.
Definition: Prefs.h:349
AudioIO uses this to send sample buffers for real-time display updates.
Definition: Meter.h:16
Directs which parts of tracks to fetch for playback.
Specialization of Setting for strings.
Definition: Prefs.h:363
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:26
wxString description
Definition: AudioIOBase.h:83
struct holding stream options, including a pointer to the time warp info and AudioIOListener and whet...
Definition: AudioIOBase.h:44
std::function< std::chrono::milliseconds() > playbackStreamPrimer
Definition: AudioIOBase.h:70
PolicyFactory policyFactory
Definition: AudioIOBase.h:74
AudioIOStartStreamOptions(const std::shared_ptr< AudacityProject > &pProject, double rate_)
Definition: AudioIOBase.h:46
std::shared_ptr< AudacityProject > pProject
Definition: AudioIOBase.h:54
std::weak_ptr< Meter > captureMeter
Definition: AudioIOBase.h:55
std::weak_ptr< Meter > playbackMeter
Definition: AudioIOBase.h:55
PRCrossfadeData * pCrossfadeData
Definition: AudioIOBase.h:65
const BoundedEnvelope * envelope
Definition: AudioIOBase.h:56
std::function< std::unique_ptr< PlaybackPolicy >(const AudioIOStartStreamOptions &) > PolicyFactory
Definition: AudioIOBase.h:73
std::optional< double > pStartTime
Definition: AudioIOBase.h:59
std::shared_ptr< AudioIOListener > listener
Definition: AudioIOBase.h:57