Skip to content

Commit

Permalink
REFAC: Get rid of non-standard variable-size arrays
Browse files Browse the repository at this point in the history
Using VSAs depends on compiler-specific, non-standard extensions and we
should generally avoid that.
The changed code could probably be made quite a bit more efficient by
keeping the vector(s) around between function calls, but this kind of
optimization is ought to be performed at some point in the future.
  • Loading branch information
Krzmbrzl committed Dec 31, 2022
1 parent f39b029 commit cbbadce
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 69 deletions.
27 changes: 15 additions & 12 deletions src/mumble/ALSAAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ void ALSAAudioInput::run() {
eMicFormat = SampleShort;
initializeMixer();

char inbuff[wantPeriod * iChannels * sizeof(short)];
std::vector< char > inbuff;
inbuff.resize(wantPeriod * iChannels * sizeof(short));

qml.unlock();

Expand All @@ -378,7 +379,7 @@ void ALSAAudioInput::run() {
snd_pcm_status_dump(status, log);
snd_pcm_status_free(status);
#endif
readblapp = snd_pcm_readi(capture_handle, inbuff, static_cast< int >(wantPeriod));
readblapp = snd_pcm_readi(capture_handle, inbuff.data(), static_cast< int >(wantPeriod));
if (readblapp == -ESTRPIPE) {
qWarning("ALSAAudioInput: PCM suspended, trying to resume");
while (bRunning && snd_pcm_resume(capture_handle) == -EAGAIN)
Expand All @@ -392,7 +393,7 @@ void ALSAAudioInput::run() {
err = snd_pcm_prepare(capture_handle);
qWarning("ALSAAudioInput: %s: %s", snd_strerror(static_cast< int >(readblapp)), snd_strerror(err));
} else if (wantPeriod == static_cast< unsigned int >(readblapp)) {
addMic(inbuff, static_cast< int >(readblapp));
addMic(inbuff.data(), static_cast< int >(readblapp));
}
}

Expand Down Expand Up @@ -483,16 +484,18 @@ void ALSAAudioOutput::run() {

const unsigned int buffsize = static_cast< unsigned int >(period_size * iChannels);

float zerobuff[buffsize];
float outbuff[buffsize];
std::vector< float > zerobuff;
zerobuff.resize(buffsize);
std::vector< float > outbuff;
outbuff.resize(buffsize);

for (unsigned int i = 0; i < buffsize; i++)
zerobuff[i] = 0;

// Fill buffer
if (bOk && pcm_handle)
for (unsigned int i = 0; i < buffer_size / period_size; i++)
snd_pcm_writei(pcm_handle, zerobuff, period_size);
snd_pcm_writei(pcm_handle, zerobuff.data(), period_size);

if (!bOk) {
Global::get().mw->msgBox(
Expand Down Expand Up @@ -533,10 +536,10 @@ void ALSAAudioOutput::run() {
snd_pcm_sframes_t avail{};
ALSA_ERRCHECK(avail = snd_pcm_avail_update(pcm_handle));
while (avail >= static_cast< int >(period_size)) {
stillRun = mix(outbuff, static_cast< int >(period_size));
stillRun = mix(outbuff.data(), static_cast< int >(period_size));
if (stillRun) {
snd_pcm_sframes_t w = 0;
ALSA_ERRCHECK(w = snd_pcm_writei(pcm_handle, outbuff, period_size));
ALSA_ERRCHECK(w = snd_pcm_writei(pcm_handle, outbuff.data(), period_size));
if (w < 0) {
avail = w;
break;
Expand All @@ -550,13 +553,13 @@ void ALSAAudioOutput::run() {
snd_pcm_drain(pcm_handle);
ALSA_ERRCHECK(snd_pcm_prepare(pcm_handle));
for (unsigned int i = 0; i < buffer_size / period_size; ++i)
ALSA_ERRCHECK(snd_pcm_writei(pcm_handle, zerobuff, period_size));
ALSA_ERRCHECK(snd_pcm_writei(pcm_handle, zerobuff.data(), period_size));
}

if (!stillRun) {
snd_pcm_drain(pcm_handle);

while (bRunning && !mix(outbuff, static_cast< unsigned int >(period_size))) {
while (bRunning && !mix(outbuff.data(), static_cast< unsigned int >(period_size))) {
this->msleep(10);
}

Expand All @@ -567,9 +570,9 @@ void ALSAAudioOutput::run() {

// Fill one frame
for (unsigned int i = 0; i < (buffer_size / period_size) - 1; i++)
snd_pcm_writei(pcm_handle, zerobuff, period_size);
snd_pcm_writei(pcm_handle, zerobuff.data(), period_size);

snd_pcm_writei(pcm_handle, outbuff, period_size);
snd_pcm_writei(pcm_handle, outbuff.data(), period_size);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/mumble/AudioInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ static void inMixerFloatMask(float *RESTRICT buffer, const void *RESTRICT ipt, u
const float *RESTRICT input = reinterpret_cast< const float * >(ipt);

unsigned int chancount = 0;
STACKVAR(unsigned int, chanindex, N);
std::vector< unsigned int > chanindex;
chanindex.resize(N);
for (unsigned int j = 0; j < N; ++j) {
if ((mask & (1ULL << j)) == 0) {
continue;
Expand All @@ -379,7 +380,8 @@ static void inMixerShortMask(float *RESTRICT buffer, const void *RESTRICT ipt, u
const short *RESTRICT input = reinterpret_cast< const short * >(ipt);

unsigned int chancount = 0;
STACKVAR(unsigned int, chanindex, N);
std::vector< unsigned int > chanindex;
chanindex.resize(N);
for (unsigned int j = 0; j < N; ++j) {
if ((mask & (1ULL << j)) == 0) {
continue;
Expand Down
11 changes: 7 additions & 4 deletions src/mumble/AudioOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,17 @@ bool AudioOutput::mix(void *outbuff, unsigned int frameCount) {

// If the audio backend uses a float-array we can sample and mix the audio sources directly into the output.
// Otherwise we'll have to use an intermediate buffer which we will convert to an array of shorts later
STACKVAR(float, fOutput, iChannels *frameCount);
float *output = (eSampleFormat == SampleFloat) ? reinterpret_cast< float * >(outbuff) : fOutput;
std::vector< float > fOutput;
fOutput.resize(iChannels * frameCount);
float *output = (eSampleFormat == SampleFloat) ? reinterpret_cast< float * >(outbuff) : fOutput.data();
memset(output, 0, sizeof(float) * frameCount * iChannels);

if (!qlMix.isEmpty()) {
// There are audio sources available -> mix those sources together and feed them into the audio backend
STACKVAR(float, speaker, iChannels * 3);
STACKVAR(float, svol, iChannels);
std::vector< float > speaker;
speaker.resize(iChannels * 3);
std::vector< float > svol;
svol.resize(iChannels);

bool validListener = false;

Expand Down
5 changes: 3 additions & 2 deletions src/mumble/AudioOutputSample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,16 @@ bool AudioOutputSample::prepareSampleBuffer(unsigned int frameCount) {
ceilf(static_cast< float >(frameCount * sfHandle->samplerate()) / static_cast< float >(iOutSampleRate)));
unsigned int iInputSamples = iInputFrames * channels;

STACKVAR(float, fOut, iInputSamples);
std::vector< float > fOut;
fOut.resize(iInputSamples);

bool eof = false;
sf_count_t read;
do {
resizeBuffer(iBufferFilled + sampleCount + INTERAURAL_DELAY);

// If we need to resample, write to the buffer on stack
float *pOut = (srs) ? fOut : pfBuffer + iBufferFilled;
float *pOut = (srs) ? fOut.data() : pfBuffer + iBufferFilled;

// Try to read all samples needed to satisfy this request
if ((read = sfHandle->read(pOut, iInputSamples)) < iInputSamples) {
Expand Down
28 changes: 17 additions & 11 deletions src/mumble/AudioStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@ void AudioEchoWidget::paintEvent(QPaintEvent *) {
spx_int32_t sz;
speex_echo_ctl(ai->sesEcho, SPEEX_ECHO_GET_IMPULSE_RESPONSE_SIZE, &sz);

STACKVAR(spx_int32_t, w, sz);
STACKVAR(float, W, sz);
std::vector< spx_int32_t > w;
w.resize(sz);
std::vector< float > W;
W.resize(sz);

speex_echo_ctl(ai->sesEcho, SPEEX_ECHO_GET_IMPULSE_RESPONSE, w);
speex_echo_ctl(ai->sesEcho, SPEEX_ECHO_GET_IMPULSE_RESPONSE, w.data());

ai->qmSpeex.unlock();

Expand Down Expand Up @@ -224,11 +226,13 @@ void AudioNoiseWidget::paintEvent(QPaintEvent *) {
spx_int32_t ps_size = 0;
speex_preprocess_ctl(ai->sppPreprocess, SPEEX_PREPROCESS_GET_PSD_SIZE, &ps_size);

STACKVAR(spx_int32_t, noise, ps_size);
STACKVAR(spx_int32_t, ps, ps_size);
std::vector< spx_int32_t > noise;
noise.resize(ps_size);
std::vector< spx_int32_t > ps;
ps.resize(ps_size);

speex_preprocess_ctl(ai->sppPreprocess, SPEEX_PREPROCESS_GET_PSD, ps);
speex_preprocess_ctl(ai->sppPreprocess, SPEEX_PREPROCESS_GET_NOISE_PSD, noise);
speex_preprocess_ctl(ai->sppPreprocess, SPEEX_PREPROCESS_GET_PSD, ps.data());
speex_preprocess_ctl(ai->sppPreprocess, SPEEX_PREPROCESS_GET_NOISE_PSD, noise.data());

ai->qmSpeex.unlock();

Expand Down Expand Up @@ -335,11 +339,13 @@ void AudioStats::on_Tick_timeout() {
spx_int32_t ps_size = 0;
speex_preprocess_ctl(ai->sppPreprocess, SPEEX_PREPROCESS_GET_PSD_SIZE, &ps_size);

STACKVAR(spx_int32_t, noise, ps_size);
STACKVAR(spx_int32_t, ps, ps_size);
std::vector< spx_int32_t > noise;
noise.resize(ps_size);
std::vector< spx_int32_t > ps;
ps.resize(ps_size);

speex_preprocess_ctl(ai->sppPreprocess, SPEEX_PREPROCESS_GET_PSD, ps);
speex_preprocess_ctl(ai->sppPreprocess, SPEEX_PREPROCESS_GET_NOISE_PSD, noise);
speex_preprocess_ctl(ai->sppPreprocess, SPEEX_PREPROCESS_GET_PSD, ps.data());
speex_preprocess_ctl(ai->sppPreprocess, SPEEX_PREPROCESS_GET_NOISE_PSD, noise.data());

float s = 0.0f;
float n = 0.0001f;
Expand Down
14 changes: 7 additions & 7 deletions src/mumble/PulseAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ void PulseAudioSystem::write_callback(pa_stream *s, size_t bytes, void *userdata
pao->eSampleFormat = PulseAudioOutput::SampleShort;
pao->iMixerFreq = pss->rate;
pao->iChannels = pss->channels;
unsigned int chanmasks[pss->channels];
std::vector< unsigned int > chanmasks;
chanmasks.resize(pss->channels);
for (int i = 0; i < pss->channels; ++i) {
unsigned int cm = 0;
switch (pcm->map[i]) {
Expand Down Expand Up @@ -629,22 +630,21 @@ void PulseAudioSystem::write_callback(pa_stream *s, size_t bytes, void *userdata
}
chanmasks[i] = cm;
}
pao->initializeMixer(chanmasks);
pao->initializeMixer(chanmasks.data());
}

const unsigned int iSampleSize = pao->iSampleSize;
const unsigned int samples = static_cast< unsigned int >(bytes) / iSampleSize;
bool oldAttenuation = pas->bAttenuating;

unsigned char buffer[bytes];
std::vector< unsigned char > buffer;
buffer.resize(bytes);
// do we have some mixed output?
if (pao->mix(buffer, samples)) {
if (pao->mix(buffer.data(), samples)) {
// attenuate if instructed to or it's in settings
pas->bAttenuating = (Global::get().bAttenuateOthers || Global::get().s.bAttenuateOthers);

} else {
memset(buffer, 0, bytes);

// attenuate if intructed to (self-activated)
pas->bAttenuating = Global::get().bAttenuateOthers;
}
Expand All @@ -654,7 +654,7 @@ void PulseAudioSystem::write_callback(pa_stream *s, size_t bytes, void *userdata
pas->setVolumes();
}

pa.stream_write(s, buffer, iSampleSize * samples, nullptr, 0, PA_SEEK_RELATIVE);
pa.stream_write(s, buffer.data(), iSampleSize * samples, nullptr, 0, PA_SEEK_RELATIVE);
}

void PulseAudioSystem::volume_sink_input_list_callback(pa_context *c, const pa_sink_input_info *i, int eol,
Expand Down
7 changes: 4 additions & 3 deletions src/mumble/ServerHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ void ServerHandler::handleVoicePacket(const Mumble::Protocol::AudioData &audioDa
}

void ServerHandler::sendMessage(const unsigned char *data, int len, bool force) {
STACKVAR(unsigned char, crypto, len + 4);
std::vector< unsigned char > crypto;
crypto.resize(len + 4);

QMutexLocker qml(&qmUdp);

Expand All @@ -314,10 +315,10 @@ void ServerHandler::sendMessage(const unsigned char *data, int len, bool force)
QApplication::postEvent(this,
new ServerHandlerMessageEvent(qba, Mumble::Protocol::TCPMessageType::UDPTunnel, true));
} else {
if (!connection->csCrypt->encrypt(reinterpret_cast< const unsigned char * >(data), crypto, len)) {
if (!connection->csCrypt->encrypt(reinterpret_cast< const unsigned char * >(data), crypto.data(), len)) {
return;
}
qusUdp->writeDatagram(reinterpret_cast< const char * >(crypto), len + 4, qhaRemote, usResolvedPort);
qusUdp->writeDatagram(reinterpret_cast< const char * >(crypto.data()), len + 4, qhaRemote, usResolvedPort);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/mumble/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ int main(int argc, char **argv) {
if (_wgetenv_s(&reqSize, nullptr, 0, L"PATH") != 0) {
qWarning() << "Failed to get PATH. Not adding application directory to PATH. DBus bindings may not work.";
} else if (reqSize > 0) {
STACKVAR(wchar_t, buff, reqSize + 1);
std::vector< wchar_t > buff;
buff.resize(reqSize + 1);
if (_wgetenv_s(&reqSize, buff, reqSize, L"PATH") != 0) {
qWarning()
<< "Failed to get PATH. Not adding application directory to PATH. DBus bindings may not work.";
Expand Down
3 changes: 2 additions & 1 deletion src/mumble/os_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ FARPROC WINAPI delayHook(unsigned dliNotify, PDelayLoadInfo pdli) {

size_t buflen = length + 10;

STACKVAR(char, filename, buflen);
std::vector< char > filename;
filename.resize(buflen);
strcpy_s(filename, buflen, pdli->szDll);

size_t offset = 0;
Expand Down
19 changes: 12 additions & 7 deletions src/murmur/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,8 @@ void Server::run() {

#ifdef Q_OS_UNIX
socklen_t fromlen;
STACKVAR(struct pollfd, fds, nfds + 1);
std::vector< struct pollfd > fds;
fds.resize(nfds + 1);

for (int i = 0; i < nfds; ++i) {
fds[i].fd = qlUdpSocket.at(i);
Expand All @@ -755,8 +756,10 @@ void Server::run() {
fds[nfds].revents = 0;
#else
int fromlen;
STACKVAR(SOCKET, fds, nfds);
STACKVAR(HANDLE, events, nfds + 1);
std::vector< SOCKET > fds;
fds.resize(nfds);
std::vector< HANDLE > events;
events.resize(nfds + 1);
for (int i = 0; i < nfds; ++i) {
fds[i] = qlUdpSocket.at(i);
events[i] = CreateEvent(nullptr, FALSE, FALSE, nullptr);
Expand All @@ -771,7 +774,7 @@ void Server::run() {
FrameMarkNamed(TracyConstants::UDP_FRAME);

#ifdef Q_OS_UNIX
int pret = poll(fds, nfds, -1);
int pret = poll(fds.data(), nfds, -1);
if (pret <= 0) {
if (errno == EINTR)
continue;
Expand Down Expand Up @@ -1013,10 +1016,12 @@ void Server::sendMessage(ServerUser &u, const unsigned char *data, int len, QByt
if ((u.aiUdpFlag.load() == 1 || force) && (u.sUdpSocket != INVALID_SOCKET)) {
#endif
#if defined(__LP64__)
STACKVAR(char, ebuffer, len + 4 + 16);
char *buffer = reinterpret_cast< char * >(((reinterpret_cast< quint64 >(ebuffer) + 8) & ~7) + 4);
std::vector< char > ebuffer;
ebuffer.resize(len + 4 + 16);
char *buffer = reinterpret_cast< char * >(((reinterpret_cast< quint64 >(ebuffer.data()) + 8) & ~7) + 4);
#else
STACKVAR(char, buffer, len + 4);
std::vector< char > buffer;
buffer.resize(len + 4);
#endif
{
QMutexLocker wl(&u.qmCrypt);
Expand Down
Loading

0 comments on commit cbbadce

Please sign in to comment.