From b2b95e96c159afe3226fe6a001ff780a38813fe4 Mon Sep 17 00:00:00 2001
From: Chloe Marcec <dmarcecguzman@gmail.com>
Date: Sun, 24 Jan 2021 19:13:34 +1100
Subject: [PATCH] audout: FlushAudioOutBuffers

Fixes Devil May Cry
---
 src/audio_core/stream.cpp               |  8 ++++++++
 src/audio_core/stream.h                 |  3 +++
 src/core/hle/service/audio/audout_u.cpp | 10 +++++++++-
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/audio_core/stream.cpp b/src/audio_core/stream.cpp
index afe68c9ed7..5b0b285cdd 100644
--- a/src/audio_core/stream.cpp
+++ b/src/audio_core/stream.cpp
@@ -51,6 +51,14 @@ void Stream::Stop() {
     UNIMPLEMENTED();
 }
 
+bool Stream::Flush() {
+    const bool had_buffers = !queued_buffers.empty();
+    while (!queued_buffers.empty()) {
+        queued_buffers.pop();
+    }
+    return had_buffers;
+}
+
 void Stream::SetVolume(float volume) {
     game_volume = volume;
 }
diff --git a/src/audio_core/stream.h b/src/audio_core/stream.h
index 506ac536be..559844b9b0 100644
--- a/src/audio_core/stream.h
+++ b/src/audio_core/stream.h
@@ -56,6 +56,9 @@ public:
     /// Queues a buffer into the audio stream, returns true on success
     bool QueueBuffer(BufferPtr&& buffer);
 
+    /// Flush audio buffers
+    bool Flush();
+
     /// Returns true if the audio stream contains a buffer with the specified tag
     [[nodiscard]] bool ContainsBuffer(Buffer::Tag tag) const;
 
diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp
index 02ca711fbf..273a462657 100644
--- a/src/core/hle/service/audio/audout_u.cpp
+++ b/src/core/hle/service/audio/audout_u.cpp
@@ -58,7 +58,7 @@ public:
             {8, &IAudioOut::GetReleasedAudioOutBufferImpl, "GetReleasedAudioOutBufferAuto"},
             {9, &IAudioOut::GetAudioOutBufferCount, "GetAudioOutBufferCount"},
             {10, nullptr, "GetAudioOutPlayedSampleCount"},
-            {11, nullptr, "FlushAudioOutBuffers"},
+            {11, &IAudioOut::FlushAudioOutBuffers, "FlushAudioOutBuffers"},
             {12, &IAudioOut::SetAudioOutVolume, "SetAudioOutVolume"},
             {13, &IAudioOut::GetAudioOutVolume, "GetAudioOutVolume"},
         };
@@ -185,6 +185,14 @@ private:
         rb.Push(static_cast<u32>(stream->GetQueueSize()));
     }
 
+    void FlushAudioOutBuffers(Kernel::HLERequestContext& ctx) {
+        LOG_DEBUG(Service_Audio, "called");
+
+        IPC::ResponseBuilder rb{ctx, 3};
+        rb.Push(RESULT_SUCCESS);
+        rb.Push(stream->Flush());
+    }
+
     void SetAudioOutVolume(Kernel::HLERequestContext& ctx) {
         IPC::RequestParser rp{ctx};
         const float volume = rp.Pop<float>();