1 #include <stdlib.h>
2 #include <shadowdive/shadowdive.h>
3 #include "resources/sounds_loader.h"
4 #include "resources/ids.h"
5 #include "resources/pathmanager.h"
6 #include "utils/log.h"
7 
8 sd_sound_file *sound_data = NULL;
9 
sounds_loader_init()10 int sounds_loader_init() {
11     // Get filename
12     const char *filename = pm_get_resource_path(DAT_SOUNDS);
13 
14     // Load sounds
15     sound_data = malloc(sizeof(sd_sound_file));
16     if(sd_sounds_create(sound_data) != SD_SUCCESS) {
17         goto error_0;
18     }
19     if(sd_sounds_load(sound_data, filename)) {
20         PERROR("Unable to load sounds file '%s'!", filename);
21         goto error_1;
22     }
23     INFO("Loaded sounds file '%s'.", filename);
24     return 0;
25 
26 error_1:
27     sd_sounds_free(sound_data);
28 error_0:
29     free(sound_data);
30     sound_data = NULL;
31     return 1;
32 }
33 
sounds_loader_get(int id,char ** buffer,int * len)34 int sounds_loader_get(int id, char **buffer, int *len) {
35     // Make sure the data is ok and sound exists
36     if(sound_data == NULL) return 1;
37 
38     // Get sound
39     const sd_sound *sample = sd_sounds_get(sound_data, id);
40     if(sample == NULL) {
41         PERROR("Requested sound %d does not exist!", id);
42         return 1;
43     }
44 
45     // Get much data!
46     *buffer = sample->data;
47     *len = sample->len;
48 
49     return 0; // Success
50 }
51 
sounds_loader_close()52 void sounds_loader_close() {
53     if(sound_data != NULL) {
54         sd_sounds_free(sound_data);
55         free(sound_data);
56         sound_data = NULL;
57     }
58 }
59