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