Audacity  2.2.2
Classes | Macros | Enumerations | Functions | Variables
AudioIO.cpp File Reference
#include "Audacity.h"
#include "Experimental.h"
#include "AudioIO.h"
#include "float_cast.h"
#include "DeviceManager.h"
#include <cfloat>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <wx/log.h>
#include <wx/textctrl.h>
#include <wx/timer.h>
#include <wx/intl.h>
#include <wx/debug.h>
#include <wx/sstream.h>
#include <wx/txtstrm.h>
#include "AudacityApp.h"
#include "AudacityException.h"
#include "Mix.h"
#include "MixerBoard.h"
#include "Resample.h"
#include "RingBuffer.h"
#include "prefs/GUISettings.h"
#include "Prefs.h"
#include "Project.h"
#include "TimeTrack.h"
#include "WaveTrack.h"
#include "AutoRecovery.h"
#include "prefs/QualityPrefs.h"
#include "toolbars/ControlToolBar.h"
#include "widgets/Meter.h"
#include "widgets/ErrorDialog.h"
#include "widgets/Warning.h"
#include "tracks/ui/Scrubbing.h"
#include <thread>

Go to the source code of this file.

Classes

struct  AudioIO::ScrubState
 
struct  AudioIO::ScrubState::Data
 
struct  AudioIO::ScrubState::Message
 
class  AudioThread
 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...
 

Macros

#define MAX(a, b)   ((a) > (b) ? (a) : (b))
 
#define ORIGINAL_DO_NOT_PLAY_ALL_MUTED_TRACKS_TO_END
 

Enumerations

enum  { MIDI_MINIMAL_LATENCY_MS = 1 }
 

Functions

 wxDEFINE_EVENT (EVT_AUDIOIO_PLAYBACK, wxCommandEvent)
 
 wxDEFINE_EVENT (EVT_AUDIOIO_CAPTURE, wxCommandEvent)
 
 wxDEFINE_EVENT (EVT_AUDIOIO_MONITOR, wxCommandEvent)
 
int audacityAudioCallback (const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
 
void InitAudioIO ()
 
void DeinitAudioIO ()
 
wxString DeviceName (const PaDeviceInfo *info)
 
wxString HostName (const PaDeviceInfo *info)
 
static PaSampleFormat AudacityToPortAudioSampleFormat (sampleFormat format)
 
static void DoSoftwarePlaythrough (const void *inputBuffer, sampleFormat inputFormat, unsigned inputChannels, float *outputBuffer, int len)
 

Variables

std::unique_ptr< AudioIOugAudioIO
 
AudioIOgAudioIO {}
 
constexpr size_t TimeQueueGrainSize = 2000
 

Macro Definition Documentation

#define MAX (   a,
 
)    ((a) > (b) ? (a) : (b))

Definition at line 4664 of file AudioIO.cpp.

Referenced by AudioIO::AudioCallback().

#define ORIGINAL_DO_NOT_PLAY_ALL_MUTED_TRACKS_TO_END

Enumeration Type Documentation

anonymous enum
Enumerator
MIDI_MINIMAL_LATENCY_MS 

Definition at line 499 of file AudioIO.cpp.

499  {
500  // This is the least positive latency we can
501  // specify to Pm_OpenOutput, 1 ms, which prevents immediate
502  // scheduling of events:
504 };

Function Documentation

int audacityAudioCallback ( const void *  inputBuffer,
void *  outputBuffer,
unsigned long  framesPerBuffer,
const PaStreamCallbackTimeInfo *  timeInfo,
PaStreamCallbackFlags  statusFlags,
void *  userData 
)

brief The function which is called from PortAudio's callback thread context to collect and deliver audio for / from the sound device.

This covers recording, playback, and doing both simultaneously. It is also invoked to do monitoring and software playthrough. Note that dealing with the two buffers needs some care to ensure that the right things happen for all possible cases.

Parameters
inputBufferBuffer of length framesPerBuffer containing samples from the sound card, or null if not capturing audio. Note that the data type will depend on the format of audio data that was chosen when the stream was created (so could be floats or various integers)
outputBufferUninitialised buffer of length framesPerBuffer which will be sent to the sound card after the callback, or null if not playing audio back.
framesPerBufferThe length of the playback and recording buffers
PaStreamCallbackTimeInfoPointer to PortAudio time information structure, which tells us how long we have been playing / recording
statusFlagsPortAudio stream status flags
userDatapointer to user-defined data structure. Provided for flexibility by PortAudio, but not used by Audacity - the data is stored in the AudioIO class instead.

Definition at line 4687 of file AudioIO.cpp.

References AudioIO::AudioCallback(), and gAudioIO.

Referenced by AudioIO::GetDeviceInfo(), AudioIO::HandleDeviceChange(), and AudioIO::StartPortAudioStream().

