1 /********************************************************************** 2 3 Audacity: A Digital Audio Editor 4 5 ProjectAudioManager.h 6 7 Paul Licameli split from ProjectManager.h 8 9 **********************************************************************/ 10 11 #ifndef __AUDACITY_PROJECT_AUDIO_MANAGER__ 12 #define __AUDACITY_PROJECT_AUDIO_MANAGER__ 13 14 #include <memory> 15 #include <vector> 16 17 #include "AudioIOListener.h" // to inherit 18 #include "ClientData.h" // to inherit 19 #include <wx/event.h> // to declare custom event type 20 21 constexpr int RATE_NOT_SELECTED{ -1 }; 22 23 class AudacityProject; 24 struct AudioIOStartStreamOptions; 25 class TrackList; 26 class SelectedRegion; 27 28 class WaveTrack; 29 using WaveTrackArray = std::vector < std::shared_ptr < WaveTrack > >; 30 31 enum class PlayMode : int { 32 normalPlay, 33 oneSecondPlay, // Disables auto-scrolling 34 loopedPlay, // Possibly looped play (not always); disables auto-scrolling 35 cutPreviewPlay 36 }; 37 38 struct TransportTracks; 39 40 enum StatusBarField : int; 41 42 class AUDACITY_DLL_API ProjectAudioManager final 43 : public ClientData::Base 44 , public AudioIOListener 45 , public std::enable_shared_from_this< ProjectAudioManager > 46 { 47 public: 48 static ProjectAudioManager &Get( AudacityProject &project ); 49 static const ProjectAudioManager &Get( const AudacityProject &project ); 50 51 // Find suitable tracks to record into, or return an empty array. 52 static WaveTrackArray ChooseExistingRecordingTracks( 53 AudacityProject &proj, bool selectedOnly, 54 double targetRate = RATE_NOT_SELECTED); 55 56 static bool UseDuplex(); 57 58 static TransportTracks GetAllPlaybackTracks( 59 TrackList &trackList, bool selectedOnly, 60 bool nonWaveToo = false //!< if true, collect all PlayableTracks 61 ); 62 63 explicit ProjectAudioManager( AudacityProject &project ); 64 ProjectAudioManager( const ProjectAudioManager & ) PROHIBITED; 65 ProjectAudioManager &operator=( const ProjectAudioManager & ) PROHIBITED; 66 ~ProjectAudioManager() override; 67 IsTimerRecordCancelled()68 bool IsTimerRecordCancelled() { return mTimerRecordCanceled; } SetTimerRecordCancelled()69 void SetTimerRecordCancelled() { mTimerRecordCanceled = true; } ResetTimerRecordCancelled()70 void ResetTimerRecordCancelled() { mTimerRecordCanceled = false; } 71 Paused()72 bool Paused() const { return mPaused; } 73 74 bool Playing() const; 75 76 // Whether recording into this project (not just into some project) is 77 // active 78 bool Recording() const; 79 Stopping()80 bool Stopping() const { return mStopping; } 81 82 // Whether the last attempt to start recording requested appending to tracks Appending()83 bool Appending() const { return mAppending; } 84 // Whether potentially looping play (using new default PlaybackPolicy) Looping()85 bool Looping() const { return mLooping; } Cutting()86 bool Cutting() const { return mCutting; } 87 88 // A project is only allowed to stop an audio stream that it owns. 89 bool CanStopAudioStream () const; 90 91 void OnRecord(bool altAppearance); 92 93 bool DoRecord(AudacityProject &project, 94 const TransportTracks &transportTracks, // If captureTracks is empty, then tracks are created 95 double t0, double t1, 96 bool altAppearance, 97 const AudioIOStartStreamOptions &options); 98 99 int PlayPlayRegion(const SelectedRegion &selectedRegion, 100 const AudioIOStartStreamOptions &options, 101 PlayMode playMode, 102 bool backwards = false); 103 104 // Play currently selected region, or if nothing selected, 105 // play from current cursor. 106 void PlayCurrentRegion( 107 bool newDefault = false, //!< See DefaultPlayOptions 108 bool cutpreview = false); 109 110 void OnPause(); 111 112 // Pause - used by AudioIO to pause sound activate recording 113 void Pause(); 114 115 // Stop playing or recording 116 void Stop(bool stopStream = true); 117 118 void StopIfPaused(); 119 120 bool DoPlayStopSelect( bool click, bool shift ); 121 void DoPlayStopSelect( ); 122 GetLastPlayMode()123 PlayMode GetLastPlayMode() const { return mLastPlayMode; } 124 125 private: SetPaused(bool value)126 void SetPaused( bool value ) { mPaused = value; } SetAppending(bool value)127 void SetAppending( bool value ) { mAppending = value; } SetLooping(bool value)128 void SetLooping( bool value ) { mLooping = value; } SetCutting(bool value)129 void SetCutting( bool value ) { mCutting = value; } SetStopping(bool value)130 void SetStopping( bool value ) { mStopping = value; } 131 132 // Cancel the addition of temporary recording tracks into the project 133 void CancelRecording(); 134 135 // Audio IO callback methods 136 void OnAudioIORate(int rate) override; 137 void OnAudioIOStartRecording() override; 138 void OnAudioIOStopRecording() override; 139 void OnAudioIONewBlocks(const WaveTrackArray *tracks) override; 140 void OnCommitRecording() override; 141 void OnSoundActivationThreshold() override; 142 143 void OnCheckpointFailure(wxCommandEvent &evt); 144 145 AudacityProject &mProject; 146 147 PlayMode mLastPlayMode{ PlayMode::normalPlay }; 148 149 //flag for cancellation of timer record. 150 bool mTimerRecordCanceled{ false }; 151 152 bool mPaused{ false }; 153 bool mAppending{ false }; 154 bool mLooping{ false }; 155 bool mCutting{ false }; 156 bool mStopping{ false }; 157 158 int mDisplayedRate{ 0 }; 159 static std::pair< TranslatableStrings, unsigned > 160 StatusWidthFunction( 161 const AudacityProject &project, StatusBarField field); 162 }; 163 164 AUDACITY_DLL_API 165 AudioIOStartStreamOptions DefaultPlayOptions( 166 AudacityProject &project, 167 bool newDefault = false /*!< "new" default playback policy adjusts to 168 changes of the looping region, "old" default plays once straight */ 169 ); 170 AudioIOStartStreamOptions DefaultSpeedPlayOptions( AudacityProject &project ); 171 172 struct PropertiesOfSelected 173 { 174 bool allSameRate{ false }; 175 int rateOfSelected{ RATE_NOT_SELECTED }; 176 int numberOfSelected{ 0 }; 177 }; 178 179 AUDACITY_DLL_API 180 PropertiesOfSelected GetPropertiesOfSelected(const AudacityProject &proj); 181 182 #include "commands/CommandFlag.h" 183 184 extern AUDACITY_DLL_API const ReservedCommandFlag 185 &CanStopAudioStreamFlag(); 186 187 #endif 188