stream.h 3.2 KB

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