diff options
| author | 2017-01-26 04:33:26 +0100 | |
|---|---|---|
| committer | 2017-01-25 22:33:26 -0500 | |
| commit | f8523699864b6000572affaa0e36d9a4d89ffce6 (patch) | |
| tree | 952a96e11d4efe860272ea7a47ebbee896fb5d61 /src/audio_core | |
| parent | Merge pull request #2434 from mailwl/nfc-amiibo (diff) | |
| download | yuzu-f8523699864b6000572affaa0e36d9a4d89ffce6.tar.gz yuzu-f8523699864b6000572affaa0e36d9a4d89ffce6.tar.xz yuzu-f8523699864b6000572affaa0e36d9a4d89ffce6.zip | |
SDL: Select audio device (#2403)
* Initial Commit
Added Device logic to Sinks
Started on UI for selecting devices
Removed redundant import
* Audio Core: Complete Device Switching
Complete the device switching implementation by allowing the output
device to be loaded, changed and saved through the configurations menu.
Worked with the Sink abstraction and tuned the "Device Selection"
configuration so that the Device List is automatically populated when
the Sink is changed.
This hopefully addresses the concerns and recommendations mentioned in
the comments of the PR.
* Clean original implementation.
* Refactor GetSinkDetails
Diffstat (limited to 'src/audio_core')
| -rw-r--r-- | src/audio_core/audio_core.cpp | 16 | ||||
| -rw-r--r-- | src/audio_core/null_sink.h | 6 | ||||
| -rw-r--r-- | src/audio_core/sdl2_sink.cpp | 30 | ||||
| -rw-r--r-- | src/audio_core/sdl2_sink.h | 5 | ||||
| -rw-r--r-- | src/audio_core/sink.h | 9 | ||||
| -rw-r--r-- | src/audio_core/sink_details.cpp | 19 | ||||
| -rw-r--r-- | src/audio_core/sink_details.h | 2 |
7 files changed, 69 insertions, 18 deletions
diff --git a/src/audio_core/audio_core.cpp b/src/audio_core/audio_core.cpp index ba6acf28e..84f9c03a7 100644 --- a/src/audio_core/audio_core.cpp +++ b/src/audio_core/audio_core.cpp | |||
| @@ -56,20 +56,8 @@ void AddAddressSpace(Kernel::VMManager& address_space) { | |||
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | void SelectSink(std::string sink_id) { | 58 | void SelectSink(std::string sink_id) { |
| 59 | auto iter = | 59 | const SinkDetails& sink_details = GetSinkDetails(sink_id); |
| 60 | std::find_if(g_sink_details.begin(), g_sink_details.end(), | 60 | DSP::HLE::SetSink(sink_details.factory()); |
| 61 | [sink_id](const auto& sink_detail) { return sink_detail.id == sink_id; }); | ||
| 62 | |||
| 63 | if (sink_id == "auto" || iter == g_sink_details.end()) { | ||
| 64 | if (sink_id != "auto") { | ||
| 65 | LOG_ERROR(Audio, "AudioCore::SelectSink given invalid sink_id %s", sink_id.c_str()); | ||
| 66 | } | ||
| 67 | // Auto-select. | ||
| 68 | // g_sink_details is ordered in terms of desirability, with the best choice at the front. | ||
| 69 | iter = g_sink_details.begin(); | ||
| 70 | } | ||
| 71 | |||
| 72 | DSP::HLE::SetSink(iter->factory()); | ||
| 73 | } | 61 | } |
| 74 | 62 | ||
| 75 | void EnableStretching(bool enable) { | 63 | void EnableStretching(bool enable) { |
diff --git a/src/audio_core/null_sink.h b/src/audio_core/null_sink.h index e7668438c..c732926a2 100644 --- a/src/audio_core/null_sink.h +++ b/src/audio_core/null_sink.h | |||
| @@ -23,6 +23,12 @@ public: | |||
| 23 | size_t SamplesInQueue() const override { | 23 | size_t SamplesInQueue() const override { |
| 24 | return 0; | 24 | return 0; |
| 25 | } | 25 | } |
| 26 | |||
| 27 | void SetDevice(int device_id) override {} | ||
| 28 | |||
| 29 | std::vector<std::string> GetDeviceList() const override { | ||
| 30 | return {}; | ||
| 31 | } | ||
| 26 | }; | 32 | }; |
| 27 | 33 | ||
| 28 | } // namespace AudioCore | 34 | } // namespace AudioCore |
diff --git a/src/audio_core/sdl2_sink.cpp b/src/audio_core/sdl2_sink.cpp index 4b66cd826..933c5f16d 100644 --- a/src/audio_core/sdl2_sink.cpp +++ b/src/audio_core/sdl2_sink.cpp | |||
| @@ -4,12 +4,12 @@ | |||
| 4 | 4 | ||
| 5 | #include <list> | 5 | #include <list> |
| 6 | #include <numeric> | 6 | #include <numeric> |
| 7 | #include <vector> | ||
| 8 | #include <SDL.h> | 7 | #include <SDL.h> |
| 9 | #include "audio_core/audio_core.h" | 8 | #include "audio_core/audio_core.h" |
| 10 | #include "audio_core/sdl2_sink.h" | 9 | #include "audio_core/sdl2_sink.h" |
| 11 | #include "common/assert.h" | 10 | #include "common/assert.h" |
| 12 | #include "common/logging/log.h" | 11 | #include "common/logging/log.h" |
| 12 | #include "core/settings.h" | ||
| 13 | 13 | ||
| 14 | namespace AudioCore { | 14 | namespace AudioCore { |
| 15 | 15 | ||
| @@ -42,10 +42,24 @@ SDL2Sink::SDL2Sink() : impl(std::make_unique<Impl>()) { | |||
| 42 | SDL_AudioSpec obtained_audiospec; | 42 | SDL_AudioSpec obtained_audiospec; |
| 43 | SDL_zero(obtained_audiospec); | 43 | SDL_zero(obtained_audiospec); |
| 44 | 44 | ||
| 45 | impl->audio_device_id = | 45 | int device_count = SDL_GetNumAudioDevices(0); |
| 46 | SDL_OpenAudioDevice(nullptr, false, &desired_audiospec, &obtained_audiospec, 0); | 46 | device_list.clear(); |
| 47 | for (int i = 0; i < device_count; ++i) { | ||
| 48 | device_list.push_back(SDL_GetAudioDeviceName(i, 0)); | ||
| 49 | } | ||
| 50 | |||
| 51 | const char* device = nullptr; | ||
| 52 | |||
| 53 | if (device_count >= 1 && Settings::values.audio_device_id != "auto" && | ||
| 54 | !Settings::values.audio_device_id.empty()) { | ||
| 55 | device = Settings::values.audio_device_id.c_str(); | ||
| 56 | } | ||
| 57 | |||
| 58 | impl->audio_device_id = SDL_OpenAudioDevice(device, false, &desired_audiospec, | ||
| 59 | &obtained_audiospec, SDL_AUDIO_ALLOW_ANY_CHANGE); | ||
| 47 | if (impl->audio_device_id <= 0) { | 60 | if (impl->audio_device_id <= 0) { |
| 48 | LOG_CRITICAL(Audio_Sink, "SDL_OpenAudioDevice failed with: %s", SDL_GetError()); | 61 | LOG_CRITICAL(Audio_Sink, "SDL_OpenAudioDevice failed with code %d for device \"%s\"", |
| 62 | impl->audio_device_id, Settings::values.audio_device_id.c_str()); | ||
| 49 | return; | 63 | return; |
| 50 | } | 64 | } |
| 51 | 65 | ||
| @@ -69,6 +83,10 @@ unsigned int SDL2Sink::GetNativeSampleRate() const { | |||
| 69 | return impl->sample_rate; | 83 | return impl->sample_rate; |
| 70 | } | 84 | } |
| 71 | 85 | ||
| 86 | std::vector<std::string> SDL2Sink::GetDeviceList() const { | ||
| 87 | return device_list; | ||
| 88 | } | ||
| 89 | |||
| 72 | void SDL2Sink::EnqueueSamples(const s16* samples, size_t sample_count) { | 90 | void SDL2Sink::EnqueueSamples(const s16* samples, size_t sample_count) { |
| 73 | if (impl->audio_device_id <= 0) | 91 | if (impl->audio_device_id <= 0) |
| 74 | return; | 92 | return; |
| @@ -96,6 +114,10 @@ size_t SDL2Sink::SamplesInQueue() const { | |||
| 96 | return total_size; | 114 | return total_size; |
| 97 | } | 115 | } |
| 98 | 116 | ||
| 117 | void SDL2Sink::SetDevice(int device_id) { | ||
| 118 | this->device_id = device_id; | ||
| 119 | } | ||
| 120 | |||
| 99 | void SDL2Sink::Impl::Callback(void* impl_, u8* buffer, int buffer_size_in_bytes) { | 121 | void SDL2Sink::Impl::Callback(void* impl_, u8* buffer, int buffer_size_in_bytes) { |
| 100 | Impl* impl = reinterpret_cast<Impl*>(impl_); | 122 | Impl* impl = reinterpret_cast<Impl*>(impl_); |
| 101 | 123 | ||
diff --git a/src/audio_core/sdl2_sink.h b/src/audio_core/sdl2_sink.h index ccd0f7c7e..ce75733b7 100644 --- a/src/audio_core/sdl2_sink.h +++ b/src/audio_core/sdl2_sink.h | |||
| @@ -21,9 +21,14 @@ public: | |||
| 21 | 21 | ||
| 22 | size_t SamplesInQueue() const override; | 22 | size_t SamplesInQueue() const override; |
| 23 | 23 | ||
| 24 | std::vector<std::string> GetDeviceList() const override; | ||
| 25 | void SetDevice(int device_id); | ||
| 26 | |||
| 24 | private: | 27 | private: |
| 25 | struct Impl; | 28 | struct Impl; |
| 26 | std::unique_ptr<Impl> impl; | 29 | std::unique_ptr<Impl> impl; |
| 30 | int device_id; | ||
| 31 | std::vector<std::string> device_list; | ||
| 27 | }; | 32 | }; |
| 28 | 33 | ||
| 29 | } // namespace AudioCore | 34 | } // namespace AudioCore |
diff --git a/src/audio_core/sink.h b/src/audio_core/sink.h index 08f3bab5b..558c8c0fe 100644 --- a/src/audio_core/sink.h +++ b/src/audio_core/sink.h | |||
| @@ -31,6 +31,15 @@ public: | |||
| 31 | 31 | ||
| 32 | /// Samples enqueued that have not been played yet. | 32 | /// Samples enqueued that have not been played yet. |
| 33 | virtual std::size_t SamplesInQueue() const = 0; | 33 | virtual std::size_t SamplesInQueue() const = 0; |
| 34 | |||
| 35 | /** | ||
| 36 | * Sets the desired output device. | ||
| 37 | * @paran device_id Id of the desired device. | ||
| 38 | */ | ||
| 39 | virtual void SetDevice(int device_id) = 0; | ||
| 40 | |||
| 41 | /// Returns the list of available devices. | ||
| 42 | virtual std::vector<std::string> GetDeviceList() const = 0; | ||
| 34 | }; | 43 | }; |
| 35 | 44 | ||
| 36 | } // namespace | 45 | } // namespace |
diff --git a/src/audio_core/sink_details.cpp b/src/audio_core/sink_details.cpp index 95ccc9e9d..6972395af 100644 --- a/src/audio_core/sink_details.cpp +++ b/src/audio_core/sink_details.cpp | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | ||
| 5 | #include <memory> | 6 | #include <memory> |
| 6 | #include <vector> | 7 | #include <vector> |
| 7 | #include "audio_core/null_sink.h" | 8 | #include "audio_core/null_sink.h" |
| @@ -9,6 +10,7 @@ | |||
| 9 | #ifdef HAVE_SDL2 | 10 | #ifdef HAVE_SDL2 |
| 10 | #include "audio_core/sdl2_sink.h" | 11 | #include "audio_core/sdl2_sink.h" |
| 11 | #endif | 12 | #endif |
| 13 | #include "common/logging/log.h" | ||
| 12 | 14 | ||
| 13 | namespace AudioCore { | 15 | namespace AudioCore { |
| 14 | 16 | ||
| @@ -20,4 +22,21 @@ const std::vector<SinkDetails> g_sink_details = { | |||
| 20 | {"null", []() { return std::make_unique<NullSink>(); }}, | 22 | {"null", []() { return std::make_unique<NullSink>(); }}, |
| 21 | }; | 23 | }; |
| 22 | 24 | ||
| 25 | const SinkDetails& GetSinkDetails(std::string sink_id) { | ||
| 26 | auto iter = | ||
| 27 | std::find_if(g_sink_details.begin(), g_sink_details.end(), | ||
| 28 | [sink_id](const auto& sink_detail) { return sink_detail.id == sink_id; }); | ||
| 29 | |||
| 30 | if (sink_id == "auto" || iter == g_sink_details.end()) { | ||
| 31 | if (sink_id != "auto") { | ||
| 32 | LOG_ERROR(Audio, "AudioCore::SelectSink given invalid sink_id %s", sink_id.c_str()); | ||
| 33 | } | ||
| 34 | // Auto-select. | ||
| 35 | // g_sink_details is ordered in terms of desirability, with the best choice at the front. | ||
| 36 | iter = g_sink_details.begin(); | ||
| 37 | } | ||
| 38 | |||
| 39 | return *iter; | ||
| 40 | } | ||
| 41 | |||
| 23 | } // namespace AudioCore | 42 | } // namespace AudioCore |
diff --git a/src/audio_core/sink_details.h b/src/audio_core/sink_details.h index 4b30cf835..9d3735171 100644 --- a/src/audio_core/sink_details.h +++ b/src/audio_core/sink_details.h | |||
| @@ -24,4 +24,6 @@ struct SinkDetails { | |||
| 24 | 24 | ||
| 25 | extern const std::vector<SinkDetails> g_sink_details; | 25 | extern const std::vector<SinkDetails> g_sink_details; |
| 26 | 26 | ||
| 27 | const SinkDetails& GetSinkDetails(std::string sink_id); | ||
| 28 | |||
| 27 | } // namespace AudioCore | 29 | } // namespace AudioCore |