mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 09:58:14 +00:00
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.
This commit is contained in:
parent
54c005754a
commit
74f1f2b5e2
10 changed files with 234 additions and 69 deletions
|
@ -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));
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue