2018-07-26 17:01:37 -07:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-07-15 15:30:06 -07:00
|
|
|
#include <chrono>
|
2018-07-26 17:01:37 -07:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2018-08-02 15:27:22 -07:00
|
|
|
#include <string>
|
2018-07-26 17:01:37 -07:00
|
|
|
#include <vector>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
#include "audio_core/buffer.h"
|
|
|
|
#include "common/common_types.h"
|
2018-09-14 09:06:00 -07:00
|
|
|
|
2019-02-12 09:32:15 -08:00
|
|
|
namespace Core::Timing {
|
2019-02-14 09:42:58 -08:00
|
|
|
class CoreTiming;
|
2018-09-14 09:06:00 -07:00
|
|
|
struct EventType;
|
2019-02-14 09:42:58 -08:00
|
|
|
} // namespace Core::Timing
|
2018-07-26 17:01:37 -07:00
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
2018-09-14 09:06:00 -07:00
|
|
|
class SinkStream;
|
|
|
|
|
2018-07-26 17:01:37 -07:00
|
|
|
/**
|
|
|
|
* Represents an audio stream, which is a sequence of queued buffers, to be outputed by AudioOut
|
|
|
|
*/
|
|
|
|
class Stream {
|
|
|
|
public:
|
|
|
|
/// Audio format of the stream
|
|
|
|
enum class Format {
|
|
|
|
Mono16,
|
|
|
|
Stereo16,
|
|
|
|
Multi51Channel16,
|
|
|
|
};
|
|
|
|
|
2018-09-23 17:01:02 -07:00
|
|
|
/// Current state of the stream
|
|
|
|
enum class State {
|
|
|
|
Stopped,
|
|
|
|
Playing,
|
|
|
|
};
|
|
|
|
|
2018-07-26 17:01:37 -07:00
|
|
|
/// Callback function type, used to change guest state on a buffer being released
|
|
|
|
using ReleaseCallback = std::function<void()>;
|
|
|
|
|
2019-02-14 09:42:58 -08:00
|
|
|
Stream(Core::Timing::CoreTiming& core_timing, u32 sample_rate, Format format,
|
|
|
|
ReleaseCallback&& release_callback, SinkStream& sink_stream, std::string&& name_);
|
2018-07-26 17:01:37 -07:00
|
|
|
|
|
|
|
/// Plays the audio stream
|
|
|
|
void Play();
|
|
|
|
|
|
|
|
/// Stops the audio stream
|
|
|
|
void Stop();
|
|
|
|
|
|
|
|
/// Queues a buffer into the audio stream, returns true on success
|
|
|
|
bool QueueBuffer(BufferPtr&& buffer);
|
|
|
|
|
|
|
|
/// Returns true if the audio stream contains a buffer with the specified tag
|
|
|
|
bool ContainsBuffer(Buffer::Tag tag) const;
|
|
|
|
|
|
|
|
/// Returns a vector of recently released buffers specified by tag
|
2018-09-15 06:21:06 -07:00
|
|
|
std::vector<Buffer::Tag> GetTagsAndReleaseBuffers(std::size_t max_count);
|
2018-07-26 17:01:37 -07:00
|
|
|
|
2019-06-16 02:06:33 -07:00
|
|
|
void SetVolume(float volume);
|
|
|
|
|
|
|
|
float GetVolume() const {
|
|
|
|
return game_volume;
|
|
|
|
}
|
|
|
|
|
2018-07-26 17:01:37 -07:00
|
|
|
/// Returns true if the stream is currently playing
|
|
|
|
bool IsPlaying() const {
|
|
|
|
return state == State::Playing;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of queued buffers
|
2018-09-15 06:21:06 -07:00
|
|
|
std::size_t GetQueueSize() const {
|
2018-07-26 17:01:37 -07:00
|
|
|
return queued_buffers.size();
|
|
|
|
}
|
|
|
|
|
2018-07-28 10:35:22 -07:00
|
|
|
/// Gets the sample rate
|
|
|
|
u32 GetSampleRate() const {
|
|
|
|
return sample_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the number of channels
|
|
|
|
u32 GetNumChannels() const;
|
|
|
|
|
2018-09-23 05:32:01 -07:00
|
|
|
/// Get the state
|
2018-09-23 17:01:02 -07:00
|
|
|
State GetState() const;
|
2018-09-23 05:32:01 -07:00
|
|
|
|
2018-07-26 17:01:37 -07:00
|
|
|
private:
|
|
|
|
/// Plays the next queued buffer in the audio stream, starting playback if necessary
|
2020-07-15 16:14:21 -07:00
|
|
|
void PlayNextBuffer(std::chrono::nanoseconds ns_late = {});
|
2018-07-26 17:01:37 -07:00
|
|
|
|
|
|
|
/// Releases the actively playing buffer, signalling that it has been completed
|
2020-07-15 16:14:21 -07:00
|
|
|
void ReleaseActiveBuffer(std::chrono::nanoseconds ns_late = {});
|
2018-07-26 17:01:37 -07:00
|
|
|
|
|
|
|
/// Gets the number of core cycles when the specified buffer will be released
|
2020-07-15 15:30:06 -07:00
|
|
|
std::chrono::nanoseconds GetBufferReleaseNS(const Buffer& buffer) const;
|
2020-05-29 14:37:57 -07:00
|
|
|
|
2019-11-26 18:48:56 -08:00
|
|
|
u32 sample_rate; ///< Sample rate of the stream
|
|
|
|
Format format; ///< Format of the stream
|
|
|
|
float game_volume = 1.0f; ///< The volume the game currently has set
|
|
|
|
ReleaseCallback release_callback; ///< Buffer release callback for the stream
|
|
|
|
State state{State::Stopped}; ///< Playback state of the stream
|
|
|
|
std::shared_ptr<Core::Timing::EventType>
|
|
|
|
release_event; ///< Core timing release event for the stream
|
|
|
|
BufferPtr active_buffer; ///< Actively playing buffer in the stream
|
|
|
|
std::queue<BufferPtr> queued_buffers; ///< Buffers queued to be played in the stream
|
|
|
|
std::queue<BufferPtr> released_buffers; ///< Buffers recently released from the stream
|
|
|
|
SinkStream& sink_stream; ///< Output sink for the stream
|
|
|
|
Core::Timing::CoreTiming& core_timing; ///< Core timing instance.
|
|
|
|
std::string name; ///< Name of the stream, must be unique
|
2018-07-26 17:01:37 -07:00
|
|
|
};
|
|
|
|
|
2018-07-28 10:35:22 -07:00
|
|
|
using StreamPtr = std::shared_ptr<Stream>;
|
|
|
|
|
2018-07-26 17:01:37 -07:00
|
|
|
} // namespace AudioCore
|