summaryrefslogtreecommitdiff
path: root/Applications/SoundPlayer/PlaybackManager.h
blob: c58334b6a2315bc3c70f093ce1c7badb169c7b71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once

#include <AK/Vector.h>
#include <LibAudio/AClientConnection.h>
#include <LibAudio/AWavLoader.h>
#include <LibCore/CTimer.h>

#define PLAYBACK_MANAGER_BUFFER_SIZE 64 * KB
#define PLAYBACK_MANAGER_RATE 44100

class PlaybackManager final {
public:
    PlaybackManager(NonnullRefPtr<AClientConnection>, AWavLoader&);
    ~PlaybackManager();

    void play();
    void stop();
    void pause();
    void seek(const int position);
    bool toggle_pause();

    int last_seek() const { return m_last_seek; }
    bool is_paused() const { return m_paused; }
    float total_length() const { return m_total_length; }
    RefPtr<ABuffer> current_buffer() const { return m_current_buffer; }

    NonnullRefPtr<AClientConnection> connection() const { return m_connection; }
    AWavLoader& loader() const { return m_loader; }

    Function<void()> on_update;

private:
    void next_buffer();
    void set_paused(bool);
    void load_next_buffer();
    void remove_dead_buffers();

    bool m_paused { true };
    int m_next_ptr { 0 };
    int m_last_seek { 0 };
    float m_total_length;
    AWavLoader& m_loader;
    NonnullRefPtr<AClientConnection> m_connection;
    RefPtr<ABuffer> m_next_buffer;
    RefPtr<ABuffer> m_current_buffer;
    Vector<RefPtr<ABuffer>> m_buffers;
    RefPtr<CTimer> m_timer;
};