LibAudio: Buffer API improvements

This consists of two changes: First, a utility function create_empty
allows the user to quickly create an empty buffer. Second, most creation
functions now return a NonnullRefPtr, as their failure causes a VERIFY
crash anyways.
This commit is contained in:
kleines Filmröllchen 2021-10-03 16:35:07 +02:00 committed by Brian Gianforcaro
parent 14d330faba
commit ec8bd8116d
2 changed files with 11 additions and 5 deletions

View file

@ -122,13 +122,13 @@ static double read_norm_sample_8(InputMemoryStream& stream)
return double(sample) / NumericLimits<u8>::max();
}
RefPtr<Buffer> Buffer::from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format)
NonnullRefPtr<Buffer> Buffer::from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format)
{
InputMemoryStream stream { data };
return from_pcm_stream(stream, num_channels, sample_format, data.size() / (pcm_bits_per_sample(sample_format) / 8));
}
RefPtr<Buffer> Buffer::from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples)
NonnullRefPtr<Buffer> Buffer::from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples)
{
Vector<Sample> fdata;
fdata.ensure_capacity(num_samples);

View file

@ -67,8 +67,8 @@ private:
// A buffer of audio samples.
class Buffer : public RefCounted<Buffer> {
public:
static RefPtr<Buffer> from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format);
static RefPtr<Buffer> from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples);
static NonnullRefPtr<Buffer> from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format);
static NonnullRefPtr<Buffer> from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples);
static NonnullRefPtr<Buffer> create_with_samples(Vector<Sample>&& samples)
{
return adopt_ref(*new Buffer(move(samples)));
@ -77,6 +77,10 @@ public:
{
return adopt_ref(*new Buffer(move(buffer), buffer_id, sample_count));
}
static NonnullRefPtr<Buffer> create_empty()
{
return adopt_ref(*new Buffer({}));
}
const Sample* samples() const { return (const Sample*)data(); }
int sample_count() const { return m_sample_count; }
@ -87,7 +91,9 @@ public:
private:
explicit Buffer(const Vector<Sample> samples)
: m_buffer(Core::AnonymousBuffer::create_with_size(samples.size() * sizeof(Sample)).release_value())
// FIXME: AnonymousBuffers can't be empty, so even for empty buffers we create a buffer of size 1 here,
// although the sample count is set to 0 to mark this.
: m_buffer(Core::AnonymousBuffer::create_with_size(max(samples.size(), 1) * sizeof(Sample)).release_value())
, m_id(allocate_id())
, m_sample_count(samples.size())
{