Userspace: Add simple AudioServer and LibAudio
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
set(USERSPACE_LIBRARIES
|
||||
LibAudio
|
||||
LibC
|
||||
LibELF
|
||||
LibFont
|
||||
|
||||
136
userspace/libraries/LibAudio/Audio.cpp
Normal file
136
userspace/libraries/LibAudio/Audio.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
#include <BAN/ScopeGuard.h>
|
||||
|
||||
#include <LibAudio/Audio.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/banan-os.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
namespace LibAudio
|
||||
{
|
||||
|
||||
BAN::ErrorOr<Audio> Audio::load(BAN::StringView path)
|
||||
{
|
||||
Audio result(TRY(AudioLoader::load(path)));
|
||||
TRY(result.initialize(256 * 1024));
|
||||
return result;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<Audio> Audio::random(uint32_t samples)
|
||||
{
|
||||
Audio result;
|
||||
TRY(result.initialize(samples));
|
||||
|
||||
result.m_audio_buffer->sample_rate = 48000;
|
||||
result.m_audio_buffer->channels = 1;
|
||||
|
||||
for (size_t i = 0; i < samples - 1; i++)
|
||||
result.m_audio_buffer->samples[i] = (rand() - RAND_MAX / 2) / (RAND_MAX / 2.0);
|
||||
result.m_audio_buffer->head = samples - 1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Audio::clear()
|
||||
{
|
||||
if (m_audio_buffer)
|
||||
munmap(m_audio_buffer, m_smo_size);
|
||||
m_audio_buffer = nullptr;
|
||||
|
||||
if (m_smo_key != -1)
|
||||
smo_delete(m_smo_key);
|
||||
m_smo_key = -1;
|
||||
|
||||
if (m_server_fd != -1)
|
||||
close(m_server_fd);
|
||||
m_server_fd = -1;
|
||||
|
||||
m_audio_loader.clear();
|
||||
}
|
||||
|
||||
Audio& Audio::operator=(Audio&& other)
|
||||
{
|
||||
clear();
|
||||
|
||||
m_server_fd = other.m_server_fd;
|
||||
m_smo_key = other.m_smo_key;
|
||||
m_smo_size = other.m_smo_size;
|
||||
m_audio_buffer = other.m_audio_buffer;
|
||||
m_audio_loader = BAN::move(other.m_audio_loader);
|
||||
|
||||
other.m_server_fd = -1;
|
||||
other.m_smo_key = -1;
|
||||
other.m_smo_size = 0;
|
||||
other.m_audio_buffer = nullptr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<void> Audio::start()
|
||||
{
|
||||
ASSERT(m_server_fd != -1);
|
||||
|
||||
const ssize_t nsend = send(m_server_fd, &m_smo_key, sizeof(m_smo_key), 0);
|
||||
if (nsend == -1)
|
||||
return BAN::Error::from_errno(errno);
|
||||
ASSERT(nsend == sizeof(m_smo_key));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
BAN::ErrorOr<void> Audio::initialize(uint32_t total_samples)
|
||||
{
|
||||
m_smo_size = sizeof(AudioBuffer) + total_samples * sizeof(AudioBuffer::sample_t);
|
||||
|
||||
m_smo_key = smo_create(m_smo_size, PROT_READ | PROT_WRITE);
|
||||
if (m_smo_key == -1)
|
||||
return BAN::Error::from_errno(errno);
|
||||
|
||||
m_audio_buffer = static_cast<AudioBuffer*>(smo_map(m_smo_key));
|
||||
if (m_audio_buffer == nullptr)
|
||||
return BAN::Error::from_errno(errno);
|
||||
new (m_audio_buffer) AudioBuffer();
|
||||
memset(m_audio_buffer->samples, 0, total_samples * sizeof(AudioBuffer::sample_t));
|
||||
|
||||
m_audio_buffer->capacity = total_samples;
|
||||
if (m_audio_loader)
|
||||
{
|
||||
m_audio_buffer->channels = m_audio_loader->channels();
|
||||
m_audio_buffer->sample_rate = m_audio_loader->sample_rate();
|
||||
}
|
||||
|
||||
update();
|
||||
|
||||
sockaddr_un server_addr;
|
||||
server_addr.sun_family = AF_UNIX;
|
||||
strcpy(server_addr.sun_path, s_audio_server_socket.data());
|
||||
|
||||
m_server_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||||
if (m_server_fd == -1)
|
||||
return BAN::Error::from_errno(errno);
|
||||
|
||||
if (connect(m_server_fd, reinterpret_cast<sockaddr*>(&server_addr), sizeof(server_addr)) == -1)
|
||||
return BAN::Error::from_errno(errno);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void Audio::update()
|
||||
{
|
||||
if (!m_audio_loader)
|
||||
return;
|
||||
|
||||
while (m_audio_loader->samples_remaining())
|
||||
{
|
||||
const uint32_t next_head = (m_audio_buffer->head + 1) % m_audio_buffer->capacity;
|
||||
if (next_head == m_audio_buffer->tail)
|
||||
break;
|
||||
m_audio_buffer->samples[m_audio_buffer->head] = m_audio_loader->get_sample();
|
||||
m_audio_buffer->head = next_head;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
66
userspace/libraries/LibAudio/AudioLoader.cpp
Normal file
66
userspace/libraries/LibAudio/AudioLoader.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <LibAudio/AudioLoader.h>
|
||||
#include <LibAudio/AudioLoaders/WAVLoader.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace LibAudio
|
||||
{
|
||||
|
||||
BAN::ErrorOr<BAN::UniqPtr<AudioLoader>> AudioLoader::load(BAN::StringView path)
|
||||
{
|
||||
if (path.empty())
|
||||
return BAN::Error::from_errno(ENOENT);
|
||||
|
||||
const bool malloced = (path.data()[path.size()] != '\0');
|
||||
|
||||
const char* path_cstr = path.data();
|
||||
if (malloced && (path_cstr = strndup(path.data(), path.size())) == nullptr)
|
||||
return BAN::Error::from_errno(errno);
|
||||
const int file_fd = open(path_cstr, O_RDONLY);
|
||||
if (malloced)
|
||||
free(const_cast<char*>(path_cstr));
|
||||
if (file_fd == -1)
|
||||
return BAN::Error::from_errno(errno);
|
||||
|
||||
struct stat st;
|
||||
if (fstat(file_fd, &st) == -1)
|
||||
{
|
||||
close(file_fd);
|
||||
return BAN::Error::from_errno(errno);
|
||||
}
|
||||
|
||||
uint8_t* mmap_addr = static_cast<uint8_t*>(mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, file_fd, 0));
|
||||
close(file_fd);
|
||||
if (mmap_addr == MAP_FAILED)
|
||||
return BAN::Error::from_errno(errno);
|
||||
|
||||
BAN::ErrorOr<BAN::UniqPtr<AudioLoader>> result_or_error { BAN::Error::from_errno(ENOTSUP) };
|
||||
|
||||
auto file_span = BAN::ConstByteSpan(mmap_addr, st.st_size);
|
||||
if (WAVAudioLoader::can_load_from(file_span))
|
||||
result_or_error = WAVAudioLoader::create(file_span);
|
||||
|
||||
if (result_or_error.is_error())
|
||||
{
|
||||
munmap(mmap_addr, st.st_size);
|
||||
return result_or_error.release_error();
|
||||
}
|
||||
|
||||
auto result = result_or_error.release_value();
|
||||
result->m_mmap_addr = mmap_addr;
|
||||
result->m_mmap_size = st.st_size;
|
||||
return result;
|
||||
}
|
||||
|
||||
AudioLoader::~AudioLoader()
|
||||
{
|
||||
if (m_mmap_addr)
|
||||
munmap(m_mmap_addr, m_mmap_size);
|
||||
m_mmap_addr = nullptr;
|
||||
m_mmap_size = 0;
|
||||
}
|
||||
|
||||
}
|
||||
146
userspace/libraries/LibAudio/AudioLoaders/WAVLoader.cpp
Normal file
146
userspace/libraries/LibAudio/AudioLoaders/WAVLoader.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
#include <LibAudio/AudioLoaders/WAVLoader.h>
|
||||
|
||||
namespace LibAudio
|
||||
{
|
||||
|
||||
struct WAVChunk
|
||||
{
|
||||
char chunk_id[4];
|
||||
uint32_t chunk_size;
|
||||
};
|
||||
|
||||
struct RIFFChunk : WAVChunk
|
||||
{
|
||||
char wave_id[4];
|
||||
};
|
||||
|
||||
struct FormatChunk : WAVChunk
|
||||
{
|
||||
uint16_t wFormatTag;
|
||||
uint16_t nChannels;
|
||||
uint32_t nSamplePerSec;
|
||||
uint32_t nAvgBytePerSec;
|
||||
uint16_t nBlockAlign;
|
||||
uint16_t wBitsPerSample;
|
||||
};
|
||||
|
||||
bool WAVAudioLoader::can_load_from(BAN::ConstByteSpan data)
|
||||
{
|
||||
if (data.size() < sizeof(RIFFChunk))
|
||||
return false;
|
||||
|
||||
const auto riff_chunk = data.as<const RIFFChunk>();
|
||||
if (memcmp(riff_chunk.chunk_id, "RIFF", 4) != 0)
|
||||
return false;
|
||||
if (memcmp(riff_chunk.wave_id, "WAVE", 4) != 0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<BAN::UniqPtr<AudioLoader>> WAVAudioLoader::create(BAN::ConstByteSpan data)
|
||||
{
|
||||
ASSERT(can_load_from(data));
|
||||
|
||||
{
|
||||
const auto riff_chunk = data.as<const RIFFChunk>();
|
||||
if (sizeof(WAVChunk) + riff_chunk.chunk_size > data.size())
|
||||
return BAN::Error::from_errno(ENOBUFS);
|
||||
data = data.slice(0, sizeof(WAVChunk) + riff_chunk.chunk_size);
|
||||
data = data.slice(sizeof(RIFFChunk));
|
||||
}
|
||||
|
||||
BAN::Optional<FormatChunk> format_chunk;
|
||||
BAN::ConstByteSpan sample_data;
|
||||
|
||||
while (!data.empty())
|
||||
{
|
||||
const auto chunk = data.as<const WAVChunk>();
|
||||
if (data.size() < sizeof(WAVChunk) + chunk.chunk_size)
|
||||
return BAN::Error::from_errno(ENOBUFS);
|
||||
|
||||
if (memcmp(chunk.chunk_id, "fmt ", 4) == 0)
|
||||
format_chunk = data.as<const FormatChunk>();
|
||||
else if (memcmp(chunk.chunk_id, "data", 4) == 0)
|
||||
sample_data = data.slice(sizeof(WAVChunk), chunk.chunk_size);
|
||||
|
||||
data = data.slice(sizeof(WAVChunk) + chunk.chunk_size);
|
||||
}
|
||||
|
||||
if (!format_chunk.has_value() || sample_data.empty())
|
||||
return BAN::Error::from_errno(EINVAL);
|
||||
|
||||
const auto format = static_cast<FormatCode>(format_chunk->wFormatTag);
|
||||
const uint16_t bps = format_chunk->wBitsPerSample;
|
||||
const uint16_t channels = format_chunk->nChannels;
|
||||
|
||||
if (channels == 0)
|
||||
return BAN::Error::from_errno(EINVAL);
|
||||
|
||||
switch (format)
|
||||
{
|
||||
case FormatCode::WAVE_FORMAT_PCM:
|
||||
if (bps != 8 && bps != 16 && bps != 32)
|
||||
return BAN::Error::from_errno(ENOTSUP);
|
||||
break;
|
||||
case FormatCode::WAVE_FORMAT_IEEE_FLOAT:
|
||||
if (bps != 32 && bps != 64)
|
||||
return BAN::Error::from_errno(ENOTSUP);
|
||||
break;
|
||||
default:
|
||||
return BAN::Error::from_errno(ENOTSUP);
|
||||
}
|
||||
|
||||
if (bps / 8 * channels != format_chunk->nBlockAlign)
|
||||
return BAN::Error::from_errno(EINVAL);
|
||||
|
||||
auto loader = TRY(BAN::UniqPtr<WAVAudioLoader>::create());
|
||||
loader->m_bits_per_sample = bps;
|
||||
loader->m_sample_format = format;
|
||||
loader->m_channels = channels;
|
||||
loader->m_sample_rate = format_chunk->nSamplePerSec;
|
||||
loader->m_sample_data = sample_data;
|
||||
loader->m_total_samples = sample_data.size() / (bps / 8);
|
||||
loader->m_current_sample = 0;
|
||||
return BAN::UniqPtr<AudioLoader>(BAN::move(loader));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static double read_sample(BAN::ConstByteSpan data)
|
||||
{
|
||||
if constexpr(BAN::is_same_v<float, T> || BAN::is_same_v<double, T>)
|
||||
return data.as<const T>();
|
||||
else if constexpr(BAN::is_signed_v<T>)
|
||||
return data.as<const T>() / static_cast<double>(BAN::numeric_limits<T>::max());
|
||||
else
|
||||
return data.as<const T>() / (BAN::numeric_limits<T>::max() / 2.0) - 1.0;
|
||||
}
|
||||
|
||||
double WAVAudioLoader::get_sample()
|
||||
{
|
||||
ASSERT(samples_remaining() > 0);
|
||||
|
||||
const auto current_sample_data = m_sample_data.slice((m_bits_per_sample / 8) * m_current_sample++);
|
||||
|
||||
switch (m_sample_format)
|
||||
{
|
||||
case FormatCode::WAVE_FORMAT_PCM:
|
||||
switch (m_bits_per_sample)
|
||||
{
|
||||
case 8: return read_sample<uint8_t>(current_sample_data);
|
||||
case 16: return read_sample<int16_t>(current_sample_data);
|
||||
case 32: return read_sample<int32_t>(current_sample_data);
|
||||
}
|
||||
break;
|
||||
case FormatCode::WAVE_FORMAT_IEEE_FLOAT:
|
||||
switch (m_bits_per_sample)
|
||||
{
|
||||
case 32: return read_sample<float>(current_sample_data);
|
||||
case 64: return read_sample<double>(current_sample_data);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
}
|
||||
12
userspace/libraries/LibAudio/CMakeLists.txt
Normal file
12
userspace/libraries/LibAudio/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
set(LIBAUDIO_SOURCES
|
||||
Audio.cpp
|
||||
AudioLoader.cpp
|
||||
AudioLoaders/WAVLoader.cpp
|
||||
)
|
||||
|
||||
add_library(libaudio ${LIBAUDIO_SOURCES})
|
||||
banan_link_library(libaudio ban)
|
||||
banan_link_library(libaudio libc)
|
||||
|
||||
banan_install_headers(libaudio)
|
||||
install(TARGETS libaudio OPTIONAL)
|
||||
64
userspace/libraries/LibAudio/include/LibAudio/Audio.h
Normal file
64
userspace/libraries/LibAudio/include/LibAudio/Audio.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Atomic.h>
|
||||
#include <BAN/StringView.h>
|
||||
#include <BAN/Vector.h>
|
||||
|
||||
#include <LibAudio/AudioLoader.h>
|
||||
|
||||
namespace LibAudio
|
||||
{
|
||||
|
||||
static constexpr BAN::StringView s_audio_server_socket = "/tmp/audio-server.socket"_sv;
|
||||
|
||||
struct AudioBuffer
|
||||
{
|
||||
using sample_t = double;
|
||||
|
||||
uint32_t sample_rate;
|
||||
uint32_t channels;
|
||||
|
||||
uint32_t capacity;
|
||||
BAN::Atomic<uint32_t> tail { 0 };
|
||||
BAN::Atomic<uint32_t> head { 0 };
|
||||
sample_t samples[/* capacity */];
|
||||
};
|
||||
|
||||
class Audio
|
||||
{
|
||||
BAN_NON_COPYABLE(Audio);
|
||||
|
||||
public:
|
||||
static BAN::ErrorOr<Audio> load(BAN::StringView path);
|
||||
static BAN::ErrorOr<Audio> random(uint32_t samples);
|
||||
~Audio() { clear(); }
|
||||
|
||||
Audio(Audio&& other) { *this = BAN::move(other); }
|
||||
Audio& operator=(Audio&& other);
|
||||
|
||||
BAN::ErrorOr<void> start();
|
||||
|
||||
bool is_playing() const { return m_audio_buffer->tail != m_audio_buffer->head; }
|
||||
void update();
|
||||
|
||||
private:
|
||||
Audio() = default;
|
||||
Audio(BAN::UniqPtr<AudioLoader>&& audio_loader)
|
||||
: m_audio_loader(BAN::move(audio_loader))
|
||||
{ }
|
||||
|
||||
void clear();
|
||||
|
||||
BAN::ErrorOr<void> initialize(uint32_t total_samples);
|
||||
|
||||
private:
|
||||
int m_server_fd { -1 };
|
||||
|
||||
BAN::UniqPtr<AudioLoader> m_audio_loader;
|
||||
|
||||
long m_smo_key { -1 };
|
||||
size_t m_smo_size { 0 };
|
||||
AudioBuffer* m_audio_buffer { nullptr };
|
||||
};
|
||||
|
||||
}
|
||||
32
userspace/libraries/LibAudio/include/LibAudio/AudioLoader.h
Normal file
32
userspace/libraries/LibAudio/include/LibAudio/AudioLoader.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/StringView.h>
|
||||
#include <BAN/UniqPtr.h>
|
||||
|
||||
namespace LibAudio
|
||||
{
|
||||
|
||||
class AudioLoader
|
||||
{
|
||||
BAN_NON_COPYABLE(AudioLoader);
|
||||
BAN_NON_MOVABLE(AudioLoader);
|
||||
|
||||
public:
|
||||
static BAN::ErrorOr<BAN::UniqPtr<AudioLoader>> load(BAN::StringView path);
|
||||
virtual ~AudioLoader();
|
||||
|
||||
virtual uint32_t channels() const = 0;
|
||||
virtual uint32_t sample_rate() const = 0;
|
||||
virtual uint32_t samples_remaining() const = 0;
|
||||
|
||||
virtual double get_sample() = 0;
|
||||
|
||||
protected:
|
||||
AudioLoader() = default;
|
||||
|
||||
private:
|
||||
void* m_mmap_addr { nullptr };
|
||||
size_t m_mmap_size { 0 };
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <LibAudio/AudioLoader.h>
|
||||
|
||||
#include <BAN/ByteSpan.h>
|
||||
|
||||
namespace LibAudio
|
||||
{
|
||||
|
||||
class WAVAudioLoader : public AudioLoader
|
||||
{
|
||||
public:
|
||||
enum FormatCode : uint16_t
|
||||
{
|
||||
WAVE_FORMAT_PCM = 0x01,
|
||||
WAVE_FORMAT_IEEE_FLOAT = 0x03,
|
||||
};
|
||||
|
||||
public:
|
||||
static bool can_load_from(BAN::ConstByteSpan data);
|
||||
static BAN::ErrorOr<BAN::UniqPtr<AudioLoader>> create(BAN::ConstByteSpan data);
|
||||
|
||||
uint32_t channels() const override { return m_channels; }
|
||||
uint32_t sample_rate() const override { return m_sample_rate; }
|
||||
uint32_t samples_remaining() const override { return m_total_samples - m_current_sample; }
|
||||
|
||||
double get_sample() override;
|
||||
|
||||
private:
|
||||
uint32_t m_channels { 0 };
|
||||
uint32_t m_sample_rate { 0 };
|
||||
uint32_t m_total_samples { 0 };
|
||||
|
||||
FormatCode m_sample_format;
|
||||
uint16_t m_bits_per_sample { 0 };
|
||||
size_t m_current_sample { 0 };
|
||||
BAN::ConstByteSpan m_sample_data;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user