1 /********************************************************************** 2 3 Audacity: A Digital Audio Editor 4 5 AudioIO.h 6 7 Dominic Mazzoni 8 9 Use the PortAudio library to play and record sound 10 11 **********************************************************************/ 12 13 #ifndef __AUDACITY_AUDIO_IO__ 14 #define __AUDACITY_AUDIO_IO__ 15 16 17 18 #include "AudioIOBase.h" // to inherit 19 #include "PlaybackSchedule.h" // member variable 20 21 #include <functional> 22 #include <memory> 23 #include <mutex> 24 #include <utility> 25 #include <wx/atomic.h> // member variable 26 27 #include <wx/event.h> // to declare custom event types 28 29 #include "SampleCount.h" 30 #include "SampleFormat.h" 31 32 class wxArrayString; 33 class AudioIOBase; 34 class AudioIO; 35 class RingBuffer; 36 class Mixer; 37 class Resample; 38 class AudioThread; 39 40 class AudacityProject; 41 42 class PlayableTrack; 43 using PlayableTrackConstArray = 44 std::vector < std::shared_ptr < const PlayableTrack > >; 45 46 class WaveTrack; 47 using WaveTrackArray = std::vector < std::shared_ptr < WaveTrack > >; 48 using WaveTrackConstArray = std::vector < std::shared_ptr < const WaveTrack > >; 49 50 struct PaStreamCallbackTimeInfo; 51 typedef unsigned long PaStreamCallbackFlags; 52 typedef int PaError; 53 54 bool ValidateDeviceNames(); 55 56 wxDECLARE_EXPORTED_EVENT(AUDACITY_DLL_API, 57 EVT_AUDIOIO_PLAYBACK, wxCommandEvent); 58 wxDECLARE_EXPORTED_EVENT(AUDACITY_DLL_API, 59 EVT_AUDIOIO_CAPTURE, wxCommandEvent); 60 wxDECLARE_EXPORTED_EVENT(AUDACITY_DLL_API, 61 EVT_AUDIOIO_MONITOR, wxCommandEvent); 62 63 struct TransportTracks { 64 WaveTrackArray playbackTracks; 65 WaveTrackArray captureTracks; 66 PlayableTrackConstArray otherPlayableTracks; 67 68 // This is a subset of playbackTracks 69 WaveTrackConstArray prerollTracks; 70 }; 71 72 /** brief The function which is called from PortAudio's callback thread 73 * context to collect and deliver audio for / from the sound device. 74 * 75 * This covers recording, playback, and doing both simultaneously. It is 76 * also invoked to do monitoring and software playthrough. Note that dealing 77 * with the two buffers needs some care to ensure that the right things 78 * happen for all possible cases. 79 * @param inputBuffer Buffer of length framesPerBuffer containing samples 80 * from the sound card, or null if not capturing audio. Note that the data 81 * type will depend on the format of audio data that was chosen when the 82 * stream was created (so could be floats or various integers) 83 * @param outputBuffer Uninitialised buffer of length framesPerBuffer which 84 * will be sent to the sound card after the callback, or null if not playing 85 * audio back. 86 * @param framesPerBuffer The length of the playback and recording buffers 87 * @param PaStreamCallbackTimeInfo Pointer to PortAudio time information 88 * structure, which tells us how long we have been playing / recording 89 * @param statusFlags PortAudio stream status flags 90 * @param userData pointer to user-defined data structure. Provided for 91 * flexibility by PortAudio, but not used by Audacity - the data is stored in 92 * the AudioIO class instead. 93 */ 94 int audacityAudioCallback( 95 const void *inputBuffer, void *outputBuffer, 96 unsigned long framesPerBuffer, 97 const PaStreamCallbackTimeInfo *timeInfo, 98 PaStreamCallbackFlags statusFlags, void *userData ); 99 100 class AudioIOExt; 101 102 class AUDACITY_DLL_API AudioIoCallback /* not final */ 103 : public AudioIOBase 104 { 105 public: 106 AudioIoCallback(); 107 ~AudioIoCallback(); 108 109 public: 110 // This function executes in a thread spawned by the PortAudio library 111 int AudioCallback( 112 constSamplePtr inputBuffer, float *outputBuffer, 113 unsigned long framesPerBuffer, 114 const PaStreamCallbackTimeInfo *timeInfo, 115 const PaStreamCallbackFlags statusFlags, void *userData); 116 117 //! @name iteration over extensions, supporting range-for syntax 118 //! @{ 119 class AUDACITY_DLL_API AudioIOExtIterator { 120 public: 121 using difference_type = ptrdiff_t; 122 using value_type = AudioIOExt &; 123 using pointer = AudioIOExt *; 124 using reference = AudioIOExt &; 125 using iterator_category = std::forward_iterator_tag; 126 AudioIOExtIterator(AudioIoCallback & audioIO,bool end)127 explicit AudioIOExtIterator( AudioIoCallback &audioIO, bool end ) 128 : mIterator{ end 129 ? audioIO.mAudioIOExt.end() 130 : audioIO.mAudioIOExt.begin() } 131 {} 132 AudioIOExtIterator &operator ++ () { ++mIterator; return *this; } 133 auto operator *() const -> AudioIOExt &; 134 friend inline bool operator == ( 135 const AudioIOExtIterator &xx, const AudioIOExtIterator &yy) 136 { 137 return xx.mIterator == yy.mIterator; 138 } 139 friend inline bool operator != ( 140 const AudioIOExtIterator &xx, const AudioIOExtIterator &yy) 141 { 142 return !(xx == yy); 143 } 144 private: 145 std::vector<std::unique_ptr<AudioIOExtBase>>::const_iterator mIterator; 146 }; 147 struct AudioIOExtRange { 148 AudioIOExtIterator first; 149 AudioIOExtIterator second; beginAudioIOExtRange150 AudioIOExtIterator begin() const { return first; } endAudioIOExtRange151 AudioIOExtIterator end() const { return second; } 152 }; 153 Extensions()154 AudioIOExtRange Extensions() { 155 return { 156 AudioIOExtIterator{ *this, false }, 157 AudioIOExtIterator{ *this, true } 158 }; 159 } 160 //! @} 161 GetListener()162 std::shared_ptr< AudioIOListener > GetListener() const 163 { return mListener.lock(); } 164 void SetListener( const std::shared_ptr< AudioIOListener > &listener); 165 166 // Part of the callback 167 int CallbackDoSeek(); 168 169 // Part of the callback 170 void CallbackCheckCompletion( 171 int &callbackReturn, unsigned long len); 172 173 int mbHasSoloTracks; 174 int mCallbackReturn; 175 // Helpers to determine if tracks have already been faded out. 176 unsigned CountSoloingTracks(); 177 bool TrackShouldBeSilent( const WaveTrack &wt ); 178 bool TrackHasBeenFadedOut( const WaveTrack &wt ); 179 bool AllTracksAlreadySilent(); 180 181 void CheckSoundActivatedRecordingLevel( 182 float *inputSamples, 183 unsigned long framesPerBuffer 184 ); 185 void AddToOutputChannel( unsigned int chan, 186 float * outputMeterFloats, 187 float * outputFloats, 188 const float * tempBuf, 189 bool drop, 190 unsigned long len, 191 WaveTrack *vt 192 ); 193 bool FillOutputBuffers( 194 float *outputBuffer, 195 unsigned long framesPerBuffer, 196 float *outputMeterFloats 197 ); 198 void DrainInputBuffers( 199 constSamplePtr inputBuffer, 200 unsigned long framesPerBuffer, 201 const PaStreamCallbackFlags statusFlags, 202 float * tempFloats 203 ); 204 void UpdateTimePosition( 205 unsigned long framesPerBuffer 206 ); 207 void DoPlaythrough( 208 constSamplePtr inputBuffer, 209 float *outputBuffer, 210 unsigned long framesPerBuffer, 211 float *outputMeterFloats 212 ); 213 void SendVuInputMeterData( 214 const float *inputSamples, 215 unsigned long framesPerBuffer 216 ); 217 void SendVuOutputMeterData( 218 const float *outputMeterFloats, 219 unsigned long framesPerBuffer 220 ); 221 222 /** \brief Get the number of audio samples ready in all of the playback 223 * buffers. 224 * 225 * Returns the smallest of the buffer ready space values in the event that 226 * they are different. */ 227 size_t GetCommonlyReadyPlayback(); 228 229 /// How many frames of zeros were output due to pauses? 230 long mNumPauseFrames; 231 232 #ifdef EXPERIMENTAL_AUTOMATED_INPUT_LEVEL_ADJUSTMENT 233 bool mAILAActive; 234 bool mAILAClipped; 235 int mAILATotalAnalysis; 236 int mAILAAnalysisCounter; 237 double mAILAMax; 238 double mAILAGoalPoint; 239 double mAILAGoalDelta; 240 double mAILAAnalysisTime; 241 double mAILALastStartTime; 242 double mAILAChangeFactor; 243 double mAILATopLevel; 244 double mAILAAnalysisEndTime; 245 double mAILAAbsolutStartTime; 246 unsigned short mAILALastChangeType; //0 - no change, 1 - increase change, 2 - decrease change 247 #endif 248 249 std::unique_ptr<AudioThread> mThread; 250 251 ArrayOf<std::unique_ptr<Resample>> mResample; 252 ArrayOf<std::unique_ptr<RingBuffer>> mCaptureBuffers; 253 WaveTrackArray mCaptureTracks; 254 ArrayOf<std::unique_ptr<RingBuffer>> mPlaybackBuffers; 255 WaveTrackArray mPlaybackTracks; 256 257 std::vector<std::unique_ptr<Mixer>> mPlaybackMixers; 258 259 float mMixerOutputVol { 1.0 }; 260 static int mNextStreamToken; 261 double mFactor; 262 unsigned long mMaxFramesOutput; // The actual number of frames output. 263 bool mbMicroFades; 264 265 double mSeek; 266 double mPlaybackRingBufferSecs; 267 double mCaptureRingBufferSecs; 268 269 /// Preferred batch size for replenishing the playback RingBuffer 270 size_t mPlaybackSamplesToCopy; 271 /// Occupancy of the queue we try to maintain, with bigger batches if needed 272 size_t mPlaybackQueueMinimum; 273 274 double mMinCaptureSecsToCopy; 275 bool mSoftwarePlaythrough; 276 /// True if Sound Activated Recording is enabled 277 bool mPauseRec; 278 float mSilenceLevel; 279 unsigned int mNumCaptureChannels; 280 unsigned int mNumPlaybackChannels; 281 sampleFormat mCaptureFormat; 282 unsigned long long mLostSamples{ 0 }; 283 volatile bool mAudioThreadShouldCallTrackBufferExchangeOnce; 284 volatile bool mAudioThreadTrackBufferExchangeLoopRunning; 285 volatile bool mAudioThreadTrackBufferExchangeLoopActive; 286 287 std::atomic<bool> mForceFadeOut{ false }; 288 289 wxLongLong mLastPlaybackTimeMillis; 290 291 volatile double mLastRecordingOffset; 292 PaError mLastPaError; 293 294 protected: 295 296 bool mUpdateMeters; 297 volatile bool mUpdatingMeters; 298 299 std::weak_ptr< AudioIOListener > mListener; 300 301 friend class AudioThread; 302 303 bool mUsingAlsa { false }; 304 305 // For cacheing supported sample rates 306 static double mCachedBestRateOut; 307 static bool mCachedBestRatePlaying; 308 static bool mCachedBestRateCapturing; 309 310 // Serialize main thread and PortAudio thread's attempts to pause and change 311 // the state used by the third, Audio thread. 312 wxMutex mSuspendAudioThread; 313 314 protected: 315 // A flag tested and set in one thread, cleared in another. Perhaps 316 // this guarantee of atomicity is more cautious than necessary. 317 wxAtomicInt mRecordingException {}; SetRecordingException()318 void SetRecordingException() 319 { wxAtomicInc( mRecordingException ); } ClearRecordingException()320 void ClearRecordingException() 321 { if (mRecordingException) wxAtomicDec( mRecordingException ); } 322 323 std::vector< std::pair<double, double> > mLostCaptureIntervals; 324 bool mDetectDropouts{ true }; 325 326 public: 327 // Pairs of starting time and duration LostCaptureIntervals()328 const std::vector< std::pair<double, double> > &LostCaptureIntervals() 329 { return mLostCaptureIntervals; } 330 331 // Used only for testing purposes in alpha builds 332 bool mSimulateRecordingErrors{ false }; 333 334 // Whether to check the error code passed to audacityAudioCallback to 335 // detect more dropouts 336 bool mDetectUpstreamDropouts{ true }; 337 338 protected: 339 RecordingSchedule mRecordingSchedule{}; 340 PlaybackSchedule mPlaybackSchedule; 341 342 private: 343 /*! 344 Privatize the inherited array but give access by Extensions(). 345 This class guarantees that this array is populated only with non-null 346 pointers to the subtype AudioIOExt 347 */ 348 using AudioIOBase::mAudioIOExt; 349 }; 350 351 struct PaStreamInfo; 352 353 class AUDACITY_DLL_API AudioIO final 354 : public AudioIoCallback 355 { 356 357 AudioIO(); 358 ~AudioIO(); 359 360 public: 361 // This might return null during application startup or shutdown 362 static AudioIO *Get(); 363 364 /** \brief Start up Portaudio for capture and recording as needed for 365 * input monitoring and software playthrough only 366 * 367 * This uses the Default project sample format, current sample rate, and 368 * selected number of input channels to open the recording device and start 369 * reading input data. If software playthrough is enabled, it also opens 370 * the output device in stereo to play the data through */ 371 void StartMonitoring( const AudioIOStartStreamOptions &options ); 372 373 /** \brief Start recording or playing back audio 374 * 375 * Allocates buffers for recording and playback, gets the Audio thread to 376 * fill them, and sets the stream rolling. 377 * If successful, returns a token identifying this particular stream 378 * instance. For use with IsStreamActive() */ 379 380 int StartStream(const TransportTracks &tracks, 381 double t0, double t1, 382 double mixerLimit, //!< Time at which mixer stops producing, maybe > t1 383 const AudioIOStartStreamOptions &options); 384 385 /** \brief Stop recording, playback or input monitoring. 386 * 387 * Does quite a bit of housekeeping, including switching off monitoring, 388 * flushing recording buffers out to wave tracks, and applies latency 389 * correction to recorded tracks if necessary */ 390 void StopStream() override; 391 /** \brief Move the playback / recording position of the current stream 392 * by the specified amount from where it is now */ SeekStream(double seconds)393 void SeekStream(double seconds) { mSeek = seconds; } 394 395 using PostRecordingAction = std::function<void()>; 396 397 //! Enqueue action for main thread idle time, not before the end of any recording in progress 398 /*! This may be called from non-main threads */ 399 void CallAfterRecording(PostRecordingAction action); 400 401 public: 402 wxString LastPaErrorString(); 403 GetLastPlaybackTime()404 wxLongLong GetLastPlaybackTime() const { return mLastPlaybackTimeMillis; } GetOwningProject()405 std::shared_ptr<AudacityProject> GetOwningProject() const 406 { return mOwningProject.lock(); } 407 408 /** \brief Pause and un-pause playback and recording */ 409 void SetPaused(bool state); 410 411 /* Mixer services are always available. If no stream is running, these 412 * methods use whatever device is specified by the preferences. If a 413 * stream *is* running, naturally they manipulate the mixer associated 414 * with that stream. If no mixer is available, output is emulated and 415 * input is stuck at 1.0f (a gain is applied to output samples). 416 */ 417 void SetMixer(int inputSource, float inputVolume, 418 float playbackVolume); 419 void GetMixer(int *inputSource, float *inputVolume, 420 float *playbackVolume); 421 /** @brief Find out if the input hardware level control is available 422 * 423 * Checks the mInputMixerWorks variable, which is set up in 424 * AudioIOBase::HandleDeviceChange(). External people care, because we want to 425 * disable the UI if it doesn't work. 426 */ 427 bool InputMixerWorks(); 428 429 /** \brief Get the list of inputs to the current mixer device 430 * 431 * Returns an array of strings giving the names of the inputs to the 432 * soundcard mixer (driven by PortMixer) */ 433 wxArrayString GetInputSourceNames(); 434 GetCaptureFormat()435 sampleFormat GetCaptureFormat() { return mCaptureFormat; } GetNumPlaybackChannels()436 unsigned GetNumPlaybackChannels() const { return mNumPlaybackChannels; } GetNumCaptureChannels()437 unsigned GetNumCaptureChannels() const { return mNumCaptureChannels; } 438 439 // Meaning really capturing, not just pre-rolling 440 bool IsCapturing() const; 441 442 /** \brief Ensure selected device names are valid 443 * 444 */ 445 static bool ValidateDeviceNames(const wxString &play, const wxString &rec); 446 447 /** \brief Function to automatically set an acceptable volume 448 * 449 */ 450 #ifdef EXPERIMENTAL_AUTOMATED_INPUT_LEVEL_ADJUSTMENT 451 void AILAInitialize(); 452 void AILADisable(); 453 bool AILAIsActive(); 454 void AILAProcess(double maxPeak); 455 void AILASetStartTime(); 456 double AILAGetLastDecisionTime(); 457 #endif 458 459 bool IsAvailable(AudacityProject &project) const; 460 461 /** \brief Return a valid sample rate that is supported by the current I/O 462 * device(s). 463 * 464 * The return from this function is used to determine the sample rate that 465 * audacity actually runs the audio I/O stream at. if there is no suitable 466 * rate available from the hardware, it returns 0. 467 * The sampleRate argument gives the desired sample rate (the rate of the 468 * audio to be handled, i.e. the currently Project Rate). 469 * capturing is true if the stream is capturing one or more audio channels, 470 * and playing is true if one or more channels are being played. */ 471 double GetBestRate(bool capturing, bool playing, double sampleRate); 472 473 /** \brief During playback, the track time most recently played 474 * 475 * When playing looped, this will start from t0 again, 476 * too. So the returned time should be always between 477 * t0 and t1 478 */ 479 double GetStreamTime(); 480 481 friend class AudioThread; 482 483 static void Init(); 484 static void Deinit(); 485 486 /*! For purposes of CallAfterRecording, treat time from now as if 487 recording (when argument is true) or not necessarily so (false) */ 488 void DelayActions(bool recording); 489 490 private: 491 492 bool DelayingActions() const; 493 494 /** \brief Set the current VU meters - this should be done once after 495 * each call to StartStream currently */ 496 void SetMeters(); 497 498 /** \brief Opens the portaudio stream(s) used to do playback or recording 499 * (or both) through. 500 * 501 * The sampleRate passed is the Project Rate of the active project. It may 502 * or may not be actually supported by playback or recording hardware 503 * currently in use (for many reasons). The number of Capture and Playback 504 * channels requested includes an allocation for doing software playthrough 505 * if necessary. The captureFormat is used for recording only, the playback 506 * being floating point always. Returns true if the stream opened successfully 507 * and false if it did not. */ 508 bool StartPortAudioStream(const AudioIOStartStreamOptions &options, 509 unsigned int numPlaybackChannels, 510 unsigned int numCaptureChannels, 511 sampleFormat captureFormat); 512 513 void SetOwningProject( const std::shared_ptr<AudacityProject> &pProject ); 514 void ResetOwningProject(); 515 516 /*! 517 Called in a loop from another worker thread that does not have the low-latency constraints 518 of the PortAudio callback thread. Does less frequent and larger batches of work that may 519 include memory allocations and database operations. RingBuffer objects mediate the transfer 520 between threads, to overcome the mismatch of their batch sizes. 521 */ 522 void TrackBufferExchange(); 523 524 //! First part of TrackBufferExchange 525 void FillPlayBuffers(); 526 527 //! Second part of TrackBufferExchange 528 void DrainRecordBuffers(); 529 530 /** \brief Get the number of audio samples free in all of the playback 531 * buffers. 532 * 533 * Returns the smallest of the buffer free space values in the event that 534 * they are different. */ 535 size_t GetCommonlyFreePlayback(); 536 537 /** \brief Get the number of audio samples ready in all of the recording 538 * buffers. 539 * 540 * Returns the smallest of the number of samples available for storage in 541 * the recording buffers (i.e. the number of samples that can be read from 542 * all record buffers without underflow). */ 543 size_t GetCommonlyAvailCapture(); 544 545 /** \brief Allocate RingBuffer structures, and others, needed for playback 546 * and recording. 547 * 548 * Returns true iff successful. 549 */ 550 bool AllocateBuffers( 551 const AudioIOStartStreamOptions &options, 552 const TransportTracks &tracks, double t0, double t1, double sampleRate ); 553 554 /** \brief Clean up after StartStream if it fails. 555 * 556 * If bOnlyBuffers is specified, it only cleans up the buffers. */ 557 void StartStreamCleanup(bool bOnlyBuffers = false); 558 559 std::mutex mPostRecordingActionMutex; 560 PostRecordingAction mPostRecordingAction; 561 562 bool mDelayingActions{ false }; 563 }; 564 565 #endif 566