stream.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. void SetVolume(float volume);
  49. float GetVolume() const {
  50. return game_volume;
  51. }
  52. /// Returns true if the stream is currently playing
  53. bool IsPlaying() const {
  54. return state == State::Playing;
  55. }
  56. /// Returns the number of queued buffers
  57. std::size_t GetQueueSize() const {
  58. return queued_buffers.size();
  59. }
  60. /// Gets the sample rate
  61. u32 GetSampleRate() const {
  62. return sample_rate;
  63. }
  64. /// Gets the number of channels
  65. u32 GetNumChannels() const;
  66. /// Get the state
  67. State GetState() const;
  68. private:
  69. /// Plays the next queued buffer in the audio stream, starting playback if necessary
  70. void PlayNextBuffer();
  71. /// Releases the actively playing buffer, signalling that it has been completed
  72. void ReleaseActiveBuffer();
  73. /// Gets the number of core cycles when the specified buffer will be released
  74. s64 GetBufferReleaseCycles(const Buffer& buffer) const;
  75. u32 sample_rate; ///< Sample rate of the stream
  76. Format format; ///< Format of the stream
  77. float game_volume = 1.0f; ///< The volume the game currently has set
  78. ReleaseCallback release_callback; ///< Buffer release callback for the stream
  79. State state{State::Stopped}; ///< Playback state of the stream
  80. Core::Timing::EventType* release_event{}; ///< Core timing release event for the stream
  81. BufferPtr active_buffer; ///< Actively playing buffer in the stream
  82. std::queue<BufferPtr> queued_buffers; ///< Buffers queued to be played in the stream
  83. std::queue<BufferPtr> released_buffers; ///< Buffers recently released from the stream
  84. SinkStream& sink_stream; ///< Output sink for the stream
  85. Core::Timing::CoreTiming& core_timing; ///< Core timing instance.
  86. std::string name; ///< Name of the stream, must be unique
  87. };
  88. using StreamPtr = std::shared_ptr<Stream>;
  89. } // namespace AudioCore