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 <map>
19#include <optional>
20#include <vector>
21#include <utility>
22#include <wx/string.h>
23#include "MemoryX.h"
24
25struct PaDeviceInfo;
26typedef void PaStream;
27
28#if USE_PORTMIXER
29typedef void PxMixer;
30#endif
31
32class AudioIOBase;
33
34class AudacityProject;
35class AudioIOListener;
36class BoundedEnvelope;
37class Meter;
38using PRCrossfadeData = std::vector< std::vector < float > >;
39
40#define BAD_STREAM_TIME (-DBL_MAX)
41
42class PlaybackPolicy;
43
44// To avoid growing the argument list of StartStream, add fields here
46{
47 explicit
49 const std::shared_ptr<AudacityProject> &pProject = {},
50 double rate_ = 44100.0
51 ) : pProject{ pProject }
52 , rate(rate_)
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 mutable std::optional<double> pStartTime;
61 double preRoll{ 0.0 };
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 duration to sleep before calling again
71 std::function< std::chrono::milliseconds() > playbackStreamPrimer;
72
73 using PolicyFactory = std::function<
74 std::unique_ptr<PlaybackPolicy>(const AudioIOStartStreamOptions&) >;
76
77 bool loopEnabled{ false };
78 bool variableSpeed{ false };
79};
80
82 wxString filename; // For crash report bundle
83 wxString text; // One big string, may be localized
84 wxString description; // Non-localized short description
85};
86
88class AUDIO_DEVICES_API AudioIOExtBase
89{
90public:
91 virtual ~AudioIOExtBase();
92
93 // Formerly in AudioIOBase
94 virtual bool IsOtherStreamActive() const = 0;
95
97 virtual AudioIODiagnostics Dump() const = 0;
98};
99
102class AUDIO_DEVICES_API AudioIOBase /* not final */
103 : public NonInterferingBase
104{
105public:
106 static AudioIOBase *Get();
107
109 virtual ~AudioIOBase();
110
111 AudioIOBase(const AudioIOBase &) = delete;
113
114 void SetCaptureMeter(
115 const std::shared_ptr<AudacityProject> &project, const std::weak_ptr<Meter> &meter);
116 void SetPlaybackMeter(
117 const std::shared_ptr<AudacityProject> &project, const std::weak_ptr<Meter> &meter);
118
127 void HandleDeviceChange();
128
139 static std::vector<long> GetSupportedPlaybackRates(int DevIndex = -1);
140
156 static long GetClosestSupportedPlaybackRate(int devIndex, long rate);
157
168 static std::vector<long> GetSupportedCaptureRates(int devIndex = -1);
169
185 static long GetClosestSupportedCaptureRate(int devIndex, long rate);
186
199 static std::vector<long> GetSupportedSampleRates(int playDevice = -1,
200 int recDevice = -1);
201
217 static long GetClosestSupportedSampleRate(int playDevice,
218 int recDevice, long rate);
219
228 static int GetOptimalSupportedSampleRate();
229
235 static bool IsPlaybackRateSupported(int devIndex, long rate);
236
242 static bool IsCaptureRateSupported(int devIndex, long rate);
243
248 static const int StandardRates[];
250 static const int NumStandardRates;
251
255 wxString GetDeviceInfo() const;
256
258 std::vector<AudioIODiagnostics> GetAllDeviceInfo();
259
261 bool IsPaused() const;
262
263 virtual void StopStream() = 0;
264
269 bool IsBusy() const;
270
277 bool IsStreamActive() const;
278 bool IsStreamActive(int token) const;
279
285 bool IsAudioTokenActive(int token) const;
286
289 bool IsMonitoring() const;
290
291 /* Mixer services are always available. If no stream is running, these
292 * methods use whatever device is specified by the preferences. If a
293 * stream *is* running, naturally they manipulate the mixer associated
294 * with that stream. If no mixer is available, output is emulated and
295 * input is stuck at 1.0f (a volume gain is applied to output samples).
296 */
297 void SetMixer(int inputSource);
298
299protected:
300 static std::unique_ptr<AudioIOBase> ugAudioIO;
301 static wxString DeviceName(const PaDeviceInfo* info);
302 static wxString HostName(const PaDeviceInfo* info);
303
304 std::weak_ptr<AudacityProject> mOwningProject;
305
307 std::atomic<bool> mPaused{ false };
308
310 int mStreamToken{ 0 };
311
313
314 double mRate;
315
317
318 std::weak_ptr<Meter> mInputMeter{};
319 std::weak_ptr<Meter> mOutputMeter{};
320
321 #if USE_PORTMIXER
322 PxMixer *mPortMixer;
323 float mPreviousHWPlaythrough;
324 #endif /* USE_PORTMIXER */
325
334
335 // For cacheing supported sample rates
336 static std::map<int, std::vector<long>> mCachedPlaybackRates;
337 static std::map<int, std::vector<long>> mCachedCaptureRates;
338 static std::map<std::pair<int, int>, std::vector<long>> mCachedSampleRates;
341 static double mCachedBestRateIn;
342
343protected:
351 static int getRecordDevIndex(const wxString &devName = {});
352
357#if USE_PORTMIXER
358 static int getRecordSourceIndex(PxMixer *portMixer);
359#endif
360
368 static int getPlayDevIndex(const wxString &devName = {});
369
374 static const int RatesToTry[];
376 static const int NumRatesToTry;
377
381 std::vector<std::unique_ptr<AudioIOExtBase>> mAudioIOExt;
382};
383
384#include "Prefs.h"
385
386extern AUDIO_DEVICES_API StringSetting AudioIOHost;
387extern AUDIO_DEVICES_API DoubleSetting AudioIOLatencyCorrection;
388extern AUDIO_DEVICES_API DoubleSetting AudioIOLatencyDuration;
389extern AUDIO_DEVICES_API StringSetting AudioIOPlaybackDevice;
390extern AUDIO_DEVICES_API StringSetting AudioIOPlaybackSource;
391extern AUDIO_DEVICES_API DoubleSetting AudioIOPlaybackVolume;
392extern AUDIO_DEVICES_API IntSetting AudioIORecordChannels;
393extern AUDIO_DEVICES_API StringSetting AudioIORecordingDevice;
394extern AUDIO_DEVICES_API StringSetting AudioIORecordingSource;
395extern AUDIO_DEVICES_API IntSetting AudioIORecordingSourceIndex;
396
397#endif
void PaStream
Definition: AudioIOBase.h:25
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
std::vector< std::vector< float > > PRCrossfadeData
Definition: AudioIOBase.h:38
AUDIO_DEVICES_API IntSetting AudioIORecordingSourceIndex
const auto project
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
A singleton object supporting queries of the state of any active audio streams, and audio device capa...
Definition: AudioIOBase.h:104
PaStream * mPortStreamV19
Definition: AudioIOBase.h:316
static std::map< std::pair< int, int >, std::vector< long > > mCachedSampleRates
Definition: AudioIOBase.h:338
double mRate
Audio playback rate in samples per second.
Definition: AudioIOBase.h:314
AudioIOBase & operator=(const AudioIOBase &)=delete
static int mCurrentPlaybackIndex
Definition: AudioIOBase.h:339
virtual ~AudioIOBase()
static std::unique_ptr< AudioIOBase > ugAudioIO
Definition: AudioIOBase.h:300
std::vector< std::unique_ptr< AudioIOExtBase > > mAudioIOExt
Definition: AudioIOBase.h:381
static double mCachedBestRateIn
Definition: AudioIOBase.h:341
virtual void StopStream()=0
static int mCurrentCaptureIndex
Definition: AudioIOBase.h:340
AudioIOBase(const AudioIOBase &)=delete
static std::map< int, std::vector< long > > mCachedCaptureRates
Definition: AudioIOBase.h:337
std::weak_ptr< AudacityProject > mOwningProject
Definition: AudioIOBase.h:304
static std::map< int, std::vector< long > > mCachedPlaybackRates
Definition: AudioIOBase.h:336
bool mInputMixerWorks
Can we control the hardware input level?
Definition: AudioIOBase.h:333
static const int NumRatesToTry
How many sample rates to try.
Definition: AudioIOBase.h:376
static const int NumStandardRates
How many standard sample rates there are.
Definition: AudioIOBase.h:250
Abstract interface to alternative, concurrent playback with the main audio (such as MIDI events)
Definition: AudioIOBase.h:89
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:363
Specialization of Setting for int.
Definition: Prefs.h:356
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:370
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:202
wxString description
Definition: AudioIOBase.h:84
struct holding stream options, including a pointer to the time warp info and AudioIOListener and whet...
Definition: AudioIOBase.h:46
std::function< std::chrono::milliseconds() > playbackStreamPrimer
Definition: AudioIOBase.h:71
PolicyFactory policyFactory
Definition: AudioIOBase.h:75
std::shared_ptr< AudacityProject > pProject
Definition: AudioIOBase.h:55
std::weak_ptr< Meter > captureMeter
Definition: AudioIOBase.h:56
std::weak_ptr< Meter > playbackMeter
Definition: AudioIOBase.h:56
AudioIOStartStreamOptions(const std::shared_ptr< AudacityProject > &pProject={}, double rate_=44100.0)
Definition: AudioIOBase.h:48
PRCrossfadeData * pCrossfadeData
Definition: AudioIOBase.h:66
const BoundedEnvelope * envelope
Definition: AudioIOBase.h:57
std::function< std::unique_ptr< PlaybackPolicy >(const AudioIOStartStreamOptions &) > PolicyFactory
Definition: AudioIOBase.h:74
std::optional< double > pStartTime
Definition: AudioIOBase.h:60
std::shared_ptr< AudioIOListener > listener
Definition: AudioIOBase.h:58