forked from Bananymous/banan-os
LibAudio: Rename AudioLoader -> AudioStream
This commit is contained in:
@@ -25,7 +25,7 @@ namespace LibAudio
|
|||||||
|
|
||||||
BAN::ErrorOr<Audio> Audio::load(BAN::StringView path)
|
BAN::ErrorOr<Audio> Audio::load(BAN::StringView path)
|
||||||
{
|
{
|
||||||
Audio result(TRY(AudioLoader::load(path)));
|
Audio result(TRY(AudioStream::load(path)));
|
||||||
TRY(result.initialize(256 * 1024));
|
TRY(result.initialize(256 * 1024));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -57,7 +57,7 @@ namespace LibAudio
|
|||||||
close(m_server_fd);
|
close(m_server_fd);
|
||||||
m_server_fd = -1;
|
m_server_fd = -1;
|
||||||
|
|
||||||
m_audio_loader.clear();
|
m_audio_stream.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
Audio& Audio::operator=(Audio&& other)
|
Audio& Audio::operator=(Audio&& other)
|
||||||
@@ -67,7 +67,7 @@ namespace LibAudio
|
|||||||
m_server_fd = other.m_server_fd;
|
m_server_fd = other.m_server_fd;
|
||||||
m_shmid = other.m_shmid;
|
m_shmid = other.m_shmid;
|
||||||
m_audio_buffer = other.m_audio_buffer;
|
m_audio_buffer = other.m_audio_buffer;
|
||||||
m_audio_loader = BAN::move(other.m_audio_loader);
|
m_audio_stream = BAN::move(other.m_audio_stream);
|
||||||
|
|
||||||
other.m_server_fd = -1;
|
other.m_server_fd = -1;
|
||||||
other.m_shmid = -1;
|
other.m_shmid = -1;
|
||||||
@@ -93,10 +93,10 @@ namespace LibAudio
|
|||||||
memset(m_audio_buffer->samples, 0, total_samples * sizeof(AudioBuffer::sample_t));
|
memset(m_audio_buffer->samples, 0, total_samples * sizeof(AudioBuffer::sample_t));
|
||||||
|
|
||||||
m_audio_buffer->capacity = total_samples;
|
m_audio_buffer->capacity = total_samples;
|
||||||
if (m_audio_loader)
|
if (m_audio_stream)
|
||||||
{
|
{
|
||||||
m_audio_buffer->channels = m_audio_loader->channels();
|
m_audio_buffer->channels = m_audio_stream->channels();
|
||||||
m_audio_buffer->sample_rate = m_audio_loader->sample_rate();
|
m_audio_buffer->sample_rate = m_audio_stream->sample_rate();
|
||||||
}
|
}
|
||||||
|
|
||||||
update();
|
update();
|
||||||
@@ -170,15 +170,15 @@ namespace LibAudio
|
|||||||
|
|
||||||
void Audio::update()
|
void Audio::update()
|
||||||
{
|
{
|
||||||
if (!m_audio_loader)
|
if (!m_audio_stream)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!m_audio_loader->samples_remaining() && !is_playing())
|
if (!m_audio_stream->samples_remaining() && !is_playing())
|
||||||
return set_paused(true);
|
return set_paused(true);
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
const uint32_t sample_count = BAN::Math::min<uint32_t>(queueable_samples(), m_audio_loader->samples_remaining());
|
const uint32_t sample_count = BAN::Math::min<uint32_t>(queueable_samples(), m_audio_stream->samples_remaining());
|
||||||
if (sample_count == 0)
|
if (sample_count == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -186,7 +186,7 @@ namespace LibAudio
|
|||||||
const uint32_t capacity = m_audio_buffer->capacity;
|
const uint32_t capacity = m_audio_buffer->capacity;
|
||||||
|
|
||||||
for (size_t i = 0; i < sample_count; i++)
|
for (size_t i = 0; i < sample_count; i++)
|
||||||
m_audio_buffer->samples[(head + i) % capacity] = m_audio_loader->get_sample();
|
m_audio_buffer->samples[(head + i) % capacity] = m_audio_stream->get_sample();
|
||||||
m_audio_buffer->head = (head + sample_count) % capacity;
|
m_audio_buffer->head = (head + sample_count) % capacity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-7
@@ -1,5 +1,5 @@
|
|||||||
#include <LibAudio/AudioLoader.h>
|
#include <LibAudio/AudioStream.h>
|
||||||
#include <LibAudio/AudioLoaders/WAVLoader.h>
|
#include <LibAudio/AudioStreams/WAVAudioStream.h>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
namespace LibAudio
|
namespace LibAudio
|
||||||
{
|
{
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::UniqPtr<AudioLoader>> AudioLoader::load(BAN::StringView path)
|
BAN::ErrorOr<BAN::UniqPtr<AudioStream>> AudioStream::load(BAN::StringView path)
|
||||||
{
|
{
|
||||||
if (path.empty())
|
if (path.empty())
|
||||||
return BAN::Error::from_errno(ENOENT);
|
return BAN::Error::from_errno(ENOENT);
|
||||||
@@ -37,11 +37,11 @@ namespace LibAudio
|
|||||||
if (mmap_addr == MAP_FAILED)
|
if (mmap_addr == MAP_FAILED)
|
||||||
return BAN::Error::from_errno(errno);
|
return BAN::Error::from_errno(errno);
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::UniqPtr<AudioLoader>> result_or_error { BAN::Error::from_errno(ENOTSUP) };
|
BAN::ErrorOr<BAN::UniqPtr<AudioStream>> result_or_error { BAN::Error::from_errno(ENOTSUP) };
|
||||||
|
|
||||||
auto file_span = BAN::ConstByteSpan(mmap_addr, st.st_size);
|
auto file_span = BAN::ConstByteSpan(mmap_addr, st.st_size);
|
||||||
if (WAVAudioLoader::can_load_from(file_span))
|
if (WAVAudioStream::can_load_from(file_span))
|
||||||
result_or_error = WAVAudioLoader::create(file_span);
|
result_or_error = WAVAudioStream::create(file_span);
|
||||||
|
|
||||||
if (result_or_error.is_error())
|
if (result_or_error.is_error())
|
||||||
{
|
{
|
||||||
@@ -55,7 +55,7 @@ namespace LibAudio
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
AudioLoader::~AudioLoader()
|
AudioStream::~AudioStream()
|
||||||
{
|
{
|
||||||
if (m_mmap_addr)
|
if (m_mmap_addr)
|
||||||
munmap(m_mmap_addr, m_mmap_size);
|
munmap(m_mmap_addr, m_mmap_size);
|
||||||
+13
-13
@@ -1,4 +1,4 @@
|
|||||||
#include <LibAudio/AudioLoaders/WAVLoader.h>
|
#include <LibAudio/AudioStreams/WAVAudioStream.h>
|
||||||
|
|
||||||
namespace LibAudio
|
namespace LibAudio
|
||||||
{
|
{
|
||||||
@@ -24,7 +24,7 @@ namespace LibAudio
|
|||||||
uint16_t wBitsPerSample;
|
uint16_t wBitsPerSample;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool WAVAudioLoader::can_load_from(BAN::ConstByteSpan data)
|
bool WAVAudioStream::can_load_from(BAN::ConstByteSpan data)
|
||||||
{
|
{
|
||||||
if (data.size() < sizeof(RIFFChunk))
|
if (data.size() < sizeof(RIFFChunk))
|
||||||
return false;
|
return false;
|
||||||
@@ -37,7 +37,7 @@ namespace LibAudio
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
BAN::ErrorOr<BAN::UniqPtr<AudioLoader>> WAVAudioLoader::create(BAN::ConstByteSpan data)
|
BAN::ErrorOr<BAN::UniqPtr<AudioStream>> WAVAudioStream::create(BAN::ConstByteSpan data)
|
||||||
{
|
{
|
||||||
ASSERT(can_load_from(data));
|
ASSERT(can_load_from(data));
|
||||||
|
|
||||||
@@ -93,15 +93,15 @@ namespace LibAudio
|
|||||||
if (bps / 8 * channels != format_chunk->nBlockAlign)
|
if (bps / 8 * channels != format_chunk->nBlockAlign)
|
||||||
return BAN::Error::from_errno(EINVAL);
|
return BAN::Error::from_errno(EINVAL);
|
||||||
|
|
||||||
auto loader = TRY(BAN::UniqPtr<WAVAudioLoader>::create());
|
auto stream = TRY(BAN::UniqPtr<WAVAudioStream>::create());
|
||||||
loader->m_bits_per_sample = bps;
|
stream->m_bits_per_sample = bps;
|
||||||
loader->m_sample_format = format;
|
stream->m_sample_format = format;
|
||||||
loader->m_channels = channels;
|
stream->m_channels = channels;
|
||||||
loader->m_sample_rate = format_chunk->nSamplePerSec;
|
stream->m_sample_rate = format_chunk->nSamplePerSec;
|
||||||
loader->m_sample_data = sample_data;
|
stream->m_sample_data = sample_data;
|
||||||
loader->m_total_samples = sample_data.size() / (bps / 8);
|
stream->m_total_samples = sample_data.size() / (bps / 8);
|
||||||
loader->m_current_sample = 0;
|
stream->m_current_sample = 0;
|
||||||
return BAN::UniqPtr<AudioLoader>(BAN::move(loader));
|
return BAN::UniqPtr<AudioStream>(BAN::move(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -115,7 +115,7 @@ namespace LibAudio
|
|||||||
return data.as<const T>() / (BAN::numeric_limits<T>::max() / 2.0f) - 1.0f;
|
return data.as<const T>() / (BAN::numeric_limits<T>::max() / 2.0f) - 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float WAVAudioLoader::get_sample()
|
float WAVAudioStream::get_sample()
|
||||||
{
|
{
|
||||||
ASSERT(samples_remaining() > 0);
|
ASSERT(samples_remaining() > 0);
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
set(LIBAUDIO_SOURCES
|
set(LIBAUDIO_SOURCES
|
||||||
Audio.cpp
|
Audio.cpp
|
||||||
AudioLoader.cpp
|
AudioStream.cpp
|
||||||
AudioLoaders/WAVLoader.cpp
|
AudioStreams/WAVAudioStream.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_library(libaudio ${LIBAUDIO_SOURCES})
|
add_library(libaudio ${LIBAUDIO_SOURCES})
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <BAN/StringView.h>
|
#include <BAN/StringView.h>
|
||||||
#include <BAN/Vector.h>
|
#include <BAN/Vector.h>
|
||||||
|
|
||||||
#include <LibAudio/AudioLoader.h>
|
#include <LibAudio/AudioStream.h>
|
||||||
|
|
||||||
namespace LibAudio
|
namespace LibAudio
|
||||||
{
|
{
|
||||||
@@ -49,8 +49,8 @@ namespace LibAudio
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Audio() = default;
|
Audio() = default;
|
||||||
Audio(BAN::UniqPtr<AudioLoader>&& audio_loader)
|
Audio(BAN::UniqPtr<AudioStream>&& audio_stream)
|
||||||
: m_audio_loader(BAN::move(audio_loader))
|
: m_audio_stream(BAN::move(audio_stream))
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
@@ -60,7 +60,7 @@ namespace LibAudio
|
|||||||
private:
|
private:
|
||||||
int m_server_fd { -1 };
|
int m_server_fd { -1 };
|
||||||
|
|
||||||
BAN::UniqPtr<AudioLoader> m_audio_loader;
|
BAN::UniqPtr<AudioStream> m_audio_stream;
|
||||||
|
|
||||||
int m_shmid { -1 };
|
int m_shmid { -1 };
|
||||||
AudioBuffer* m_audio_buffer { nullptr };
|
AudioBuffer* m_audio_buffer { nullptr };
|
||||||
|
|||||||
+6
-6
@@ -6,14 +6,14 @@
|
|||||||
namespace LibAudio
|
namespace LibAudio
|
||||||
{
|
{
|
||||||
|
|
||||||
class AudioLoader
|
class AudioStream
|
||||||
{
|
{
|
||||||
BAN_NON_COPYABLE(AudioLoader);
|
BAN_NON_COPYABLE(AudioStream);
|
||||||
BAN_NON_MOVABLE(AudioLoader);
|
BAN_NON_MOVABLE(AudioStream);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static BAN::ErrorOr<BAN::UniqPtr<AudioLoader>> load(BAN::StringView path);
|
static BAN::ErrorOr<BAN::UniqPtr<AudioStream>> load(BAN::StringView path);
|
||||||
virtual ~AudioLoader();
|
virtual ~AudioStream();
|
||||||
|
|
||||||
virtual uint32_t channels() const = 0;
|
virtual uint32_t channels() const = 0;
|
||||||
virtual uint32_t sample_rate() const = 0;
|
virtual uint32_t sample_rate() const = 0;
|
||||||
@@ -22,7 +22,7 @@ namespace LibAudio
|
|||||||
virtual float get_sample() = 0;
|
virtual float get_sample() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
AudioLoader() = default;
|
AudioStream() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void* m_mmap_addr { nullptr };
|
void* m_mmap_addr { nullptr };
|
||||||
+3
-3
@@ -1,13 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <LibAudio/AudioLoader.h>
|
#include <LibAudio/AudioStream.h>
|
||||||
|
|
||||||
#include <BAN/ByteSpan.h>
|
#include <BAN/ByteSpan.h>
|
||||||
|
|
||||||
namespace LibAudio
|
namespace LibAudio
|
||||||
{
|
{
|
||||||
|
|
||||||
class WAVAudioLoader : public AudioLoader
|
class WAVAudioStream : public AudioStream
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum FormatCode : uint16_t
|
enum FormatCode : uint16_t
|
||||||
@@ -18,7 +18,7 @@ namespace LibAudio
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
static bool can_load_from(BAN::ConstByteSpan data);
|
static bool can_load_from(BAN::ConstByteSpan data);
|
||||||
static BAN::ErrorOr<BAN::UniqPtr<AudioLoader>> create(BAN::ConstByteSpan data);
|
static BAN::ErrorOr<BAN::UniqPtr<AudioStream>> create(BAN::ConstByteSpan data);
|
||||||
|
|
||||||
uint32_t channels() const override { return m_channels; }
|
uint32_t channels() const override { return m_channels; }
|
||||||
uint32_t sample_rate() const override { return m_sample_rate; }
|
uint32_t sample_rate() const override { return m_sample_rate; }
|
||||||
Reference in New Issue
Block a user