main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2019-2020, William McPherson <willmcpherson2@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "AudioEngine.h"
  28. #include "MainWidget.h"
  29. #include <LibAudio/ClientConnection.h>
  30. #include <LibAudio/WavWriter.h>
  31. #include <LibCore/EventLoop.h>
  32. #include <LibCore/File.h>
  33. #include <LibGUI/AboutDialog.h>
  34. #include <LibGUI/Action.h>
  35. #include <LibGUI/Application.h>
  36. #include <LibGUI/FilePicker.h>
  37. #include <LibGUI/Menu.h>
  38. #include <LibGUI/MenuBar.h>
  39. #include <LibGUI/MessageBox.h>
  40. #include <LibGUI/Window.h>
  41. #include <LibGfx/Bitmap.h>
  42. #include <LibThread/Thread.h>
  43. int main(int argc, char** argv)
  44. {
  45. GUI::Application app(argc, argv);
  46. auto audio_client = Audio::ClientConnection::construct();
  47. audio_client->handshake();
  48. AudioEngine audio_engine;
  49. auto window = GUI::Window::construct();
  50. auto& main_widget = window->set_main_widget<MainWidget>(audio_engine);
  51. window->set_title("Piano");
  52. window->set_rect(90, 90, 840, 600);
  53. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-piano.png"));
  54. window->show();
  55. Audio::WavWriter wav_writer;
  56. Optional<String> save_path;
  57. bool need_to_write_wav = false;
  58. LibThread::Thread audio_thread([&] {
  59. auto audio = Core::File::construct("/dev/audio");
  60. if (!audio->open(Core::IODevice::WriteOnly)) {
  61. dbgprintf("Can't open audio device: %s", audio->error_string());
  62. return 1;
  63. }
  64. FixedArray<Sample> buffer(sample_count);
  65. for (;;) {
  66. audio_engine.fill_buffer(buffer);
  67. audio->write(reinterpret_cast<u8*>(buffer.data()), buffer_size);
  68. Core::EventLoop::current().post_event(main_widget, make<Core::CustomEvent>(0));
  69. Core::EventLoop::wake();
  70. if (need_to_write_wav) {
  71. need_to_write_wav = false;
  72. audio_engine.reset();
  73. audio_engine.set_should_loop(false);
  74. do {
  75. audio_engine.fill_buffer(buffer);
  76. wav_writer.write_samples(reinterpret_cast<u8*>(buffer.data()), buffer_size);
  77. } while (audio_engine.time());
  78. audio_engine.reset();
  79. audio_engine.set_should_loop(true);
  80. wav_writer.finalize();
  81. }
  82. }
  83. });
  84. audio_thread.start();
  85. auto menubar = make<GUI::MenuBar>();
  86. auto app_menu = GUI::Menu::construct("Piano");
  87. app_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) {
  88. GUI::Application::the().quit(0);
  89. return;
  90. }));
  91. app_menu->add_action(GUI::Action::create("Export", { Mod_Ctrl, Key_E }, [&](const GUI::Action&) {
  92. save_path = GUI::FilePicker::get_save_filepath("Untitled", "wav");
  93. if (!save_path.has_value())
  94. return;
  95. wav_writer.set_file(save_path.value());
  96. if (wav_writer.has_error()) {
  97. GUI::MessageBox::show(String::format("Failed to export WAV file: %s", wav_writer.error_string()), "Error", GUI::MessageBox::Type::Error);
  98. wav_writer.clear_error();
  99. return;
  100. }
  101. need_to_write_wav = true;
  102. }));
  103. menubar->add_menu(move(app_menu));
  104. auto help_menu = GUI::Menu::construct("Help");
  105. help_menu->add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  106. GUI::AboutDialog::show("Piano", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-piano.png"), window);
  107. }));
  108. menubar->add_menu(move(help_menu));
  109. app.set_menubar(move(menubar));
  110. return app.exec();
  111. }