| 1 | /* |
| 2 | Demonstrates how to apply an effect to sounds using the high level engine API. |
| 3 | |
| 4 | This example will load a file from the command line and apply an echo/delay effect to it. It will |
| 5 | show you how to manage `ma_sound` objects and how to insert an effect into the graph. |
| 6 | |
| 7 | The `ma_engine` object is a node graph and is compatible with the `ma_node_graph` API. The |
| 8 | `ma_sound` object is a node within the node and is compatible with the `ma_node` API. This means |
| 9 | that applying an effect is as simple as inserting an effect node into the graph and plugging in the |
| 10 | sound's output into the effect's input. See the Node Graph example for how to use the node graph. |
| 11 | |
| 12 | This example is playing only a single sound at a time which means only a single `ma_sound` object |
| 13 | it being used. If you want to play multiple sounds at the same time, even if they're for the same |
| 14 | sound file, you need multiple `ma_sound` objects. |
| 15 | */ |
| 16 | #include "../miniaudio.c" |
| 17 | |
| 18 | #define DELAY_IN_SECONDS 0.2f |
| 19 | #define DECAY 0.25f /* Volume falloff for each echo. */ |
| 20 | |
| 21 | static ma_engine g_engine; |
| 22 | static ma_sound g_sound; /* This example will play only a single sound at once, so we only need one `ma_sound` object. */ |
| 23 | static ma_delay_node g_delayNode; /* The echo effect is achieved using a delay node. */ |
| 24 | |
| 25 | int main(int argc, char** argv) |
| 26 | { |
| 27 | ma_result result; |
| 28 | |
| 29 | if (argc < 2) { |
| 30 | printf("No input file."); |
| 31 | return -1; |
| 32 | } |
| 33 | |
| 34 | /* The engine needs to be initialized first. */ |
| 35 | result = ma_engine_init(NULL, &g_engine); |
| 36 | if (result != MA_SUCCESS) { |
| 37 | printf("Failed to initialize audio engine."); |
| 38 | return -1; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /* |
| 43 | We'll build our graph starting from the end so initialize the delay node now. The output of |
| 44 | this node will be connected straight to the output. You could also attach it to a sound group |
| 45 | or any other node that accepts an input. |
| 46 | |
| 47 | Creating a node requires a pointer to the node graph that owns it. The engine itself is a node |
| 48 | graph. In the code below we can get a pointer to the node graph with `ma_engine_get_node_graph()` |
| 49 | or we could simple cast the engine to a ma_node_graph* like so: |
| 50 | |
| 51 | (ma_node_graph*)&g_engine |
| 52 | |
| 53 | The endpoint of the graph can be retrieved with `ma_engine_get_endpoint()`. |
| 54 | */ |
| 55 | { |
| 56 | ma_delay_node_config delayNodeConfig; |
| 57 | ma_uint32 channels; |
| 58 | ma_uint32 sampleRate; |
| 59 | |
| 60 | channels = ma_engine_get_channels(&g_engine); |
| 61 | sampleRate = ma_engine_get_sample_rate(&g_engine); |
| 62 | |
| 63 | delayNodeConfig = ma_delay_node_config_init(channels, sampleRate, (ma_uint32)(sampleRate * DELAY_IN_SECONDS), DECAY); |
| 64 | |
| 65 | result = ma_delay_node_init(ma_engine_get_node_graph(&g_engine), &delayNodeConfig, NULL, &g_delayNode); |
| 66 | if (result != MA_SUCCESS) { |
| 67 | printf("Failed to initialize delay node."); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | /* Connect the output of the delay node to the input of the endpoint. */ |
| 72 | ma_node_attach_output_bus(&g_delayNode, 0, ma_engine_get_endpoint(&g_engine), 0); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /* Now we can load the sound and connect it to the delay node. */ |
| 77 | { |
| 78 | result = ma_sound_init_from_file(&g_engine, argv[1], 0, NULL, NULL, &g_sound); |
| 79 | if (result != MA_SUCCESS) { |
| 80 | printf("Failed to initialize sound \"%s\".", argv[1]); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | /* Connect the output of the sound to the input of the effect. */ |
| 85 | ma_node_attach_output_bus(&g_sound, 0, &g_delayNode, 0); |
| 86 | |
| 87 | /* |
| 88 | Start the sound after it's applied to the sound. Otherwise there could be a scenario where |
| 89 | the very first part of it is read before the attachment to the effect is made. |
| 90 | */ |
| 91 | ma_sound_start(&g_sound); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | printf("Press Enter to quit..."); |
| 96 | getchar(); |
| 97 | |
| 98 | ma_sound_uninit(&g_sound); |
| 99 | ma_delay_node_uninit(&g_delayNode, NULL); |
| 100 | ma_engine_uninit(&g_engine); |
| 101 | |
| 102 | return 0; |
| 103 | } |