summaryrefslogtreecommitdiff
path: root/src/audio_core/stream.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio_core/stream.h')
-rw-r--r--src/audio_core/stream.h89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/audio_core/stream.h b/src/audio_core/stream.h
new file mode 100644
index 000000000..5f43b0798
--- /dev/null
+++ b/src/audio_core/stream.h
@@ -0,0 +1,89 @@
1// Copyright 2018 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <functional>
8#include <memory>
9#include <vector>
10#include <queue>
11
12#include "audio_core/buffer.h"
13#include "common/assert.h"
14#include "common/common_types.h"
15#include "core/core_timing.h"
16
17namespace AudioCore {
18
19using BufferPtr = std::shared_ptr<Buffer>;
20
21/**
22 * Represents an audio stream, which is a sequence of queued buffers, to be outputed by AudioOut
23 */
24class Stream {
25public:
26 /// Audio format of the stream
27 enum class Format {
28 Mono16,
29 Stereo16,
30 Multi51Channel16,
31 };
32
33 /// Callback function type, used to change guest state on a buffer being released
34 using ReleaseCallback = std::function<void()>;
35
36 Stream(int sample_rate, Format format, ReleaseCallback&& release_callback);
37
38 /// Plays the audio stream
39 void Play();
40
41 /// Stops the audio stream
42 void Stop();
43
44 /// Queues a buffer into the audio stream, returns true on success
45 bool QueueBuffer(BufferPtr&& buffer);
46
47 /// Returns true if the audio stream contains a buffer with the specified tag
48 bool ContainsBuffer(Buffer::Tag tag) const;
49
50 /// Returns a vector of recently released buffers specified by tag
51 std::vector<Buffer::Tag> GetTagsAndReleaseBuffers(size_t max_count);
52
53 /// Returns true if the stream is currently playing
54 bool IsPlaying() const {
55 return state == State::Playing;
56 }
57
58 /// Returns the number of queued buffers
59 size_t GetQueueSize() const {
60 return queued_buffers.size();
61 }
62
63private:
64 /// Current state of the stream
65 enum class State {
66 Stopped,
67 Playing,
68 };
69
70 /// Plays the next queued buffer in the audio stream, starting playback if necessary
71 void PlayNextBuffer();
72
73 /// Releases the actively playing buffer, signalling that it has been completed
74 void ReleaseActiveBuffer();
75
76 /// Gets the number of core cycles when the specified buffer will be released
77 s64 GetBufferReleaseCycles(const Buffer& buffer) const;
78
79 int sample_rate; ///< Sample rate of the stream
80 Format format; ///< Format of the stream
81 ReleaseCallback release_callback; ///< Buffer release callback for the stream
82 State state{State::Stopped}; ///< Playback state of the stream
83 CoreTiming::EventType* release_event{}; ///< Core timing release event for the stream
84 BufferPtr active_buffer; ///< Actively playing buffer in the stream
85 std::queue<BufferPtr> queued_buffers; ///< Buffers queued to be played in the stream
86 std::queue<BufferPtr> released_buffers; ///< Buffers recently released from the stream
87};
88
89} // namespace AudioCore