main.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "SampleWidget.h"
  2. #include <LibAudio/ABuffer.h>
  3. #include <LibAudio/AClientConnection.h>
  4. #include <LibAudio/AWavLoader.h>
  5. #include <LibCore/CTimer.h>
  6. #include <LibGUI/GApplication.h>
  7. #include <LibGUI/GBoxLayout.h>
  8. #include <LibGUI/GButton.h>
  9. #include <LibGUI/GWidget.h>
  10. #include <LibGUI/GWindow.h>
  11. #include <stdio.h>
  12. int main(int argc, char** argv)
  13. {
  14. if (argc != 2) {
  15. printf("usage: %s <wav-file>\n", argv[0]);
  16. return 0;
  17. }
  18. GApplication app(argc, argv);
  19. String path = argv[1];
  20. AWavLoader loader(path);
  21. if (loader.has_error()) {
  22. fprintf(stderr, "Failed to load WAV file: %s (%s)\n", path.characters(), loader.error_string());
  23. return 1;
  24. }
  25. AClientConnection audio_client;
  26. audio_client.handshake();
  27. auto* window = new GWindow;
  28. window->set_title("SoundPlayer");
  29. window->set_rect(300, 300, 300, 200);
  30. auto widget = GWidget::construct();
  31. window->set_main_widget(widget);
  32. widget->set_fill_with_background_color(true);
  33. widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
  34. widget->layout()->set_margins({ 2, 2, 2, 2 });
  35. auto* sample_widget = new SampleWidget(widget);
  36. auto* button = new GButton("Quit", widget);
  37. button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  38. button->set_preferred_size(0, 20);
  39. button->on_click = [&](auto&) {
  40. app.quit();
  41. };
  42. auto next_sample_buffer = loader.get_more_samples();
  43. auto timer = CTimer::construct(100, [&] {
  44. if (!next_sample_buffer) {
  45. sample_widget->set_buffer(nullptr);
  46. return;
  47. }
  48. bool enqueued = audio_client.try_enqueue(*next_sample_buffer);
  49. if (!enqueued)
  50. return;
  51. sample_widget->set_buffer(next_sample_buffer);
  52. next_sample_buffer = loader.get_more_samples(16 * KB);
  53. if (!next_sample_buffer) {
  54. dbg() << "Exhausted samples :^)";
  55. }
  56. });
  57. window->show();
  58. return app.exec();
  59. }