102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
system.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
10#include <math.h>
11#include <stdarg.h>
12#include <string.h>
13
14// Original pcdogs.exe: 0x437fc0
15// Starts Miles, negotiates a digital output format, allocates nine sample handles when a driver opens, and resets sound channels. The emulated-device check retains an uninitialized local description buffer from the recovered body.
17{
18 if (!AIL_startup())
19 {
21
22 return;
23 }
24
26
28 uint16_t precision = AUDIO_PCM_PRECISION_16;
29 uint32_t samples_per_second = AUDIO_DEFAULT_SAMPLE_RATE;
30
31 struct Audio_PCMWaveFormat16 pcm_format;
32
33 pcm_format.format_tag = WAVE_FORMAT_PCM;
37
38 while (true)
39 {
40 // Native 0x438019-0x438022 keeps only the low word of this product.
41 pcm_format.block_align = (precision >> AUDIO_BITS_TO_BYTES_SHIFT) * pcm_format.channel_count;
42
43 uint16_t block_align = pcm_format.block_align;
44
46
48 &pcm_format))
49 {
51 break;
52
53 // Native 0x438056-0x438060 reads this buffer without a producer.
54 // Its caller-stack residue is a compatibility boundary, not a known device name.
55 char unobserved_device_description[AUDIO_DEVICE_DESCRIPTION_CAPACITY];
56
57 if (!CRT_strstr((const char *)(&unobserved_device_description), "Emulated"))
58 break;
59
63
64 // Both calls occur in the native body (0x438082 and 0x438094).
66 precision = pcm_format.bits_per_sample;
68
70 break;
71
72 continue;
73 }
74
75 int32_t waveout_preference = AIL_get_preference(AUDIO_MILES_PREFERENCE_WAVEOUT);
76
77 if (waveout_preference)
78 {
79 precision = pcm_format.bits_per_sample;
80 samples_per_second = pcm_format.samples_per_second >> 1;
82
84 continue;
85 else if (precision == AUDIO_PCM_PRECISION_16)
86 {
88 precision = AUDIO_PCM_PRECISION_8;
91
92 continue;
93 }
94 }
95 else
96 {
98 precision = pcm_format.bits_per_sample;
100 }
101
103 break;
104 }
105
107 {
108 for (struct Audio_SoundSlot *sound_slot = g_soundSlots;
109 sound_slot < g_soundSlots + AUDIO_CHANNEL_COUNT; sound_slot += 1)
110 {
112 sound_slot->cached_playback_rate = -1;
113 sound_slot->cached_pan = -1;
114 sound_slot->cached_volume = -1;
115 }
116 }
117
119}
120
121// Original pcdogs.exe: 0x438130
122// Releases sample handles, shuts down Miles, clears the digital driver, and reports remaining wave and stream counts. Returns immediately when no driver exists.
124{
126 return;
127
128 for (struct Audio_SoundSlot *sound_slot = g_soundSlots;
129 sound_slot < g_soundSlots + AUDIO_CHANNEL_COUNT; sound_slot += 1)
130 {
131 AIL_HSAMPLE sample = sound_slot->sample_handle;
132
133 if (sample)
135
136 sound_slot->sample_handle = NULL;
137 sound_slot->base_playback_rate = 0;
138 }
139
140 AIL_shutdown();
141
142 int32_t remaining_streams = g_openStreamCount;
143 int32_t remaining_waves = g_activeWaveCount;
144
146 CRT_printf("%d waves, %d themes still active\n", remaining_waves, remaining_streams);
147}
148
149// Original pcdogs.exe: 0x438240
150// Sets digital master volume to the Miles maximum of 127.
152{
153 // Set Miles Sound System digital driver master volume to 0x7f (127 = full volume)
155}
156
157// Original pcdogs.exe: 0x4385d0
158// Returns the audio-system flag as a sign-extended byte; callers interpret a zero value as enabled.
160{
161 return (int32_t)(int8_t)g_audioSystemFlag;
162}
163
164// Original pcdogs.exe: 0x4385e0
165// Stores the audio-system flag byte without validation or normalization.
166void Audio_SetEnabledFlag(uint8_t enabled)
167{
168 g_audioSystemFlag = enabled;
169}
170
171// Original pcdogs.exe: 0x43ebc0
172// Relocates sample tables and their two optional sample pointers in place. Odd table values copy another definition's current table pointer, so alias indices and relocation order must be valid.
173void Audio_FixupSoundDefinitions(struct Audio_SoundDefinition *sound_defs, int32_t count, void *sample_data_base)
174{
175 struct Audio_SoundDefinition *definition_cursor = sound_defs;
176
177 if (!definition_cursor || !sample_data_base)
178 return;
179
180 int32_t definitions_remaining = count;
181
182 if (definitions_remaining <= 0)
183 return;
184
185 struct Audio_SoundDefinition *definition_base = definition_cursor;
186
187 do
188 {
189 void **sample_table_ptr = definition_cursor->sample_table_ptr;
190
192 {
193 if (!((uint8_t)(uintptr_t)sample_table_ptr & AUDIO_SAMPLE_TABLE_ALIAS))
194 {
195 // The original 0x43ebe5 JE reaches this path for even offsets.
196 void **resolved_sample_table = (void **)((uint32_t)sample_table_ptr + (char *)definition_base);
197
198 definition_cursor->sample_table_ptr = (void **)(resolved_sample_table);
199
200 void *first_sample_offset = resolved_sample_table[0];
201
202 if (first_sample_offset)
203 definition_cursor->sample_table_ptr[0] = (uint32_t)first_sample_offset + (char *)sample_data_base;
204
205 void *second_sample_offset = definition_cursor->sample_table_ptr[1];
206
207 if (second_sample_offset)
208 definition_cursor->sample_table_ptr[1] = (uint32_t)second_sample_offset + (char *)sample_data_base;
209 }
210 else
211 definition_cursor->sample_table_ptr =
212 definition_base[(uint32_t)sample_table_ptr >> 1].sample_table_ptr;
213 }
214
215 definition_cursor = &definition_cursor[1];
216 definitions_remaining -= 1;
217 } while (definitions_remaining);
218}
#define AUDIO_DEFAULT_SAMPLE_RATE
Definition constants.h:36
#define AUDIO_SAMPLE_TABLE_ALIAS
Definition constants.h:20
@ AUDIO_MILES_PREFERENCE_WAVEOUT
Definition constants.h:75
@ AUDIO_MILES_PREFERENCE_STARTUP_OPTION
Definition constants.h:76
#define AUDIO_PCM_PRECISION_16
Definition constants.h:38
#define AUDIO_PCM_PRECISION_8
Definition constants.h:39
#define AUDIO_PCM_STEREO_CHANNELS
Definition constants.h:44
#define AUDIO_MILES_MASTER_VOLUME_MAX
Definition constants.h:30
#define AUDIO_MIN_SAMPLE_RATE
Definition constants.h:37
#define AUDIO_BITS_TO_BYTES_SHIFT
Definition constants.h:45
#define AUDIO_DEVICE_DESCRIPTION_CAPACITY
Definition constants.h:43
#define AUDIO_CHANNEL_COUNT
Definition constants.h:41
void Audio_UpdateSoundChannels(int32_t channel_mask)
Definition channels.c:275
#define g_audioSystemFlag
Definition globals.h:21
#define Audio_InitializeSystem_DigitalDriver
Definition globals.h:24
#define g_soundSlots
Definition globals.h:27
void * AIL_HSAMPLE
Definition common.h:7
void AIL_waveOutClose(AIL_HDIGDRIVER driver)
int32_t AIL_waveOutOpen(AIL_HDIGDRIVER *out_driver, void *wave_handle_out, uint32_t device_id, struct Audio_PCMWaveFormat16 *format)
int32_t AIL_set_preference(int32_t preference, int32_t value)
int32_t AIL_set_digital_master_volume(AIL_HDIGDRIVER driver, int32_t volume)
int32_t AIL_shutdown(void)
int32_t AIL_startup(void)
AIL_HSAMPLE AIL_allocate_sample_handle(AIL_HDIGDRIVER driver)
int32_t AIL_release_sample_handle(AIL_HSAMPLE sample)
int32_t AIL_get_preference(int32_t preference)
int32_t CRT_printf(char const *format,...)
Definition formatting.c:63
char * CRT_strstr(char const *haystack, char const *needle)
Definition strings.c:85
uint32_t samples_per_second
Definition types.h:24
uint16_t channel_count
Definition types.h:23
uint16_t bits_per_sample
Definition types.h:27
uint32_t average_bytes_per_second
Definition types.h:25
uint16_t format_tag
Definition types.h:22
uint16_t block_align
Definition types.h:26
void ** sample_table_ptr
Definition types.h:67
void Audio_FixupSoundDefinitions(struct Audio_SoundDefinition *sound_defs, int32_t count, void *sample_data_base)
Definition system.c:173
void Audio_InitializeSystem()
Definition system.c:16
void Audio_SetEnabledFlag(uint8_t enabled)
Definition system.c:166
void Audio_InitializeLevelAudio()
Definition system.c:151
int32_t Audio_GetEnabledFlag()
Definition system.c:159
void Audio_ShutdownSystem()
Definition system.c:123
#define g_openStreamCount
Definition values.h:456
#define g_activeWaveCount
Definition values.h:462