4691 {
4692  return gAudioIO->AudioCallback(
4693  inputBuffer, outputBuffer, framesPerBuffer,
4694  timeInfo, statusFlags, userData);
4695 }
int AudioCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, const PaStreamCallbackFlags statusFlags, void *userData)
Definition: AudioIO.cpp:4697
AudioIO * gAudioIO
Definition: AudioIO.cpp:483
static PaSampleFormat AudacityToPortAudioSampleFormat ( sampleFormat  format)
static

Definition at line 1441 of file AudioIO.cpp.

References floatSample, int16Sample, and int24Sample.

Referenced by AudioIO::StartPortAudioStream().

1442 {
1443  switch(format) {
1444  case int16Sample:
1445  return paInt16;
1446  case int24Sample:
1447  return paInt24;
1448  case floatSample:
1449  default:
1450  return paFloat32;
1451  }
1452 }
int format
Definition: ExportPCM.cpp:56
void DeinitAudioIO ( )

Definition at line 956 of file AudioIO.cpp.

Referenced by AudacityApp::OnExit().

957 {
958  ugAudioIO.reset();
959 }
std::unique_ptr< AudioIO > ugAudioIO
Definition: AudioIO.cpp:482
wxString DeviceName ( const PaDeviceInfo *  info)

Definition at line 961 of file AudioIO.cpp.

Referenced by AudioIO::GetDeviceInfo(), AudioIO::getPlayDevIndex(), AudioIO::getRecordDevIndex(), and InitAudioIO().

962 {
963  wxString infoName = wxSafeConvertMB2WX(info->name);
964 
965  return infoName;
966 }
static void DoSoftwarePlaythrough ( const void *  inputBuffer,
sampleFormat  inputFormat,
unsigned  inputChannels,
float *  outputBuffer,
int  len 
)
static

Definition at line 4666 of file AudioIO.cpp.

References CopySamples(), floatSample, and SAMPLE_SIZE.

Referenced by AudioIO::AudioCallback().

4671 {
4672  for (unsigned int i=0; i < inputChannels; i++) {
4673  samplePtr inputPtr = ((samplePtr)inputBuffer) + (i * SAMPLE_SIZE(inputFormat));
4674  samplePtr outputPtr = ((samplePtr)outputBuffer) + (i * SAMPLE_SIZE(floatSample));
4675 
4676  CopySamples(inputPtr, inputFormat,
4677  (samplePtr)outputPtr, floatSample,
4678  len, true, inputChannels, 2);
4679  }
4680 
4681  // One mono input channel goes to both output channels...
4682  if (inputChannels == 1)
4683  for (int i=0; i < len; i++)
4684  outputBuffer[2*i + 1] = outputBuffer[2*i];
4685 }
void CopySamples(samplePtr src, sampleFormat srcFormat, samplePtr dst, sampleFormat dstFormat, unsigned int len, bool highQuality, unsigned int srcStride, unsigned int dstStride)
#define SAMPLE_SIZE(SampleFormat)
Definition: Types.h:198
char * samplePtr
Definition: Types.h:203
wxString HostName ( const PaDeviceInfo *  info)

Definition at line 968 of file AudioIO.cpp.

Referenced by AudioIO::GetDeviceInfo(), and InitAudioIO().

969 {
970  wxString hostapiName = wxSafeConvertMB2WX(Pa_GetHostApiInfo(info->hostApi)->name);
971 
972  return hostapiName;
973 }
void InitAudioIO ( )

Definition at line 923 of file AudioIO.cpp.

References DeviceName(), gAudioIO, AudioIO::getPlayDevIndex(), AudioIO::getRecordDevIndex(), gPrefs, HostName(), AudioIO::mThread, and safenew.

Referenced by AudacityApp::OnInit().

