diff --git a/Applications/Piano/Track.cpp b/Applications/Piano/Track.cpp index 754a43407a..8bbdf5474f 100644 --- a/Applications/Piano/Track.cpp +++ b/Applications/Piano/Track.cpp @@ -27,7 +27,7 @@ #include "Track.h" #include -#include +#include #include 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 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; } } diff --git a/Applications/SoundPlayer/PlaybackManager.cpp b/Applications/SoundPlayer/PlaybackManager.cpp index ce616ff71c..1421e75215 100644 --- a/Applications/SoundPlayer/PlaybackManager.cpp +++ b/Applications/SoundPlayer/PlaybackManager.cpp @@ -41,10 +41,10 @@ PlaybackManager::~PlaybackManager() { } -void PlaybackManager::set_loader(OwnPtr&& loader) +void PlaybackManager::set_loader(NonnullRefPtr&& loader) { stop(); - m_loader = move(loader); + m_loader = loader; if (m_loader) { m_total_length = m_loader->total_samples() / static_cast(m_loader->sample_rate()); m_timer->start(); diff --git a/Applications/SoundPlayer/PlaybackManager.h b/Applications/SoundPlayer/PlaybackManager.h index da6adf8cd3..35be78fe9d 100644 --- a/Applications/SoundPlayer/PlaybackManager.h +++ b/Applications/SoundPlayer/PlaybackManager.h @@ -27,8 +27,9 @@ #pragma once #include +#include #include -#include +#include #include #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&&); + void set_loader(NonnullRefPtr&&); 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 m_loader { nullptr }; + RefPtr m_loader { nullptr }; NonnullRefPtr m_connection; RefPtr m_next_buffer; RefPtr m_current_buffer; diff --git a/Applications/SoundPlayer/SoundPlayerWidget.cpp b/Applications/SoundPlayer/SoundPlayerWidget.cpp index 0402e38817..1f44f97d1c 100644 --- a/Applications/SoundPlayer/SoundPlayerWidget.cpp +++ b/Applications/SoundPlayer/SoundPlayerWidget.cpp @@ -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 loader = make(path); + NonnullRefPtr 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; } diff --git a/Userland/aplay.cpp b/Userland/aplay.cpp index 16629fc431..5c5d221bf5 100644 --- a/Userland/aplay.cpp +++ b/Userland/aplay.cpp @@ -26,7 +26,7 @@ #include #include -#include +#include #include #include #include @@ -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 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 {