aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/alconvolve.c37
-rw-r--r--examples/alffplay.cpp140
-rw-r--r--examples/alhrtf.c2
-rw-r--r--examples/allatency.c2
-rw-r--r--examples/almultireverb.c8
-rw-r--r--examples/alplay.c2
-rw-r--r--examples/alreverb.c2
-rw-r--r--examples/alstream.c41
-rw-r--r--examples/alstreamcb.cpp47
-rw-r--r--examples/altonegen.c1
10 files changed, 145 insertions, 137 deletions
diff --git a/examples/alconvolve.c b/examples/alconvolve.c
index 8580d443..597d6ea2 100644
--- a/examples/alconvolve.c
+++ b/examples/alconvolve.c
@@ -38,6 +38,8 @@
#include "common/alhelpers.h"
+#include "win_main_utf8.h"
+
#ifndef AL_SOFT_convolution_effect
#define AL_SOFT_convolution_effect
@@ -88,11 +90,11 @@ static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv;
/* This stuff defines a simple streaming player object, the same as alstream.c.
* Comments are removed for brevity, see alstream.c for more details.
*/
-#define NUM_BUFFERS 4
-#define BUFFER_SAMPLES 8192
+enum { NumBuffers = 4 };
+enum { BufferSamples = 8192 };
typedef struct StreamPlayer {
- ALuint buffers[NUM_BUFFERS];
+ ALuint buffers[NumBuffers];
ALuint source;
SNDFILE *sndfile;
@@ -109,7 +111,7 @@ static StreamPlayer *NewPlayer(void)
player = calloc(1, sizeof(*player));
assert(player != NULL);
- alGenBuffers(NUM_BUFFERS, player->buffers);
+ alGenBuffers(NumBuffers, player->buffers);
assert(alGetError() == AL_NO_ERROR && "Could not create buffers");
alGenSources(1, &player->source);
@@ -138,11 +140,11 @@ static void DeletePlayer(StreamPlayer *player)
ClosePlayerFile(player);
alDeleteSources(1, &player->source);
- alDeleteBuffers(NUM_BUFFERS, player->buffers);
+ alDeleteBuffers(NumBuffers, player->buffers);
if(alGetError() != AL_NO_ERROR)
fprintf(stderr, "Failed to delete object IDs\n");
- memset(player, 0, sizeof(*player));
+ memset(player, 0, sizeof(*player)); /* NOLINT(clang-analyzer-security.insecureAPI.*) */
free(player);
}
@@ -184,7 +186,7 @@ static int OpenPlayerFile(StreamPlayer *player, const char *filename)
return 0;
}
- frame_size = (size_t)(BUFFER_SAMPLES * player->sfinfo.channels) * sizeof(float);
+ frame_size = (size_t)(BufferSamples * player->sfinfo.channels) * sizeof(float);
player->membuf = malloc(frame_size);
return 1;
@@ -197,9 +199,9 @@ static int StartPlayer(StreamPlayer *player)
alSourceRewind(player->source);
alSourcei(player->source, AL_BUFFER, 0);
- for(i = 0;i < NUM_BUFFERS;i++)
+ for(i = 0;i < NumBuffers;i++)
{
- sf_count_t slen = sf_readf_float(player->sndfile, player->membuf, BUFFER_SAMPLES);
+ sf_count_t slen = sf_readf_float(player->sndfile, player->membuf, BufferSamples);
if(slen < 1) break;
slen *= player->sfinfo.channels * (sf_count_t)sizeof(float);
@@ -243,7 +245,7 @@ static int UpdatePlayer(StreamPlayer *player)
alSourceUnqueueBuffers(player->source, 1, &bufid);
processed--;
- slen = sf_readf_float(player->sndfile, player->membuf, BUFFER_SAMPLES);
+ slen = sf_readf_float(player->sndfile, player->membuf, BufferSamples);
if(slen > 0)
{
slen *= player->sfinfo.channels * (sf_count_t)sizeof(float);
@@ -359,10 +361,10 @@ static ALuint LoadSound(const char *filename)
}
namepart = strrchr(filename, '/');
- if(namepart || (namepart=strrchr(filename, '\\')))
- namepart++;
- else
- namepart = filename;
+ if(!namepart) namepart = strrchr(filename, '\\');
+ if(!namepart) namepart = filename;
+ else namepart++;
+
printf("Loading: %s (%s, %dhz, %" PRId64 " samples / %.2f seconds)\n", namepart,
FormatName(format), sfinfo.samplerate, sfinfo.frames,
(double)sfinfo.frames / sfinfo.samplerate);
@@ -555,10 +557,9 @@ int main(int argc, char **argv)
continue;
namepart = strrchr(argv[i], '/');
- if(namepart || (namepart=strrchr(argv[i], '\\')))
- namepart++;
- else
- namepart = argv[i];
+ if(!namepart) namepart = strrchr(argv[i], '\\');
+ if(!namepart) namepart = argv[i];
+ else namepart++;
printf("Playing: %s (%s, %dhz)\n", namepart, FormatName(player->format),
player->sfinfo.samplerate);
diff --git a/examples/alffplay.cpp b/examples/alffplay.cpp
index 1f02ef70..cfbe5564 100644
--- a/examples/alffplay.cpp
+++ b/examples/alffplay.cpp
@@ -285,7 +285,7 @@ struct AudioState {
AVStream *mStream{nullptr};
AVCodecCtxPtr mCodecCtx;
- DataQueue<2*1024*1024> mQueue;
+ DataQueue<size_t{2}*1024*1024> mQueue;
/* Used for clock difference average computation */
seconds_d64 mClockDiffAvg{0};
@@ -310,8 +310,9 @@ struct AudioState {
int mSamplesPos{0};
int mSamplesMax{0};
- std::unique_ptr<uint8_t[]> mBufferData;
- size_t mBufferDataSize{0};
+ std::vector<uint8_t> mBufferData_;
+ //std::unique_ptr<uint8_t[]> mBufferData;
+ //size_t mBufferDataSize{0};
std::atomic<size_t> mReadPos{0};
std::atomic<size_t> mWritePos{0};
@@ -321,7 +322,7 @@ struct AudioState {
std::mutex mSrcMutex;
std::condition_variable mSrcCond;
- std::atomic_flag mConnected;
+ std::atomic_flag mConnected{};
ALuint mSource{0};
std::array<ALuint,AudioBufferCount> mBuffers{};
ALuint mBufferIdx{0};
@@ -371,7 +372,7 @@ struct VideoState {
AVStream *mStream{nullptr};
AVCodecCtxPtr mCodecCtx;
- DataQueue<14*1024*1024> mQueue;
+ DataQueue<size_t{14}*1024*1024> mQueue;
/* The pts of the currently displayed frame, and the time (av_gettime) it
* was last updated - used to have running video pts
@@ -474,8 +475,8 @@ nanoseconds AudioState::getClockNoLock()
// Get the current device clock time and latency.
auto device = alcGetContextsDevice(alcGetCurrentContext());
- ALCint64SOFT devtimes[2]{0,0};
- alcGetInteger64vSOFT(device, ALC_DEVICE_CLOCK_LATENCY_SOFT, 2, devtimes);
+ std::array<ALCint64SOFT,2> devtimes{};
+ alcGetInteger64vSOFT(device, ALC_DEVICE_CLOCK_LATENCY_SOFT, 2, devtimes.data());
auto latency = nanoseconds{devtimes[1]};
auto device_time = nanoseconds{devtimes[0]};
@@ -485,7 +486,7 @@ nanoseconds AudioState::getClockNoLock()
return device_time - mDeviceStartTime - latency;
}
- if(mBufferDataSize > 0)
+ if(!mBufferData_.empty())
{
if(mDeviceStartTime == nanoseconds::min())
return nanoseconds::zero();
@@ -494,15 +495,14 @@ nanoseconds AudioState::getClockNoLock()
* actually the timestamp of the first sample frame played. The audio
* clock, then, is that plus the current source offset.
*/
- ALint64SOFT offset[2];
+ std::array<ALint64SOFT,2> offset{};
if(alGetSourcei64vSOFT)
- alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_LATENCY_SOFT, offset);
+ alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_LATENCY_SOFT, offset.data());
else
{
ALint ioffset;
alGetSourcei(mSource, AL_SAMPLE_OFFSET, &ioffset);
offset[0] = ALint64SOFT{ioffset} << 32;
- offset[1] = 0;
}
/* NOTE: The source state must be checked last, in case an underrun
* occurs and the source stops between getting the state and retrieving
@@ -523,7 +523,7 @@ nanoseconds AudioState::getClockNoLock()
*/
const size_t woffset{mWritePos.load(std::memory_order_acquire)};
const size_t roffset{mReadPos.load(std::memory_order_relaxed)};
- const size_t readable{((woffset >= roffset) ? woffset : (mBufferDataSize+woffset)) -
+ const size_t readable{((woffset>=roffset) ? woffset : (mBufferData_.size()+woffset)) -
roffset};
pts = mCurrentPts - nanoseconds{seconds{readable/mFrameSize}}/mCodecCtx->sample_rate;
@@ -550,15 +550,14 @@ nanoseconds AudioState::getClockNoLock()
nanoseconds pts{mCurrentPts};
if(mSource)
{
- ALint64SOFT offset[2];
+ std::array<ALint64SOFT,2> offset{};
if(alGetSourcei64vSOFT)
- alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_LATENCY_SOFT, offset);
+ alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_LATENCY_SOFT, offset.data());
else
{
ALint ioffset;
alGetSourcei(mSource, AL_SAMPLE_OFFSET, &ioffset);
offset[0] = ALint64SOFT{ioffset} << 32;
- offset[1] = 0;
}
ALint queued, status;
alGetSourcei(mSource, AL_BUFFERS_QUEUED, &queued);
@@ -586,10 +585,10 @@ bool AudioState::startPlayback()
{
const size_t woffset{mWritePos.load(std::memory_order_acquire)};
const size_t roffset{mReadPos.load(std::memory_order_relaxed)};
- const size_t readable{((woffset >= roffset) ? woffset : (mBufferDataSize+woffset)) -
+ const size_t readable{((woffset >= roffset) ? woffset : (mBufferData_.size()+woffset)) -
roffset};
- if(mBufferDataSize > 0)
+ if(!mBufferData_.empty())
{
if(readable == 0)
return false;
@@ -610,8 +609,8 @@ bool AudioState::startPlayback()
/* Subtract the total buffer queue time from the current pts to get the
* pts of the start of the queue.
*/
- int64_t srctimes[2]{0,0};
- alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_CLOCK_SOFT, srctimes);
+ std::array<int64_t,2> srctimes{};
+ alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_CLOCK_SOFT, srctimes.data());
auto device_time = nanoseconds{srctimes[1]};
auto src_offset = duration_cast<nanoseconds>(fixed32{srctimes[0]}) /
mCodecCtx->sample_rate;
@@ -622,7 +621,7 @@ bool AudioState::startPlayback()
* the device time the stream would have started at to reach where it
* is now.
*/
- if(mBufferDataSize > 0)
+ if(!mBufferData_.empty())
{
nanoseconds startpts{mCurrentPts -
nanoseconds{seconds{readable/mFrameSize}}/mCodecCtx->sample_rate};
@@ -739,8 +738,8 @@ bool AudioState::readAudio(uint8_t *samples, unsigned int length, int &sample_sk
{
const auto len = static_cast<unsigned int>(mSamplesLen - mSamplesPos);
if(rem > len) rem = len;
- std::copy_n(mSamples + static_cast<unsigned int>(mSamplesPos)*mFrameSize,
- rem*mFrameSize, samples);
+ std::copy_n(mSamples + static_cast<unsigned int>(mSamplesPos)*size_t{mFrameSize},
+ rem*size_t{mFrameSize}, samples);
}
else
{
@@ -750,9 +749,9 @@ bool AudioState::readAudio(uint8_t *samples, unsigned int length, int &sample_sk
sample_dup(samples, mSamples, rem, mFrameSize);
}
- mSamplesPos += rem;
+ mSamplesPos += static_cast<int>(rem);
mCurrentPts += nanoseconds{seconds{rem}} / mCodecCtx->sample_rate;
- samples += rem*mFrameSize;
+ samples += rem*size_t{mFrameSize};
audio_size += rem;
while(mSamplesPos >= mSamplesLen)
@@ -791,17 +790,17 @@ bool AudioState::readAudio(int sample_skip)
while(mSamplesLen > 0)
{
const size_t nsamples{((roffset > woffset) ? roffset-woffset-1
- : (roffset == 0) ? (mBufferDataSize-woffset-1)
- : (mBufferDataSize-woffset)) / mFrameSize};
+ : (roffset == 0) ? (mBufferData_.size()-woffset-1)
+ : (mBufferData_.size()-woffset)) / mFrameSize};
if(!nsamples) break;
if(mSamplesPos < 0)
{
const size_t rem{std::min<size_t>(nsamples, static_cast<ALuint>(-mSamplesPos))};
- sample_dup(&mBufferData[woffset], mSamples, rem, mFrameSize);
+ sample_dup(&mBufferData_[woffset], mSamples, rem, mFrameSize);
woffset += rem * mFrameSize;
- if(woffset == mBufferDataSize) woffset = 0;
+ if(woffset == mBufferData_.size()) woffset = 0;
mWritePos.store(woffset, std::memory_order_release);
mCurrentPts += nanoseconds{seconds{rem}} / mCodecCtx->sample_rate;
@@ -813,9 +812,9 @@ bool AudioState::readAudio(int sample_skip)
const size_t boffset{static_cast<ALuint>(mSamplesPos) * size_t{mFrameSize}};
const size_t nbytes{rem * mFrameSize};
- memcpy(&mBufferData[woffset], mSamples + boffset, nbytes);
+ memcpy(&mBufferData_[woffset], mSamples + boffset, nbytes);
woffset += nbytes;
- if(woffset == mBufferDataSize) woffset = 0;
+ if(woffset == mBufferData_.size()) woffset = 0;
mWritePos.store(woffset, std::memory_order_release);
mCurrentPts += nanoseconds{seconds{rem}} / mCodecCtx->sample_rate;
@@ -888,15 +887,15 @@ ALsizei AudioState::bufferCallback(void *data, ALsizei size) noexcept
const size_t woffset{mWritePos.load(std::memory_order_relaxed)};
if(woffset == roffset) break;
- size_t todo{((woffset < roffset) ? mBufferDataSize : woffset) - roffset};
+ size_t todo{((woffset < roffset) ? mBufferData_.size() : woffset) - roffset};
todo = std::min<size_t>(todo, static_cast<ALuint>(size-got));
- memcpy(data, &mBufferData[roffset], todo);
+ memcpy(data, &mBufferData_[roffset], todo);
data = static_cast<ALbyte*>(data) + todo;
got += static_cast<ALsizei>(todo);
roffset += todo;
- if(roffset == mBufferDataSize)
+ if(roffset == mBufferData_.size())
roffset = 0;
}
mReadPos.store(roffset, std::memory_order_release);
@@ -936,7 +935,7 @@ int AudioState::handler()
};
EventControlManager event_controller{sleep_time};
- std::unique_ptr<uint8_t[]> samples;
+ std::vector<uint8_t> samples;
ALsizei buffer_len{0};
/* Find a suitable format for OpenAL. */
@@ -1151,9 +1150,9 @@ int AudioState::handler()
mSwresCtx.reset(ps);
if(err != 0)
{
- char errstr[AV_ERROR_MAX_STRING_SIZE]{};
+ std::array<char,AV_ERROR_MAX_STRING_SIZE> errstr{};
std::cerr<< "Failed to allocate SwrContext: "
- <<av_make_error_string(errstr, AV_ERROR_MAX_STRING_SIZE, err) <<std::endl;
+ <<av_make_error_string(errstr.data(), AV_ERROR_MAX_STRING_SIZE, err) <<std::endl;
return 0;
}
@@ -1166,7 +1165,7 @@ int AudioState::handler()
* ordering and normalization, so a custom matrix is needed to
* scale and reorder the source from AmbiX.
*/
- std::vector<double> mtx(64*64, 0.0);
+ std::vector<double> mtx(size_t{64}*64, 0.0);
mtx[0 + 0*64] = std::sqrt(0.5);
mtx[3 + 1*64] = 1.0;
mtx[1 + 2*64] = 1.0;
@@ -1185,17 +1184,17 @@ int AudioState::handler()
mSwresCtx.reset(ps);
if(err != 0)
{
- char errstr[AV_ERROR_MAX_STRING_SIZE]{};
+ std::array<char,AV_ERROR_MAX_STRING_SIZE> errstr{};
std::cerr<< "Failed to allocate SwrContext: "
- <<av_make_error_string(errstr, AV_ERROR_MAX_STRING_SIZE, err) <<std::endl;
+ <<av_make_error_string(errstr.data(), AV_ERROR_MAX_STRING_SIZE, err) <<std::endl;
return 0;
}
}
if(int err{swr_init(mSwresCtx.get())})
{
- char errstr[AV_ERROR_MAX_STRING_SIZE]{};
+ std::array<char,AV_ERROR_MAX_STRING_SIZE> errstr{};
std::cerr<< "Failed to initialize audio converter: "
- <<av_make_error_string(errstr, AV_ERROR_MAX_STRING_SIZE, err) <<std::endl;
+ <<av_make_error_string(errstr.data(), AV_ERROR_MAX_STRING_SIZE, err) <<std::endl;
return 0;
}
@@ -1206,8 +1205,8 @@ int AudioState::handler()
alSourcei(mSource, AL_DIRECT_CHANNELS_SOFT, DirectOutMode);
if(EnableWideStereo)
{
- const float angles[2]{static_cast<float>(M_PI / 3.0), static_cast<float>(-M_PI / 3.0)};
- alSourcefv(mSource, AL_STEREO_ANGLES, angles);
+ const std::array angles{static_cast<float>(M_PI / 3.0), static_cast<float>(-M_PI / 3.0)};
+ alSourcefv(mSource, AL_STEREO_ANGLES, angles.data());
}
if(has_bfmt_ex)
{
@@ -1237,13 +1236,12 @@ int AudioState::handler()
}
else
{
- mBufferDataSize = static_cast<size_t>(duration_cast<seconds>(mCodecCtx->sample_rate *
- AudioBufferTotalTime).count()) * mFrameSize;
- mBufferData = std::make_unique<uint8_t[]>(mBufferDataSize);
- std::fill_n(mBufferData.get(), mBufferDataSize, uint8_t{});
+ mBufferData_.resize(static_cast<size_t>(duration_cast<seconds>(mCodecCtx->sample_rate *
+ AudioBufferTotalTime).count()) * mFrameSize);
+ std::fill(mBufferData_.begin(), mBufferData_.end(), uint8_t{});
mReadPos.store(0, std::memory_order_relaxed);
- mWritePos.store(mBufferDataSize/mFrameSize/2*mFrameSize, std::memory_order_relaxed);
+ mWritePos.store(mBufferData_.size()/mFrameSize/2*mFrameSize, std::memory_order_relaxed);
ALCint refresh{};
alcGetIntegerv(alcGetContextsDevice(alcGetCurrentContext()), ALC_REFRESH, 1, &refresh);
@@ -1255,12 +1253,12 @@ int AudioState::handler()
buffer_len = static_cast<int>(duration_cast<seconds>(mCodecCtx->sample_rate *
AudioBufferTime).count() * mFrameSize);
if(buffer_len > 0)
- samples = std::make_unique<uint8_t[]>(static_cast<ALuint>(buffer_len));
+ samples.resize(static_cast<ALuint>(buffer_len));
/* Prefill the codec buffer. */
auto packet_sender = [this]()
{
- while(1)
+ while(true)
{
const int ret{mQueue.sendPacket(mCodecCtx.get())};
if(ret == AVErrorEOF) break;
@@ -1287,7 +1285,7 @@ int AudioState::handler()
mCurrentPts += skip;
}
- while(1)
+ while(true)
{
if(mMovie.mQuit.load(std::memory_order_relaxed))
{
@@ -1299,11 +1297,11 @@ int AudioState::handler()
mSamplesLen = decodeFrame();
mSamplesPos = mSamplesLen;
} while(mSamplesLen > 0);
- goto finish;
+ break;
}
ALenum state;
- if(mBufferDataSize > 0)
+ if(!mBufferData_.empty())
{
alGetSourcei(mSource, AL_SOURCE_STATE, &state);
@@ -1333,13 +1331,13 @@ int AudioState::handler()
/* Read the next chunk of data, filling the buffer, and queue
* it on the source.
*/
- if(!readAudio(samples.get(), static_cast<ALuint>(buffer_len), sync_skip))
+ if(!readAudio(samples.data(), static_cast<ALuint>(buffer_len), sync_skip))
break;
const ALuint bufid{mBuffers[mBufferIdx]};
mBufferIdx = static_cast<ALuint>((mBufferIdx+1) % mBuffers.size());
- alBufferData(bufid, mFormat, samples.get(), buffer_len, mCodecCtx->sample_rate);
+ alBufferData(bufid, mFormat, samples.data(), buffer_len, mCodecCtx->sample_rate);
alSourceQueueBuffers(mSource, 1, &bufid);
++queued;
}
@@ -1380,7 +1378,6 @@ int AudioState::handler()
mSrcCond.wait_for(srclock, sleep_time);
}
-finish:
alSourceRewind(mSource);
alSourcei(mSource, AL_BUFFER, 0);
@@ -1451,7 +1448,7 @@ void VideoState::updateVideo(SDL_Window *screen, SDL_Renderer *renderer, bool re
auto clocktime = mMovie.getMasterClock();
bool updated{false};
- while(1)
+ while(true)
{
size_t next_idx{(read_idx+1)%mPictQ.size()};
if(next_idx == mPictQWrite.load(std::memory_order_acquire))
@@ -1511,9 +1508,9 @@ void VideoState::updateVideo(SDL_Window *screen, SDL_Renderer *renderer, bool re
{
double aspect_ratio = av_q2d(frame->sample_aspect_ratio);
if(aspect_ratio >= 1.0)
- frame_width = static_cast<int>(frame_width*aspect_ratio + 0.5);
+ frame_width = static_cast<int>(std::lround(frame_width * aspect_ratio));
else if(aspect_ratio > 0.0)
- frame_height = static_cast<int>(frame_height/aspect_ratio + 0.5);
+ frame_height = static_cast<int>(std::lround(frame_height / aspect_ratio));
}
SDL_SetWindowSize(screen, frame_width, frame_height);
}
@@ -1546,18 +1543,15 @@ void VideoState::updateVideo(SDL_Window *screen, SDL_Renderer *renderer, bool re
}
/* point pict at the queue */
- uint8_t *pict_data[3];
+ std::array<uint8_t*,3> pict_data;
pict_data[0] = static_cast<uint8_t*>(pixels);
- pict_data[1] = pict_data[0] + w*h;
- pict_data[2] = pict_data[1] + w*h/4;
+ pict_data[1] = pict_data[0] + ptrdiff_t{w}*h;
+ pict_data[2] = pict_data[1] + ptrdiff_t{w}*h/4;
- int pict_linesize[3];
- pict_linesize[0] = pitch;
- pict_linesize[1] = pitch / 2;
- pict_linesize[2] = pitch / 2;
+ std::array pict_linesize{pitch, pitch/2, pitch/2};
- sws_scale(mSwscaleCtx.get(), reinterpret_cast<uint8_t**>(frame->data), frame->linesize,
- 0, h, pict_data, pict_linesize);
+ sws_scale(mSwscaleCtx.get(), reinterpret_cast<uint8_t**>(frame->data),
+ frame->linesize, 0, h, pict_data.data(), pict_linesize.data());
SDL_UnlockTexture(mImage);
}
@@ -1599,7 +1593,7 @@ int VideoState::handler()
/* Prefill the codec buffer. */
auto packet_sender = [this]()
{
- while(1)
+ while(true)
{
const int ret{mQueue.sendPacket(mCodecCtx.get())};
if(ret == AVErrorEOF) break;
@@ -1613,7 +1607,7 @@ int VideoState::handler()
}
auto current_pts = nanoseconds::zero();
- while(1)
+ while(true)
{
size_t write_idx{mPictQWrite.load(std::memory_order_relaxed)};
Picture *vp{&mPictQ[write_idx]};
@@ -2062,7 +2056,7 @@ int main(int argc, char *argv[])
while(fileidx < argc && !movState)
{
- movState = std::unique_ptr<MovieState>{new MovieState{argv[fileidx++]}};
+ movState = std::make_unique<MovieState>(argv[fileidx++]);
if(!movState->prepare()) movState = nullptr;
}
if(!movState)
@@ -2077,7 +2071,7 @@ int main(int argc, char *argv[])
Next, Quit
} eom_action{EomAction::Next};
seconds last_time{seconds::min()};
- while(1)
+ while(true)
{
/* SDL_WaitEventTimeout is broken, just force a 10ms sleep. */
std::this_thread::sleep_for(milliseconds{10});
@@ -2145,7 +2139,7 @@ int main(int argc, char *argv[])
movState = nullptr;
while(fileidx < argc && !movState)
{
- movState = std::unique_ptr<MovieState>{new MovieState{argv[fileidx++]}};
+ movState = std::make_unique<MovieState>(argv[fileidx++]);
if(!movState->prepare()) movState = nullptr;
}
if(movState)
diff --git a/examples/alhrtf.c b/examples/alhrtf.c
index 7ea1b99e..a2f1824a 100644
--- a/examples/alhrtf.c
+++ b/examples/alhrtf.c
@@ -40,6 +40,8 @@
#include "common/alhelpers.h"
+#include "win_main_utf8.h"
+
#ifndef M_PI
#define M_PI (3.14159265358979323846)
diff --git a/examples/allatency.c b/examples/allatency.c
index 01f4eb69..9a535442 100644
--- a/examples/allatency.c
+++ b/examples/allatency.c
@@ -37,6 +37,8 @@
#include "common/alhelpers.h"
+#include "win_main_utf8.h"
+
static LPALSOURCEDSOFT alSourcedSOFT;
static LPALSOURCE3DSOFT alSource3dSOFT;
diff --git a/examples/almultireverb.c b/examples/almultireverb.c
index dcb76c87..f9a46c4f 100644
--- a/examples/almultireverb.c
+++ b/examples/almultireverb.c
@@ -47,6 +47,8 @@
#include "common/alhelpers.h"
+#include "win_main_utf8.h"
+
#ifndef M_PI
#define M_PI 3.14159265358979323846
@@ -106,7 +108,8 @@ static int LoadEffect(ALuint effect, const EFXEAXREVERBPROPERTIES *reverb)
* the needed panning vectors).
*/
alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_EAXREVERB);
- if((err=alGetError()) != AL_NO_ERROR)
+ err = alGetError();
+ if(err != AL_NO_ERROR)
{
fprintf(stderr, "Failed to set EAX Reverb: %s (0x%04x)\n", alGetString(err), err);
return 0;
@@ -138,7 +141,8 @@ static int LoadEffect(ALuint effect, const EFXEAXREVERBPROPERTIES *reverb)
alEffecti(effect, AL_EAXREVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
/* Check if an error occurred, and return failure if so. */
- if((err=alGetError()) != AL_NO_ERROR)
+ err = alGetError();
+ if(err != AL_NO_ERROR)
{
fprintf(stderr, "Error setting up reverb: %s\n", alGetString(err));
return 0;
diff --git a/examples/alplay.c b/examples/alplay.c
index 1eabcccd..9af7ca40 100644
--- a/examples/alplay.c
+++ b/examples/alplay.c
@@ -37,6 +37,8 @@
#include "common/alhelpers.h"
+#include "win_main_utf8.h"
+
enum FormatType {
Int16,
diff --git a/examples/alreverb.c b/examples/alreverb.c
index ff49db25..15b92235 100644
--- a/examples/alreverb.c
+++ b/examples/alreverb.c
@@ -40,6 +40,8 @@
#include "common/alhelpers.h"
+#include "win_main_utf8.h"
+
/* Effect object functions */
static LPALGENEFFECTS alGenEffects;
diff --git a/examples/alstream.c b/examples/alstream.c
index a61680d2..028290f5 100644
--- a/examples/alstream.c
+++ b/examples/alstream.c
@@ -37,13 +37,15 @@
#include "common/alhelpers.h"
+#include "win_main_utf8.h"
+
/* Define the number of buffers and buffer size (in milliseconds) to use. 4
* buffers at 200ms each gives a nice per-chunk size, and lets the queue last
* for almost one second.
*/
-#define NUM_BUFFERS 4
-#define BUFFER_MILLISEC 200
+enum { NumBuffers = 4 };
+enum { BufferMillisec = 200 };
typedef enum SampleType {
Int16, Float, IMA4, MSADPCM
@@ -51,7 +53,7 @@ typedef enum SampleType {
typedef struct StreamPlayer {
/* These are the buffers and source to play out through OpenAL with. */
- ALuint buffers[NUM_BUFFERS];
+ ALuint buffers[NumBuffers];
ALuint source;
/* Handle for the audio file */
@@ -88,7 +90,7 @@ static StreamPlayer *NewPlayer(void)
assert(player != NULL);
/* Generate the buffers and source */
- alGenBuffers(NUM_BUFFERS, player->buffers);
+ alGenBuffers(NumBuffers, player->buffers);
assert(alGetError() == AL_NO_ERROR && "Could not create buffers");
alGenSources(1, &player->source);
@@ -111,11 +113,11 @@ static void DeletePlayer(StreamPlayer *player)
ClosePlayerFile(player);
alDeleteSources(1, &player->source);
- alDeleteBuffers(NUM_BUFFERS, player->buffers);
+ alDeleteBuffers(NumBuffers, player->buffers);
if(alGetError() != AL_NO_ERROR)
fprintf(stderr, "Failed to delete object IDs\n");
- memset(player, 0, sizeof(*player));
+ memset(player, 0, sizeof(*player)); /* NOLINT(clang-analyzer-security.insecureAPI.*) */
free(player);
}
@@ -291,8 +293,8 @@ static int OpenPlayerFile(StreamPlayer *player, const char *filename)
}
player->block_count = player->sfinfo.samplerate / player->sampleblockalign;
- player->block_count = player->block_count * BUFFER_MILLISEC / 1000;
- player->membuf = malloc((size_t)(player->block_count * player->byteblockalign));
+ player->block_count = player->block_count * BufferMillisec / 1000;
+ player->membuf = malloc((size_t)player->block_count * (size_t)player->byteblockalign);
return 1;
}
@@ -310,7 +312,7 @@ static void ClosePlayerFile(StreamPlayer *player)
if(player->sampleblockalign > 1)
{
ALsizei i;
- for(i = 0;i < NUM_BUFFERS;i++)
+ for(i = 0;i < NumBuffers;i++)
alBufferi(player->buffers[i], AL_UNPACK_BLOCK_ALIGNMENT_SOFT, 0);
player->sampleblockalign = 0;
player->byteblockalign = 0;
@@ -328,7 +330,7 @@ static int StartPlayer(StreamPlayer *player)
alSourcei(player->source, AL_BUFFER, 0);
/* Fill the buffer queue */
- for(i = 0;i < NUM_BUFFERS;i++)
+ for(i = 0;i < NumBuffers;i++)
{
sf_count_t slen;
@@ -336,21 +338,21 @@ static int StartPlayer(StreamPlayer *player)
if(player->sample_type == Int16)
{
slen = sf_readf_short(player->sndfile, player->membuf,
- player->block_count * player->sampleblockalign);
+ (sf_count_t)player->block_count * player->sampleblockalign);
if(slen < 1) break;
slen *= player->byteblockalign;
}
else if(player->sample_type == Float)
{
slen = sf_readf_float(player->sndfile, player->membuf,
- player->block_count * player->sampleblockalign);
+ (sf_count_t)player->block_count * player->sampleblockalign);
if(slen < 1) break;
slen *= player->byteblockalign;
}
else
{
slen = sf_read_raw(player->sndfile, player->membuf,
- player->block_count * player->byteblockalign);
+ (sf_count_t)player->block_count * player->byteblockalign);
if(slen > 0) slen -= slen%player->byteblockalign;
if(slen < 1) break;
}
@@ -407,19 +409,19 @@ static int UpdatePlayer(StreamPlayer *player)
if(player->sample_type == Int16)
{
slen = sf_readf_short(player->sndfile, player->membuf,
- player->block_count * player->sampleblockalign);
+ (sf_count_t)player->block_count * player->sampleblockalign);
if(slen > 0) slen *= player->byteblockalign;
}
else if(player->sample_type == Float)
{
slen = sf_readf_float(player->sndfile, player->membuf,
- player->block_count * player->sampleblockalign);
+ (sf_count_t)player->block_count * player->sampleblockalign);
if(slen > 0) slen *= player->byteblockalign;
}
else
{
slen = sf_read_raw(player->sndfile, player->membuf,
- player->block_count * player->byteblockalign);
+ (sf_count_t)player->block_count * player->byteblockalign);
if(slen > 0) slen -= slen%player->byteblockalign;
}
@@ -486,10 +488,9 @@ int main(int argc, char **argv)
/* Get the name portion, without the path, for display. */
namepart = strrchr(argv[i], '/');
- if(namepart || (namepart=strrchr(argv[i], '\\')))
- namepart++;
- else
- namepart = argv[i];
+ if(!namepart) namepart = strrchr(argv[i], '\\');
+ if(!namepart) namepart = argv[i];
+ else namepart++;
printf("Playing: %s (%s, %dhz)\n", namepart, FormatName(player->format),
player->sfinfo.samplerate);
diff --git a/examples/alstreamcb.cpp b/examples/alstreamcb.cpp
index 6cc24034..ebf3d3b0 100644
--- a/examples/alstreamcb.cpp
+++ b/examples/alstreamcb.cpp
@@ -24,12 +24,12 @@
/* This file contains a streaming audio player using a callback buffer. */
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
#include <atomic>
#include <chrono>
+#include <cstring>
+#include <cstdlib>
+#include <cstdio>
#include <memory>
#include <stdexcept>
#include <string>
@@ -44,6 +44,8 @@
#include "common/alhelpers.h"
+#include "win_main_utf8.h"
+
namespace {
@@ -56,8 +58,7 @@ struct StreamPlayer {
/* A lockless ring-buffer (supports single-provider, single-consumer
* operation).
*/
- std::unique_ptr<ALbyte[]> mBufferData;
- size_t mBufferDataSize{0};
+ std::vector<ALbyte> mBufferData;
std::atomic<size_t> mReadPos{0};
std::atomic<size_t> mWritePos{0};
size_t mSamplesPerBlock{1};
@@ -78,7 +79,7 @@ struct StreamPlayer {
size_t mDecoderOffset{0};
/* The format of the callback samples. */
- ALenum mFormat;
+ ALenum mFormat{};
StreamPlayer()
{
@@ -166,8 +167,8 @@ struct StreamPlayer {
mSampleFormat = SampleType::Int16;
else
{
- auto fmtbuf = std::make_unique<ALubyte[]>(inf.datalen);
- inf.data = fmtbuf.get();
+ auto fmtbuf = std::vector<ALubyte>(inf.datalen);
+ inf.data = fmtbuf.data();
if(sf_get_chunk_data(iter, &inf) != SF_ERR_NO_ERROR)
mSampleFormat = SampleType::Int16;
else
@@ -194,12 +195,12 @@ struct StreamPlayer {
if(mSampleFormat == SampleType::Int16)
{
mSamplesPerBlock = 1;
- mBytesPerBlock = static_cast<size_t>(mSfInfo.channels * 2);
+ mBytesPerBlock = static_cast<size_t>(mSfInfo.channels) * 2;
}
else if(mSampleFormat == SampleType::Float)
{
mSamplesPerBlock = 1;
- mBytesPerBlock = static_cast<size_t>(mSfInfo.channels * 4);
+ mBytesPerBlock = static_cast<size_t>(mSfInfo.channels) * 4;
}
else
{
@@ -232,7 +233,7 @@ struct StreamPlayer {
}
else if(mSfInfo.channels == 3)
{
- if(sf_command(mSndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
+ if(sf_command(mSndfile, SFC_WAVEX_GET_AMBISONIC, nullptr, 0) == SF_AMBISONIC_B_FORMAT)
{
if(mSampleFormat == SampleType::Int16)
mFormat = AL_FORMAT_BFORMAT2D_16;
@@ -242,7 +243,7 @@ struct StreamPlayer {
}
else if(mSfInfo.channels == 4)
{
- if(sf_command(mSndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
+ if(sf_command(mSndfile, SFC_WAVEX_GET_AMBISONIC, nullptr, 0) == SF_AMBISONIC_B_FORMAT)
{
if(mSampleFormat == SampleType::Int16)
mFormat = AL_FORMAT_BFORMAT3D_16;
@@ -262,8 +263,7 @@ struct StreamPlayer {
/* Set a 1s ring buffer size. */
size_t numblocks{(static_cast<ALuint>(mSfInfo.samplerate) + mSamplesPerBlock-1)
/ mSamplesPerBlock};
- mBufferDataSize = static_cast<ALuint>(numblocks * mBytesPerBlock);
- mBufferData.reset(new ALbyte[mBufferDataSize]);
+ mBufferData.resize(static_cast<ALuint>(numblocks * mBytesPerBlock));
mReadPos.store(0, std::memory_order_relaxed);
mWritePos.store(0, std::memory_order_relaxed);
mDecoderOffset = 0;
@@ -303,7 +303,7 @@ struct StreamPlayer {
* that case, otherwise read up to the write offset. Also limit the
* amount to copy given how much is remaining to write.
*/
- size_t todo{((woffset < roffset) ? mBufferDataSize : woffset) - roffset};
+ size_t todo{((woffset < roffset) ? mBufferData.size() : woffset) - roffset};
todo = std::min<size_t>(todo, static_cast<ALuint>(size-got));
/* Copy from the ring buffer to the provided output buffer. Wrap
@@ -315,7 +315,7 @@ struct StreamPlayer {
got += static_cast<ALsizei>(todo);
roffset += todo;
- if(roffset == mBufferDataSize)
+ if(roffset == mBufferData.size())
roffset = 0;
}
/* Finally, store the updated read offset, and return how many bytes
@@ -351,7 +351,7 @@ struct StreamPlayer {
if(state != AL_INITIAL)
{
const size_t roffset{mReadPos.load(std::memory_order_relaxed)};
- const size_t readable{((woffset >= roffset) ? woffset : (mBufferDataSize+woffset)) -
+ const size_t readable{((woffset >= roffset) ? woffset : (mBufferData.size()+woffset)) -
roffset};
/* For a stopped (underrun) source, the current playback offset is
* the current decoder offset excluding the readable buffered data.
@@ -362,7 +362,7 @@ struct StreamPlayer {
? (mDecoderOffset-readable) / mBytesPerBlock * mSamplesPerBlock
: (static_cast<ALuint>(pos) + mStartOffset/mBytesPerBlock*mSamplesPerBlock))
/ static_cast<ALuint>(mSfInfo.samplerate)};
- printf("\r%3zus (%3zu%% full)", curtime, readable * 100 / mBufferDataSize);
+ printf("\r%3zus (%3zu%% full)", curtime, readable * 100 / mBufferData.size());
}
else
fputs("Starting...", stdout);
@@ -415,8 +415,8 @@ struct StreamPlayer {
* data can fit, and calculate how much can go in front before
* wrapping.
*/
- const size_t writable{(!roffset ? mBufferDataSize-woffset-1 :
- (mBufferDataSize-woffset)) / mBytesPerBlock};
+ const size_t writable{(!roffset ? mBufferData.size()-woffset-1 :
+ (mBufferData.size()-woffset)) / mBytesPerBlock};
if(!writable) break;
if(mSampleFormat == SampleType::Int16)
@@ -444,7 +444,7 @@ struct StreamPlayer {
}
woffset += read_bytes;
- if(woffset == mBufferDataSize)
+ if(woffset == mBufferData.size())
woffset = 0;
}
mWritePos.store(woffset, std::memory_order_release);
@@ -459,7 +459,7 @@ struct StreamPlayer {
* what's available.
*/
const size_t roffset{mReadPos.load(std::memory_order_relaxed)};
- const size_t readable{((woffset >= roffset) ? woffset : (mBufferDataSize+woffset)) -
+ const size_t readable{((woffset >= roffset) ? woffset : (mBufferData.size()+woffset)) -
roffset};
if(readable == 0)
return false;
@@ -522,7 +522,8 @@ int main(int argc, char **argv)
/* Get the name portion, without the path, for display. */
const char *namepart{strrchr(argv[i], '/')};
- if(namepart || (namepart=strrchr(argv[i], '\\')))
+ if(!namepart) namepart = strrchr(argv[i], '\\');
+ if(namepart)
++namepart;
else
namepart = argv[i];
diff --git a/examples/altonegen.c b/examples/altonegen.c
index ec7f77bb..2fab783d 100644
--- a/examples/altonegen.c
+++ b/examples/altonegen.c
@@ -321,7 +321,6 @@ int main(int argc, char *argv[])
if(pos > last_pos)
{
- last_pos = 0;
printf("%d...\n", seconds - pos);
fflush(stdout);
}