Audacity 3.2.0
Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
AudioThread Class Reference

Defined different on Mac and other platforms (on Mac it does not use wxWidgets wxThread), this class sits in a thread loop reading and writing audio. More...

Public Types

typedef int ExitCode
 

Public Member Functions

 AudioThread ()
 
virtual ExitCode Entry ()
 
void Create ()
 
void Delete ()
 
bool TestDestroy ()
 
void Sleep (int ms)
 
void Run ()
 

Static Public Member Functions

static void * callback (void *p)
 

Private Attributes

bool mDestroy
 
pthread_t mThread
 

Detailed Description

Defined different on Mac and other platforms (on Mac it does not use wxWidgets wxThread), this class sits in a thread loop reading and writing audio.

Definition at line 214 of file AudioIO.cpp.

Member Typedef Documentation

◆ ExitCode

typedef int AudioThread::ExitCode

Definition at line 216 of file AudioIO.cpp.

Constructor & Destructor Documentation

◆ AudioThread()

AudioThread::AudioThread ( )
inline

Definition at line 217 of file AudioIO.cpp.

217{ mDestroy = false; mThread = NULL; }
pthread_t mThread
Definition: AudioIO.cpp:240
bool mDestroy
Definition: AudioIO.cpp:239

References mDestroy, and mThread.

Member Function Documentation

◆ callback()

static void * AudioThread::callback ( void *  p)
inlinestatic

Definition at line 231 of file AudioIO.cpp.

231 {
232 AudioThread *th = (AudioThread *)p;
233 return reinterpret_cast<void *>( th->Entry() );
234 }
Defined different on Mac and other platforms (on Mac it does not use wxWidgets wxThread),...
Definition: AudioIO.cpp:214
virtual ExitCode Entry()
Definition: AudioIO.cpp:1739

References Entry().

Referenced by Run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Create()

void AudioThread::Create ( )
inline

Definition at line 219 of file AudioIO.cpp.

219{}

◆ Delete()

void AudioThread::Delete ( )
inline

Definition at line 220 of file AudioIO.cpp.

220 {
221 mDestroy = true;
222 pthread_join(mThread, NULL);
223 }

References mDestroy, and mThread.

◆ Entry()

AudioThread::ExitCode AudioThread::Entry ( )
virtual

Definition at line 1739 of file AudioIO.cpp.

1740{
1741 enum class State { eUndefined, eOnce, eLoopRunning, eDoNothing, eMonitoring } lastState = State::eUndefined;
1742
1743 AudioIO *gAudioIO;
1744 while( !TestDestroy() &&
1745 nullptr != ( gAudioIO = AudioIO::Get() ) )
1746 {
1747 using Clock = std::chrono::steady_clock;
1748 auto loopPassStart = Clock::now();
1749 auto &schedule = gAudioIO->mPlaybackSchedule;
1750 const auto interval = schedule.GetPolicy().SleepInterval(schedule);
1751
1752 // Set LoopActive outside the tests to avoid race condition
1754 .store(true, std::memory_order_relaxed);
1756 .load(std::memory_order_acquire) )
1757 {
1758 gAudioIO->TrackBufferExchange();
1760 .store(false, std::memory_order_release);
1761
1762 lastState = State::eOnce;
1763 }
1765 .load(std::memory_order_relaxed))
1766 {
1767 if (lastState != State::eLoopRunning)
1768 {
1769 // Main thread has told us to start - acknowledge that we do
1771 std::memory_order::memory_order_release);
1772 }
1773 lastState = State::eLoopRunning;
1774
1775 // We call the processing after raising the acknowledge flag, because the main thread
1776 // only needs to know that the message was seen.
1777 //
1778 // This is unlike the case with mAudioThreadShouldCallTrackBufferExchangeOnce where the
1779 // store really means that the one-time exchange was done.
1780
1781 gAudioIO->TrackBufferExchange();
1782 }
1783 else
1784 {
1785 if ( (lastState == State::eLoopRunning)
1786 || (lastState == State::eMonitoring ) )
1787 {
1788 // Main thread has told us to stop; (actually: to neither process "once" nor "loop running")
1789 // acknowledge that we received the order and that no more processing will be done.
1791 std::memory_order::memory_order_release);
1792 }
1793 lastState = State::eDoNothing;
1794
1795 if (gAudioIO->IsMonitoring())
1796 {
1797 lastState = State::eMonitoring;
1798 }
1799 }
1800
1802 .store(false, std::memory_order_relaxed);
1803
1804 std::this_thread::sleep_until( loopPassStart + interval );
1805 }
1806
1807 return 0;
1808}
std::chrono::system_clock Clock
bool IsMonitoring() const
Returns true if we're monitoring input (but not recording or playing actual audio)
AudioIO uses the PortAudio library to play and record sound.
Definition: AudioIO.h:407
void TrackBufferExchange()
Definition: AudioIO.cpp:1843
static AudioIO * Get()
Definition: AudioIO.cpp:140
PlaybackSchedule mPlaybackSchedule
Definition: AudioIO.h:387
std::atomic< Acknowledge > mAudioThreadAcknowledge
Definition: AudioIO.h:309
std::atomic< bool > mAudioThreadTrackBufferExchangeLoopRunning
Definition: AudioIO.h:306
std::atomic< bool > mAudioThreadTrackBufferExchangeLoopActive
Definition: AudioIO.h:307
std::atomic< bool > mAudioThreadShouldCallTrackBufferExchangeOnce
Definition: AudioIO.h:305
bool TestDestroy()
Definition: AudioIO.cpp:224
virtual std::chrono::milliseconds SleepInterval(PlaybackSchedule &schedule)
How long to wait between calls to AudioIO::TrackBufferExchange.
PlaybackPolicy & GetPolicy()
Definition: Dither.cpp:67

References eStart, eStop, AudioIO::Get(), PlaybackSchedule::GetPolicy(), AudioIOBase::IsMonitoring(), AudioIoCallback::mAudioThreadAcknowledge, AudioIoCallback::mAudioThreadShouldCallTrackBufferExchangeOnce, AudioIoCallback::mAudioThreadTrackBufferExchangeLoopActive, AudioIoCallback::mAudioThreadTrackBufferExchangeLoopRunning, AudioIoCallback::mPlaybackSchedule, PlaybackPolicy::SleepInterval(), TestDestroy(), and AudioIO::TrackBufferExchange().

Referenced by callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Run()

void AudioThread::Run ( )
inline

Definition at line 235 of file AudioIO.cpp.

235 {
236 pthread_create(&mThread, NULL, callback, this);
237 }
static void * callback(void *p)
Definition: AudioIO.cpp:231

References callback(), and mThread.

Here is the call graph for this function:

◆ Sleep()

void AudioThread::Sleep ( int  ms)
inline

Definition at line 225 of file AudioIO.cpp.

225 {
226 struct timespec spec;
227 spec.tv_sec = 0;
228 spec.tv_nsec = ms * 1000 * 1000;
229 nanosleep(&spec, NULL);
230 }

◆ TestDestroy()

bool AudioThread::TestDestroy ( )
inline

Definition at line 224 of file AudioIO.cpp.

224{ return mDestroy; }

References mDestroy.

Referenced by Entry().

Here is the caller graph for this function:

Member Data Documentation

◆ mDestroy

bool AudioThread::mDestroy
private

Definition at line 239 of file AudioIO.cpp.

Referenced by AudioThread(), Delete(), and TestDestroy().

◆ mThread

pthread_t AudioThread::mThread
private

Definition at line 240 of file AudioIO.cpp.

Referenced by AudioThread(), Delete(), and Run().


The documentation for this class was generated from the following file: