mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 17:44:56 +09:00
LibAudio: Use InputMemoryStream instead of BufferStream.
This commit is contained in:
parent
ae9f0e1cd8
commit
fa43bf92e4
Notes:
sideshowbarker
2024-07-19 02:18:51 +09:00
Author: https://github.com/asynts
Commit: fa43bf92e4
Pull-request: https://github.com/SerenityOS/serenity/pull/3561
3 changed files with 12 additions and 21 deletions
|
@ -93,7 +93,6 @@ public:
|
||||||
return m_bytes[m_offset];
|
return m_bytes[m_offset];
|
||||||
}
|
}
|
||||||
|
|
||||||
// LEB128 is a variable-length encoding for integers
|
|
||||||
bool read_LEB128_unsigned(size_t& result)
|
bool read_LEB128_unsigned(size_t& result)
|
||||||
{
|
{
|
||||||
const auto backup = m_offset;
|
const auto backup = m_offset;
|
||||||
|
@ -101,8 +100,6 @@ public:
|
||||||
result = 0;
|
result = 0;
|
||||||
size_t num_bytes = 0;
|
size_t num_bytes = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
// Note. The implementation in AK::BufferStream::read_LEB128_unsigned read one
|
|
||||||
// past the end, this is fixed here.
|
|
||||||
if (eof()) {
|
if (eof()) {
|
||||||
m_offset = backup;
|
m_offset = backup;
|
||||||
set_recoverable_error();
|
set_recoverable_error();
|
||||||
|
@ -120,7 +117,6 @@ public:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// LEB128 is a variable-length encoding for integers
|
|
||||||
bool read_LEB128_signed(ssize_t& result)
|
bool read_LEB128_signed(ssize_t& result)
|
||||||
{
|
{
|
||||||
const auto backup = m_offset;
|
const auto backup = m_offset;
|
||||||
|
@ -130,8 +126,6 @@ public:
|
||||||
u8 byte = 0;
|
u8 byte = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
// Note. The implementation in AK::BufferStream::read_LEB128_unsigned read one
|
|
||||||
// past the end, this is fixed here.
|
|
||||||
if (eof()) {
|
if (eof()) {
|
||||||
m_offset = backup;
|
m_offset = backup;
|
||||||
set_recoverable_error();
|
set_recoverable_error();
|
||||||
|
|
|
@ -108,7 +108,7 @@ private:
|
||||||
// A buffer of audio samples, normalized to 44100hz.
|
// A buffer of audio samples, normalized to 44100hz.
|
||||||
class Buffer : public RefCounted<Buffer> {
|
class Buffer : public RefCounted<Buffer> {
|
||||||
public:
|
public:
|
||||||
static RefPtr<Buffer> from_pcm_data(ByteBuffer& data, ResampleHelper& resampler, int num_channels, int bits_per_sample);
|
static RefPtr<Buffer> from_pcm_data(ReadonlyBytes, ResampleHelper& resampler, int num_channels, int bits_per_sample);
|
||||||
static NonnullRefPtr<Buffer> create_with_samples(Vector<Sample>&& samples)
|
static NonnullRefPtr<Buffer> create_with_samples(Vector<Sample>&& samples)
|
||||||
{
|
{
|
||||||
return adopt(*new Buffer(move(samples)));
|
return adopt(*new Buffer(move(samples)));
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/BufferStream.h>
|
#include <AK/MemoryStream.h>
|
||||||
#include <AK/NumericLimits.h>
|
#include <AK/NumericLimits.h>
|
||||||
#include <AK/OwnPtr.h>
|
#include <AK/OwnPtr.h>
|
||||||
#include <LibAudio/WavLoader.h>
|
#include <LibAudio/WavLoader.h>
|
||||||
|
@ -219,7 +219,7 @@ bool ResampleHelper::read_sample(double& next_l, double& next_r)
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename SampleReader>
|
template<typename SampleReader>
|
||||||
static void read_samples_from_stream(BufferStream& stream, SampleReader read_sample, Vector<Sample>& samples, ResampleHelper& resampler, int num_channels)
|
static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector<Sample>& samples, ResampleHelper& resampler, int num_channels)
|
||||||
{
|
{
|
||||||
double norm_l = 0;
|
double norm_l = 0;
|
||||||
double norm_r = 0;
|
double norm_r = 0;
|
||||||
|
@ -232,7 +232,7 @@ static void read_samples_from_stream(BufferStream& stream, SampleReader read_sam
|
||||||
}
|
}
|
||||||
norm_l = read_sample(stream);
|
norm_l = read_sample(stream);
|
||||||
|
|
||||||
if (stream.handle_read_failure()) {
|
if (stream.handle_any_error()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
resampler.process_sample(norm_l, norm_r);
|
resampler.process_sample(norm_l, norm_r);
|
||||||
|
@ -246,7 +246,7 @@ static void read_samples_from_stream(BufferStream& stream, SampleReader read_sam
|
||||||
norm_l = read_sample(stream);
|
norm_l = read_sample(stream);
|
||||||
norm_r = read_sample(stream);
|
norm_r = read_sample(stream);
|
||||||
|
|
||||||
if (stream.handle_read_failure()) {
|
if (stream.handle_any_error()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
resampler.process_sample(norm_l, norm_r);
|
resampler.process_sample(norm_l, norm_r);
|
||||||
|
@ -257,7 +257,7 @@ static void read_samples_from_stream(BufferStream& stream, SampleReader read_sam
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static double read_norm_sample_24(BufferStream& stream)
|
static double read_norm_sample_24(InputMemoryStream& stream)
|
||||||
{
|
{
|
||||||
u8 byte = 0;
|
u8 byte = 0;
|
||||||
stream >> byte;
|
stream >> byte;
|
||||||
|
@ -274,26 +274,23 @@ static double read_norm_sample_24(BufferStream& stream)
|
||||||
return double(value) / NumericLimits<i32>::max();
|
return double(value) / NumericLimits<i32>::max();
|
||||||
}
|
}
|
||||||
|
|
||||||
static double read_norm_sample_16(BufferStream& stream)
|
static double read_norm_sample_16(InputMemoryStream& stream)
|
||||||
{
|
{
|
||||||
i16 sample = 0;
|
LittleEndian<i16> sample;
|
||||||
stream >> sample;
|
stream >> sample;
|
||||||
return double(sample) / NumericLimits<i16>::max();
|
return double(sample) / NumericLimits<i16>::max();
|
||||||
}
|
}
|
||||||
|
|
||||||
static double read_norm_sample_8(BufferStream& stream)
|
static double read_norm_sample_8(InputMemoryStream& stream)
|
||||||
{
|
{
|
||||||
u8 sample = 0;
|
u8 sample = 0;
|
||||||
stream >> sample;
|
stream >> sample;
|
||||||
return double(sample) / NumericLimits<u8>::max();
|
return double(sample) / NumericLimits<u8>::max();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ### can't const this because BufferStream is non-const
|
RefPtr<Buffer> Buffer::from_pcm_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, int bits_per_sample)
|
||||||
// perhaps we need a reading class separate from the writing one, that can be
|
|
||||||
// entirely consted.
|
|
||||||
RefPtr<Buffer> Buffer::from_pcm_data(ByteBuffer& data, ResampleHelper& resampler, int num_channels, int bits_per_sample)
|
|
||||||
{
|
{
|
||||||
BufferStream stream(data);
|
InputMemoryStream stream { data };
|
||||||
Vector<Sample> fdata;
|
Vector<Sample> fdata;
|
||||||
fdata.ensure_capacity(data.size() / (bits_per_sample / 8));
|
fdata.ensure_capacity(data.size() / (bits_per_sample / 8));
|
||||||
#ifdef AWAVLOADER_DEBUG
|
#ifdef AWAVLOADER_DEBUG
|
||||||
|
@ -317,7 +314,7 @@ RefPtr<Buffer> Buffer::from_pcm_data(ByteBuffer& data, ResampleHelper& resampler
|
||||||
// We should handle this in a better way above, but for now --
|
// We should handle this in a better way above, but for now --
|
||||||
// just make sure we're good. Worst case we just write some 0s where they
|
// just make sure we're good. Worst case we just write some 0s where they
|
||||||
// don't belong.
|
// don't belong.
|
||||||
ASSERT(!stream.handle_read_failure());
|
ASSERT(!stream.handle_any_error());
|
||||||
|
|
||||||
return Buffer::create_with_samples(move(fdata));
|
return Buffer::create_with_samples(move(fdata));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue