Sound Bakery  v0.1.0
Open-source audio middleware for games
Loading...
Searching...
No Matches
sound_chef_common.h
1#ifndef SOUND_CHEF_COMMON
2#define SOUND_CHEF_COMMON
3
4#ifdef sound_chef_shared_EXPORTS
5 #define SC_DLL
6 #define MA_DLL
7#endif
8
9#include "miniaudio.h"
10
11#include <assert.h>
12#include <string.h>
13
14#if defined(_WIN32)
15 #define SC_CALL __stdcall
16#else
17 #define SC_CALL
18#endif
19
20#if defined(_WIN32)
21 #define SC_DLL_IMPORT __declspec(dllimport)
22 #define SC_DLL_EXPORT __declspec(dllexport)
23 #define SC_DLL_PRIVATE static
24#elif defined(__APPLE__) || defined(__ANDROID__) || defined(__linux__)
25 #define SC_DLL_IMPORT __attribute__((visibility("default")))
26 #define SC_DLL_EXPORT __attribute__((visibility("default")))
27 #define SC_DLL_PRIVATE __attribute__((visibility("hidden")))
28#else
29 #define SC_DLL_IMPORT
30 #define SC_DLL_EXPORT
31 #define SC_DLL_PRIVATE
32#endif
33
34#ifdef SC_DLL
35 #define SC_API SC_DLL_EXPORT SC_CALL
36 #define SC_CLASS SC_DLL_EXPORT
37#else
38 #define SC_API SC_CALL
39 #define SC_CLASS
40#endif
41
42#define SC_CHECK(condition, result) \
43 if ((condition) == MA_FALSE) \
44 return (result)
45#define SC_CHECK_RESULT(result) \
46 if ((result) != MA_SUCCESS) \
47 return (result)
48#define SC_CHECK_ARG(condition) \
49 if ((condition) == MA_FALSE) \
50 return MA_INVALID_ARGS
51#define SC_CHECK_MEM(ptr) \
52 if ((ptr) == NULL) \
53 return MA_OUT_OF_MEMORY
54#define SC_CHECK_MEM_FREE(ptr, freePtr) \
55 if ((ptr) == NULL) \
56 { \
57 ma_free((freePtr), NULL); \
58 return MA_OUT_OF_MEMORY; \
59 }
60#define SC_CHECK_AND_GOTO(condition, dest) \
61 if ((condition) == MA_FALSE) \
62 goto dest
63
64#define SC_ZERO_OBJECT(p) memset((p), 0, sizeof(*(p)))
65
66#define SC_COUNTOF(x) (sizeof(x) / sizeof(x[0]))
67#define SC_MAX(x, y) (((x) > (y)) ? (x) : (y))
68#define SC_MIN(x, y) (((x) < (y)) ? (x) : (y))
69#define SC_ABS(x) (((x) > 0) ? (x) : -(x))
70#define SC_CLAMP(x, lo, hi) (ma_max(lo, ma_min(x, hi)))
71#define SC_OFFSET_PTR(p, offset) (((ma_uint8*)(p)) + (offset))
72#define SC_ALIGN(x, a) (((x) + ((a)-1)) & ~((a)-1))
73#define SC_ALIGN_64(x) ma_align(x, 8)
74
75#ifdef __cplusplus
76extern "C"
77{
78#endif
79
80#define SC_VERSION_MAJOR 0
81#define SC_VERSION_MINOR 1
82#define SC_VERSION_PATCH 0
83#define SC_VERSION_STRING MA_XSTRINGIFY(SC_VERSION_MAJOR) "." MA_XSTRINGIFY(SC_VERSION_MINOR) "." MA_XSTRINGIFY(SC_VERSION_PATCH)
84
85#define SC_PRODUCT_NAME "Sound Chef"
86
87typedef ma_bool32 sc_bool;
88typedef ma_result sc_result;
89
90typedef struct sc_system sc_system;
91
92typedef struct sc_sound sc_sound;
93typedef struct sc_sound sc_sound_instance;
94
95typedef struct sc_node_group sc_node_group;
96
97typedef struct sc_dsp sc_dsp;
98typedef struct sc_dsp_state sc_dsp_state;
99typedef struct sc_dsp_config sc_dsp_config;
101typedef struct sc_dsp_vtable sc_dsp_vtable;
102
103typedef enum sc_sound_mode
104{
105 SC_SOUND_MODE_DEFAULT = 0x00000000, //< Creates a sound in memory and decompresses during runtime
106 SC_SOUND_MODE_DECODE = 0x00000001, //< Decodes the sound upon loading, instead of runtime
107 SC_SOUND_MODE_ASYNC = 0x00000002, //< Loads the sound in a background thread
108 SC_SOUND_MODE_STREAM = 0x00000004, //< Streams parts of the sound from disk during runtime
109} sc_sound_mode;
110
111typedef enum sc_dsp_type
112{
113 SC_DSP_TYPE_UNKOWN, //< User created
114 SC_DSP_TYPE_FADER,
115 SC_DSP_TYPE_LOWPASS,
116 SC_DSP_TYPE_HIGHPASS,
117 SC_DSP_TYPE_DELAY,
118 SC_DSP_TYPE_METER
119} sc_dsp_type;
120
121typedef enum sc_dsp_index
122{
123 SC_DSP_INDEX_TAIL = -2, //< Left/back of the chain and becomes the new input
124 SC_DSP_INDEX_HEAD = -1 //< Right/top of the chain and becomes the new output
125} sc_dsp_index;
126
127typedef enum sc_encoding_format
128{
129 sc_encoding_format_unknown = 0,
130 sc_encoding_format_wav,
131 sc_encoding_format_adpcm = 10,
132 sc_encoding_format_vorbis,
133 sc_encoding_format_opus
134} sc_encoding_format;
135
136typedef sc_result(SC_CALL* SC_DSP_CREATE_CALLBACK)(sc_dsp_state* dspState);
137typedef sc_result(SC_CALL* SC_DSP_RELEASE_CALLBACK)(sc_dsp_state* dspState);
138typedef sc_result(SC_CALL* SC_DSP_SET_PARAM_FLOAT_CALLBACK)(sc_dsp_state* dspState, int index, float value);
139typedef sc_result(SC_CALL* SC_DSP_GET_PARAM_FLOAT_CALLBACK)(sc_dsp_state* dspState, int index, float* value);
140
142{
143 SC_DSP_CREATE_CALLBACK create;
144 SC_DSP_RELEASE_CALLBACK release;
145 SC_DSP_SET_PARAM_FLOAT_CALLBACK setFloat;
146 SC_DSP_GET_PARAM_FLOAT_CALLBACK getFloat;
147
148 sc_dsp_parameter** params;
149 int numParams;
150};
151
160{
161 void* instance; //< points to the current sc_dsp object
162 void* userData; //< points to the user created object, likely some type of ma_node
163 void* system; //< points to the owning sc_system object
164};
165
167{
168 sc_dsp_type type;
169 sc_dsp_vtable* vtable;
170};
171
178struct sc_dsp
179{
180 sc_dsp_state* state; //< holds the instance data for the dsp
181 sc_dsp_vtable* vtable; //< holds the functions for interacting with the underlying node type. Must be not null
182 sc_dsp_type type;
183 sc_dsp* next; //< when in a node group, the get_parent/next dsp. Can be null if the head node
184 sc_dsp* prev; //< when in a node group, the child/previous dsp. Can be null if the tail node
185};
186
188{
189 ma_sound sound;
190 sc_sound_mode mode;
191 ma_decoder* memoryDecoder;
192};
193
205{
206 sc_dsp* tail; //< Left/right most node. Sounds and child groups connect to this
207 sc_dsp* fader; //< Controls the volume and more of the group. Exists at start
208 sc_dsp* head; //< Right/top most node. Nodes in the group route to this. The head then outputs to a get_parent
209};
210
226{
227 ma_engine engine;
228 ma_resource_manager resourceManager; //< We need a custom resource manager for custom decoders
229 ma_log log;
230
231 sc_node_group* masterNodeGroup;
232};
233
234#ifdef __cplusplus
235}
236#endif
237
238#endif
Definition sound_chef_common.h:167
Definition sound_chef_dsp.h:34
Holds instance data for a single sc_dsp.
Definition sound_chef_common.h:160
Definition sound_chef_common.h:142
ma_node with an additional enum descriptor.
Definition sound_chef_common.h:179
Groups nodes/DSPs together into one.
Definition sound_chef_common.h:205
Definition sound_chef_common.h:188
Object that manages the node graph, sounds, output etc.
Definition sound_chef_common.h:226