generated from 32blit/32blit-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutputManager.cpp
298 lines (241 loc) · 7.35 KB
/
OutputManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* OutputManager.cpp - part of 32Blox (revised edition!)
*
* Copyright (C) 2020 Pete Favelle <32blit@ahnlak.com>
*
* This file is released under the MIT License; see LICENSE for details
*
* The OutputManager is a singleton class to manage outputs; the options as to
* which output channels are enabled are saved, so that we always remember if
* we should be playing music, sounds or haptics.
*/
/* System headers. */
/* Local headers. */
#include "32blit.hpp"
#include "32blox.hpp"
#include "OutputManager.hpp"
#include "assets_audio.hpp"
/* Functions. */
/*
* constructor - Initialises all the asset objects
*/
OutputManager::OutputManager( void )
{
/* See if we've saved any defaults. */
if ( !blit::read_save( flags, SAVE_SLOT_OUTPUT ) )
{
/* Then set some defaults, which is "all enabled" */
flags.sound_enabled = true;
flags.music_enabled = true;
flags.haptic_enabled = false;
}
/* Set up the sound channels. */
blit::channels[CHANNEL_LEVEL].waveforms = blit::Waveform::TRIANGLE | blit::Waveform::SINE | blit::Waveform::SQUARE;
blit::channels[CHANNEL_LEVEL].frequency = 3500;
blit::channels[CHANNEL_LEVEL].volume = 0xffff;
blit::channels[CHANNEL_LEVEL].attack_ms = 32;
blit::channels[CHANNEL_LEVEL].decay_ms = 512;
blit::channels[CHANNEL_LEVEL].sustain = 0;
blit::channels[CHANNEL_LEVEL].release_ms = 128;
blit::channels[CHANNEL_FALLING].waveforms = blit::Waveform::SINE;
blit::channels[CHANNEL_FALLING].frequency = 1000;
blit::channels[CHANNEL_FALLING].volume = 0x3fff;
blit::channels[CHANNEL_FALLING].attack_ms = 4;
blit::channels[CHANNEL_FALLING].decay_ms = 32;
blit::channels[CHANNEL_FALLING].sustain = 0;
blit::channels[CHANNEL_FALLING].release_ms = 32;
blit::channels[CHANNEL_PICKUP].waveforms = blit::Waveform::TRIANGLE;
blit::channels[CHANNEL_PICKUP].frequency = 1400;
blit::channels[CHANNEL_PICKUP].volume = 0xffff;
blit::channels[CHANNEL_PICKUP].attack_ms = 8;
blit::channels[CHANNEL_PICKUP].decay_ms = 128;
blit::channels[CHANNEL_PICKUP].sustain = 0;
blit::channels[CHANNEL_PICKUP].release_ms = 64;
blit::channels[CHANNEL_BOUNCE].waveforms = blit::Waveform::SAW | blit::Waveform::NOISE;
blit::channels[CHANNEL_BOUNCE].volume = 0x7fff;
blit::channels[CHANNEL_BOUNCE].attack_ms = 4;
blit::channels[CHANNEL_BOUNCE].decay_ms = 64;
blit::channels[CHANNEL_BOUNCE].sustain = 0;
blit::channels[CHANNEL_BOUNCE].release_ms = 16;
/* All done. */
return;
}
/*
* get_instance - fetches the singleton instance of the OutputManager
*/
OutputManager &OutputManager::get_instance( void )
{
static OutputManager myself;
return myself;
}
/*
* x_enabled - functions to access the various flags.
*/
bool OutputManager::sound_enabled( void )
{
return flags.sound_enabled;
}
bool OutputManager::music_enabled( void )
{
return flags.music_enabled;
}
bool OutputManager::haptic_enabled( void )
{
return flags.haptic_enabled;
}
/*
* enable_x - functions to set the flag to the provided boolean value.
*
* These functions also save the config out once the change is made.
*/
void OutputManager::enable_sound( bool p_flag )
{
/* Set and save the flag. */
flags.sound_enabled = p_flag;
blit::write_save( flags, SAVE_SLOT_OUTPUT );
/* And turn off any currently playing sounds. */
blit::channels[CHANNEL_LEVEL].off();
blit::channels[CHANNEL_FALLING].off();
blit::channels[CHANNEL_PICKUP].off();
blit::channels[CHANNEL_BOUNCE].off();
return;
}
void OutputManager::enable_music( bool p_flag )
{
/* Set and save the flag. */
flags.music_enabled = p_flag;
blit::write_save( flags, SAVE_SLOT_OUTPUT );
/* And either stop or start the music, as required. */
if ( p_flag )
{
play_music();
}
else
{
stop_music();
}
return;
}
void OutputManager::enable_haptic( bool p_flag )
{
flags.haptic_enabled = p_flag;
blit::write_save( flags, SAVE_SLOT_OUTPUT );
return;
}
/*
* update - called every tick to do any output processing that is required;
* playing or decaying music and sound effects, and the haptic stuff.
*
* uint32_t - the time index from the main update() function.
*/
void OutputManager::update( uint32_t p_time )
{
/* Update the haptic setting if the tween is active, and haptics are on. */
if ( flags.haptic_enabled && haptic_tween.is_running() )
{
blit::vibration = haptic_tween.value;
}
else
{
blit::vibration = 0.0f;
}
/* Check if any channels have hit their sustain phase. */
if ( blit::channels[CHANNEL_LEVEL].adsr_phase == blit::ADSRPhase::SUSTAIN )
blit::channels[CHANNEL_LEVEL].trigger_release();
if ( blit::channels[CHANNEL_FALLING].adsr_phase == blit::ADSRPhase::SUSTAIN )
blit::channels[CHANNEL_FALLING].trigger_release();
if ( blit::channels[CHANNEL_PICKUP].adsr_phase == blit::ADSRPhase::SUSTAIN )
blit::channels[CHANNEL_PICKUP].trigger_release();
if ( blit::channels[CHANNEL_BOUNCE].adsr_phase == blit::ADSRPhase::SUSTAIN )
blit::channels[CHANNEL_BOUNCE].trigger_release();
/* All done. */
return;
}
/*
* trigger_haptic - launches a haptic buzz
*
* float - the strength of the buzz, from 0.0 to 1.0
* uint32_t - the duratio nof the buzz, in milliseconds
*/
void OutputManager::trigger_haptic( float p_strength, uint32_t p_duration )
{
/* Just set the tween running. */
haptic_tween.init( blit::tween_linear, p_strength, 0.0f, p_duration, 1 );
haptic_tween.start();
/* All done. */
return;
}
/*
* play_effect_bounce - plays the sound effect of the ball bouncing off something
*
* uint16_t - the frequency to use
*/
void OutputManager::play_effect_bounce( uint16_t p_frequency )
{
/* Only do this if sound effects are enabled. */
if ( flags.sound_enabled )
{
blit::channels[CHANNEL_BOUNCE].frequency = p_frequency;
blit::channels[CHANNEL_BOUNCE].trigger_attack();
}
/* All done. */
return;
}
/*
* play_effect_pickup - plays the sound effect of a powerup being picked up.
*/
void OutputManager::play_effect_pickup( void )
{
/* Only do this if sound effects are enabled. */
if ( flags.sound_enabled )
{
blit::channels[CHANNEL_PICKUP].trigger_attack();
}
/* All done. */
return;
}
/*
* play_effect_falling - plays the sound of a powerup dropping from the sky
*
* uint8_t - the current row on the screen of the powerup, or 0 to turn off
*/
void OutputManager::play_effect_falling( uint8_t p_height )
{
/* Only do this if sound effects are enabled. */
if ( flags.sound_enabled )
{
blit::channels[CHANNEL_FALLING].frequency = 1000 - p_height * 4;
blit::channels[CHANNEL_FALLING].trigger_attack();
}
/* All done. */
return;
}
/*
* play_effect_level_complete - plays a beep when you clear the level.
*/
void OutputManager::play_effect_level_complete( void )
{
/* Only do this if sound effects are enabled. */
if ( flags.sound_enabled )
{
blit::channels[CHANNEL_LEVEL].trigger_attack();
}
/* All done. */
return;
}
/*
* play_music / stop_music - runs the compiled-in WAV file. These functions
* are pretty much lifted from the ever-talented DaftFreak.
*/
void play_wav(int channel, const uint8_t *ptr, bool loop = false);
void stop_wav(int channel);
void OutputManager::play_music( void )
{
blit::channels[CHANNEL_MUSIC].volume = 0x7fff;
play_wav( CHANNEL_MUSIC, a_audio_music, true );
}
void OutputManager::stop_music( void )
{
stop_wav( CHANNEL_MUSIC );
}
/* End of OutputManager.cpp */