Commit 5abdf34c authored by Tristan Matthews's avatar Tristan Matthews

* #30063: audiobuffer: don't crash if empty

parent ddf23c0e
......@@ -48,7 +48,7 @@ AudioBuffer::AudioBuffer(const SFLAudioSample* in, size_t sample_num, unsigned c
AudioBuffer::AudioBuffer(const AudioBuffer& other, bool copy_content /* = false */)
: sampleRate_(other.sampleRate_),
samples_(copy_content ? other.samples_ :
std::vector<std::vector<SFLAudioSample> >(other.samples_.size(), std::vector<SFLAudioSample>(other.samples_[0].size())))
std::vector<std::vector<SFLAudioSample> >(other.samples_.size(), std::vector<SFLAudioSample>(other.samples())))
{}
int AudioBuffer::getSampleRate() const
......@@ -68,10 +68,7 @@ void AudioBuffer::setChannelNum(unsigned n, bool copy_content /* = false */)
if (n == samples_.size())
return;
size_t start_size = 0;
if (not samples_.empty())
start_size = samples_[0].size();
const size_t start_size = samples();
if (copy_content and not samples_.empty())
samples_.resize(n, samples_[0]);
......@@ -81,7 +78,7 @@ void AudioBuffer::setChannelNum(unsigned n, bool copy_content /* = false */)
void AudioBuffer::resize(size_t sample_num)
{
if (samples_[0].size() == sample_num)
if (samples() == sample_num)
return;
for (unsigned i = 0; i < samples_.size(); i++)
......@@ -117,20 +114,20 @@ void AudioBuffer::applyGain(double gain)
size_t AudioBuffer::interleave(SFLAudioSample* out) const
{
for (unsigned i = 0; i < samples_[0].size(); i++)
for (unsigned i = 0; i < samples(); i++)
for (unsigned j = 0; j < samples_.size(); j++)
*out++ = samples_[j][i];
return samples_[0].size() * samples_.size();
return samples() * samples_.size();
}
size_t AudioBuffer::interleaveFloat(float* out) const
{
for (unsigned i = 0; i < samples_[0].size(); i++)
for (unsigned i = 0; i < samples(); i++)
for (unsigned j = 0; j < samples_.size(); j++)
*out++ = (float) samples_[j][i] * .000030517578125f;
return samples_[0].size() * samples_.size();
return samples() * samples_.size();
}
void AudioBuffer::deinterleave(const SFLAudioSample* in, size_t sample_num, unsigned channel_num)
......@@ -142,7 +139,7 @@ void AudioBuffer::deinterleave(const SFLAudioSample* in, size_t sample_num, unsi
setChannelNum(channel_num);
resize(sample_num);
for (unsigned i = 0; i < samples_[0].size(); i++)
for (unsigned i = 0; i < samples(); i++)
for (unsigned j = 0; j < samples_.size(); j++)
samples_[j][i] = *in++;
}
......@@ -150,7 +147,7 @@ void AudioBuffer::deinterleave(const SFLAudioSample* in, size_t sample_num, unsi
size_t AudioBuffer::mix(const AudioBuffer& other, bool up /* = true */)
{
const bool upmix = up && (other.samples_.size() < samples_.size());
const size_t samp_num = std::min(samples_[0].size(), other.samples_[0].size());
const size_t samp_num = std::min(samples(), other.samples());
const unsigned chan_num = upmix ? samples_.size() : std::min(samples_.size(), other.samples_.size());
for (unsigned i = 0; i < chan_num; i++) {
......@@ -175,7 +172,7 @@ size_t AudioBuffer::copy(AudioBuffer& in, int sample_num /* = -1 */, size_t pos_
const bool upmix = up && (in.samples_.size() < samples_.size());
const size_t chan_num = upmix ? samples_.size() : std::min(in.samples_.size(), samples_.size());
if ((pos_out + to_copy) > samples_[0].size())
if ((pos_out + to_copy) > samples())
resize(pos_out + to_copy);
sampleRate_ = in.sampleRate_;
......@@ -193,7 +190,7 @@ size_t AudioBuffer::copy(SFLAudioSample* in, size_t sample_num, size_t pos_out /
{
if (in == NULL) return 0;
if ((pos_out + sample_num) > samples_[0].size())
if ((pos_out + sample_num) > samples())
resize(pos_out + sample_num);
const size_t chan_num = samples_.size();
......@@ -208,7 +205,7 @@ size_t AudioBuffer::copy(SFLAudioSample* in, size_t sample_num, size_t pos_out /
std::ostream& operator<<(std::ostream& os, const AudioBuffer& buf)
{
for (unsigned i = 0; i < buf.samples_[0].size(); i++) {
for (unsigned i = 0; i < buf.samples(); i++) {
for (unsigned j = 0; j < buf.samples_.size(); j++)
os << buf.samples_[j][i];
}
......
......@@ -60,9 +60,9 @@ class AudioBuffer {
std::fill(i->begin(), i->end(), 0);
}
inline size_t size() {
return samples_[0].size() * channels() * sizeof(SFLAudioSample);
}
inline size_t size() {
return samples() * channels() * sizeof(SFLAudioSample);
}
/**
* Returns the sample rate (in samples/sec) associated to this buffer.
......@@ -94,8 +94,11 @@ class AudioBuffer {
* Returns the number of (multichannel) samples in this buffer.
*/
inline size_t samples() const {
return samples_[0].size();
}
if (not samples_.empty())
return samples_[0].size();
else
return 0;
}
/**
* Return the total number of single samples in the buffer (same as samples()*channels()).
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment