diff options
author | Chris Robinson <[email protected]> | 2021-07-30 06:13:54 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2021-07-30 06:13:54 -0700 |
commit | 3b3df06195ad94588b4b1d2d9a8ddb3d9eba84c6 (patch) | |
tree | 121988f4b6ce3698d635e70ca994113f7dd7f5c8 /alc | |
parent | 1fd4c865fc084f134363db5155361d5483679235 (diff) |
Be more robust with unexpected channel counts
Diffstat (limited to 'alc')
-rw-r--r-- | alc/backends/portaudio.cpp | 2 | ||||
-rw-r--r-- | alc/backends/sdl2.cpp | 11 | ||||
-rw-r--r-- | alc/backends/solaris.cpp | 29 | ||||
-rw-r--r-- | alc/backends/winmm.cpp | 27 |
4 files changed, 35 insertions, 34 deletions
diff --git a/alc/backends/portaudio.cpp b/alc/backends/portaudio.cpp index 2d5cd36d..9c94587d 100644 --- a/alc/backends/portaudio.cpp +++ b/alc/backends/portaudio.cpp @@ -200,7 +200,7 @@ bool PortPlayback::reset() return false; } - if(mParams.channelCount == 2) + if(mParams.channelCount >= 2) mDevice->FmtChans = DevFmtStereo; else if(mParams.channelCount == 1) mDevice->FmtChans = DevFmtMono; diff --git a/alc/backends/sdl2.cpp b/alc/backends/sdl2.cpp index b1f9cf12..c0726033 100644 --- a/alc/backends/sdl2.cpp +++ b/alc/backends/sdl2.cpp @@ -28,6 +28,7 @@ #include <string> #include "almalloc.h" +#include "alnumeric.h" #include "core/device.h" #include "core/logging.h" @@ -98,7 +99,7 @@ void Sdl2Backend::open(const char *name) case DevFmtFloat: want.format = AUDIO_F32; break; } want.channels = (mDevice->FmtChans == DevFmtMono) ? 1 : 2; - want.samples = static_cast<Uint16>(mDevice->UpdateSize); + want.samples = static_cast<Uint16>(minu(mDevice->UpdateSize, 8192)); want.callback = &Sdl2Backend::audioCallbackC; want.userdata = this; @@ -121,10 +122,10 @@ void Sdl2Backend::open(const char *name) throw al::backend_exception{al::backend_error::NoDevice, "%s", SDL_GetError()}; DevFmtChannels devchans{}; - if(have.channels == 1) - devchans = DevFmtMono; - else if(have.channels == 2) + if(have.channels >= 2) devchans = DevFmtStereo; + else if(have.channels == 1) + devchans = DevFmtMono; else { SDL_CloseAudioDevice(devid); @@ -151,7 +152,7 @@ void Sdl2Backend::open(const char *name) SDL_CloseAudioDevice(mDeviceID); mDeviceID = devid; - mFrameSize = FrameSizeFromDevFmt(devchans, devtype, 0); + mFrameSize = BytesFromDevFmt(devtype) * have.channels; mFrequency = static_cast<uint>(have.freq); mFmtChans = devchans; mFmtType = devtype; diff --git a/alc/backends/solaris.cpp b/alc/backends/solaris.cpp index 26f5a5c5..791609ce 100644 --- a/alc/backends/solaris.cpp +++ b/alc/backends/solaris.cpp @@ -70,6 +70,7 @@ struct SolarisBackend final : public BackendBase { int mFd{-1}; + uint mFrameStep{}; al::vector<al::byte> mBuffer; std::atomic<bool> mKillNow{true}; @@ -165,12 +166,7 @@ bool SolarisBackend::reset() AUDIO_INITINFO(&info); info.play.sample_rate = mDevice->Frequency; - - if(mDevice->FmtChans != DevFmtMono) - mDevice->FmtChans = DevFmtStereo; - uint numChannels{mDevice->channelsFromFmt()}; - info.play.channels = numChannels; - + info.play.channels = mDevice->channelsFromFmt(); switch(mDevice->FmtType) { case DevFmtByte: @@ -192,9 +188,7 @@ bool SolarisBackend::reset() info.play.encoding = AUDIO_ENCODING_LINEAR; break; } - - uint frameSize{numChannels * mDevice->bytesFromFmt()}; - info.play.buffer_size = mDevice->BufferSize * frameSize; + info.play.buffer_size = mDevice->BufferSize * mDevice->frameSizeFromFmt(); if(ioctl(mFd, AUDIO_SETINFO, &info) < 0) { @@ -204,9 +198,13 @@ bool SolarisBackend::reset() if(mDevice->channelsFromFmt() != info.play.channels) { - ERR("Failed to set %s, got %u channels instead\n", DevFmtChannelsString(mDevice->FmtChans), - info.play.channels); - return false; + if(info.play.channels >= 2) + mDevice->FmtChans = DevFmtStereo; + else if(info.play.channels == 1) + mDevice->FmtChans = DevFmtMono; + else + throw al::backend_exception{al::backend_error::DeviceError, + "Got %u device channels", info.play.channels}; } if(info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8) @@ -223,13 +221,16 @@ bool SolarisBackend::reset() return false; } + uint frame_size{mDevice->bytesFromFmt() * info.play.channels}; + mFrameStep = info.play.channels; mDevice->Frequency = info.play.sample_rate; - mDevice->BufferSize = info.play.buffer_size / frameSize; + mDevice->BufferSize = info.play.buffer_size / frame_size; + /* How to get the actual period size/count? */ mDevice->UpdateSize = mDevice->BufferSize / 2; setDefaultChannelOrder(); - mBuffer.resize(mDevice->UpdateSize * mDevice->frameSizeFromFmt()); + mBuffer.resize(mDevice->UpdateSize * size_t{frame_size}); std::fill(mBuffer.begin(), mBuffer.end(), al::byte{}); return true; diff --git a/alc/backends/winmm.cpp b/alc/backends/winmm.cpp index 42aee313..5a2f00f8 100644 --- a/alc/backends/winmm.cpp +++ b/alc/backends/winmm.cpp @@ -181,7 +181,7 @@ FORCE_ALIGN int WinMMPlayback::mixerProc() SetRTPriority(); althrd_setname(MIXER_THREAD_NAME); - const size_t frame_step{mDevice->channelsFromFmt()}; + const size_t frame_step{mDevice->bytesFromFmt() * mFormat.nChannels}; while(!mKillNow.load(std::memory_order_acquire) && mDevice->Connected.load(std::memory_order_acquire)) @@ -196,7 +196,7 @@ FORCE_ALIGN int WinMMPlayback::mixerProc() size_t widx{mIdx}; do { WAVEHDR &waveHdr = mWaveBuffer[widx]; - widx = (widx+1) % mWaveBuffer.size(); + if(++widx == mWaveBuffer.size()) widx = 0; mDevice->renderSamples(waveHdr.lpData, mDevice->UpdateSize, frame_step); mWritable.fetch_sub(1, std::memory_order_acq_rel); @@ -223,9 +223,10 @@ void WinMMPlayback::open(const char *name) name}; auto DeviceID = static_cast<UINT>(std::distance(PlaybackDevices.cbegin(), iter)); + DevFmtType fmttype{mDevice->FmtType}; retry_open: WAVEFORMATEX format{}; - if(mDevice->FmtType == DevFmtFloat) + if(fmttype == DevFmtFloat) { format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT; format.wBitsPerSample = 32; @@ -233,7 +234,7 @@ retry_open: else { format.wFormatTag = WAVE_FORMAT_PCM; - if(mDevice->FmtType == DevFmtUByte || mDevice->FmtType == DevFmtByte) + if(fmttype == DevFmtUByte || fmttype == DevFmtByte) format.wBitsPerSample = 8; else format.wBitsPerSample = 16; @@ -250,9 +251,9 @@ retry_open: reinterpret_cast<DWORD_PTR>(this), CALLBACK_FUNCTION)}; if(res != MMSYSERR_NOERROR) { - if(mDevice->FmtType == DevFmtFloat) + if(fmttype == DevFmtFloat) { - mDevice->FmtType = DevFmtShort; + fmttype = DevFmtShort; goto retry_open; } throw al::backend_exception{al::backend_error::DeviceError, "waveOutOpen failed: %u", res}; @@ -303,7 +304,7 @@ bool WinMMPlayback::reset() } uint chanmask{}; - if(mFormat.nChannels == 2) + if(mFormat.nChannels >= 2) { mDevice->FmtChans = DevFmtStereo; chanmask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT; @@ -320,7 +321,7 @@ bool WinMMPlayback::reset() } setChannelOrderFromWFXMask(chanmask); - uint BufferSize{mDevice->UpdateSize * mDevice->frameSizeFromFmt()}; + uint BufferSize{mDevice->UpdateSize * mFormat.nChannels * mDevice->bytesFromFmt()}; al_free(mWaveBuffer[0].lpData); mWaveBuffer[0] = WAVEHDR{}; @@ -340,9 +341,8 @@ bool WinMMPlayback::reset() void WinMMPlayback::start() { try { - std::for_each(mWaveBuffer.begin(), mWaveBuffer.end(), - [this](WAVEHDR &waveHdr) -> void - { waveOutPrepareHeader(mOutHdl, &waveHdr, sizeof(WAVEHDR)); }); + for(auto &waveHdr : mWaveBuffer) + waveOutPrepareHeader(mOutHdl, &waveHdr, sizeof(WAVEHDR)); mWritable.store(static_cast<uint>(mWaveBuffer.size()), std::memory_order_release); mKillNow.store(false, std::memory_order_release); @@ -362,9 +362,8 @@ void WinMMPlayback::stop() while(mWritable.load(std::memory_order_acquire) < mWaveBuffer.size()) mSem.wait(); - std::for_each(mWaveBuffer.begin(), mWaveBuffer.end(), - [this](WAVEHDR &waveHdr) -> void - { waveOutUnprepareHeader(mOutHdl, &waveHdr, sizeof(WAVEHDR)); }); + for(auto &waveHdr : mWaveBuffer) + waveOutUnprepareHeader(mOutHdl, &waveHdr, sizeof(WAVEHDR)); mWritable.store(0, std::memory_order_release); } |