summaryrefslogtreecommitdiff
path: root/Userland/Applications/Piano/main.cpp
diff options
context:
space:
mode:
authorJJ Roberts-White <computerfido@gmail.com>2021-07-11 12:58:17 +1000
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-07-14 12:07:43 +0430
commit74f1f2b5e2ed9b06f7dc5c4a247c8512c8b3ec32 (patch)
treea0db796ae899adbd02630ad293a0ed76722a2f6c /Userland/Applications/Piano/main.cpp
parent54c005754aedbedec44c1c98f135bfbb0de78068 (diff)
downloadserenity-74f1f2b5e2ed9b06f7dc5c4a247c8512c8b3ec32.zip
Piano: Add Play/Pause, Forward and Back buttons
Piano now has a toolbar allowing the playback to be paused, or to be stepped forward or back a note.
Diffstat (limited to 'Userland/Applications/Piano/main.cpp')
-rw-r--r--Userland/Applications/Piano/main.cpp77
1 files changed, 10 insertions, 67 deletions
diff --git a/Userland/Applications/Piano/main.cpp b/Userland/Applications/Piano/main.cpp
index b9e0608f68..671b7a65fb 100644
--- a/Userland/Applications/Piano/main.cpp
+++ b/Userland/Applications/Piano/main.cpp
@@ -2,10 +2,12 @@
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2019-2020, William McPherson <willmcpherson2@gmail.com>
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
+ * Copyright (c) 2021, JJ Roberts-White <computerfido@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include "AudioPlayerLoop.h"
#include "MainWidget.h"
#include "TrackManager.h"
#include <AK/Array.h>
@@ -26,65 +28,6 @@
#include <LibGfx/Bitmap.h>
#include <LibThreading/Thread.h>
-// Converts Piano-internal data to an Audio::Buffer that AudioServer receives
-static NonnullRefPtr<Audio::Buffer> music_samples_to_buffer(Array<Sample, sample_count> samples)
-{
- Vector<Audio::Frame, sample_count> frames;
- frames.ensure_capacity(sample_count);
- for (auto sample : samples) {
- Audio::Frame frame = { sample.left / (double)NumericLimits<i16>::max(), sample.right / (double)NumericLimits<i16>::max() };
- frames.unchecked_append(frame);
- }
- return Audio::Buffer::create_with_samples(frames);
-}
-
-// Wrapper class accepting custom events to advance the track playing and forward audio data to the system.
-// This does not run on a separate thread, preventing IPC multithreading madness.
-class AudioPlayerLoop : public Core::Object {
- C_OBJECT(AudioPlayerLoop)
-public:
- AudioPlayerLoop(TrackManager& track_manager, bool& need_to_write_wav, Audio::WavWriter& wav_writer)
- : m_track_manager(track_manager)
- , m_need_to_write_wav(need_to_write_wav)
- , m_wav_writer(wav_writer)
- {
- m_audio_client = Audio::ClientConnection::construct();
- m_audio_client->on_finish_playing_buffer = [this](int buffer_id) {
- (void)buffer_id;
- enqueue_audio();
- };
- }
-
- void enqueue_audio()
- {
- m_track_manager.fill_buffer(m_buffer);
- NonnullRefPtr<Audio::Buffer> audio_buffer = music_samples_to_buffer(m_buffer);
- m_audio_client->async_enqueue(audio_buffer);
-
- // FIXME: This should be done somewhere else.
- if (m_need_to_write_wav) {
- m_need_to_write_wav = false;
- m_track_manager.reset();
- m_track_manager.set_should_loop(false);
- do {
- m_track_manager.fill_buffer(m_buffer);
- m_wav_writer.write_samples(reinterpret_cast<u8*>(m_buffer.data()), buffer_size);
- } while (m_track_manager.time());
- m_track_manager.reset();
- m_track_manager.set_should_loop(true);
- m_wav_writer.finalize();
- }
- }
-
-private:
- TrackManager& m_track_manager;
- Array<Sample, sample_count> m_buffer;
- RefPtr<Audio::ClientConnection> m_audio_client;
-
- bool& m_need_to_write_wav;
- Audio::WavWriter& m_wav_writer;
-};
-
int main(int argc, char** argv)
{
if (pledge("stdio thread rpath cpath wpath recvfd sendfd unix", nullptr) < 0) {
@@ -96,14 +39,6 @@ int main(int argc, char** argv)
TrackManager track_manager;
- auto app_icon = GUI::Icon::default_icon("app-piano");
- auto window = GUI::Window::construct();
- auto& main_widget = window->set_main_widget<MainWidget>(track_manager);
- window->set_title("Piano");
- window->resize(840, 600);
- window->set_icon(app_icon.bitmap_for_size(16));
- window->show();
-
Audio::WavWriter wav_writer;
Optional<String> save_path;
bool need_to_write_wav = false;
@@ -112,6 +47,14 @@ int main(int argc, char** argv)
audio_loop->enqueue_audio();
audio_loop->enqueue_audio();
+ auto app_icon = GUI::Icon::default_icon("app-piano");
+ auto window = GUI::Window::construct();
+ auto& main_widget = window->set_main_widget<MainWidget>(track_manager, audio_loop);
+ window->set_title("Piano");
+ window->resize(840, 600);
+ window->set_icon(app_icon.bitmap_for_size(16));
+ window->show();
+
auto main_widget_updater = Core::Timer::construct(static_cast<int>((1 / 60.0) * 1000), [&] {
Core::EventLoop::current().post_event(main_widget, make<Core::CustomEvent>(0));
});