Skip to content

Commit

Permalink
Initialize variables with appropriate values
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Jan 19, 2024
1 parent b9f25aa commit cb24fe6
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 180 deletions.
27 changes: 13 additions & 14 deletions alc/alc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2082,8 +2082,6 @@ ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum para

static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span<int> values)
{
size_t i;

if(values.empty())
{
alcSetError(device, ALC_INVALID_VALUE);
Expand Down Expand Up @@ -2144,11 +2142,9 @@ static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span<int>
values[0] = MaxCaptureAttributes;
return 1;
case ALC_ALL_ATTRIBUTES:
i = 0;
if(values.size() < MaxCaptureAttributes)
alcSetError(device, ALC_INVALID_VALUE);
else
if(values.size() >= MaxCaptureAttributes)
{
size_t i{0};
values[i++] = ALC_MAJOR_VERSION;
values[i++] = alcMajorVersion;
values[i++] = ALC_MINOR_VERSION;
Expand All @@ -2159,8 +2155,10 @@ static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span<int>
values[i++] = device->Connected.load(std::memory_order_relaxed);
values[i++] = 0;
assert(i == MaxCaptureAttributes);
return i;
}
return i;
alcSetError(device, ALC_INVALID_VALUE);
return 0;

case ALC_MAJOR_VERSION:
values[0] = alcMajorVersion;
Expand All @@ -2184,7 +2182,7 @@ static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span<int>
}

/* render device */
auto NumAttrsForDevice = [](ALCdevice *aldev) noexcept
auto NumAttrsForDevice = [](const ALCdevice *aldev) noexcept -> uint8_t
{
if(aldev->Type == DeviceType::Loopback && aldev->FmtChans == DevFmtAmbi3D)
return 37;
Expand All @@ -2197,11 +2195,9 @@ static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span<int>
return 1;

case ALC_ALL_ATTRIBUTES:
i = 0;
if(values.size() < static_cast<size_t>(NumAttrsForDevice(device)))
alcSetError(device, ALC_INVALID_VALUE);
else
if(values.size() >= NumAttrsForDevice(device))
{
size_t i{0};
values[i++] = ALC_MAJOR_VERSION;
values[i++] = alcMajorVersion;
values[i++] = ALC_MINOR_VERSION;
Expand Down Expand Up @@ -2267,8 +2263,11 @@ static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span<int>
values[i++] = static_cast<ALCenum>(device->getOutputMode1());

values[i++] = 0;
assert(i == NumAttrsForDevice(device));
return i;
}
return i;
alcSetError(device, ALC_INVALID_VALUE);
return 0;

case ALC_MAJOR_VERSION:
values[0] = alcMajorVersion;
Expand Down Expand Up @@ -2503,7 +2502,7 @@ ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname,
values[i++] = clock.Latency.count();

values[i++] = ALC_OUTPUT_MODE_SOFT;
values[i++] = static_cast<ALCenum>(device->getOutputMode1());
values[i++] = al::to_underlying(device->getOutputMode1());

values[i++] = 0;
}
Expand Down
86 changes: 58 additions & 28 deletions alc/backends/alsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,52 @@ const std::string_view prefix_name(snd_pcm_stream_t stream)
return "capture-prefix"sv;
}

struct SndCtlCardInfo {
snd_ctl_card_info_t *mInfo{};

SndCtlCardInfo() { snd_ctl_card_info_malloc(&mInfo); }
~SndCtlCardInfo() { if(mInfo) snd_ctl_card_info_free(mInfo); }
SndCtlCardInfo(const SndCtlCardInfo&) = delete;
SndCtlCardInfo& operator=(const SndCtlCardInfo&) = delete;

[[nodiscard]]
operator snd_ctl_card_info_t*() const noexcept { return mInfo; }
};

struct SndPcmInfo {
snd_pcm_info_t *mInfo{};

SndPcmInfo() { snd_pcm_info_malloc(&mInfo); }
~SndPcmInfo() { if(mInfo) snd_pcm_info_free(mInfo); }
SndPcmInfo(const SndPcmInfo&) = delete;
SndPcmInfo& operator=(const SndPcmInfo&) = delete;

[[nodiscard]]
operator snd_pcm_info_t*() const noexcept { return mInfo; }
};

struct SndCtl {
snd_ctl_t *mHandle{};

SndCtl() = default;
~SndCtl() { if(mHandle) snd_ctl_close(mHandle); }
SndCtl(const SndCtl&) = delete;
SndCtl& operator=(const SndCtl&) = delete;

[[nodiscard]]
auto open(const char *name, int mode) { return snd_ctl_open(&mHandle, name, mode); }

[[nodiscard]]
operator snd_ctl_t*() const noexcept { return mHandle; }
};


