summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorkleines Filmröllchen <malu.bertsch@gmail.com>2021-10-03 16:35:07 +0200
committerBrian Gianforcaro <b.gianfo@gmail.com>2021-11-28 13:33:51 -0800
commitec8bd8116d4683ae51897e8e49c62823bfdd9005 (patch)
treef7209f527f9b0034925497e884cc4b86d7b561c3 /Userland
parent14d330fabaff7c24218377601056b79ddef144cb (diff)
downloadserenity-ec8bd8116d4683ae51897e8e49c62823bfdd9005.zip
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.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibAudio/Buffer.cpp4
-rw-r--r--Userland/Libraries/LibAudio/Buffer.h12
2 files changed, 11 insertions, 5 deletions
diff --git a/Userland/Libraries/LibAudio/Buffer.cpp b/Userland/Libraries/LibAudio/Buffer.cpp
index a88c20184e..c58cfa5f72 100644
--- a/Userland/Libraries/LibAudio/Buffer.cpp
+++ b/Userland/Libraries/LibAudio/Buffer.cpp
@@ -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);
diff --git a/Userland/Libraries/LibAudio/Buffer.h b/Userland/Libraries/LibAudio/Buffer.h
index dbc08814e6..8f9e8457af 100644
--- a/Userland/Libraries/LibAudio/Buffer.h
+++ b/Userland/Libraries/LibAudio/Buffer.h
@@ -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())
{