diff options
| author | 2018-07-27 20:25:32 -0700 | |
|---|---|---|
| committer | 2018-07-27 20:25:32 -0700 | |
| commit | e1d66ea7942800d51aff57c44d0e9ab9ffd949dc (patch) | |
| tree | aaff4a680cafbd1111baa602af8d0d2d7297f585 /src/audio_core/stream.cpp | |
| parent | Merge pull request #696 from DarkLordZach/romfs (diff) | |
| parent | audout: Implement IAudioOut interface with AudioCore. (diff) | |
| download | yuzu-e1d66ea7942800d51aff57c44d0e9ab9ffd949dc.tar.gz yuzu-e1d66ea7942800d51aff57c44d0e9ab9ffd949dc.tar.xz yuzu-e1d66ea7942800d51aff57c44d0e9ab9ffd949dc.zip | |
Merge pull request #842 from bunnei/audio-core
Initial implementation of Audio Core
Diffstat (limited to 'src/audio_core/stream.cpp')
| -rw-r--r-- | src/audio_core/stream.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/audio_core/stream.cpp b/src/audio_core/stream.cpp new file mode 100644 index 000000000..82bff4b9e --- /dev/null +++ b/src/audio_core/stream.cpp | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | // Copyright 2018 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/assert.h" | ||
| 6 | #include "common/logging/log.h" | ||
| 7 | #include "core/core_timing.h" | ||
| 8 | #include "core/core_timing_util.h" | ||
| 9 | |||
| 10 | #include "audio_core/stream.h" | ||
| 11 | |||
| 12 | namespace AudioCore { | ||
| 13 | |||
| 14 | constexpr size_t MaxAudioBufferCount{32}; | ||
| 15 | |||
| 16 | /// Returns the sample size for the specified audio stream format | ||
| 17 | static size_t SampleSizeFromFormat(Stream::Format format) { | ||
| 18 | switch (format) { | ||
| 19 | case Stream::Format::Mono16: | ||
| 20 | return 2; | ||
| 21 | case Stream::Format::Stereo16: | ||
| 22 | return 4; | ||
| 23 | case Stream::Format::Multi51Channel16: | ||
| 24 | return 12; | ||
| 25 | }; | ||
| 26 | |||
| 27 | LOG_CRITICAL(Audio, "Unimplemented format={}", static_cast<u32>(format)); | ||
| 28 | UNREACHABLE(); | ||
| 29 | return {}; | ||
| 30 | } | ||
| 31 | |||
| 32 | Stream::Stream(int sample_rate, Format format, ReleaseCallback&& release_callback) | ||
| 33 | : sample_rate{sample_rate}, format{format}, release_callback{std::move(release_callback)} { | ||
| 34 | release_event = CoreTiming::RegisterEvent( | ||
| 35 | "Stream::Release", [this](u64 userdata, int cycles_late) { ReleaseActiveBuffer(); }); | ||
| 36 | } | ||
| 37 | |||
| 38 | void Stream::Play() { | ||
| 39 | state = State::Playing; | ||
| 40 | PlayNextBuffer(); | ||
| 41 | } | ||
| 42 | |||
| 43 | void Stream::Stop() { | ||
| 44 | ASSERT_MSG(false, "Unimplemented"); | ||
| 45 | } | ||
| 46 | |||
| 47 | s64 Stream::GetBufferReleaseCycles(const Buffer& buffer) const { | ||
| 48 | const size_t num_samples{buffer.GetData().size() / SampleSizeFromFormat(format)}; | ||
| 49 | return CoreTiming::usToCycles((static_cast<u64>(num_samples) * 1000000) / sample_rate); | ||
| 50 | } | ||
| 51 | |||
| 52 | void Stream::PlayNextBuffer() { | ||
| 53 | if (!IsPlaying()) { | ||
| 54 | // Ensure we are in playing state before playing the next buffer | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | |||
| 58 | if (active_buffer) { | ||
| 59 | // Do not queue a new buffer if we are already playing a buffer | ||
| 60 | return; | ||
| 61 | } | ||
| 62 | |||
| 63 | if (queued_buffers.empty()) { | ||
| 64 | // No queued buffers - we are effectively paused | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | active_buffer = queued_buffers.front(); | ||
| 69 | queued_buffers.pop(); | ||
| 70 | |||
| 71 | CoreTiming::ScheduleEventThreadsafe(GetBufferReleaseCycles(*active_buffer), release_event, {}); | ||
| 72 | } | ||
| 73 | |||
| 74 | void Stream::ReleaseActiveBuffer() { | ||
| 75 | released_buffers.push(std::move(active_buffer)); | ||
| 76 | release_callback(); | ||
| 77 | PlayNextBuffer(); | ||
| 78 | } | ||
| 79 | |||
| 80 | bool Stream::QueueBuffer(BufferPtr&& buffer) { | ||
| 81 | if (queued_buffers.size() < MaxAudioBufferCount) { | ||
| 82 | queued_buffers.push(std::move(buffer)); | ||
| 83 | PlayNextBuffer(); | ||
| 84 | return true; | ||
| 85 | } | ||
| 86 | return false; | ||
| 87 | } | ||
| 88 | |||
| 89 | bool Stream::ContainsBuffer(Buffer::Tag tag) const { | ||
| 90 | ASSERT_MSG(false, "Unimplemented"); | ||
| 91 | return {}; | ||
| 92 | } | ||
| 93 | |||
| 94 | std::vector<Buffer::Tag> Stream::GetTagsAndReleaseBuffers(size_t max_count) { | ||
| 95 | std::vector<Buffer::Tag> tags; | ||
| 96 | for (size_t count = 0; count < max_count && !released_buffers.empty(); ++count) { | ||
| 97 | tags.push_back(released_buffers.front()->GetTag()); | ||
| 98 | released_buffers.pop(); | ||
| 99 | } | ||
| 100 | return tags; | ||
| 101 | } | ||
| 102 | |||
| 103 | } // namespace AudioCore | ||