Audacity  2.3.1
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 "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 "../lib-src/portmidi/pm_common/portmidi.h"
#include "../lib-src/portaudio-v19/src/common/pa_util.h"
#include "NoteTrack.h"
#include "tracks/ui/Scrubbing.h"
#include <thread>

Go to the source code of this file.

Classes

struct  AudioIoCallback::ScrubState
 
struct  AudioIoCallback::ScrubState::Data
 
struct  AudioIoCallback::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...
 
class  MidiThread
 

Macros

#define MIDI_SLEEP   10 /* milliseconds */
 
#define THREAD_LATENCY   10 /* milliseconds */
 
#define ROUND(x)   (int) ((x)+0.5)
 
#define MAX(a, b)   ((a) > (b) ? (a) : (b))
 

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)
 
static double SystemTime (bool usingAlsa)
 
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)
 
PmTimestamp MidiTime (void *WXUNUSED(info))
 
static void DoSoftwarePlaythrough (const void *inputBuffer, sampleFormat inputFormat, unsigned inputChannels, float *outputBuffer, int len)
 
void ClampBuffer (float *pBuffer, unsigned long len)
 

Variables

std::unique_ptr< AudioIOugAudioIO
 
AudioIOgAudioIO {}
 
constexpr size_t TimeQueueGrainSize = 2000
 
static double streamStartTime = 0
 
static Alg_update gAllNotesOff
 

Macro Definition Documentation

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

Definition at line 4672 of file AudioIO.cpp.

Referenced by AudioIoCallback::AudioCallback().

#define MIDI_SLEEP   10 /* milliseconds */

Definition at line 466 of file AudioIO.cpp.

Referenced by MidiThread::Entry(), and AudioIO::FillMidiBuffers().

#define ROUND (   x)    (int) ((x)+0.5)

Definition at line 475 of file AudioIO.cpp.

Referenced by AudioIO::OutputEvent().

#define THREAD_LATENCY   10 /* milliseconds */

Definition at line 471 of file AudioIO.cpp.

Referenced by AudioIO::FillMidiBuffers().

Enumeration Type Documentation

anonymous enum
Enumerator
MIDI_MINIMAL_LATENCY_MS 

Definition at line 507 of file AudioIO.cpp.

507  {
508  // This is the least positive latency we can
509  // specify to Pm_OpenOutput, 1 ms, which prevents immediate
510  // scheduling of events:
512 };

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 4695 of file AudioIO.cpp.

References AudioIoCallback::AudioCallback(), and gAudioIO.

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

4699 {
4700  return gAudioIO->AudioCallback(
4701  inputBuffer, outputBuffer, framesPerBuffer,
4702  timeInfo, statusFlags, userData);
4703 }
int AudioCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, const PaStreamCallbackFlags statusFlags, void *userData)
Definition: AudioIO.cpp:5423
AudioIO * gAudioIO
Definition: AudioIO.cpp:491
static PaSampleFormat AudacityToPortAudioSampleFormat ( sampleFormat  format)
static

Definition at line 1447 of file AudioIO.cpp.

References floatSample, int16Sample, and int24Sample.

Referenced by AudioIO::StartPortAudioStream().

1448 {
1449  switch(format) {
1450  case int16Sample:
1451  return paInt16;
1452  case int24Sample:
1453  return paInt24;
1454  case floatSample:
1455  default:
1456  return paFloat32;
1457  }
1458 }
int format
Definition: ExportPCM.cpp:56
void ClampBuffer ( float *  pBuffer,
unsigned long  len 
)

Definition at line 4858 of file AudioIO.cpp.

Referenced by AudioIoCallback::FillOutputBuffers().

4858  {
4859  for(unsigned i = 0; i < len; i++)
4860  pBuffer[i] = wxClip( -1.0f, pBuffer[i], 1.0f);
4861 };
void DeinitAudioIO ( )

Definition at line 962 of file AudioIO.cpp.

Referenced by AudacityApp::OnExit().

963 {
964  ugAudioIO.reset();
965 }
std::unique_ptr< AudioIO > ugAudioIO
Definition: AudioIO.cpp:490
wxString DeviceName ( const PaDeviceInfo *  info)

Definition at line 967 of file AudioIO.cpp.

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

968 {
969  wxString infoName = wxSafeConvertMB2WX(info->name);
970 
971  return infoName;
972 }
static void DoSoftwarePlaythrough ( const void *  inputBuffer,
sampleFormat  inputFormat,
unsigned  inputChannels,
float *  outputBuffer,
int  len 
)
static

Definition at line 4674 of file AudioIO.cpp.

References CopySamples(), floatSample, and SAMPLE_SIZE.

Referenced by AudioIoCallback::DoPlaythrough().

4679 {
4680  for (unsigned int i=0; i < inputChannels; i++) {
4681  samplePtr inputPtr = ((samplePtr)inputBuffer) + (i * SAMPLE_SIZE(inputFormat));
4682  samplePtr outputPtr = ((samplePtr)outputBuffer) + (i * SAMPLE_SIZE(floatSample));
4683 
4684  CopySamples(inputPtr, inputFormat,
4685  (samplePtr)outputPtr, floatSample,
4686  len, true, inputChannels, 2);
4687  }
4688 
4689  // One mono input channel goes to both output channels...
4690  if (inputChannels == 1)
4691  for (int i=0; i < len; i++)
4692  outputBuffer[2*i + 1] = outputBuffer[2*i];
4693 }
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 974 of file AudioIO.cpp.

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

