diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-12-14 17:00:51 +0200 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-12-14 17:00:51 +0200 |
commit | 5a6f7804d9a486ee0dd1e04df18cd40c4639c1ce (patch) | |
tree | da611aa5073b25220b1dda8918195be54eeff46e /src/audio | |
parent | b65c60d2419095c2010b6aeea16887967d7c9a8c (diff) |
Audio: Initialize SDL audio only when actually playing
Playing audio is somewhat uncommon, so there is no need to have it set up always.
Diffstat (limited to 'src/audio')
-rw-r--r-- | src/audio/player.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/audio/player.c b/src/audio/player.c index 94bcd065..bf853e3f 100644 --- a/src/audio/player.c +++ b/src/audio/player.c | |||
@@ -31,6 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | |||
31 | #include <the_Foundation/thread.h> | 31 | #include <the_Foundation/thread.h> |
32 | #include <SDL_audio.h> | 32 | #include <SDL_audio.h> |
33 | #include <SDL_timer.h> | 33 | #include <SDL_timer.h> |
34 | #include <SDL.h> | ||
34 | 35 | ||
35 | #if defined (LAGRANGE_ENABLE_MPG123) | 36 | #if defined (LAGRANGE_ENABLE_MPG123) |
36 | # include <mpg123.h> | 37 | # include <mpg123.h> |
@@ -739,6 +740,22 @@ size_t sourceDataSize_Player(const iPlayer *d) { | |||
739 | return size; | 740 | return size; |
740 | } | 741 | } |
741 | 742 | ||
743 | static iBool setupSDLAudio_(iBool init) { | ||
744 | static iBool isAudioInited_ = iFalse; | ||
745 | if (init) { | ||
746 | if (SDL_InitSubSystem(SDL_INIT_AUDIO)) { | ||
747 | fprintf(stderr, "[SDL] audio init failed: %s\n", SDL_GetError()); | ||
748 | return iFalse; | ||
749 | } | ||
750 | isAudioInited_ = iTrue; | ||
751 | } | ||
752 | else if (isAudioInited_) { | ||
753 | SDL_QuitSubSystem(SDL_INIT_AUDIO); | ||
754 | isAudioInited_ = iFalse; | ||
755 | } | ||
756 | return isAudioInited_; | ||
757 | } | ||
758 | |||
742 | iBool start_Player(iPlayer *d) { | 759 | iBool start_Player(iPlayer *d) { |
743 | if (isStarted_Player(d)) { | 760 | if (isStarted_Player(d)) { |
744 | return iFalse; | 761 | return iFalse; |
@@ -757,6 +774,9 @@ iBool start_Player(iPlayer *d) { | |||
757 | } | 774 | } |
758 | content.output.callback = writeOutputSamples_Player_; | 775 | content.output.callback = writeOutputSamples_Player_; |
759 | content.output.userdata = d; | 776 | content.output.userdata = d; |
777 | if (!setupSDLAudio_(iTrue)) { | ||
778 | return iFalse; | ||
779 | } | ||
760 | d->device = SDL_OpenAudioDevice(NULL, SDL_FALSE /* playback */, &content.output, &d->spec, 0); | 780 | d->device = SDL_OpenAudioDevice(NULL, SDL_FALSE /* playback */, &content.output, &d->spec, 0); |
761 | if (!d->device) { | 781 | if (!d->device) { |
762 | return iFalse; | 782 | return iFalse; |
@@ -796,6 +816,7 @@ void stop_Player(iPlayer *d) { | |||
796 | d->device = 0; | 816 | d->device = 0; |
797 | delete_Decoder(d->decoder); | 817 | delete_Decoder(d->decoder); |
798 | d->decoder = NULL; | 818 | d->decoder = NULL; |
819 | setupSDLAudio_(iFalse); | ||
799 | } | 820 | } |
800 | } | 821 | } |
801 | 822 | ||