mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:37:44 +00:00
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:
parent
14d330faba
commit
ec8bd8116d
2 changed files with 11 additions and 5 deletions
|
@ -122,13 +122,13 @@ static double read_norm_sample_8(InputMemoryStream& stream)
|
||||||
return double(sample) / NumericLimits<u8>::max();
|
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 };
|
InputMemoryStream stream { data };
|
||||||
return from_pcm_stream(stream, num_channels, sample_format, data.size() / (pcm_bits_per_sample(sample_format) / 8));
|
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;
|
Vector<Sample> fdata;
|
||||||
fdata.ensure_capacity(num_samples);
|
fdata.ensure_capacity(num_samples);
|
||||||
|
|
|
@ -67,8 +67,8 @@ private:
|
||||||
// A buffer of audio samples.
|
// A buffer of audio samples.
|
||||||
class Buffer : public RefCounted<Buffer> {
|
class Buffer : public RefCounted<Buffer> {
|
||||||
public:
|
public:
|
||||||
static RefPtr<Buffer> from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format);
|
static NonnullRefPtr<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_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples);
|
||||||
static NonnullRefPtr<Buffer> create_with_samples(Vector<Sample>&& samples)
|
static NonnullRefPtr<Buffer> create_with_samples(Vector<Sample>&& samples)
|
||||||
{
|
{
|
||||||
return adopt_ref(*new Buffer(move(samples)));
|
return adopt_ref(*new Buffer(move(samples)));
|
||||||
|
@ -77,6 +77,10 @@ public:
|
||||||
{
|
{
|
||||||
return adopt_ref(*new Buffer(move(buffer), buffer_id, sample_count));
|
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(); }
|
const Sample* samples() const { return (const Sample*)data(); }
|
||||||
int sample_count() const { return m_sample_count; }
|
int sample_count() const { return m_sample_count; }
|
||||||
|
@ -87,7 +91,9 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit Buffer(const Vector<Sample> samples)
|
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_id(allocate_id())
|
||||||
, m_sample_count(samples.size())
|
, m_sample_count(samples.size())
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue