| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include "../../../miniaudio.c" |
| 10 |
#include "ma_vocoder_node.c" |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
|
| 14 |
#define DEVICE_FORMAT ma_format_f32 |
| 15 |
#define DEVICE_CHANNELS 1 |
| 16 |
|
| 17 |
static ma_waveform g_sourceData; |
| 18 |
static ma_audio_buffer_ref g_exciteData; |
| 19 |
static ma_data_source_node g_sourceNode; |
| 20 |
static ma_data_source_node g_exciteNode; |
| 21 |
static ma_vocoder_node g_vocoderNode; |
| 22 |
static ma_node_graph g_nodeGraph; |
| 23 |
|
| 24 |
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
if (pDevice->capture.format != DEVICE_FORMAT || pDevice->playback.format != DEVICE_FORMAT || pDevice->capture.channels != pDevice->playback.channels) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
ma_audio_buffer_ref_set_data(&g_exciteData, pInput, frameCount); |
| 41 |
|
| 42 |
|
| 43 |
ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL); |
| 44 |
} |
| 45 |
|
| 46 |
int main(int argc, char** argv) |
| 47 |
{ |
| 48 |
ma_result result; |
| 49 |
ma_device_config deviceConfig; |
| 50 |
ma_device device; |
| 51 |
ma_node_graph_config nodeGraphConfig; |
| 52 |
ma_vocoder_node_config vocoderNodeConfig; |
| 53 |
ma_data_source_node_config sourceNodeConfig; |
| 54 |
ma_data_source_node_config exciteNodeConfig; |
| 55 |
ma_waveform_config waveformConfig; |
| 56 |
|
| 57 |
deviceConfig = ma_device_config_init(ma_device_type_duplex); |
| 58 |
deviceConfig.capture.pDeviceID = NULL; |
| 59 |
deviceConfig.capture.format = DEVICE_FORMAT; |
| 60 |
deviceConfig.capture.channels = DEVICE_CHANNELS; |
| 61 |
deviceConfig.capture.shareMode = ma_share_mode_shared; |
| 62 |
deviceConfig.playback.pDeviceID = NULL; |
| 63 |
deviceConfig.playback.format = DEVICE_FORMAT; |
| 64 |
deviceConfig.playback.channels = DEVICE_CHANNELS; |
| 65 |
deviceConfig.dataCallback = data_callback; |
| 66 |
result = ma_device_init(NULL, &deviceConfig, &device); |
| 67 |
if (result != MA_SUCCESS) { |
| 68 |
return result; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
nodeGraphConfig = ma_node_graph_config_init(device.capture.channels); |
| 74 |
|
| 75 |
result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph); |
| 76 |
if (result != MA_SUCCESS) { |
| 77 |
printf("Failed to initialize node graph."); |
| 78 |
goto done0; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
vocoderNodeConfig = ma_vocoder_node_config_init(device.capture.channels, device.sampleRate); |
| 84 |
|
| 85 |
result = ma_vocoder_node_init(&g_nodeGraph, &vocoderNodeConfig, NULL, &g_vocoderNode); |
| 86 |
if (result != MA_SUCCESS) { |
| 87 |
printf("Failed to initialize vocoder node."); |
| 88 |
goto done1; |
| 89 |
} |
| 90 |
|
| 91 |
ma_node_attach_output_bus(&g_vocoderNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0); |
| 92 |
|
| 93 |
|
| 94 |
ma_node_set_output_bus_volume(&g_vocoderNode, 0, 4); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
waveformConfig = ma_waveform_config_init(device.capture.format, device.capture.channels, device.sampleRate, ma_waveform_type_sawtooth, 1.0, 50); |
| 99 |
|
| 100 |
result = ma_waveform_init(&waveformConfig, &g_sourceData); |
| 101 |
if (result != MA_SUCCESS) { |
| 102 |
printf("Failed to initialize waveform for excite node."); |
| 103 |
goto done3; |
| 104 |
} |
| 105 |
|
| 106 |
sourceNodeConfig = ma_data_source_node_config_init(&g_sourceData); |
| 107 |
|
| 108 |
result = ma_data_source_node_init(&g_nodeGraph, &sourceNodeConfig, NULL, &g_sourceNode); |
| 109 |
if (result != MA_SUCCESS) { |
| 110 |
printf("Failed to initialize excite node."); |
| 111 |
goto done3; |
| 112 |
} |
| 113 |
|
| 114 |
ma_node_attach_output_bus(&g_sourceNode, 0, &g_vocoderNode, 0); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
result = ma_audio_buffer_ref_init(device.capture.format, device.capture.channels, NULL, 0, &g_exciteData); |
| 119 |
if (result != MA_SUCCESS) { |
| 120 |
printf("Failed to initialize audio buffer for source."); |
| 121 |
goto done2; |
| 122 |
} |
| 123 |
|
| 124 |
exciteNodeConfig = ma_data_source_node_config_init(&g_exciteData); |
| 125 |
|
| 126 |
result = ma_data_source_node_init(&g_nodeGraph, &exciteNodeConfig, NULL, &g_exciteNode); |
| 127 |
if (result != MA_SUCCESS) { |
| 128 |
printf("Failed to initialize source node."); |
| 129 |
goto done2; |
| 130 |
} |
| 131 |
|
| 132 |
ma_node_attach_output_bus(&g_exciteNode, 0, &g_vocoderNode, 1); |
| 133 |
|
| 134 |
|
| 135 |
ma_device_start(&device); |
| 136 |
|
| 137 |
printf("Press Enter to quit...\n"); |
| 138 |
getchar(); |
| 139 |
|
| 140 |
|
| 141 |
ma_device_stop(&device); |
| 142 |
|
| 143 |
ma_data_source_node_uninit(&g_exciteNode, NULL); |
| 144 |
done3: ma_data_source_node_uninit(&g_sourceNode, NULL); |
| 145 |
done2: ma_vocoder_node_uninit(&g_vocoderNode, NULL); |
| 146 |
done1: ma_node_graph_uninit(&g_nodeGraph, NULL); |
| 147 |
done0: ma_device_uninit(&device); |
| 148 |
|
| 149 |
(void)argc; |
| 150 |
(void)argv; |
| 151 |
return 0; |
| 152 |
} |