mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 17:10:23 +00:00
Applications+Userland: Switch to new Audio::Loader API
This commit is contained in:
parent
1f47b01e3b
commit
bad8cd3d8f
Notes:
sideshowbarker
2024-07-19 01:05:35 +09:00
Author: https://github.com/janso3 Commit: https://github.com/SerenityOS/serenity/commit/bad8cd3d8f1 Pull-request: https://github.com/SerenityOS/serenity/pull/4295 Reviewed-by: https://github.com/awesomekling
5 changed files with 31 additions and 35 deletions
|
@ -27,7 +27,7 @@
|
|||
|
||||
#include "Track.h"
|
||||
#include <AK/NumericLimits.h>
|
||||
#include <LibAudio/WavLoader.h>
|
||||
#include <LibAudio/Loader.h>
|
||||
#include <math.h>
|
||||
|
||||
Track::Track(const u32& time)
|
||||
|
@ -138,19 +138,19 @@ void Track::reset()
|
|||
|
||||
String Track::set_recorded_sample(const StringView& path)
|
||||
{
|
||||
Audio::WavLoader wav_loader(path);
|
||||
if (wav_loader.has_error())
|
||||
return String(wav_loader.error_string());
|
||||
auto wav_buffer = wav_loader.get_more_samples(60 * sample_rate * sizeof(Sample)); // 1 minute maximum
|
||||
NonnullRefPtr<Audio::Loader> loader = Audio::Loader::create(path);
|
||||
if (loader->has_error())
|
||||
return String(loader->error_string());
|
||||
auto buffer = loader->get_more_samples(60 * sample_rate * sizeof(Sample)); // 1 minute maximum
|
||||
|
||||
if (!m_recorded_sample.is_empty())
|
||||
m_recorded_sample.clear();
|
||||
m_recorded_sample.resize(wav_buffer->sample_count());
|
||||
m_recorded_sample.resize(buffer->sample_count());
|
||||
|
||||
double peak = 0;
|
||||
for (int i = 0; i < wav_buffer->sample_count(); ++i) {
|
||||
double left_abs = fabs(wav_buffer->samples()[i].left);
|
||||
double right_abs = fabs(wav_buffer->samples()[i].right);
|
||||
for (int i = 0; i < buffer->sample_count(); ++i) {
|
||||
double left_abs = fabs(buffer->samples()[i].left);
|
||||
double right_abs = fabs(buffer->samples()[i].right);
|
||||
if (left_abs > peak)
|
||||
peak = left_abs;
|
||||
if (right_abs > peak)
|
||||
|
@ -158,9 +158,9 @@ String Track::set_recorded_sample(const StringView& path)
|
|||
}
|
||||
|
||||
if (peak) {
|
||||
for (int i = 0; i < wav_buffer->sample_count(); ++i) {
|
||||
m_recorded_sample[i].left = wav_buffer->samples()[i].left / peak;
|
||||
m_recorded_sample[i].right = wav_buffer->samples()[i].right / peak;
|
||||
for (int i = 0; i < buffer->sample_count(); ++i) {
|
||||
m_recorded_sample[i].left = buffer->samples()[i].left / peak;
|
||||
m_recorded_sample[i].right = buffer->samples()[i].right / peak;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -41,10 +41,10 @@ PlaybackManager::~PlaybackManager()
|
|||
{
|
||||
}
|
||||
|
||||
void PlaybackManager::set_loader(OwnPtr<Audio::WavLoader>&& loader)
|
||||
void PlaybackManager::set_loader(NonnullRefPtr<Audio::Loader>&& loader)
|
||||
{
|
||||
stop();
|
||||
m_loader = move(loader);
|
||||
m_loader = loader;
|
||||
if (m_loader) {
|
||||
m_total_length = m_loader->total_samples() / static_cast<float>(m_loader->sample_rate());
|
||||
m_timer->start();
|
||||
|
|
|
@ -27,8 +27,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Vector.h>
|
||||
#include <LibAudio/Buffer.h>
|
||||
#include <LibAudio/ClientConnection.h>
|
||||
#include <LibAudio/WavLoader.h>
|
||||
#include <LibAudio/Loader.h>
|
||||
#include <LibCore/Timer.h>
|
||||
|
||||
#define PLAYBACK_MANAGER_BUFFER_SIZE 64 * KiB
|
||||
|
@ -45,7 +46,7 @@ public:
|
|||
void seek(const int position);
|
||||
void loop(bool);
|
||||
bool toggle_pause();
|
||||
void set_loader(OwnPtr<Audio::WavLoader>&&);
|
||||
void set_loader(NonnullRefPtr<Audio::Loader>&&);
|
||||
|
||||
int last_seek() const { return m_last_seek; }
|
||||
bool is_paused() const { return m_paused; }
|
||||
|
@ -67,7 +68,7 @@ private:
|
|||
size_t m_next_ptr { 0 };
|
||||
size_t m_last_seek { 0 };
|
||||
float m_total_length { 0 };
|
||||
OwnPtr<Audio::WavLoader> m_loader { nullptr };
|
||||
RefPtr<Audio::Loader> m_loader { nullptr };
|
||||
NonnullRefPtr<Audio::ClientConnection> m_connection;
|
||||
RefPtr<Audio::Buffer> m_next_buffer;
|
||||
RefPtr<Audio::Buffer> m_current_buffer;
|
||||
|
|
|
@ -119,15 +119,10 @@ void SoundPlayerWidget::hide_scope(bool hide)
|
|||
|
||||
void SoundPlayerWidget::open_file(String path)
|
||||
{
|
||||
if (!path.ends_with(".wav")) {
|
||||
GUI::MessageBox::show(window(), "Selected file is not a \".wav\" file!", "Filetype error", GUI::MessageBox::Type::Error);
|
||||
return;
|
||||
}
|
||||
|
||||
OwnPtr<Audio::WavLoader> loader = make<Audio::WavLoader>(path);
|
||||
NonnullRefPtr<Audio::Loader> loader = Audio::Loader::create(path);
|
||||
if (loader->has_error()) {
|
||||
GUI::MessageBox::show(window(),
|
||||
String::formatted("Failed to load WAV file: {} ({})", path, loader->error_string()),
|
||||
String::formatted("Failed to load audio file: {} ({})", path, loader->error_string()),
|
||||
"Filetype error", GUI::MessageBox::Type::Error);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
|
||||
#include <LibAudio/Buffer.h>
|
||||
#include <LibAudio/ClientConnection.h>
|
||||
#include <LibAudio/WavLoader.h>
|
||||
#include <LibAudio/Loader.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <stdio.h>
|
||||
|
@ -37,7 +37,7 @@ int main(int argc, char** argv)
|
|||
bool should_loop = false;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(path, "Path to WAV file", "path");
|
||||
args_parser.add_positional_argument(path, "Path to audio file", "path");
|
||||
args_parser.add_option(should_loop, "Loop playback", "loop", 'l');
|
||||
args_parser.parse(argc, argv);
|
||||
|
||||
|
@ -45,27 +45,27 @@ int main(int argc, char** argv)
|
|||
|
||||
auto audio_client = Audio::ClientConnection::construct();
|
||||
audio_client->handshake();
|
||||
Audio::WavLoader loader(path);
|
||||
if (loader.has_error()) {
|
||||
fprintf(stderr, "Failed to load WAV file: %s\n", loader.error_string());
|
||||
NonnullRefPtr<Audio::Loader> loader = Audio::Loader::create(path);
|
||||
if (loader->has_error()) {
|
||||
fprintf(stderr, "Failed to load audio file: %s\n", loader->error_string());
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("\033[34;1m Playing\033[0m: %s\n", path);
|
||||
printf("\033[34;1m Format\033[0m: %u Hz, %u-bit, %s\n",
|
||||
loader.sample_rate(),
|
||||
loader.bits_per_sample(),
|
||||
loader.num_channels() == 1 ? "Mono" : "Stereo");
|
||||
loader->sample_rate(),
|
||||
loader->bits_per_sample(),
|
||||
loader->num_channels() == 1 ? "Mono" : "Stereo");
|
||||
printf("\033[34;1mProgress\033[0m: \033[s");
|
||||
for (;;) {
|
||||
auto samples = loader.get_more_samples();
|
||||
auto samples = loader->get_more_samples();
|
||||
if (samples) {
|
||||
printf("\033[u");
|
||||
printf("%d/%d", loader.loaded_samples(), loader.total_samples());
|
||||
printf("%d/%d", loader->loaded_samples(), loader->total_samples());
|
||||
fflush(stdout);
|
||||
audio_client->enqueue(*samples);
|
||||
} else if (should_loop) {
|
||||
loader.reset();
|
||||
loader->reset();
|
||||
} else if (audio_client->get_remaining_samples()) {
|
||||
sleep(1);
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue