libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
GuiSound.cpp
1/****************************************************************************
2 * libgui
3 * Daryl Borth 2009-2026
4 * GuiSound.cpp
5 *
6 * Generic - Everything platform-specific lives behind audioSystem
7 ***************************************************************************/
8
9#include "Gui.h"
10
15
17
18GuiSound::GuiSound(const uint8_t * s, int32_t l, SOUND t)
19{
20 sound = s;
21 length = l;
22 type = t;
23 voice = -1;
24 volume = 100;
25 loop = false;
26}
27
29{
30 if(type == SOUND::OGG) {
31 // Only stop the shared hardware stream if this instance is the one actually occupying it
32 if (playingOGG == this) {
33 platform->getAudio()->stopStream();
34 playingOGG = nullptr;
35 }
36 if (activeMusic == this) {
37 activeMusic = nullptr;
38 }
39 }
40}
41
43{
45 int vol = 255 * (volume / 100.0) * (typeVol / 100.0);
46
47 switch(type)
48 {
49 case SOUND::PCM:
50 voice = platform->getAudio()->playVoice(sound, length, vol);
51 break;
52 case SOUND::OGG:
53 playingOGG = this;
54 if (loop) {
55 activeMusic = this;
56 }
57 voice = 0;
58 platform->getAudio()->playStream(sound, length, loop, vol);
59 break;
60 case SOUND::NONE:
61 break;
62 }
63}
64
66{
67 if(voice < 0)
68 return;
69
70 switch(type)
71 {
72 case SOUND::PCM:
73 platform->getAudio()->stopVoice(voice);
74 break;
75 case SOUND::OGG:
76 platform->getAudio()->stopStream();
77 if (playingOGG == this) {
78 playingOGG = nullptr;
79 }
80 if (activeMusic == this) {
81 activeMusic = nullptr;
82 }
83 break;
84 case SOUND::NONE:
85 break;
86 }
87}
88
90{
91 if(voice < 0)
92 return;
93
94 switch(type)
95 {
96 case SOUND::PCM:
97 platform->getAudio()->pauseVoice(voice);
98 break;
99 case SOUND::OGG:
100 platform->getAudio()->pauseStream();
101 break;
102 case SOUND::NONE:
103 break;
104 }
105}
106
108{
109 if(voice < 0)
110 return;
111
112 switch(type)
113 {
114 case SOUND::PCM:
115 platform->getAudio()->resumeVoice(voice);
116 break;
117 case SOUND::OGG:
118 platform->getAudio()->resumeStream();
119 break;
120 case SOUND::NONE:
121 break;
122 }
123}
124
126{
127 if(voice < 0)
128 return false;
129
130 switch(type)
131 {
132 case SOUND::PCM:
133 return platform->getAudio()->isVoicePlaying(voice);
134 case SOUND::OGG:
135 return platform->getAudio()->isStreamPlaying();
136 case SOUND::NONE:
137 return false;
138 }
139
140 return false;
141}
142
144{
145 volume = vol;
146
147 if(voice < 0)
148 return;
149
150 int typeVol = loop ? defaultMusicVolume : defaultSfxVolume;
151 int newvol = 255 * (volume / 100.0) * (typeVol / 100.0);
152
153 switch(type)
154 {
155 case SOUND::PCM:
156 platform->getAudio()->setVoiceVolume(voice, newvol);
157 break;
158 case SOUND::OGG:
159 platform->getAudio()->setStreamVolume(newvol);
160 break;
161 case SOUND::NONE:
162 break;
163 }
164}
165
167{
168 loop = l;
169}
170
172{
173 if (t == VOLUME_TYPE::SFX) {
175 } else if (t == VOLUME_TYPE::MUSIC) {
177
178 if (activeMusic) {
179 if (playingOGG == activeMusic && activeMusic->isPlaying()) {
180 // Still the active stream - just push the new volume live
181 activeMusic->setVolume(activeMusic->volume);
182 } else {
183 // The music track was displaced - restart it
184 activeMusic->play();
185 }
186 }
187 }
188}
Umbrella header for the platform-agnostic libgui core.
Sound conversion and playback.
Definition GuiSound.h:27
int32_t volume
Sound volume (0-100)
Definition GuiSound.h:66
void setVolume(int v)
Set sound volume.
Definition GuiSound.cpp:143
bool loop
Loop sound playback.
Definition GuiSound.h:67
static GuiSound * activeMusic
Pointer to the last GuiSound with loop==true that was told to play, ie: the "background music" track.
Definition GuiSound.h:76
GuiSound()
Constructor.
Definition GuiSound.cpp:16
void setLoop(bool l)
Set the sound to loop playback (only applies to OGG)
Definition GuiSound.cpp:166
~GuiSound()
Destructor.
Definition GuiSound.cpp:28
static int defaultMusicVolume
Global music volume (0-100) – applies to any looping sound.
Definition GuiSound.h:69
void resume()
Resume sound playback.
Definition GuiSound.cpp:107
const uint8_t * sound
Pointer to the sound data.
Definition GuiSound.h:62
void play()
Start sound playback.
Definition GuiSound.cpp:42
SOUND type
Sound format type (PCM or OGG)
Definition GuiSound.h:63
void stop()
Stop sound playback.
Definition GuiSound.cpp:65
void pause()
Pause sound playback.
Definition GuiSound.cpp:89
int32_t voice
Backend-assigned voice handle (PCM only)
Definition GuiSound.h:65
int32_t length
Length of sound data.
Definition GuiSound.h:64
bool isPlaying()
Checks if the sound is currently playing.
Definition GuiSound.cpp:125
static GuiSound * playingOGG
Pointer to the GuiSound instance currently occupying the single shared hardware OGG stream.
Definition GuiSound.h:73
static int defaultSfxVolume
Global sound effects volume (0-100) – applies to any non-looping sound.
Definition GuiSound.h:70
static void setDefaultVolume(VOLUME_TYPE t, int v)
Set global default volume for a volume category (music or sound effects)
Definition GuiSound.cpp:171
SOUND
Encoding of a GuiSound: a short PCM one-shot or a streamed OGG.
Definition GuiSound.h:10
VOLUME_TYPE
Global volume category: music (looping stream) or sound effects.
Definition GuiSound.h:18