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#include "stb_ds.h"
11#include "clap/clap.h"
12
13#include <assert.h>
14#include <string.h>
15
16#if defined(_WIN32)
17 #define SC_CALL __stdcall
18#else
19 #define SC_CALL
20#endif
21
22#if defined(_WIN32)
23 #define SC_DLL_IMPORT __declspec(dllimport)
24 #define SC_DLL_EXPORT __declspec(dllexport)
25 #define SC_DLL_PRIVATE static
26#elif defined(__APPLE__) || defined(__ANDROID__) || defined(__linux__)
27 #define SC_DLL_IMPORT __attribute__((visibility("default")))
28 #define SC_DLL_EXPORT __attribute__((visibility("default")))
29 #define SC_DLL_PRIVATE __attribute__((visibility("hidden")))
30#else
31 #define SC_DLL_IMPORT
32 #define SC_DLL_EXPORT
33 #define SC_DLL_PRIVATE
34#endif
35
36#ifdef SC_DLL
37 #define SC_API SC_DLL_EXPORT SC_CALL
38 #define SC_CLASS SC_DLL_EXPORT
39#else
40 #define SC_API SC_CALL
41 #define SC_CLASS
42#endif
43
44#define SC_CHECK(condition, result) \
45 if ((condition) == MA_FALSE) \
46 return (result)
47#define SC_CHECK_RESULT(result) \
48 if ((result) != MA_SUCCESS) \
49 return (result)
50#define SC_CHECK_ARG(condition) \
51 if ((condition) == MA_FALSE) \
52 return MA_INVALID_ARGS
53#define SC_CHECK_MEM(ptr) \
54 if ((ptr) == NULL) \
55 return MA_OUT_OF_MEMORY
56#define SC_CHECK_MEM_FREE(ptr, freePtr) \
57 if ((ptr) == NULL) \
58 { \
59 ma_free((freePtr), NULL); \
60 return MA_OUT_OF_MEMORY; \
61 }
62#define SC_CHECK_AND_GOTO(condition, dest) \
63 if ((condition) == MA_FALSE) \
64 goto dest
65
66#define SC_ZERO_OBJECT(p) memset((p), 0, sizeof(*(p)))
67
68#define SC_COUNTOF(x) (sizeof(x) / sizeof(x[0]))
69#define SC_MAX(x, y) (((x) > (y)) ? (x) : (y))
70#define SC_MIN(x, y) (((x) < (y)) ? (x) : (y))
71#define SC_ABS(x) (((x) > 0) ? (x) : -(x))
72#define SC_CLAMP(x, lo, hi) (ma_max(lo, ma_min(x, hi)))
73#define SC_OFFSET_PTR(p, offset) (((ma_uint8*)(p)) + (offset))
74#define SC_ALIGN(x, a) (((x) + ((a)-1)) & ~((a)-1))
75#define SC_ALIGN_64(x) ma_align(x, 8)
76
77#ifdef __cplusplus
78extern "C"
79{
80#endif
81
82#include "sound_chef/sound_chef_version.h"
83
84typedef ma_bool32 sc_bool;
85typedef ma_result sc_result;
86
87typedef struct sc_system sc_system;
89
90typedef struct sc_sound sc_sound;
91typedef struct sc_sound sc_sound_instance;
92
93typedef struct sc_node_group sc_node_group;
94
95typedef struct sc_dsp sc_dsp;
96typedef struct sc_dsp_state sc_dsp_state;
97typedef struct sc_dsp_config sc_dsp_config;
99typedef struct sc_dsp_vtable sc_dsp_vtable;
100
101typedef struct sc_clap sc_clap;
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_CLAP //< Wraps a CLAP plugin
120} sc_dsp_type;
121
122typedef enum sc_dsp_index
123{
124 SC_DSP_INDEX_TAIL = -2, //< Left/back of the chain and becomes the new input
125 SC_DSP_INDEX_HEAD = -1 //< Right/top of the chain and becomes the new output
126} sc_dsp_index;
127
128typedef enum sc_encoding_format
129{
130 sc_encoding_format_unknown = 0,
131 sc_encoding_format_wav,
132 sc_encoding_format_adpcm = 10,
133 sc_encoding_format_vorbis,
134 sc_encoding_format_opus
135} sc_encoding_format;
136
137typedef sc_result(SC_CALL* SC_DSP_CREATE_CALLBACK)(sc_dsp_state* dspState);
138typedef sc_result(SC_CALL* SC_DSP_RELEASE_CALLBACK)(sc_dsp_state* dspState);
139typedef sc_result(SC_CALL* SC_DSP_SET_PARAM_FLOAT_CALLBACK)(sc_dsp_state* dspState, int index, float value);
140typedef sc_result(SC_CALL* SC_DSP_GET_PARAM_FLOAT_CALLBACK)(sc_dsp_state* dspState, int index, float* value);
141
143{
144 SC_DSP_CREATE_CALLBACK create;
145 SC_DSP_RELEASE_CALLBACK release;
146 SC_DSP_SET_PARAM_FLOAT_CALLBACK setFloat;
147 SC_DSP_GET_PARAM_FLOAT_CALLBACK getFloat;
148
149 sc_dsp_parameter** params;
150 int numParams;
151};
152
161{
162 void* instance; //< points to the current sc_dsp object
163 void* userData; //< points to the user created object, likely some type of ma_node
164 void* system; //< points to the owning sc_system object
165};
166
168{
169 sc_dsp_type type;
170 sc_dsp_vtable* vtable;
171 clap_plugin_factory_t* clapFactory;
172};
173
180struct sc_dsp
181{
182 sc_dsp_state* state; //< holds the instance data for the dsp
183 sc_dsp_vtable* vtable; //< holds the functions for interacting with the underlying node type. Must be not null
184 sc_dsp_type type;
185 clap_plugin_factory_t* clapFactory; //< If this is a CLAP plugin, the factory to the plugin
186 sc_dsp* next; //< when in a node group, the get_parent/next dsp. Can be null if the head node
187 sc_dsp* prev; //< when in a node group, the child/previous dsp. Can be null if the tail node
188};
189
191{
192 ma_sound sound;
193 sc_sound_mode mode;
194 ma_decoder* memoryDecoder;
195};
196
208{
209 sc_dsp* tail; //< Left/right most node. Sounds and child groups connect to this
210 sc_dsp* fader; //< Controls the volume and more of the group. Exists at start
211 sc_dsp* head; //< Right/top most node. Nodes in the group route to this. The head then outputs to a get_parent
212};
213
218{
219 ma_handle dynamicLibraryHandle; //< Handle to the .clap file
220 clap_plugin_entry_t* clapEntry; //< Entry point of the plugin
221 clap_plugin_factory_t* pluginFactory; //< Plugin factory to poll and create plugins from
222};
223
239{
240 ma_engine engine;
241 ma_resource_manager resourceManager; //< We need a custom resource manager for custom decoders
242 ma_log log;
243
244 clap_host_t clapHost;
245 sc_clap* clapPlugins; //< Dynamic array of opened CLAP plugins
246
247 sc_node_group* masterNodeGroup;
248};
249
255{
256 const char* pluginPath; //< Folder path containing CLAP plugins to load
257};
258
259#ifdef __cplusplus
260}
261#endif
262
263#endif
Holds a DLL handle and plugin entry for a CLAP plugin.
Definition sound_chef_common.h:218
Definition sound_chef_common.h:168
Definition sound_chef_dsp.h:34
Holds instance data for a single sc_dsp.
Definition sound_chef_common.h:161
Definition sound_chef_common.h:143
ma_node with an additional enum descriptor.
Definition sound_chef_common.h:181
Groups nodes/DSPs together into one.
Definition sound_chef_common.h:208
Definition sound_chef_common.h:191
Configuration for initializing the sc_system.
Definition sound_chef_common.h:255
Object that manages the node graph, sounds, output etc.
Definition sound_chef_common.h:239