std::vector<DevMap> probe_devices(snd_pcm_stream_t stream)
{
std::vector<DevMap> devlist;

snd_ctl_card_info_t *info;
snd_ctl_card_info_malloc(&info);
snd_pcm_info_t *pcminfo;
snd_pcm_info_malloc(&pcminfo);
SndCtlCardInfo info;
SndPcmInfo pcminfo;

auto defname = ConfigValueStr({}, "alsa"sv,
(stream == SND_PCM_STREAM_PLAYBACK) ? "device"sv : "capture"sv);
Expand All @@ -278,28 +316,27 @@ std::vector<DevMap> probe_devices(snd_pcm_stream_t stream)
if(auto customdevs = ConfigValueStr({}, "alsa"sv,
(stream == SND_PCM_STREAM_PLAYBACK) ? "custom-devices"sv : "custom-captures"sv))
{
size_t nextpos{customdevs->find_first_not_of(';')};
size_t curpos;
while((curpos=nextpos) < customdevs->length())
size_t curpos{customdevs->find_first_not_of(';')};
while(curpos < customdevs->length())
{
nextpos = customdevs->find_first_of(';', curpos+1);

size_t seppos{customdevs->find_first_of('=', curpos)};
size_t nextpos{customdevs->find(';', curpos+1)};
const size_t seppos{customdevs->find('=', curpos)};
if(seppos == curpos || seppos >= nextpos)
{
std::string spec{customdevs->substr(curpos, nextpos-curpos)};
const std::string spec{customdevs->substr(curpos, nextpos-curpos)};
ERR("Invalid ALSA device specification \"%s\"\n", spec.c_str());
}
else
{
devlist.emplace_back(customdevs->substr(curpos, seppos-curpos),
customdevs->substr(seppos+1, nextpos-seppos-1));
const auto &entry = devlist.back();
const std::string_view strview{*customdevs};
const auto &entry = devlist.emplace_back(strview.substr(curpos, seppos-curpos),
strview.substr(seppos+1, nextpos-seppos-1));
TRACE("Got device \"%s\", \"%s\"\n", entry.name.c_str(), entry.device_name.c_str());
}

if(nextpos < customdevs->length())
nextpos = customdevs->find_first_not_of(';', nextpos+1);
curpos = nextpos;
}
}

Expand All @@ -312,8 +349,8 @@ std::vector<DevMap> probe_devices(snd_pcm_stream_t stream)
{
std::string name{"hw:" + std::to_string(card)};

snd_ctl_t *handle;
err = snd_ctl_open(&handle, name.c_str(), 0);
SndCtl handle;
err = handle.open(name.c_str(), 0);
if(err < 0)
{
ERR("control open (hw:%d): %s\n", card, snd_strerror(err));
Expand All @@ -323,7 +360,6 @@ std::vector<DevMap> probe_devices(snd_pcm_stream_t stream)
if(err < 0)
{
ERR("control hardware info (hw:%d): %s\n", card, snd_strerror(err));
snd_ctl_close(handle);
continue;
}

Expand Down Expand Up @@ -378,18 +414,13 @@ std::vector<DevMap> probe_devices(snd_pcm_stream_t stream)
device += ",DEV=";
device += std::to_string(dev);

devlist.emplace_back(std::move(name), std::move(device));
const auto &entry = devlist.back();
const auto &entry = devlist.emplace_back(std::move(name), std::move(device));
TRACE("Got device \"%s\", \"%s\"\n", entry.name.c_str(), entry.device_name.c_str());
}
snd_ctl_close(handle);
}
if(err < 0)
ERR("snd_card_next failed: %s\n", snd_strerror(err));

snd_pcm_info_free(pcminfo);
snd_ctl_card_info_free(info);

return devlist;
}