975 {
976  wxString hostapiName = wxSafeConvertMB2WX(Pa_GetHostApiInfo(info->hostApi)->name);
977 
978  return hostapiName;
979 }
void InitAudioIO ( )

Definition at line 929 of file AudioIO.cpp.

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

Referenced by AudacityApp::OnInit().

930 {
931  ugAudioIO.reset(safenew AudioIO());
932  gAudioIO = ugAudioIO.get();
933  gAudioIO->mThread->Run();
934 #ifdef EXPERIMENTAL_MIDI_OUT
935 #ifdef USE_MIDI_THREAD
936  gAudioIO->mMidiThread->Run();
937 #endif
938 #endif
939 
940  // Make sure device prefs are initialized
941  if (gPrefs->Read(wxT("AudioIO/RecordingDevice"), wxT("")) == wxT("")) {
942  int i = AudioIO::getRecordDevIndex();
943  const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
944  if (info) {
945  gPrefs->Write(wxT("/AudioIO/RecordingDevice"), DeviceName(info));
946  gPrefs->Write(wxT("/AudioIO/Host"), HostName(info));
947  }
948  }
949 
950  if (gPrefs->Read(wxT("AudioIO/PlaybackDevice"), wxT("")) == wxT("")) {
951  int i = AudioIO::getPlayDevIndex();
952  const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
953  if (info) {
954  gPrefs->Write(wxT("/AudioIO/PlaybackDevice"), DeviceName(info));
955  gPrefs->Write(wxT("/AudioIO/Host"), HostName(info));
956  }
957  }
958 
959  gPrefs->Flush();
960 }
AudioIO uses the PortAudio library to play and record sound.
Definition: AudioIO.h:800
AudacityPrefs * gPrefs
Definition: Prefs.cpp:73
std::unique_ptr< AudioIO > ugAudioIO
Definition: AudioIO.cpp:490
wxString DeviceName(const PaDeviceInfo *info)
Definition: AudioIO.cpp:967
static int getPlayDevIndex(const wxString &devName=wxEmptyString)
get the index of the device selected in the preferences.
Definition: AudioIO.cpp:3285
wxString HostName(const PaDeviceInfo *info)
Definition: AudioIO.cpp:974
#define safenew
Definition: Audacity.h:230
std::unique_ptr< AudioThread > mThread
Definition: AudioIO.h:489
AudioIO * gAudioIO
Definition: AudioIO.cpp:491
std::unique_ptr< AudioThread > mMidiThread
Definition: AudioIO.h:492
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:3342
PmTimestamp MidiTime ( void *  WXUNUSEDinfo)

Definition at line 2269 of file AudioIO.cpp.

References gAudioIO, and AudioIO::MidiTime().

Referenced by AudioIO::AllNotesOff(), and AudioIO::FillMidiBuffers().

2270 {
2271  return gAudioIO->MidiTime();
2272 }
PmTimestamp MidiTime()
Compute the current PortMidi timestamp time.
Definition: AudioIO.cpp:4430
AudioIO * gAudioIO
Definition: AudioIO.cpp:491
static double SystemTime ( bool  usingAlsa)
static

Definition at line 796 of file AudioIO.cpp.

References streamStartTime.

Referenced by AudioIoCallback::ComputeMidiTimings(), AudioIO::MidiTime(), AudioIO::StartPortAudioStream(), and AudioIO::StartStream().

797 {
798 #ifdef __WXGTK__
799  if (usingAlsa) {
800  struct timespec now;
801  // CLOCK_MONOTONIC_RAW is unaffected by NTP or adj-time
802  clock_gettime(CLOCK_MONOTONIC_RAW, &now);
803  //return now.tv_sec + now.tv_nsec * 0.000000001;
804  return (now.tv_sec + now.tv_nsec * 0.000000001) - streamStartTime;
805  }
806 #else
807  static_cast<void>(usingAlsa);//compiler food.
808 #endif
809 
810  return PaUtil_GetTime() - streamStartTime;
811 }
static double streamStartTime
Definition: AudioIO.cpp:794
wxDEFINE_EVENT ( EVT_AUDIOIO_PLAYBACK  ,
wxCommandEvent   
)
wxDEFINE_EVENT ( EVT_AUDIOIO_CAPTURE  ,
wxCommandEvent   
)
wxDEFINE_EVENT ( EVT_AUDIOIO_MONITOR  ,
wxCommandEvent   
)

Variable Documentation

Alg_update gAllNotesOff
static

Definition at line 4141 of file AudioIO.cpp.

Referenced by AudioIO::GetNextEvent().

AudioIO* gAudioIO {}

Definition at line 491 of file AudioIO.cpp.

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

double streamStartTime = 0
static

Definition at line 794 of file AudioIO.cpp.

Referenced by SystemTime().

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

Definition at line 490 of file AudioIO.cpp.