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