Commit 3efc8da6 authored by Tristan Matthews's avatar Tristan Matthews

* #30063: audiobuffer: rename samples() to frames()

An audiobuffer of a given duration will return the same number of frames
regardless of how many channels it has.
parent 5abdf34c
......@@ -706,7 +706,7 @@ void AlsaLayer::capture()
AudioBuffer in(toGetSamples, 1, sampleRate_);
// TODO: handle ALSA multichannel capture
const int toGetBytes = in.samples() * sizeof(SFLAudioSample);
const int toGetBytes = in.frames() * sizeof(SFLAudioSample);
SFLAudioSample * const in_ptr = in.getChannel(0)->data();
if (read(in_ptr, toGetBytes) != toGetBytes) {
......
......@@ -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())))
std::vector<std::vector<SFLAudioSample> >(other.samples_.size(), std::vector<SFLAudioSample>(other.frames())))
{}
int AudioBuffer::getSampleRate() const
......@@ -68,7 +68,7 @@ void AudioBuffer::setChannelNum(unsigned n, bool copy_content /* = false */)
if (n == samples_.size())
return;
const size_t start_size = samples();
const size_t start_size = frames();
if (copy_content and not samples_.empty())
samples_.resize(n, samples_[0]);
......@@ -78,7 +78,7 @@ void AudioBuffer::setChannelNum(unsigned n, bool copy_content /* = false */)
void AudioBuffer::resize(size_t sample_num)
{
if (samples() == sample_num)
if (frames() == sample_num)
return;
for (unsigned i = 0; i < samples_.size(); i++)
......@@ -114,20 +114,20 @@ void AudioBuffer::applyGain(double gain)
size_t AudioBuffer::interleave(SFLAudioSample* out) const
{
for (unsigned i = 0; i < samples(); i++)
for (unsigned i = 0; i < frames(); i++)
for (unsigned j = 0; j < samples_.size(); j++)
*out++ = samples_[j][i];
return samples() * samples_.size();
return frames() * samples_.size();
}
size_t AudioBuffer::interleaveFloat(float* out) const
{
for (unsigned i = 0; i < samples(); i++)
for (unsigned i = 0; i < frames(); i++)
for (unsigned j = 0; j < samples_.size(); j++)
*out++ = (float) samples_[j][i] * .000030517578125f;
return samples() * samples_.size();
return frames() * samples_.size();
}
void AudioBuffer::deinterleave(const SFLAudioSample* in, size_t sample_num, unsigned channel_num)
......@@ -139,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(); i++)
for (unsigned i = 0; i < frames(); i++)
for (unsigned j = 0; j < samples_.size(); j++)
samples_[j][i] = *in++;
}
......@@ -147,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(), other.samples());
const size_t samp_num = std::min(frames(), other.frames());
const unsigned chan_num = upmix ? samples_.size() : std::min(samples_.size(), other.samples_.size());
for (unsigned i = 0; i < chan_num; i++) {
......@@ -163,16 +163,16 @@ size_t AudioBuffer::mix(const AudioBuffer& other, bool up /* = true */)
size_t AudioBuffer::copy(AudioBuffer& in, int sample_num /* = -1 */, size_t pos_in /* = 0 */, size_t pos_out /* = 0 */, bool up /* = true */)
{
if (sample_num == -1)
sample_num = in.samples();
sample_num = in.frames();
int to_copy = std::min((int)in.samples() - (int)pos_in, sample_num);
int to_copy = std::min((int)in.frames() - (int)pos_in, sample_num);
if (to_copy <= 0) return 0;
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())
if ((pos_out + to_copy) > frames())
resize(pos_out + to_copy);
sampleRate_ = in.sampleRate_;
......@@ -190,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())
if ((pos_out + sample_num) > frames())
resize(pos_out + sample_num);
const size_t chan_num = samples_.size();
......@@ -205,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(); i++) {
for (unsigned i = 0; i < buf.frames(); i++) {
for (unsigned j = 0; j < buf.samples_.size(); j++)
os << buf.samples_[j][i];
}
......
......@@ -61,7 +61,7 @@ class AudioBuffer {
}
inline size_t size() {
return samples() * channels() * sizeof(SFLAudioSample);
return frames() * channels() * sizeof(SFLAudioSample);
}
/**
......@@ -91,9 +91,9 @@ class AudioBuffer {
void setChannelNum(unsigned n, bool copy_first = false);
/**
* Returns the number of (multichannel) samples in this buffer.
* Returns the number of (multichannel) frames in this buffer.
*/
inline size_t samples() const {
inline size_t frames() const {
if (not samples_.empty())
return samples_[0].size();
else
......@@ -101,10 +101,10 @@ class AudioBuffer {
}
/**
* Return the total number of single samples in the buffer (same as samples()*channels()).
* Return the total number of single samples in the buffer
*/
inline size_t capacity() const {
return samples() * channels();
return frames() * channels();
}
/**
......@@ -145,7 +145,7 @@ class AudioBuffer {
/**
* Write interleaved multichannel data to the out buffer, while samples are converted to float.
* The buffer must be at least of size getChannelNum()*samples()*sizeof(float).
* The buffer must be at least of size getChannelNum()*frames()*sizeof(float).
*
* @returns Number of samples writen.
*/
......
......@@ -58,7 +58,7 @@ AudioLoop::~AudioLoop()
void
AudioLoop::seek(double relative_position)
{
pos_ = static_cast<double>(buffer_->samples() * relative_position * 0.01);
pos_ = static_cast<double>(buffer_->frames() * relative_position * 0.01);
}
void
......@@ -69,9 +69,9 @@ AudioLoop::getNext(AudioBuffer& output, double gain)
return;
}
const size_t buf_samples = buffer_->samples();
const size_t buf_samples = buffer_->frames();
size_t pos = pos_;
size_t total_samples = output.samples();
size_t total_samples = output.frames();
size_t output_pos = 0;
if (buf_samples == 0) {
......
......@@ -72,7 +72,7 @@ class AudioLoop {
* @return unsigned int The size
*/
size_t getSize() {
return buffer_->samples();
return buffer_->frames();
}
protected:
......
......@@ -231,7 +231,7 @@ void AudioRecord::recData(AudioBuffer& buffer)
return;
}
const int nSamples = buffer.samples();
const int nSamples = buffer.frames();
// FIXME: mono only
if (fileHandle_->write(buffer.getChannel(0)->data(), nSamples) != nSamples) {
......
......@@ -375,7 +375,7 @@ void AudioRtpRecordHandler::processDataDecode(unsigned char *spkrData, size_t si
}
}
size = std::min(size, audioRtpRecord_.decData_.samples());
size = std::min(size, audioRtpRecord_.decData_.frames());
{
ScopedLock lock(audioRtpRecord_.audioCodecMutex_);
......
......@@ -62,7 +62,7 @@ void DcBlocker::process(SFLAudioSample *out, SFLAudioSample *in, int samples)
void DcBlocker::process(AudioBuffer& buf)
{
const size_t chans = buf.channels();
const size_t samples = buf.samples();
const size_t samples = buf.frames();
if(chans > states.size())
states.resize(buf.channels(), (struct StreamState){0, 0, 0, 0});
......
......@@ -58,7 +58,7 @@ GainControl::GainControl(double sr, double target) : averager_(sr, SFL_GAIN_ATTA
void GainControl::process(AudioBuffer& buf)
{
process(buf.getChannel(0)->data(), buf.samples());
process(buf.getChannel(0)->data(), buf.frames());
}
void GainControl::process(SFLAudioSample *buf, int samples)
......
......@@ -544,7 +544,7 @@ void PulseLayer::readFromMic()
Manager::instance().getMainBuffer().putData(*out, MainBuffer::DEFAULT_ID);
#ifdef RECTODISK
outfileResampled.write((const char *)out->getChannel(0), out->samples() * sizeof(SFLAudioSample));
outfileResampled.write((const char *)out->getChannel(0), out->frames() * sizeof(SFLAudioSample));
#endif
if (pa_stream_drop(record_->pulseStream()) < 0)
......
......@@ -73,21 +73,21 @@ RingBuffer::flushAll()
size_t
RingBuffer::putLength() const
{
const size_t buffer_size = buffer_.samples();
const size_t buffer_size = buffer_.frames();
const size_t startPos = (not readpointers_.empty()) ? getSmallestReadPointer() : 0;
return (endPos_ + buffer_size - startPos) % buffer_size;
}
size_t RingBuffer::getLength(const std::string &call_id) const
{
const size_t buffer_size = buffer_.samples();
const size_t buffer_size = buffer_.frames();
return (endPos_ + buffer_size - getReadPointer(call_id)) % buffer_size;
}
void
RingBuffer::debug()
{
DEBUG("Start=%d; End=%d; BufferSize=%d", getSmallestReadPointer(), endPos_, buffer_.samples());
DEBUG("Start=%d; End=%d; BufferSize=%d", getSmallestReadPointer(), endPos_, buffer_.frames());
}
size_t RingBuffer::getReadPointer(const std::string &call_id) const
......@@ -105,7 +105,7 @@ RingBuffer::getSmallestReadPointer() const
if (hasNoReadPointers())
return 0;
size_t smallest = buffer_.samples();
size_t smallest = buffer_.frames();
ReadPointer::const_iterator iter;
......@@ -166,8 +166,8 @@ bool RingBuffer::hasNoReadPointers() const
void RingBuffer::put(AudioBuffer& buf)
{
const size_t len = putLength();
const size_t sample_num = buf.samples();
const size_t buffer_size = buffer_.samples();
const size_t sample_num = buf.frames();
const size_t buffer_size = buffer_.frames();
size_t toCopy = sample_num;
// Add more channels if the input buffer holds more channels than the ring.
......@@ -216,8 +216,8 @@ size_t RingBuffer::get(AudioBuffer& buf, const std::string &call_id)
return 0;
const size_t len = getLength(call_id);
const size_t sample_num = buf.samples();
const size_t buffer_size = buffer_.samples();
const size_t sample_num = buf.frames();
const size_t buffer_size = buffer_.frames();
size_t toCopy = std::min(sample_num, len);
const size_t copied = toCopy;
......@@ -250,7 +250,7 @@ RingBuffer::discard(size_t toDiscard, const std::string &call_id)
if (toDiscard > len)
toDiscard = len;
size_t buffer_size = buffer_.samples();
size_t buffer_size = buffer_.frames();
size_t startPos = (getReadPointer(call_id) + toDiscard) % buffer_size;
storeReadPointer(startPos, call_id);
......
......@@ -69,7 +69,7 @@ void SamplerateConverter::resample(const AudioBuffer &dataIn, AudioBuffer &dataO
if (sampleFactor == 1.0)
return;
const size_t nbFrames = dataIn.samples();
const size_t nbFrames = dataIn.frames();
const size_t nbChans = dataIn.channels();
if (nbChans != channels_) {
......
......@@ -58,7 +58,7 @@ AudioFile::onBufferFinish()
if ((updatePlaybackScale_ % 5) == 0) {
CallManager *cm = Manager::instance().getClient()->getCallManager();
cm->updatePlaybackScale(filepath_, pos_ / divisor, buffer_->samples() / divisor);
cm->updatePlaybackScale(filepath_, pos_ / divisor, buffer_->frames() / divisor);
}
updatePlaybackScale_++;
......
......@@ -43,24 +43,24 @@ void AudioBufferTest::testAudioBufferConstructors()
SFLAudioSample test_samples2[] = {10, 11, 12, 13, 14, 15, 16, 17};
AudioBuffer empty_buf(0);
CPPUNIT_ASSERT(empty_buf.samples() == 0);
CPPUNIT_ASSERT(empty_buf.frames() == 0);
CPPUNIT_ASSERT(empty_buf.channels() == 1);
CPPUNIT_ASSERT(empty_buf.getChannel(0)->size() == 0);
AudioBuffer test_buf1(8, 2);
CPPUNIT_ASSERT(test_buf1.samples() == 8);
CPPUNIT_ASSERT(test_buf1.frames() == 8);
CPPUNIT_ASSERT(test_buf1.channels() == 2);
CPPUNIT_ASSERT(test_buf1.getChannel(0)->size() == 8);
CPPUNIT_ASSERT(test_buf1.getChannel(1)->size() == 8);
CPPUNIT_ASSERT(test_buf1.getChannel(2) == NULL);
AudioBuffer test_buf2(test_samples1, 0, 0);
CPPUNIT_ASSERT(test_buf2.samples() == 0);
CPPUNIT_ASSERT(test_buf2.frames() == 0);
CPPUNIT_ASSERT(test_buf2.channels() == 1);
CPPUNIT_ASSERT(test_buf2.getChannel(0)->size() == 0);
AudioBuffer test_buf3(test_samples2, 4, 2);
CPPUNIT_ASSERT(test_buf3.samples() == 4);
CPPUNIT_ASSERT(test_buf3.frames() == 4);
CPPUNIT_ASSERT(test_buf3.channels() == 2);
CPPUNIT_ASSERT(test_buf3.getChannel(0)->size() == 4);
}
......@@ -98,7 +98,7 @@ void AudioBufferTest::testAudioBufferMix()
test_buf1.mix(test_buf2);
CPPUNIT_ASSERT(test_buf1.channels() == 2);
CPPUNIT_ASSERT(test_buf1.samples() == 4);
CPPUNIT_ASSERT(test_buf1.frames() == 4);
CPPUNIT_ASSERT((*test_buf1.getChannel(0))[0] == test_samples1[0]+test_samples2[0]);
CPPUNIT_ASSERT((*test_buf1.getChannel(1))[0] == test_samples1[0]+test_samples2[1]);
}
......
......@@ -215,7 +215,7 @@ void ResamplerTest::performUpsampling(SamplerateConverter &converter)
AudioBuffer tmpInputBuffer(TMP_LOWSMPLR_BUFFER_LENGTH, 1, 8000);
AudioBuffer tmpOutputBuffer(TMP_HIGHSMPLR_BUFFER_LENGTH, 1, 16000);
for (size_t i = 0, j = 0; i < (inputBuffer.samples() / 2); i += tmpInputBuffer.samples(), j += tmpOutputBuffer.samples()) {
for (size_t i = 0, j = 0; i < (inputBuffer.frames() / 2); i += tmpInputBuffer.frames(), j += tmpOutputBuffer.frames()) {
tmpInputBuffer.copy(inputBuffer, i);
converter.resample(tmpInputBuffer, tmpOutputBuffer);
outputBuffer.copy(tmpOutputBuffer, -1, 0, j);
......@@ -227,7 +227,7 @@ void ResamplerTest::performDownsampling(SamplerateConverter &converter)
AudioBuffer tmpInputBuffer(TMP_HIGHSMPLR_BUFFER_LENGTH, 1, 16000);
AudioBuffer tmpOutputBuffer(TMP_LOWSMPLR_BUFFER_LENGTH, 1, 8000);
for (size_t i = 0, j = 0; i < inputBuffer.samples(); i += tmpInputBuffer.samples(), j += tmpOutputBuffer.samples()) {
for (size_t i = 0, j = 0; i < inputBuffer.frames(); i += tmpInputBuffer.frames(), j += tmpOutputBuffer.frames()) {
tmpInputBuffer.copy(inputBuffer, i);
converter.resample(tmpInputBuffer, tmpOutputBuffer);
outputBuffer.copy(tmpOutputBuffer, -1, 0, j);
......
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