Expand All @@ -398,7 +429,6 @@ int verify_state(snd_pcm_t *handle)
{
snd_pcm_state_t state{snd_pcm_state(handle)};

int err;
switch(state)
{
case SND_PCM_STATE_OPEN:
Expand All @@ -411,12 +441,12 @@ int verify_state(snd_pcm_t *handle)
break;

case SND_PCM_STATE_XRUN:
err=snd_pcm_recover(handle, -EPIPE, 1);
if(err < 0) return err;
if(int err{snd_pcm_recover(handle, -EPIPE, 1)}; err < 0)
return err;
break;
case SND_PCM_STATE_SUSPENDED:
err = snd_pcm_recover(handle, -ESTRPIPE, 1);
if(err < 0) return err;
if(int err{snd_pcm_recover(handle, -ESTRPIPE, 1)}; err < 0)
return err;
break;

case SND_PCM_STATE_DISCONNECTED:
Expand Down
6 changes: 3 additions & 3 deletions alc/backends/jack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ void JackPlayback::open(std::string_view name)
const PathNamePair &binname = GetProcBinary();
const char *client_name{binname.fname.empty() ? "alsoft" : binname.fname.c_str()};

jack_status_t status;
jack_status_t status{};
mClient = jack_client_open(client_name, ClientOptions, &status, nullptr);
if(mClient == nullptr)
throw al::backend_exception{al::backend_error::DeviceError,
Expand Down Expand Up @@ -675,7 +675,7 @@ bool JackBackendFactory::init()

void (*old_error_cb)(const char*){&jack_error_callback ? jack_error_callback : nullptr};
jack_set_error_function(jack_msg_handler);
jack_status_t status;
jack_status_t status{};
jack_client_t *client{jack_client_open(client_name, ClientOptions, &status, nullptr)};
jack_set_error_function(old_error_cb);
if(!client)
Expand Down Expand Up @@ -704,7 +704,7 @@ std::string JackBackendFactory::probe(BackendType type)

const PathNamePair &binname = GetProcBinary();
const char *client_name{binname.fname.empty() ? "alsoft" : binname.fname.c_str()};
jack_status_t status;
jack_status_t status{};
switch(type)
{
case BackendType::Playback:
Expand Down
2 changes: 1 addition & 1 deletion alc/backends/pipewire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ using Pod_t = typename PodInfo<T>::Type;
template<uint32_t T>
al::span<const Pod_t<T>> get_array_span(const spa_pod *pod)
{
uint32_t nvals;
uint32_t nvals{};
if(void *v{spa_pod_get_array(pod, &nvals)})
{
if(get_array_value_type(pod) == T)
Expand Down
33 changes: 16 additions & 17 deletions alc/backends/pulseaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,18 +513,17 @@ void MainloopUniqueLock::connectContext()
int err{pa_context_connect(mutex()->mContext, nullptr, pulse_ctx_flags, nullptr)};
if(err >= 0)
{
pa_context_state_t state;
while((state=pa_context_get_state(mutex()->mContext)) != PA_CONTEXT_READY)
wait([&err,this]()
{
pa_context_state_t state{pa_context_get_state(mutex()->mContext)};
if(!PA_CONTEXT_IS_GOOD(state))
{
err = pa_context_errno(mutex()->mContext);
if(err > 0) err = -err;
break;
if(err > 0) err = -err;
return true;
}

wait();
}
return state == PA_CONTEXT_READY;
});
}
pa_context_set_state_callback(mutex()->mContext, nullptr, nullptr);

Expand Down Expand Up @@ -559,19 +558,19 @@ pa_stream *MainloopUniqueLock::connectStream(const char *device_name, pa_stream_
stream_id, pa_strerror(err)};
}

pa_stream_state_t state;
while((state=pa_stream_get_state(stream)) != PA_STREAM_READY)
wait([&err,stream,stream_id,this]()
{
pa_stream_state_t state{pa_stream_get_state(stream)};
if(!PA_STREAM_IS_GOOD(state))
{
err = pa_context_errno(mutex()->mContext);
pa_stream_unref(stream);
throw al::backend_exception{al::backend_error::DeviceError,
"%s did not get ready (%s)", stream_id, pa_strerror(err)};
}
return state == PA_STREAM_READY;
});

wait();
}
pa_stream_set_state_callback(stream, nullptr, nullptr);

return stream;
Expand Down Expand Up @@ -1031,8 +1030,8 @@ void PulsePlayback::stop()
ClockLatency PulsePlayback::getClockLatency()
{
ClockLatency ret;
pa_usec_t latency;
int neg, err;
pa_usec_t latency{};
int neg{}, err{};

{
MainloopUniqueLock plock{mMainloop};
Expand Down Expand Up @@ -1302,8 +1301,8 @@ void PulseCapture::captureSamples(std::byte *buffer, uint samples)
break;
}

const void *capbuf;
size_t caplen;
const void *capbuf{};
size_t caplen{};
if(pa_stream_peek(mStream, &capbuf, &caplen) < 0) UNLIKELY
{
mDevice->handleDisconnect("Failed retrieving capture samples: %s",
Expand Down Expand Up @@ -1359,8 +1358,8 @@ uint PulseCapture::availableSamples()
ClockLatency PulseCapture::getClockLatency()
{
ClockLatency ret;
pa_usec_t latency;
int neg, err;
pa_usec_t latency{};
int neg{}, err{};

{
MainloopUniqueLock plock{mMainloop};
Expand Down
14 changes: 3 additions & 11 deletions common/polyphase_resampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,9 @@ void PPhaseResampler::init(const uint srcRate, const uint dstRate)
* ends before the nyquist (0.5). Both are scaled by the downsampling
* factor.
*/
double cutoff, width;
if(mP > mQ)
{
cutoff = 0.475 / mP;
width = 0.05 / mP;
}
else
{
cutoff = 0.475 / mQ;
width = 0.05 / mQ;
}
const auto [cutoff, width] = (mP > mQ) ? std::make_tuple(0.475 / mP, 0.05 / mP)
: std::make_tuple(0.475 / mQ, 0.05 / mQ);

// A rejection of -180 dB is used for the stop band. Round up when
// calculating the left offset to avoid increasing the transition width.
const uint l{(CalcKaiserOrder(180.0, width)+1) / 2};
Expand Down

0 comments on commit cb24fe6

Please sign in to comment.