924 {
925  ugAudioIO.reset(safenew AudioIO());
926  gAudioIO = ugAudioIO.get();
927  gAudioIO->mThread->Run();
928 #ifdef EXPERIMENTAL_MIDI_OUT
929 #ifdef USE_MIDI_THREAD
930  gAudioIO->mMidiThread->Run();
931 #endif
932 #endif
933 
934  // Make sure device prefs are initialized
935  if (gPrefs->Read(wxT("AudioIO/RecordingDevice"), wxT("")) == wxT("")) {
936  int i = AudioIO::getRecordDevIndex();
937  const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
938  if (info) {
939  gPrefs->Write(wxT("/AudioIO/RecordingDevice"), DeviceName(info));
940  gPrefs->Write(wxT("/AudioIO/Host"), HostName(info));
941  }
942  }
943 
944  if (gPrefs->Read(wxT("AudioIO/PlaybackDevice"), wxT("")) == wxT("")) {
945  int i = AudioIO::getPlayDevIndex();
946  const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
947  if (info) {
948  gPrefs->Write(wxT("/AudioIO/PlaybackDevice"), DeviceName(info));
949  gPrefs->Write(wxT("/AudioIO/Host"), HostName(info));
950  }
951  }
952 
953  gPrefs->Flush();
954 }
AudioIO uses the PortAudio library to play and record sound.
Definition: AudioIO.h:304
AudacityPrefs * gPrefs
Definition: Prefs.cpp:73
std::unique_ptr< AudioIO > ugAudioIO
Definition: AudioIO.cpp:482
wxString DeviceName(const PaDeviceInfo *info)
Definition: AudioIO.cpp:961
static int getPlayDevIndex(const wxString &devName=wxEmptyString)
get the index of the device selected in the preferences.
Definition: AudioIO.cpp:3282
wxString HostName(const PaDeviceInfo *info)
Definition: AudioIO.cpp:968
#define safenew
Definition: Audacity.h:230
std::unique_ptr< AudioThread > mThread
Definition: AudioIO.h:801
AudioIO * gAudioIO
Definition: AudioIO.cpp:483
static int getRecordDevIndex(const wxString &devName=wxEmptyString)
get the index of the supplied (named) recording device, or the device selected in the preferences if ...
Definition: AudioIO.cpp:3339
wxDEFINE_EVENT ( EVT_AUDIOIO_PLAYBACK  ,
wxCommandEvent   
)
wxDEFINE_EVENT ( EVT_AUDIOIO_CAPTURE  ,
wxCommandEvent   
)
wxDEFINE_EVENT ( EVT_AUDIOIO_MONITOR  ,
wxCommandEvent   
)

Variable Documentation

AudioIO* gAudioIO {}

Definition at line 483 of file AudioIO.cpp.

Referenced by audacityAudioCallback(), ControlToolBar::CanStopAudioStream(), Scrubber::ContinueScrubbingPoll(), AudacityProject::CreateMenusAndCommands(), CreateNewAudacityProject(), LabelTrack::DoCaptureKey(), AudacityProject::DoPlayStopSelect(), ControlToolBar::DoRecord(), PlayIndicatorOverlayBase::Draw(), DeviceToolBar::EnableDisableButtons(), ControlToolBar::EnableDisableButtons(), AudioThread::Entry(), AudacityProject::GetSpeedPlayOptions(), AudacityProject::GetUpdateFlags(), InitAudioIO(), EffectUIHost::Initialize(), WaveTrackMenuTable::InitMenu(), AudacityProject::IsAudioActive(), AudacityProject::MakeReadyToPlay(), Scrubber::MaybeStartScrubbing(), AudacityProject::OnAddLabelPlaying(), AudacityProject::OnAudioDeviceInfo(), AudacityProject::OnAudioIOStopRecording(), AudacityProject::OnBoundaryMove(), DeviceToolBar::OnChoice(), AudacityProject::OnCloseWindow(), AudacityProject::OnCursorPositionStore(), AudacityProject::OnDetectUpstreamDropouts(), EffectUIHost::OnFFwd(), AudacityApp::OnKeyDown(), ControlToolBar::OnKeyEvent(), TranscriptionToolBar::OnKeyEvent(), MeterPanel::OnMeterUpdate(), PrefsDialog::OnOK(), ControlToolBar::OnPause(), EffectUIHost::OnPlay(), AudacityProject::OnPlayStop(), AudacityProject::OnPlayStopSelect(), AudacityProject::OnPunchAndRoll(), EffectUIHost::OnRewind(), AudacityProject::OnSelectCursorStoredCursor(), AudacityProject::OnSetLeftSelection(), AudacityProject::OnSetRightSelection(), AudacityProject::OnSimulateRecordingErrors(), WaveTrackMenuTable::OnSpectrogramSettings(), AudacityProject::OnStopSelect(), LyricsWindow::OnTimer(), PlayIndicatorOverlay::OnTimer(), ViewInfo::OnTimer(), MixerBoard::OnTimer(), TrackPanel::OnTimer(), AudacityProject::OnTimer(), ControlToolBar::Pause(), TranscriptionToolBar::PlayAtSpeed(), ControlToolBar::PlayPlayRegion(), MixerToolBar::Populate(), Effect::Preview(), AudacityProject::PushState(), DeviceManager::Rescan(), AudacityProject::SeekWhenAudioActive(), AudacityProject::SetCaptureMeter(), MixerToolBar::SetMixer(), AudacityProject::SetPlaybackMeter(), MixerToolBar::SetToolTips(), MeterPanel::StartMonitoring(), AudioIO::StartMonitoring(), ControlToolBar::StartScrolling(), Scrubber::StartSpeedPlay(), MeterPanel::StopMonitoring(), ControlToolBar::StopPlaying(), Scrubber::StopScrubbing(), AudacityProject::TP_DisplaySelection(), AdornedRulerPanel::UpdateButtonStates(), MixerToolBar::UpdateControls(), MixerToolBar::UpdatePrefs(), AudacityProject::ZoomInByFactor(), and AudioIO::~AudioIO().

constexpr size_t TimeQueueGrainSize = 2000
std::unique_ptr<AudioIO> ugAudioIO

Definition at line 482 of file AudioIO.cpp.