summaryrefslogtreecommitdiff
path: root/src/audiofile.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/audiofile.cc')
-rw-r--r--src/audiofile.cc46
1 files changed, 26 insertions, 20 deletions
diff --git a/src/audiofile.cc b/src/audiofile.cc
index 3f59fcf..6fcdcb5 100644
--- a/src/audiofile.cc
+++ b/src/audiofile.cc
@@ -30,6 +30,7 @@
#include <cassert>
#include <sndfile.h>
+#include <array>
#include <config.h>
@@ -37,9 +38,9 @@
#include "channel.h"
-AudioFile::AudioFile(const std::string& filename, std::size_t filechannel,
+AudioFile::AudioFile(std::string filename, std::size_t filechannel,
InstrumentChannel* instrument_channel)
- : filename(filename)
+ : filename(std::move(filename))
, filechannel(filechannel)
, magic{this}
, instrument_channel(instrument_channel)
@@ -71,28 +72,28 @@ void AudioFile::unload()
data = nullptr;
}
-#define BUFFER_SIZE 4096
+constexpr std::size_t buffer_size{4096};
-void AudioFile::load(LogFunction logger, std::size_t sample_limit)
+void AudioFile::load(const LogFunction& logger, std::size_t sample_limit)
{
// Make sure we don't unload the object while loading it...
const std::lock_guard<std::mutex> guard(mutex);
- if(this->data) // already loaded
+ if(this->data != nullptr) // already loaded
{
return;
}
SF_INFO sf_info{};
- SNDFILE *fh = sf_open(filename.c_str(), SFM_READ, &sf_info);
- if(!fh)
+ SNDFILE *file_handle = sf_open(filename.c_str(), SFM_READ, &sf_info);
+ if(file_handle == nullptr)
{
ERR(audiofile,"SNDFILE Error (%s): %s\n",
- filename.c_str(), sf_strerror(fh));
+ filename.c_str(), sf_strerror(file_handle));
if(logger)
{
logger(LogLevel::Warning, "Could not load '" + filename +
- "': " + sf_strerror(fh));
+ "': " + sf_strerror(file_handle));
}
return;
}
@@ -108,7 +109,7 @@ void AudioFile::load(LogFunction logger, std::size_t sample_limit)
return;
}
- std::size_t size = sf_info.frames;
+ const std::size_t size = sf_info.frames;
std::size_t preloadedsize = sf_info.frames;
if(preloadedsize > sample_limit)
@@ -116,10 +117,12 @@ void AudioFile::load(LogFunction logger, std::size_t sample_limit)
preloadedsize = sample_limit;
}
- sample_t* data = new sample_t[preloadedsize];
+ gsl::owner<sample_t*> data{};
+ data = new sample_t[preloadedsize];
if(sf_info.channels == 1)
{
- preloadedsize = sf_read_float(fh, data, preloadedsize);
+ preloadedsize =
+ sf_read_float(file_handle, data, static_cast<sf_count_t>(preloadedsize));
}
else
{
@@ -134,19 +137,22 @@ void AudioFile::load(LogFunction logger, std::size_t sample_limit)
filechannel = sf_info.channels - 1;
}
- sample_t buffer[BUFFER_SIZE];
- std::size_t frame_count = BUFFER_SIZE / sf_info.channels;
- std::size_t total_frames_read = 0;
- int frames_read;
+ std::array<sample_t, buffer_size> buffer{};
+ const sf_count_t frame_count{static_cast<sf_count_t>(buffer.size()) /
+ sf_info.channels};
+ std::size_t total_frames_read{};
+ std::size_t frames_read{};
do
{
- frames_read = sf_readf_float(fh, buffer, frame_count);
- for(int i = 0;
+ frames_read = static_cast<std::size_t>(
+ sf_readf_float(file_handle, buffer.data(), frame_count));
+ for(std::size_t i = 0;
(i < frames_read) && (total_frames_read < sample_limit);
++i)
{
- data[total_frames_read++] = buffer[i * sf_info.channels + filechannel];
+ data[total_frames_read++] =
+ buffer[i * sf_info.channels + filechannel]; // NOLINT - span
}
}
while( (frames_read > 0) &&
@@ -157,7 +163,7 @@ void AudioFile::load(LogFunction logger, std::size_t sample_limit)
preloadedsize = total_frames_read;
}
- sf_close(fh);
+ sf_close(file_handle);
this->data = data;
this->size = size;