summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/audiofile.cc46
-rw-r--r--src/audiofile.h12
2 files changed, 33 insertions, 25 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;
diff --git a/src/audiofile.h b/src/audiofile.h
index 504d0ae..9fdd8c5 100644
--- a/src/audiofile.h
+++ b/src/audiofile.h
@@ -35,24 +35,26 @@
#include "audio.h"
#include "channel.h"
#include "logger.h"
+#include "owner.h"
class InstrumentChannel;
class AudioFile
{
public:
- AudioFile(const std::string& filename, std::size_t filechannel,
+ AudioFile(std::string filename, std::size_t filechannel,
InstrumentChannel* instrument_channel = nullptr);
~AudioFile();
- void load(LogFunction logger, std::size_t sample_limit = std::numeric_limits<std::size_t>::max());
+ void load(const LogFunction& logger,
+ std::size_t sample_limit = std::numeric_limits<std::size_t>::max());
void unload();
bool isLoaded() const;
volatile std::size_t size{0}; // Full size of the file
volatile std::size_t preloadedsize{0}; // Number of samples preloaded (in data)
- sample_t* data{nullptr};
+ gsl::owner<sample_t*> data{nullptr};
std::string filename;
@@ -70,7 +72,7 @@ private:
friend class DOMLoaderTest;
friend class InstrumentParserTest;
- void* magic{nullptr};
+ void* magic{};
volatile bool is_loaded{false};
- InstrumentChannel* instrument_channel;
+ InstrumentChannel* instrument_channel{};
};