stream.h 3.4 KB

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