Skip to content

Commit

Permalink
Rewrite AudioIO::GetMixer and SetMixer
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Licameli committed Apr 2, 2024
1 parent 553e857 commit 9d33508
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 46 deletions.
34 changes: 12 additions & 22 deletions libraries/lib-audio-io/AudioIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,10 @@ void AudioIO::RemoveState(AudacityProject &project,
RealtimeEffectManager::Get(project).RemoveState(pInit, pGroup, pState);
}

void AudioIO::SetMixer(int inputSource, float recordVolume,
float playbackVolume)
void AudioIO::SetMixer(MixerSettings settings)
{
auto [recordDevice, recordVolume, playbackVolume] = settings;

SetMixerOutputVol(playbackVolume);
AudioIOPlaybackVolume.Write(playbackVolume);

Expand All @@ -392,38 +393,27 @@ void AudioIO::SetMixer(int inputSource, float recordVolume,

float oldRecordVolume = Px_GetInputVolume(mixer);

AudioIoCallback::SetMixer(inputSource);
AudioIoCallback::SetMixer(recordDevice);
if( oldRecordVolume != recordVolume )
Px_SetInputVolume(mixer, recordVolume);

#endif
}

void AudioIO::GetMixer(int *recordDevice, float *recordVolume,
float *playbackVolume)
auto AudioIO::GetMixer() -> MixerSettings
{
*playbackVolume = GetMixerOutputVol();

int recordDevice = 0;
float recordVolume = 1.0f;
float playbackVolume = GetMixerOutputVol();
#if defined(USE_PORTMIXER)

PxMixer *mixer = mPortMixer;

if( mixer )
{
*recordDevice = Px_GetCurrentInputSource(mixer);
if (mPortMixer) {
recordDevice = Px_GetCurrentInputSource(mPortMixer);

if (mInputMixerWorks)
*recordVolume = Px_GetInputVolume(mixer);
else
*recordVolume = 1.0f;

return;
recordVolume = Px_GetInputVolume(mPortMixer);
}

#endif

*recordDevice = 0;
*recordVolume = 1.0f;
return { recordDevice, recordVolume, playbackVolume };
}

bool AudioIO::InputMixerWorks()
Expand Down
12 changes: 8 additions & 4 deletions libraries/lib-audio-io/AudioIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,16 +508,20 @@ class AUDIO_IO_API AudioIO final
/** \brief Pause and un-pause playback and recording */
void SetPaused(bool state);

struct MixerSettings {
int inputSource;
float inputVolume;
float playbackVolume;
};

/* Mixer services are always available. If no stream is running, these
* methods use whatever device is specified by the preferences. If a
* stream *is* running, naturally they manipulate the mixer associated
* with that stream. If no mixer is available, output is emulated and
* input is stuck at 1.0f (a gain is applied to output samples).
*/
void SetMixer(int inputSource, float inputVolume,
float playbackVolume);
void GetMixer(int *inputSource, float *inputVolume,
float *playbackVolume);
void SetMixer(MixerSettings settings);
MixerSettings GetMixer();
/** @brief Find out if the input hardware level control is available
*
* Checks the mInputMixerWorks variable, which is set up in
Expand Down
26 changes: 6 additions & 20 deletions src/widgets/MeterPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,22 +478,15 @@ void MeterPanel::UpdateSelectedPrefs(int id)
void MeterPanel::UpdateSliderControl()
{
#if USE_PORTMIXER
float inputVolume;
float playbackVolume;
int inputSource;

// Show or hide the input slider based on whether it works
auto gAudioIO = AudioIO::Get();
if (mIsInput && mSlider)
mSlider->SetEnabled(mEnabled && gAudioIO->InputMixerWorks());

gAudioIO->GetMixer(&inputSource, &inputVolume, &playbackVolume);

const auto [inputSource, inputVolume, playbackVolume] =
gAudioIO->GetMixer();
const auto volume = mIsInput ? inputVolume : playbackVolume;

if (mSlider && (mSlider->Get() != volume))
mSlider->Set(volume);

#endif // USE_PORTMIXER
}

Expand Down Expand Up @@ -828,23 +821,16 @@ void MeterPanel::SetStyle(Style newStyle)
void MeterPanel::SetMixer(wxCommandEvent & WXUNUSED(event))
{
#if USE_PORTMIXER
if (mSlider)
{
float inputVolume;
float outputVolume;
int inputSource;

if (mSlider) {
Refresh();

auto gAudioIO = AudioIO::Get();
gAudioIO->GetMixer(&inputSource, &inputVolume, &outputVolume);

auto settings = gAudioIO->GetMixer();
auto &[inputSource, inputVolume, outputVolume] = settings;
if (mIsInput)
inputVolume = mSlider->Get();
else
outputVolume = mSlider->Get();

gAudioIO->SetMixer(inputSource, inputVolume, outputVolume);
gAudioIO->SetMixer(settings);

#if wxUSE_ACCESSIBILITY
GetAccessible()->NotifyEvent( wxACC_EVENT_OBJECT_VALUECHANGE,
Expand Down

0 comments on commit 9d33508

Please sign in to comment.