mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-23 08:00:20 +00:00
bc319d9e88
Okay, I've spent a whole day on this now, and it finally kinda works! With this patch, CObject and all of its derived classes are reference counted instead of tree-owned. The previous, Qt-like model was nice and familiar, but ultimately also outdated and difficult to reason about. CObject-derived types should now be stored in RefPtr/NonnullRefPtr and each class can be constructed using the forwarding construct() helper: auto widget = GWidget::construct(parent_widget); Note that construct() simply forwards all arguments to an existing constructor. It is inserted into each class by the C_OBJECT macro, see CObject.h to understand how that works. CObject::delete_later() disappears in this patch, as there is no longer a single logical owner of a CObject.
72 lines
2 KiB
C++
72 lines
2 KiB
C++
#include "SampleWidget.h"
|
|
#include <LibAudio/ABuffer.h>
|
|
#include <LibAudio/AClientConnection.h>
|
|
#include <LibAudio/AWavLoader.h>
|
|
#include <LibCore/CTimer.h>
|
|
#include <LibGUI/GApplication.h>
|
|
#include <LibGUI/GBoxLayout.h>
|
|
#include <LibGUI/GButton.h>
|
|
#include <LibGUI/GWidget.h>
|
|
#include <LibGUI/GWindow.h>
|
|
#include <stdio.h>
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
if (argc != 2) {
|
|
printf("usage: %s <wav-file>\n", argv[0]);
|
|
return 0;
|
|
}
|
|
|
|
GApplication app(argc, argv);
|
|
|
|
String path = argv[1];
|
|
AWavLoader loader(path);
|
|
|
|
if (loader.has_error()) {
|
|
fprintf(stderr, "Failed to load WAV file: %s (%s)\n", path.characters(), loader.error_string());
|
|
return 1;
|
|
}
|
|
|
|
auto audio_client = AClientConnection::construct();
|
|
audio_client->handshake();
|
|
|
|
auto window = GWindow::construct();
|
|
window->set_title("SoundPlayer");
|
|
window->set_rect(300, 300, 300, 200);
|
|
|
|
auto widget = GWidget::construct();
|
|
window->set_main_widget(widget);
|
|
|
|
widget->set_fill_with_background_color(true);
|
|
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
|
|
widget->layout()->set_margins({ 2, 2, 2, 2 });
|
|
|
|
auto sample_widget = SampleWidget::construct(widget);
|
|
|
|
auto button = GButton::construct("Quit", widget);
|
|
button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
|
button->set_preferred_size(0, 20);
|
|
button->on_click = [&](auto&) {
|
|
app.quit();
|
|
};
|
|
|
|
auto next_sample_buffer = loader.get_more_samples();
|
|
|
|
auto timer = CTimer::construct(100, [&] {
|
|
if (!next_sample_buffer) {
|
|
sample_widget->set_buffer(nullptr);
|
|
return;
|
|
}
|
|
bool enqueued = audio_client->try_enqueue(*next_sample_buffer);
|
|
if (!enqueued)
|
|
return;
|
|
sample_widget->set_buffer(next_sample_buffer);
|
|
next_sample_buffer = loader.get_more_samples(16 * KB);
|
|
if (!next_sample_buffer) {
|
|
dbg() << "Exhausted samples :^)";
|
|
}
|
|
});
|
|
|
|
window->show();
|
|
return app.exec();
|
|
}
|