| 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 |
#include "../miniaudio.c" |
| 55 |
|
| 56 |
|
| 57 |
#define FORMAT ma_format_f32 |
| 58 |
#define CHANNELS 2 |
| 59 |
#define SAMPLE_RATE 48000 |
| 60 |
|
| 61 |
|
| 62 |
#define LPF_BIAS 0.9f |
| 63 |
#define LPF_CUTOFF_FACTOR 80 |
| 64 |
#define LPF_ORDER 8 |
| 65 |
#define DELAY_IN_SECONDS 0.2f |
| 66 |
#define DECAY 0.5f |
| 67 |
|
| 68 |
typedef struct |
| 69 |
{ |
| 70 |
ma_data_source_node node; |
| 71 |
ma_decoder decoder; |
| 72 |
} sound_node; |
| 73 |
|
| 74 |
static ma_node_graph g_nodeGraph; |
| 75 |
static ma_lpf_node g_lpfNode; |
| 76 |
static ma_delay_node g_delayNode; |
| 77 |
static ma_splitter_node g_splitterNode; |
| 78 |
static sound_node* g_pSoundNodes; |
| 79 |
static int g_soundNodeCount; |
| 80 |
|
| 81 |
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) |
| 82 |
{ |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL); |
| 88 |
|
| 89 |
(void)pInput; |
| 90 |
(void)pDevice; |
| 91 |
} |
| 92 |
|
| 93 |
int main(int argc, char** argv) |
| 94 |
{ |
| 95 |
int iarg; |
| 96 |
ma_result result; |
| 97 |
|
| 98 |
|
| 99 |
{ |
| 100 |
ma_node_graph_config nodeGraphConfig = ma_node_graph_config_init(CHANNELS); |
| 101 |
|
| 102 |
result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph); |
| 103 |
if (result != MA_SUCCESS) { |
| 104 |
printf("ERROR: Failed to initialize node graph."); |
| 105 |
return -1; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
{ |
| 112 |
ma_lpf_node_config lpfNodeConfig = ma_lpf_node_config_init(CHANNELS, SAMPLE_RATE, SAMPLE_RATE / LPF_CUTOFF_FACTOR, LPF_ORDER); |
| 113 |
|
| 114 |
result = ma_lpf_node_init(&g_nodeGraph, &lpfNodeConfig, NULL, &g_lpfNode); |
| 115 |
if (result != MA_SUCCESS) { |
| 116 |
printf("ERROR: Failed to initialize low pass filter node."); |
| 117 |
return -1; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
ma_node_attach_output_bus(&g_lpfNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0); |
| 122 |
|
| 123 |
|
| 124 |
ma_node_set_output_bus_volume(&g_lpfNode, 0, LPF_BIAS); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
{ |
| 130 |
ma_delay_node_config delayNodeConfig = ma_delay_node_config_init(CHANNELS, SAMPLE_RATE, (ma_uint32)(SAMPLE_RATE * DELAY_IN_SECONDS), DECAY); |
| 131 |
|
| 132 |
result = ma_delay_node_init(&g_nodeGraph, &delayNodeConfig, NULL, &g_delayNode); |
| 133 |
if (result != MA_SUCCESS) { |
| 134 |
printf("ERROR: Failed to initialize delay node."); |
| 135 |
return -1; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
ma_node_attach_output_bus(&g_delayNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0); |
| 140 |
|
| 141 |
|
| 142 |
ma_node_set_output_bus_volume(&g_delayNode, 0, 1 - LPF_BIAS); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
{ |
| 148 |
ma_splitter_node_config splitterNodeConfig = ma_splitter_node_config_init(CHANNELS); |
| 149 |
|
| 150 |
result = ma_splitter_node_init(&g_nodeGraph, &splitterNodeConfig, NULL, &g_splitterNode); |
| 151 |
if (result != MA_SUCCESS) { |
| 152 |
printf("ERROR: Failed to initialize splitter node."); |
| 153 |
return -1; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
ma_node_attach_output_bus(&g_splitterNode, 0, &g_lpfNode, 0); |
| 158 |
ma_node_attach_output_bus(&g_splitterNode, 1, &g_delayNode, 0); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
g_pSoundNodes = (sound_node*)ma_malloc(sizeof(*g_pSoundNodes) * argc-1, NULL); |
| 164 |
if (g_pSoundNodes == NULL) { |
| 165 |
printf("Failed to allocate memory for sounds."); |
| 166 |
return -1; |
| 167 |
} |
| 168 |
|
| 169 |
g_soundNodeCount = 0; |
| 170 |
for (iarg = 1; iarg < argc; iarg += 1) { |
| 171 |
ma_decoder_config decoderConfig = ma_decoder_config_init(FORMAT, CHANNELS, SAMPLE_RATE); |
| 172 |
|
| 173 |
result = ma_decoder_init_file(argv[iarg], &decoderConfig, &g_pSoundNodes[g_soundNodeCount].decoder); |
| 174 |
if (result == MA_SUCCESS) { |
| 175 |
ma_data_source_node_config dataSourceNodeConfig = ma_data_source_node_config_init(&g_pSoundNodes[g_soundNodeCount].decoder); |
| 176 |
|
| 177 |
result = ma_data_source_node_init(&g_nodeGraph, &dataSourceNodeConfig, NULL, &g_pSoundNodes[g_soundNodeCount].node); |
| 178 |
if (result == MA_SUCCESS) { |
| 179 |
|
| 180 |
ma_node_attach_output_bus(&g_pSoundNodes[g_soundNodeCount].node, 0, &g_splitterNode, 0); |
| 181 |
g_soundNodeCount += 1; |
| 182 |
} else { |
| 183 |
printf("WARNING: Failed to init data source node for sound \"%s\". Ignoring.", argv[iarg]); |
| 184 |
ma_decoder_uninit(&g_pSoundNodes[g_soundNodeCount].decoder); |
| 185 |
} |
| 186 |
} else { |
| 187 |
printf("WARNING: Failed to load sound \"%s\". Ignoring.", argv[iarg]); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
{ |
| 193 |
ma_device_config deviceConfig; |
| 194 |
ma_device device; |
| 195 |
|
| 196 |
deviceConfig = ma_device_config_init(ma_device_type_playback); |
| 197 |
deviceConfig.playback.format = FORMAT; |
| 198 |
deviceConfig.playback.channels = CHANNELS; |
| 199 |
deviceConfig.sampleRate = SAMPLE_RATE; |
| 200 |
deviceConfig.dataCallback = data_callback; |
| 201 |
deviceConfig.pUserData = NULL; |
| 202 |
|
| 203 |
result = ma_device_init(NULL, &deviceConfig, &device); |
| 204 |
if (result != MA_SUCCESS) { |
| 205 |
printf("ERROR: Failed to initialize device."); |
| 206 |
goto cleanup_graph; |
| 207 |
} |
| 208 |
|
| 209 |
result = ma_device_start(&device); |
| 210 |
if (result != MA_SUCCESS) { |
| 211 |
ma_device_uninit(&device); |
| 212 |
goto cleanup_graph; |
| 213 |
} |
| 214 |
|
| 215 |
printf("Press Enter to quit...\n"); |
| 216 |
getchar(); |
| 217 |
|
| 218 |
|
| 219 |
ma_device_uninit(&device); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
cleanup_graph: |
| 224 |
{ |
| 225 |
|
| 226 |
int iSound; |
| 227 |
|
| 228 |
|
| 229 |
for (iSound = 0; iSound < g_soundNodeCount; iSound += 1) { |
| 230 |
ma_data_source_node_uninit(&g_pSoundNodes[iSound].node, NULL); |
| 231 |
ma_decoder_uninit(&g_pSoundNodes[iSound].decoder); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
ma_splitter_node_uninit(&g_splitterNode, NULL); |
| 236 |
|
| 237 |
|
| 238 |
ma_delay_node_uninit(&g_delayNode, NULL); |
| 239 |
|
| 240 |
|
| 241 |
ma_lpf_node_uninit(&g_lpfNode, NULL); |
| 242 |
|
| 243 |
|
| 244 |
ma_node_graph_uninit(&g_nodeGraph, NULL); |
| 245 |
} |
| 246 |
|
| 247 |
return 0; |
| 248 |
} |