Commit 73fdf848 authored by David Reid's avatar David Reid

Move external nodes out of the research folder.

parent 2671e075
#include "ma_channel_combiner_node.h"
MA_API ma_channel_combiner_node_config ma_channel_combiner_node_config_init(ma_uint32 channels)
{
ma_channel_combiner_node_config config;
MA_ZERO_OBJECT(&config);
config.nodeConfig = ma_node_config_init(); /* Input and output channels will be set in ma_channel_combiner_node_init(). */
config.channels = channels;
return config;
}
static void ma_channel_combiner_node_process_pcm_frames(ma_node* pNode, const float** ppFramesIn, ma_uint32* pFrameCountIn, float** ppFramesOut, ma_uint32* pFrameCountOut)
{
ma_channel_combiner_node* pCombinerNode = (ma_channel_combiner_node*)pNode;
(void)pFrameCountIn;
ma_interleave_pcm_frames(ma_format_f32, ma_node_get_output_channels(pCombinerNode, 0), *pFrameCountOut, (const void**)ppFramesIn, (void*)ppFramesOut[0]);
}
static ma_node_vtable g_ma_channel_combiner_node_vtable =
{
ma_channel_combiner_node_process_pcm_frames,
NULL,
MA_NODE_BUS_COUNT_UNKNOWN, /* Input bus count is determined by the channel count and is unknown until the node instance is initialized. */
1, /* 1 output bus. */
0 /* Default flags. */
};
MA_API ma_result ma_channel_combiner_node_init(ma_node_graph* pNodeGraph, const ma_channel_combiner_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_channel_combiner_node* pCombinerNode)
{
ma_result result;
ma_node_config baseConfig;
ma_uint32 inputChannels[MA_MAX_NODE_BUS_COUNT];
ma_uint32 outputChannels[1];
ma_uint32 iChannel;
if (pCombinerNode == NULL) {
return MA_INVALID_ARGS;
}
MA_ZERO_OBJECT(pCombinerNode);
if (pConfig == NULL) {
return MA_INVALID_ARGS;
}
/* All input channels are mono. */
for (iChannel = 0; iChannel < pConfig->channels; iChannel += 1) {
inputChannels[iChannel] = 1;
}
outputChannels[0] = pConfig->channels;
baseConfig = pConfig->nodeConfig;
baseConfig.vtable = &g_ma_channel_combiner_node_vtable;
baseConfig.inputBusCount = pConfig->channels; /* The vtable has an unknown channel count, so must specify it here. */
baseConfig.pInputChannels = inputChannels;
baseConfig.pOutputChannels = outputChannels;
result = ma_node_init(pNodeGraph, &baseConfig, pAllocationCallbacks, &pCombinerNode->baseNode);
if (result != MA_SUCCESS) {
return result;
}
return MA_SUCCESS;
}
MA_API void ma_channel_combiner_node_uninit(ma_channel_combiner_node* pCombinerNode, const ma_allocation_callbacks* pAllocationCallbacks)
{
/* The base node is always uninitialized first. */
ma_node_uninit(pCombinerNode, pAllocationCallbacks);
}
\ No newline at end of file
/* Include ma_reverb_node.h after miniaudio.h */
#ifndef ma_channel_combiner_node_h
#define ma_channel_combiner_node_h
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
ma_node_config nodeConfig;
ma_uint32 channels; /* The number of channels of the source, which will be the same as the output. Must be 1 or 2. The excite bus must always have one channel. */
} ma_channel_combiner_node_config;
MA_API ma_channel_combiner_node_config ma_channel_combiner_node_config_init(ma_uint32 channels);
typedef struct
{
ma_node_base baseNode;
} ma_channel_combiner_node;
MA_API ma_result ma_channel_combiner_node_init(ma_node_graph* pNodeGraph, const ma_channel_combiner_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_channel_combiner_node* pSeparatorNode);
MA_API void ma_channel_combiner_node_uninit(ma_channel_combiner_node* pSeparatorNode, const ma_allocation_callbacks* pAllocationCallbacks);
#ifdef __cplusplus
}
#endif
#endif /* ma_reverb_node_h */
/* The channel separtor example also demonstrates how to use the combiner. */
#include "../ma_channel_separator_node/ma_channel_separator_node_example.c"
#include "ma_channel_separator_node.h"
MA_API ma_channel_separator_node_config ma_channel_separator_node_config_init(ma_uint32 channels)
{
ma_channel_separator_node_config config;
MA_ZERO_OBJECT(&config);
config.nodeConfig = ma_node_config_init(); /* Input and output channels will be set in ma_channel_separator_node_init(). */
config.channels = channels;
return config;
}
static void ma_channel_separator_node_process_pcm_frames(ma_node* pNode, const float** ppFramesIn, ma_uint32* pFrameCountIn, float** ppFramesOut, ma_uint32* pFrameCountOut)
{
ma_channel_separator_node* pSplitterNode = (ma_channel_separator_node*)pNode;
(void)pFrameCountIn;
ma_deinterleave_pcm_frames(ma_format_f32, ma_node_get_input_channels(pSplitterNode, 0), *pFrameCountOut, (const void*)ppFramesIn[0], (void**)ppFramesOut);
}
static ma_node_vtable g_ma_channel_separator_node_vtable =
{
ma_channel_separator_node_process_pcm_frames,
NULL,
1, /* 1 input bus. */
MA_NODE_BUS_COUNT_UNKNOWN, /* Output bus count is determined by the channel count and is unknown until the node instance is initialized. */
0 /* Default flags. */
};
MA_API ma_result ma_channel_separator_node_init(ma_node_graph* pNodeGraph, const ma_channel_separator_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_channel_separator_node* pSeparatorNode)
{
ma_result result;
ma_node_config baseConfig;
ma_uint32 inputChannels[1];
ma_uint32 outputChannels[MA_MAX_NODE_BUS_COUNT];
ma_uint32 iChannel;
if (pSeparatorNode == NULL) {
return MA_INVALID_ARGS;
}
MA_ZERO_OBJECT(pSeparatorNode);
if (pConfig == NULL) {
return MA_INVALID_ARGS;
}
if (pConfig->channels > MA_MAX_NODE_BUS_COUNT) {
return MA_INVALID_ARGS; /* Channel count cannot exceed the maximum number of buses. */
}
inputChannels[0] = pConfig->channels;
/* All output channels are mono. */
for (iChannel = 0; iChannel < pConfig->channels; iChannel += 1) {
outputChannels[iChannel] = 1;
}
baseConfig = pConfig->nodeConfig;
baseConfig.vtable = &g_ma_channel_separator_node_vtable;
baseConfig.outputBusCount = pConfig->channels; /* The vtable has an unknown channel count, so must specify it here. */
baseConfig.pInputChannels = inputChannels;
baseConfig.pOutputChannels = outputChannels;
result = ma_node_init(pNodeGraph, &baseConfig, pAllocationCallbacks, &pSeparatorNode->baseNode);
if (result != MA_SUCCESS) {
return result;
}
return MA_SUCCESS;
}
MA_API void ma_channel_separator_node_uninit(ma_channel_separator_node* pSeparatorNode, const ma_allocation_callbacks* pAllocationCallbacks)
{
/* The base node is always uninitialized first. */
ma_node_uninit(pSeparatorNode, pAllocationCallbacks);
}
/* Include ma_reverb_node.h after miniaudio.h */
#ifndef ma_channel_separator_node_h
#define ma_channel_separator_node_h
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
ma_node_config nodeConfig;
ma_uint32 channels; /* The number of channels of the source, which will be the same as the output. Must be 1 or 2. The excite bus must always have one channel. */
} ma_channel_separator_node_config;
MA_API ma_channel_separator_node_config ma_channel_separator_node_config_init(ma_uint32 channels);
typedef struct
{
ma_node_base baseNode;
} ma_channel_separator_node;
MA_API ma_result ma_channel_separator_node_init(ma_node_graph* pNodeGraph, const ma_channel_separator_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_channel_separator_node* pSeparatorNode);
MA_API void ma_channel_separator_node_uninit(ma_channel_separator_node* pSeparatorNode, const ma_allocation_callbacks* pAllocationCallbacks);
#ifdef __cplusplus
}
#endif
#endif /* ma_reverb_node_h */
#define MINIAUDIO_IMPLEMENTATION
#include "../../../../miniaudio.h"
#include "../../../miniaudio_engine.h"
#include "ma_channel_separator_node.c"
#include "../ma_channel_combiner_node/ma_channel_combiner_node.c"
#include <stdio.h>
#define DEVICE_FORMAT ma_format_f32 /* Must always be f32 for this example because the node graph system only works with this. */
#define DEVICE_CHANNELS 0 /* The input file will determine the channel count. */
#define DEVICE_SAMPLE_RATE 48000
/*
In this example we're just separating out the channels with a `ma_channel_separator_node`, and then
combining them back together with a `ma_channel_combiner_node` before playing them back.
*/
static ma_decoder g_decoder; /* The decoder that we'll read data from. */
static ma_data_source_node g_dataSupplyNode; /* The node that will sit at the root level. Will be reading data from g_dataSupply. */
static ma_channel_separator_node g_separatorNode; /* The separator node. */
static ma_channel_combiner_node g_combinerNode; /* The combiner node. */
static ma_node_graph g_nodeGraph; /* The main node graph that we'll be feeding data through. */
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
(void)pInput;
(void)pDevice;
/* All we need to do is read from the node graph. */
ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
}
int main(int argc, char** argv)
{
ma_result result;
ma_decoder_config decoderConfig;
ma_device_config deviceConfig;
ma_device device;
ma_node_graph_config nodeGraphConfig;
ma_channel_separator_node_config separatorNodeConfig;
ma_channel_combiner_node_config combinerNodeConfig;
ma_data_source_node_config dataSupplyNodeConfig;
ma_uint32 iChannel;
if (argc < 1) {
printf("No input file.\n");
return -1;
}
/* Decoder. */
decoderConfig = ma_decoder_config_init(DEVICE_FORMAT, 0, DEVICE_SAMPLE_RATE);
result = ma_decoder_init_file(argv[1], &decoderConfig, &g_decoder);
if (result != MA_SUCCESS) {
printf("Failed to load decoder.\n");
return -1;
}
/* Device. */
deviceConfig = ma_device_config_init(ma_device_type_playback);
deviceConfig.playback.pDeviceID = NULL;
deviceConfig.playback.format = g_decoder.outputFormat;
deviceConfig.playback.channels = g_decoder.outputChannels;
deviceConfig.sampleRate = g_decoder.outputSampleRate;
deviceConfig.dataCallback = data_callback;
result = ma_device_init(NULL, &deviceConfig, &device);
if (result != MA_SUCCESS) {
return result;
}
/* Node graph. */
nodeGraphConfig = ma_node_graph_config_init(device.playback.channels);
result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
if (result != MA_SUCCESS) {
printf("Failed to initialize node graph.");
goto done0;
}
/* Combiner. Attached straight to the endpoint. Input will be the separator node. */
combinerNodeConfig = ma_channel_combiner_node_config_init(device.playback.channels);
result = ma_channel_combiner_node_init(&g_nodeGraph, &combinerNodeConfig, NULL, &g_combinerNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize channel combiner node.");
goto done1;
}
ma_node_attach_output_bus(&g_combinerNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
/*
Separator. Attached to the combiner. We need to attach each of the outputs of the
separator to each of the inputs of the combiner.
*/
separatorNodeConfig = ma_channel_separator_node_config_init(device.playback.channels);
result = ma_channel_separator_node_init(&g_nodeGraph, &separatorNodeConfig, NULL, &g_separatorNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize channel separator node.");
goto done2;
}
/* The separator and combiner must have the same number of output and input buses respectively. */
MA_ASSERT(ma_node_get_output_bus_count(&g_separatorNode) == ma_node_get_input_bus_count(&g_combinerNode));
/* Each of the separator's outputs need to be attached to the corresponding input of the combiner. */
for (iChannel = 0; iChannel < ma_node_get_output_bus_count(&g_separatorNode); iChannel += 1) {
ma_node_attach_output_bus(&g_separatorNode, iChannel, &g_combinerNode, iChannel);
}
/* Data supply. Attached to input bus 0 of the reverb node. */
dataSupplyNodeConfig = ma_data_source_node_config_init(&g_decoder, MA_FALSE);
result = ma_data_source_node_init(&g_nodeGraph, &dataSupplyNodeConfig, NULL, &g_dataSupplyNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize source node.");
goto done3;
}
ma_node_attach_output_bus(&g_dataSupplyNode, 0, &g_separatorNode, 0);
/* Now we just start the device and wait for the user to terminate the program. */
ma_device_start(&device);
printf("Press Enter to quit...\n");
getchar();
/* It's important that we stop the device first or else we'll uninitialize the graph from under the device. */
ma_device_stop(&device);
/*done4:*/ ma_data_source_node_uninit(&g_dataSupplyNode, NULL);
done3: ma_channel_separator_node_uninit(&g_separatorNode, NULL);
done2: ma_channel_combiner_node_uninit(&g_combinerNode, NULL);
done1: ma_node_graph_uninit(&g_nodeGraph, NULL);
done0: ma_device_uninit(&device);
(void)argc;
(void)argv;
return 0;
}
\ No newline at end of file
#define MINIAUDIO_IMPLEMENTATION
#include "../../../../miniaudio.h"
#include "../../../miniaudio_engine.h"
#include <stdio.h>
#define DEVICE_FORMAT ma_format_f32 /* Must always be f32 for this example because the node graph system only works with this. */
#define DEVICE_CHANNELS 2
#define DEVICE_SAMPLE_RATE 48000
static ma_audio_buffer_ref g_dataSupply; /* The underlying data source of the source node. */
static ma_data_source_node g_dataSupplyNode; /* The node that will sit at the root level. Will be reading data from g_dataSupply. */
static ma_delay_node g_delayNode; /* The delay node. */
static ma_node_graph g_nodeGraph; /* The main node graph that we'll be feeding data through. */
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
MA_ASSERT(pDevice->capture.format == pDevice->playback.format && pDevice->capture.format == ma_format_f32);
MA_ASSERT(pDevice->capture.channels == pDevice->playback.channels);
/*
The node graph system is a pulling style of API. At the lowest level of the chain will be a
node acting as a data source for the purpose of delivering the initial audio data. In our case,
the data source is our `pInput` buffer. We need to update the underlying data source so that it
read data from `pInput`.
*/
ma_audio_buffer_ref_set_data(&g_dataSupply, pInput, frameCount);
/* With the source buffer configured we can now read directly from the node graph. */
ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
}
int main(int argc, char** argv)
{
ma_result result;
ma_device_config deviceConfig;
ma_device device;
ma_node_graph_config nodeGraphConfig;
ma_delay_node_config delayNodeConfig;
ma_data_source_node_config dataSupplyNodeConfig;
deviceConfig = ma_device_config_init(ma_device_type_duplex);
deviceConfig.capture.pDeviceID = NULL;
deviceConfig.capture.format = DEVICE_FORMAT;
deviceConfig.capture.channels = DEVICE_CHANNELS;
deviceConfig.capture.shareMode = ma_share_mode_shared;
deviceConfig.playback.pDeviceID = NULL;
deviceConfig.playback.format = DEVICE_FORMAT;
deviceConfig.playback.channels = DEVICE_CHANNELS;
deviceConfig.dataCallback = data_callback;
result = ma_device_init(NULL, &deviceConfig, &device);
if (result != MA_SUCCESS) {
return result;
}
/* Node graph. */
nodeGraphConfig = ma_node_graph_config_init(device.capture.channels);
result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
if (result != MA_SUCCESS) {
printf("Failed to initialize node graph.");
goto done0;
}
/* Delay. Attached straight to the endpoint. */
delayNodeConfig = ma_delay_node_config_init(device.capture.channels, device.sampleRate, (100 * device.sampleRate) / 1000, 0.5f);
result = ma_delay_node_init(&g_nodeGraph, &delayNodeConfig, NULL, &g_delayNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize delay node.");
goto done1;
}
ma_node_attach_output_bus(&g_delayNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
/* Data supply. Attached to input bus 0 of the delay node. */
result = ma_audio_buffer_ref_init(device.capture.format, device.capture.channels, NULL, 0, &g_dataSupply);
if (result != MA_SUCCESS) {
printf("Failed to initialize audio buffer for source.");
goto done2;
}
dataSupplyNodeConfig = ma_data_source_node_config_init(&g_dataSupply, MA_FALSE);
result = ma_data_source_node_init(&g_nodeGraph, &dataSupplyNodeConfig, NULL, &g_dataSupplyNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize source node.");
goto done2;
}
ma_node_attach_output_bus(&g_dataSupplyNode, 0, &g_delayNode, 0);
ma_device_start(&device);
printf("Press Enter to quit...\n");
getchar();
/* It's important that we stop the device first or else we'll uninitialize the graph from under the device. */
ma_device_stop(&device);
/*done3:*/ ma_data_source_node_uninit(&g_dataSupplyNode, NULL);
done2: ma_delay_node_uninit(&g_delayNode, NULL);
done1: ma_node_graph_uninit(&g_nodeGraph, NULL);
done0: ma_device_uninit(&device);
(void)argc;
(void)argv;
return 0;
}
#define VERBLIB_IMPLEMENTATION
#include "ma_reverb_node.h"
MA_API ma_reverb_node_config ma_reverb_node_config_init(ma_uint32 channels, ma_uint32 sampleRate)
{
ma_reverb_node_config config;
MA_ZERO_OBJECT(&config);
config.nodeConfig = ma_node_config_init(); /* Input and output channels will be set in ma_reverb_node_init(). */
config.channels = channels;
config.sampleRate = sampleRate;
config.roomSize = verblib_initialroom;
config.damping = verblib_initialdamp;
config.width = verblib_initialwidth;
config.wetVolume = verblib_initialwet;
config.dryVolume = verblib_initialdry;
config.mode = verblib_initialmode;
return config;
}
static void ma_reverb_node_process_pcm_frames(ma_node* pNode, const float** ppFramesIn, ma_uint32* pFrameCountIn, float** ppFramesOut, ma_uint32* pFrameCountOut)
{
ma_reverb_node* pReverbNode = (ma_reverb_node*)pNode;
(void)pFrameCountIn;
verblib_process(&pReverbNode->reverb, ppFramesIn[0], ppFramesOut[0], *pFrameCountOut);
}
static ma_node_vtable g_ma_reverb_node_vtable =
{
ma_reverb_node_process_pcm_frames,
NULL,
1, /* 1 input channels. */
1, /* 1 output channel. */
MA_NODE_FLAG_CONTINUOUS_PROCESSING /* Reverb requires continuous processing to ensure the tail get's processed. */
};
MA_API ma_result ma_reverb_node_init(ma_node_graph* pNodeGraph, const ma_reverb_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_reverb_node* pReverbNode)
{
ma_result result;
ma_node_config baseConfig;
if (pReverbNode == NULL) {
return MA_INVALID_ARGS;
}
MA_ZERO_OBJECT(pReverbNode);
if (pConfig == NULL) {
return MA_INVALID_ARGS;
}
if (verblib_initialize(&pReverbNode->reverb, (unsigned long)pConfig->sampleRate, (unsigned int)pConfig->channels) == 0) {
return MA_INVALID_ARGS;
}
baseConfig = pConfig->nodeConfig;
baseConfig.vtable = &g_ma_reverb_node_vtable;
baseConfig.pInputChannels = &pConfig->channels;
baseConfig.pOutputChannels = &pConfig->channels;
result = ma_node_init(pNodeGraph, &baseConfig, pAllocationCallbacks, &pReverbNode->baseNode);
if (result != MA_SUCCESS) {
return result;
}
return MA_SUCCESS;
}
MA_API void ma_reverb_node_uninit(ma_reverb_node* pReverbNode, const ma_allocation_callbacks* pAllocationCallbacks)
{
/* The base node is always uninitialized first. */
ma_node_uninit(pReverbNode, pAllocationCallbacks);
}
/* Include ma_reverb_node.h after miniaudio.h */
#ifndef ma_reverb_node_h
#define ma_reverb_node_h
#include "verblib.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
The reverb node has one input and one output.
*/
typedef struct
{
ma_node_config nodeConfig;
ma_uint32 channels; /* The number of channels of the source, which will be the same as the output. Must be 1 or 2. The excite bus must always have one channel. */
ma_uint32 sampleRate;
float roomSize;
float damping;
float width;
float wetVolume;
float dryVolume;
float mode;
} ma_reverb_node_config;
MA_API ma_reverb_node_config ma_reverb_node_config_init(ma_uint32 channels, ma_uint32 sampleRate);
typedef struct
{
ma_node_base baseNode;
verblib reverb;
} ma_reverb_node;
MA_API ma_result ma_reverb_node_init(ma_node_graph* pNodeGraph, const ma_reverb_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_reverb_node* pReverbNode);
MA_API void ma_reverb_node_uninit(ma_reverb_node* pReverbNode, const ma_allocation_callbacks* pAllocationCallbacks);
#ifdef __cplusplus
}
#endif
#endif /* ma_reverb_node_h */
#define MINIAUDIO_IMPLEMENTATION
#include "../../../../miniaudio.h"
#include "../../../miniaudio_engine.h"
#include "ma_reverb_node.c"
#include <stdio.h>
#define DEVICE_FORMAT ma_format_f32 /* Must always be f32 for this example because the node graph system only works with this. */
#define DEVICE_CHANNELS 1 /* For this example, always set to 1. */
#define DEVICE_SAMPLE_RATE 48000 /* Cannot be less than 22050 for this example. */
static ma_audio_buffer_ref g_dataSupply; /* The underlying data source of the source node. */
static ma_data_source_node g_dataSupplyNode; /* The node that will sit at the root level. Will be reading data from g_dataSupply. */
static ma_reverb_node g_reverbNode; /* The reverb node. */
static ma_node_graph g_nodeGraph; /* The main node graph that we'll be feeding data through. */
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
MA_ASSERT(pDevice->capture.format == pDevice->playback.format && pDevice->capture.format == ma_format_f32);
MA_ASSERT(pDevice->capture.channels == pDevice->playback.channels);
/*
The node graph system is a pulling style of API. At the lowest level of the chain will be a
node acting as a data source for the purpose of delivering the initial audio data. In our case,
the data source is our `pInput` buffer. We need to update the underlying data source so that it
read data from `pInput`.
*/
ma_audio_buffer_ref_set_data(&g_dataSupply, pInput, frameCount);
/* With the source buffer configured we can now read directly from the node graph. */
ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
}
int main(int argc, char** argv)
{
ma_result result;
ma_device_config deviceConfig;
ma_device device;
ma_node_graph_config nodeGraphConfig;
ma_reverb_node_config reverbNodeConfig;
ma_data_source_node_config dataSupplyNodeConfig;
deviceConfig = ma_device_config_init(ma_device_type_duplex);
deviceConfig.capture.pDeviceID = NULL;
deviceConfig.capture.format = DEVICE_FORMAT;
deviceConfig.capture.channels = DEVICE_CHANNELS;
deviceConfig.capture.shareMode = ma_share_mode_shared;
deviceConfig.playback.pDeviceID = NULL;
deviceConfig.playback.format = DEVICE_FORMAT;
deviceConfig.playback.channels = DEVICE_CHANNELS;
deviceConfig.sampleRate = DEVICE_SAMPLE_RATE;
deviceConfig.dataCallback = data_callback;
result = ma_device_init(NULL, &deviceConfig, &device);
if (result != MA_SUCCESS) {
return result;
}
/* Node graph. */
nodeGraphConfig = ma_node_graph_config_init(device.capture.channels);
result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
if (result != MA_SUCCESS) {
printf("Failed to initialize node graph.");
goto done0;
}
/* Reverb. Attached straight to the endpoint. */
reverbNodeConfig = ma_reverb_node_config_init(device.capture.channels, device.sampleRate);
result = ma_reverb_node_init(&g_nodeGraph, &reverbNodeConfig, NULL, &g_reverbNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize reverb node.");
goto done1;
}
ma_node_attach_output_bus(&g_reverbNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
/* Data supply. Attached to input bus 0 of the reverb node. */
result = ma_audio_buffer_ref_init(device.capture.format, device.capture.channels, NULL, 0, &g_dataSupply);
if (result != MA_SUCCESS) {
printf("Failed to initialize audio buffer for source.");
goto done2;
}
dataSupplyNodeConfig = ma_data_source_node_config_init(&g_dataSupply, MA_FALSE);
result = ma_data_source_node_init(&g_nodeGraph, &dataSupplyNodeConfig, NULL, &g_dataSupplyNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize source node.");
goto done2;
}
ma_node_attach_output_bus(&g_dataSupplyNode, 0, &g_reverbNode, 0);
/* Now we just start the device and wait for the user to terminate the program. */
ma_device_start(&device);
printf("Press Enter to quit...\n");
getchar();
/* It's important that we stop the device first or else we'll uninitialize the graph from under the device. */
ma_device_stop(&device);
/*done3:*/ ma_data_source_node_uninit(&g_dataSupplyNode, NULL);
done2: ma_reverb_node_uninit(&g_reverbNode, NULL);
done1: ma_node_graph_uninit(&g_nodeGraph, NULL);
done0: ma_device_uninit(&device);
(void)argc;
(void)argv;
return 0;
}
\ No newline at end of file
This diff is collapsed.
#define VOCLIB_IMPLEMENTATION
#include "ma_vocoder_node.h"
MA_API ma_vocoder_node_config ma_vocoder_node_config_init(ma_uint32 channels, ma_uint32 sampleRate)
{
ma_vocoder_node_config config;
MA_ZERO_OBJECT(&config);
config.nodeConfig = ma_node_config_init(); /* Input and output channels will be set in ma_vocoder_node_init(). */
config.channels = channels;
config.sampleRate = sampleRate;
config.bands = 16;
config.filtersPerBand = 6;
return config;
}
static void ma_vocoder_node_process_pcm_frames(ma_node* pNode, const float** ppFramesIn, ma_uint32* pFrameCountIn, float** ppFramesOut, ma_uint32* pFrameCountOut)
{
ma_vocoder_node* pVocoderNode = (ma_vocoder_node*)pNode;
(void)pFrameCountIn;
voclib_process(&pVocoderNode->voclib, ppFramesIn[0], ppFramesIn[1], ppFramesOut[0], *pFrameCountOut);
}
static ma_node_vtable g_ma_vocoder_node_vtable =
{
ma_vocoder_node_process_pcm_frames,
NULL,
2, /* 2 input channels. */
1, /* 1 output channel. */
0
};
MA_API ma_result ma_vocoder_node_init(ma_node_graph* pNodeGraph, const ma_vocoder_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_vocoder_node* pVocoderNode)
{
ma_result result;
ma_node_config baseConfig;
ma_uint32 inputChannels[2];
ma_uint32 outputChannels[1];
if (pVocoderNode == NULL) {
return MA_INVALID_ARGS;
}
MA_ZERO_OBJECT(pVocoderNode);
if (pConfig == NULL) {
return MA_INVALID_ARGS;
}
if (voclib_initialize(&pVocoderNode->voclib, (unsigned char)pConfig->bands, (unsigned char)pConfig->filtersPerBand, (unsigned int)pConfig->sampleRate, (unsigned char)pConfig->channels) == 0) {
return MA_INVALID_ARGS;
}
inputChannels [0] = pConfig->channels; /* Source/carrier. */
inputChannels [1] = 1; /* Excite/modulator. Must always be single channel. */
outputChannels[0] = pConfig->channels; /* Output channels is always the same as the source/carrier. */
baseConfig = pConfig->nodeConfig;
baseConfig.vtable = &g_ma_vocoder_node_vtable;
baseConfig.pInputChannels = inputChannels;
baseConfig.pOutputChannels = outputChannels;
result = ma_node_init(pNodeGraph, &baseConfig, pAllocationCallbacks, &pVocoderNode->baseNode);
if (result != MA_SUCCESS) {
return result;
}
return MA_SUCCESS;
}
MA_API void ma_vocoder_node_uninit(ma_vocoder_node* pVocoderNode, const ma_allocation_callbacks* pAllocationCallbacks)
{
/* The base node must always be initialized first. */
ma_node_uninit(pVocoderNode, pAllocationCallbacks);
}
/* Include ma_vocoder_node.h after miniaudio.h */
#ifndef ma_vocoder_node_h
#define ma_vocoder_node_h
#include "voclib.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
The vocoder node has two inputs and one output. Inputs:
Input Bus 0: The source/carrier stream.
Input Bus 1: The excite/modulator stream.
The source (input bus 0) and output must have the same channel count, and is restricted to 1 or 2.
The excite (input bus 1) is restricted to 1 channel.
*/
typedef struct
{
ma_node_config nodeConfig;
ma_uint32 channels; /* The number of channels of the source, which will be the same as the output. Must be 1 or 2. The excite bus must always have one channel. */
ma_uint32 sampleRate;
ma_uint32 bands; /* Defaults to 16. */
ma_uint32 filtersPerBand; /* Defaults to 6. */
} ma_vocoder_node_config;
MA_API ma_vocoder_node_config ma_vocoder_node_config_init(ma_uint32 channels, ma_uint32 sampleRate);
typedef struct
{
ma_node_base baseNode;
voclib_instance voclib;
} ma_vocoder_node;
MA_API ma_result ma_vocoder_node_init(ma_node_graph* pNodeGraph, const ma_vocoder_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_vocoder_node* pVocoderNode);
MA_API void ma_vocoder_node_uninit(ma_vocoder_node* pVocoderNode, const ma_allocation_callbacks* pAllocationCallbacks);
#ifdef __cplusplus
}
#endif
#endif /* ma_vocoder_node_h */
/*
Demonstrates how to apply an effect to a duplex stream using the node graph system.
This example applies a vocoder effect to the input stream before outputting it. A custom node
called `ma_vocoder_node` is used to achieve the effect which can be found in the extras folder in
the miniaudio repository. The vocoder node uses https://github.com/blastbay/voclib to achieve the
effect.
*/
#define MINIAUDIO_IMPLEMENTATION
#include "../../../../miniaudio.h"
#include "../../../miniaudio_engine.h"
#include "ma_vocoder_node.c"
#include <stdio.h>
#define DEVICE_FORMAT ma_format_f32 /* Must always be f32 for this example because the node graph system only works with this. */
#define DEVICE_CHANNELS 1 /* For this example, always set to 1. */
static ma_waveform g_sourceData; /* The underlying data source of the excite node. */
static ma_audio_buffer_ref g_exciteData; /* The underlying data source of the source node. */
static ma_data_source_node g_sourceNode; /* A data source node containing the source data we'll be sending through to the vocoder. This will be routed into the first bus of the vocoder node. */
static ma_data_source_node g_exciteNode; /* A data source node containing the excite data we'll be sending through to the vocoder. This will be routed into the second bus of the vocoder node. */
static ma_vocoder_node g_vocoderNode; /* The vocoder node. */
static ma_node_graph g_nodeGraph;
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
MA_ASSERT(pDevice->capture.format == pDevice->playback.format);
MA_ASSERT(pDevice->capture.channels == pDevice->playback.channels);
/*
The node graph system is a pulling style of API. At the lowest level of the chain will be a
node acting as a data source for the purpose of delivering the initial audio data. In our case,
the data source is our `pInput` buffer. We need to update the underlying data source so that it
read data from `pInput`.
*/
ma_audio_buffer_ref_set_data(&g_exciteData, pInput, frameCount);
/* With the source buffer configured we can now read directly from the node graph. */
ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
}
int main(int argc, char** argv)
{
ma_result result;
ma_device_config deviceConfig;
ma_device device;
ma_node_graph_config nodeGraphConfig;
ma_vocoder_node_config vocoderNodeConfig;
ma_data_source_node_config sourceNodeConfig;
ma_data_source_node_config exciteNodeConfig;
ma_waveform_config waveformConfig;
deviceConfig = ma_device_config_init(ma_device_type_duplex);
deviceConfig.capture.pDeviceID = NULL;
deviceConfig.capture.format = DEVICE_FORMAT;
deviceConfig.capture.channels = DEVICE_CHANNELS;
deviceConfig.capture.shareMode = ma_share_mode_shared;
deviceConfig.playback.pDeviceID = NULL;
deviceConfig.playback.format = DEVICE_FORMAT;
deviceConfig.playback.channels = DEVICE_CHANNELS;
deviceConfig.dataCallback = data_callback;
result = ma_device_init(NULL, &deviceConfig, &device);
if (result != MA_SUCCESS) {
return result;
}
/* Now we can setup our node graph. */
nodeGraphConfig = ma_node_graph_config_init(device.capture.channels);
result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
if (result != MA_SUCCESS) {
printf("Failed to initialize node graph.");
goto done0;
}
/* Vocoder. Attached straight to the endpoint. */
vocoderNodeConfig = ma_vocoder_node_config_init(device.capture.channels, device.sampleRate);
result = ma_vocoder_node_init(&g_nodeGraph, &vocoderNodeConfig, NULL, &g_vocoderNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize vocoder node.");
goto done1;
}
ma_node_attach_output_bus(&g_vocoderNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
/* Amplify the volume of the vocoder output because in my testing it is a bit quiet. */
ma_node_set_output_bus_volume(&g_vocoderNode, 0, 4);
/* Source/carrier. Attached to input bus 0 of the vocoder node. */
waveformConfig = ma_waveform_config_init(device.capture.format, device.capture.channels, device.sampleRate, ma_waveform_type_sawtooth, 1.0, 50);
result = ma_waveform_init(&waveformConfig, &g_sourceData);
if (result != MA_SUCCESS) {
printf("Failed to initialize waveform for excite node.");
goto done3;
}
sourceNodeConfig = ma_data_source_node_config_init(&g_sourceData, MA_FALSE);
result = ma_data_source_node_init(&g_nodeGraph, &sourceNodeConfig, NULL, &g_sourceNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize excite node.");
goto done3;
}
ma_node_attach_output_bus(&g_sourceNode, 0, &g_vocoderNode, 0);
/* Excite/modulator. Attached to input bus 1 of the vocoder node. */
result = ma_audio_buffer_ref_init(device.capture.format, device.capture.channels, NULL, 0, &g_exciteData);
if (result != MA_SUCCESS) {
printf("Failed to initialize audio buffer for source.");
goto done2;
}
exciteNodeConfig = ma_data_source_node_config_init(&g_exciteData, MA_FALSE);
result = ma_data_source_node_init(&g_nodeGraph, &exciteNodeConfig, NULL, &g_exciteNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize source node.");
goto done2;
}
ma_node_attach_output_bus(&g_exciteNode, 0, &g_vocoderNode, 1);
ma_device_start(&device);
printf("Press Enter to quit...\n");
getchar();
/* It's important that we stop the device first or else we'll uninitialize the graph from under the device. */
ma_device_stop(&device);
/*done4:*/ ma_data_source_node_uninit(&g_exciteNode, NULL);
done3: ma_data_source_node_uninit(&g_sourceNode, NULL);
done2: ma_vocoder_node_uninit(&g_vocoderNode, NULL);
done1: ma_node_graph_uninit(&g_nodeGraph, NULL);
done0: ma_device_uninit(&device);
(void)argc;
(void)argv;
return 0;
}
This diff is collapsed.
#include "../../../../extras/nodes/ma_channel_combiner_node/ma_channel_combiner_node.c"
#include "ma_channel_combiner_node.h" \ No newline at end of file
MA_API ma_channel_combiner_node_config ma_channel_combiner_node_config_init(ma_uint32 channels)
{
ma_channel_combiner_node_config config;
MA_ZERO_OBJECT(&config);
config.nodeConfig = ma_node_config_init(); /* Input and output channels will be set in ma_channel_combiner_node_init(). */
config.channels = channels;
return config;
}
static void ma_channel_combiner_node_process_pcm_frames(ma_node* pNode, const float** ppFramesIn, ma_uint32* pFrameCountIn, float** ppFramesOut, ma_uint32* pFrameCountOut)
{
ma_channel_combiner_node* pCombinerNode = (ma_channel_combiner_node*)pNode;
(void)pFrameCountIn;
ma_interleave_pcm_frames(ma_format_f32, ma_node_get_output_channels(pCombinerNode, 0), *pFrameCountOut, (const void**)ppFramesIn, (void*)ppFramesOut[0]);
}
static ma_node_vtable g_ma_channel_combiner_node_vtable =
{
ma_channel_combiner_node_process_pcm_frames,
NULL,
MA_NODE_BUS_COUNT_UNKNOWN, /* Input bus count is determined by the channel count and is unknown until the node instance is initialized. */
1, /* 1 output bus. */
0 /* Default flags. */
};
MA_API ma_result ma_channel_combiner_node_init(ma_node_graph* pNodeGraph, const ma_channel_combiner_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_channel_combiner_node* pCombinerNode)
{
ma_result result;
ma_node_config baseConfig;
ma_uint32 inputChannels[MA_MAX_NODE_BUS_COUNT];
ma_uint32 outputChannels[1];
ma_uint32 iChannel;
if (pCombinerNode == NULL) {
return MA_INVALID_ARGS;
}
MA_ZERO_OBJECT(pCombinerNode);
if (pConfig == NULL) {
return MA_INVALID_ARGS;
}
/* All input channels are mono. */
for (iChannel = 0; iChannel < pConfig->channels; iChannel += 1) {
inputChannels[iChannel] = 1;
}
outputChannels[0] = pConfig->channels;
baseConfig = pConfig->nodeConfig;
baseConfig.vtable = &g_ma_channel_combiner_node_vtable;
baseConfig.inputBusCount = pConfig->channels; /* The vtable has an unknown channel count, so must specify it here. */
baseConfig.pInputChannels = inputChannels;
baseConfig.pOutputChannels = outputChannels;
result = ma_node_init(pNodeGraph, &baseConfig, pAllocationCallbacks, &pCombinerNode->baseNode);
if (result != MA_SUCCESS) {
return result;
}
return MA_SUCCESS;
}
MA_API void ma_channel_combiner_node_uninit(ma_channel_combiner_node* pCombinerNode, const ma_allocation_callbacks* pAllocationCallbacks)
{
/* The base node is always uninitialized first. */
ma_node_uninit(pCombinerNode, pAllocationCallbacks);
}
\ No newline at end of file
/* Include ma_reverb_node.h after miniaudio.h */ #include "../../../../extras/nodes/ma_channel_combiner_node/ma_channel_combiner_node.h"
#ifndef ma_channel_combiner_node_h \ No newline at end of file
#define ma_channel_combiner_node_h
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
ma_node_config nodeConfig;
ma_uint32 channels; /* The number of channels of the source, which will be the same as the output. Must be 1 or 2. The excite bus must always have one channel. */
} ma_channel_combiner_node_config;
MA_API ma_channel_combiner_node_config ma_channel_combiner_node_config_init(ma_uint32 channels);
typedef struct
{
ma_node_base baseNode;
} ma_channel_combiner_node;
MA_API ma_result ma_channel_combiner_node_init(ma_node_graph* pNodeGraph, const ma_channel_combiner_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_channel_combiner_node* pSeparatorNode);
MA_API void ma_channel_combiner_node_uninit(ma_channel_combiner_node* pSeparatorNode, const ma_allocation_callbacks* pAllocationCallbacks);
#ifdef __cplusplus
}
#endif
#endif /* ma_reverb_node_h */
/* The channel separtor example also demonstrates how to use the combiner. */ #include "../../../../extras/nodes/ma_channel_combiner_node/ma_channel_combiner_node_example.c"
#include "../ma_channel_separator_node/ma_channel_separator_node_example.c"
#include "../../../../extras/nodes/ma_channel_separator_node/ma_channel_separator_node.c"
#include "ma_channel_separator_node.h" \ No newline at end of file
MA_API ma_channel_separator_node_config ma_channel_separator_node_config_init(ma_uint32 channels)
{
ma_channel_separator_node_config config;
MA_ZERO_OBJECT(&config);
config.nodeConfig = ma_node_config_init(); /* Input and output channels will be set in ma_channel_separator_node_init(). */
config.channels = channels;
return config;
}
static void ma_channel_separator_node_process_pcm_frames(ma_node* pNode, const float** ppFramesIn, ma_uint32* pFrameCountIn, float** ppFramesOut, ma_uint32* pFrameCountOut)
{
ma_channel_separator_node* pSplitterNode = (ma_channel_separator_node*)pNode;
(void)pFrameCountIn;
ma_deinterleave_pcm_frames(ma_format_f32, ma_node_get_input_channels(pSplitterNode, 0), *pFrameCountOut, (const void*)ppFramesIn[0], (void**)ppFramesOut);
}
static ma_node_vtable g_ma_channel_separator_node_vtable =
{
ma_channel_separator_node_process_pcm_frames,
NULL,
1, /* 1 input bus. */
MA_NODE_BUS_COUNT_UNKNOWN, /* Output bus count is determined by the channel count and is unknown until the node instance is initialized. */
0 /* Default flags. */
};
MA_API ma_result ma_channel_separator_node_init(ma_node_graph* pNodeGraph, const ma_channel_separator_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_channel_separator_node* pSeparatorNode)
{
ma_result result;
ma_node_config baseConfig;
ma_uint32 inputChannels[1];
ma_uint32 outputChannels[MA_MAX_NODE_BUS_COUNT];
ma_uint32 iChannel;
if (pSeparatorNode == NULL) {
return MA_INVALID_ARGS;
}
MA_ZERO_OBJECT(pSeparatorNode);
if (pConfig == NULL) {
return MA_INVALID_ARGS;
}
if (pConfig->channels > MA_MAX_NODE_BUS_COUNT) {
return MA_INVALID_ARGS; /* Channel count cannot exceed the maximum number of buses. */
}
inputChannels[0] = pConfig->channels;
/* All output channels are mono. */
for (iChannel = 0; iChannel < pConfig->channels; iChannel += 1) {
outputChannels[iChannel] = 1;
}
baseConfig = pConfig->nodeConfig;
baseConfig.vtable = &g_ma_channel_separator_node_vtable;
baseConfig.outputBusCount = pConfig->channels; /* The vtable has an unknown channel count, so must specify it here. */
baseConfig.pInputChannels = inputChannels;
baseConfig.pOutputChannels = outputChannels;
result = ma_node_init(pNodeGraph, &baseConfig, pAllocationCallbacks, &pSeparatorNode->baseNode);
if (result != MA_SUCCESS) {
return result;
}
return MA_SUCCESS;
}
MA_API void ma_channel_separator_node_uninit(ma_channel_separator_node* pSeparatorNode, const ma_allocation_callbacks* pAllocationCallbacks)
{
/* The base node is always uninitialized first. */
ma_node_uninit(pSeparatorNode, pAllocationCallbacks);
}
/* Include ma_reverb_node.h after miniaudio.h */ #include "../../../../extras/nodes/ma_channel_separator_node/ma_channel_separator_node.h"
#ifndef ma_channel_separator_node_h \ No newline at end of file
#define ma_channel_separator_node_h
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
ma_node_config nodeConfig;
ma_uint32 channels; /* The number of channels of the source, which will be the same as the output. Must be 1 or 2. The excite bus must always have one channel. */
} ma_channel_separator_node_config;
MA_API ma_channel_separator_node_config ma_channel_separator_node_config_init(ma_uint32 channels);
typedef struct
{
ma_node_base baseNode;
} ma_channel_separator_node;
MA_API ma_result ma_channel_separator_node_init(ma_node_graph* pNodeGraph, const ma_channel_separator_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_channel_separator_node* pSeparatorNode);
MA_API void ma_channel_separator_node_uninit(ma_channel_separator_node* pSeparatorNode, const ma_allocation_callbacks* pAllocationCallbacks);
#ifdef __cplusplus
}
#endif
#endif /* ma_reverb_node_h */
#define MINIAUDIO_IMPLEMENTATION #include "../../../../extras/nodes/ma_channel_separator_node/ma_channel_separator_node_example.c"
#include "../../../../miniaudio.h" \ No newline at end of file
#include "../../../miniaudio_engine.h"
#include "ma_channel_separator_node.c"
#include "../ma_channel_combiner_node/ma_channel_combiner_node.c"
#include <stdio.h>
#define DEVICE_FORMAT ma_format_f32 /* Must always be f32 for this example because the node graph system only works with this. */
#define DEVICE_CHANNELS 0 /* The input file will determine the channel count. */
#define DEVICE_SAMPLE_RATE 48000
/*
In this example we're just separating out the channels with a `ma_channel_separator_node`, and then
combining them back together with a `ma_channel_combiner_node` before playing them back.
*/
static ma_decoder g_decoder; /* The decoder that we'll read data from. */
static ma_data_source_node g_dataSupplyNode; /* The node that will sit at the root level. Will be reading data from g_dataSupply. */
static ma_channel_separator_node g_separatorNode; /* The separator node. */
static ma_channel_combiner_node g_combinerNode; /* The combiner node. */
static ma_node_graph g_nodeGraph; /* The main node graph that we'll be feeding data through. */
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
(void)pInput;
(void)pDevice;
/* All we need to do is read from the node graph. */
ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
}
int main(int argc, char** argv)
{
ma_result result;
ma_decoder_config decoderConfig;
ma_device_config deviceConfig;
ma_device device;
ma_node_graph_config nodeGraphConfig;
ma_channel_separator_node_config separatorNodeConfig;
ma_channel_combiner_node_config combinerNodeConfig;
ma_data_source_node_config dataSupplyNodeConfig;
ma_uint32 iChannel;
if (argc < 1) {
printf("No input file.\n");
return -1;
}
/* Decoder. */
decoderConfig = ma_decoder_config_init(DEVICE_FORMAT, 0, DEVICE_SAMPLE_RATE);
result = ma_decoder_init_file(argv[1], &decoderConfig, &g_decoder);
if (result != MA_SUCCESS) {
printf("Failed to load decoder.\n");
return -1;
}
/* Device. */
deviceConfig = ma_device_config_init(ma_device_type_playback);
deviceConfig.playback.pDeviceID = NULL;
deviceConfig.playback.format = g_decoder.outputFormat;
deviceConfig.playback.channels = g_decoder.outputChannels;
deviceConfig.sampleRate = g_decoder.outputSampleRate;
deviceConfig.dataCallback = data_callback;
result = ma_device_init(NULL, &deviceConfig, &device);
if (result != MA_SUCCESS) {
return result;
}
/* Node graph. */
nodeGraphConfig = ma_node_graph_config_init(device.playback.channels);
result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
if (result != MA_SUCCESS) {
printf("Failed to initialize node graph.");
goto done0;
}
/* Combiner. Attached straight to the endpoint. Input will be the separator node. */
combinerNodeConfig = ma_channel_combiner_node_config_init(device.playback.channels);
result = ma_channel_combiner_node_init(&g_nodeGraph, &combinerNodeConfig, NULL, &g_combinerNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize channel combiner node.");
goto done1;
}
ma_node_attach_output_bus(&g_combinerNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
/*
Separator. Attached to the combiner. We need to attach each of the outputs of the
separator to each of the inputs of the combiner.
*/
separatorNodeConfig = ma_channel_separator_node_config_init(device.playback.channels);
result = ma_channel_separator_node_init(&g_nodeGraph, &separatorNodeConfig, NULL, &g_separatorNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize channel separator node.");
goto done2;
}
/* The separator and combiner must have the same number of output and input buses respectively. */
MA_ASSERT(ma_node_get_output_bus_count(&g_separatorNode) == ma_node_get_input_bus_count(&g_combinerNode));
/* Each of the separator's outputs need to be attached to the corresponding input of the combiner. */
for (iChannel = 0; iChannel < ma_node_get_output_bus_count(&g_separatorNode); iChannel += 1) {
ma_node_attach_output_bus(&g_separatorNode, iChannel, &g_combinerNode, iChannel);
}
/* Data supply. Attached to input bus 0 of the reverb node. */
dataSupplyNodeConfig = ma_data_source_node_config_init(&g_decoder, MA_FALSE);
result = ma_data_source_node_init(&g_nodeGraph, &dataSupplyNodeConfig, NULL, &g_dataSupplyNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize source node.");
goto done3;
}
ma_node_attach_output_bus(&g_dataSupplyNode, 0, &g_separatorNode, 0);
/* Now we just start the device and wait for the user to terminate the program. */
ma_device_start(&device);
printf("Press Enter to quit...\n");
getchar();
/* It's important that we stop the device first or else we'll uninitialize the graph from under the device. */
ma_device_stop(&device);
/*done4:*/ ma_data_source_node_uninit(&g_dataSupplyNode, NULL);
done3: ma_channel_separator_node_uninit(&g_separatorNode, NULL);
done2: ma_channel_combiner_node_uninit(&g_combinerNode, NULL);
done1: ma_node_graph_uninit(&g_nodeGraph, NULL);
done0: ma_device_uninit(&device);
(void)argc;
(void)argv;
return 0;
}
\ No newline at end of file
#include "../../../../extras/nodes/ma_delay_node/ma_delay_node_example.c"
#define MINIAUDIO_IMPLEMENTATION \ No newline at end of file
#include "../../../../miniaudio.h"
#include "../../../miniaudio_engine.h"
#include <stdio.h>
#define DEVICE_FORMAT ma_format_f32 /* Must always be f32 for this example because the node graph system only works with this. */
#define DEVICE_CHANNELS 2
#define DEVICE_SAMPLE_RATE 48000
static ma_audio_buffer_ref g_dataSupply; /* The underlying data source of the source node. */
static ma_data_source_node g_dataSupplyNode; /* The node that will sit at the root level. Will be reading data from g_dataSupply. */
static ma_delay_node g_delayNode; /* The delay node. */
static ma_node_graph g_nodeGraph; /* The main node graph that we'll be feeding data through. */
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
MA_ASSERT(pDevice->capture.format == pDevice->playback.format && pDevice->capture.format == ma_format_f32);
MA_ASSERT(pDevice->capture.channels == pDevice->playback.channels);
/*
The node graph system is a pulling style of API. At the lowest level of the chain will be a
node acting as a data source for the purpose of delivering the initial audio data. In our case,
the data source is our `pInput` buffer. We need to update the underlying data source so that it
read data from `pInput`.
*/
ma_audio_buffer_ref_set_data(&g_dataSupply, pInput, frameCount);
/* With the source buffer configured we can now read directly from the node graph. */
ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
}
int main(int argc, char** argv)
{
ma_result result;
ma_device_config deviceConfig;
ma_device device;
ma_node_graph_config nodeGraphConfig;
ma_delay_node_config delayNodeConfig;
ma_data_source_node_config dataSupplyNodeConfig;
deviceConfig = ma_device_config_init(ma_device_type_duplex);
deviceConfig.capture.pDeviceID = NULL;
deviceConfig.capture.format = DEVICE_FORMAT;
deviceConfig.capture.channels = DEVICE_CHANNELS;
deviceConfig.capture.shareMode = ma_share_mode_shared;
deviceConfig.playback.pDeviceID = NULL;
deviceConfig.playback.format = DEVICE_FORMAT;
deviceConfig.playback.channels = DEVICE_CHANNELS;
deviceConfig.dataCallback = data_callback;
result = ma_device_init(NULL, &deviceConfig, &device);
if (result != MA_SUCCESS) {
return result;
}
/* Node graph. */
nodeGraphConfig = ma_node_graph_config_init(device.capture.channels);
result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
if (result != MA_SUCCESS) {
printf("Failed to initialize node graph.");
goto done0;
}
/* Delay. Attached straight to the endpoint. */
delayNodeConfig = ma_delay_node_config_init(device.capture.channels, device.sampleRate, (100 * device.sampleRate) / 1000, 0.5f);
result = ma_delay_node_init(&g_nodeGraph, &delayNodeConfig, NULL, &g_delayNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize delay node.");
goto done1;
}
ma_node_attach_output_bus(&g_delayNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
/* Data supply. Attached to input bus 0 of the delay node. */
result = ma_audio_buffer_ref_init(device.capture.format, device.capture.channels, NULL, 0, &g_dataSupply);
if (result != MA_SUCCESS) {
printf("Failed to initialize audio buffer for source.");
goto done2;
}
dataSupplyNodeConfig = ma_data_source_node_config_init(&g_dataSupply, MA_FALSE);
result = ma_data_source_node_init(&g_nodeGraph, &dataSupplyNodeConfig, NULL, &g_dataSupplyNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize source node.");
goto done2;
}
ma_node_attach_output_bus(&g_dataSupplyNode, 0, &g_delayNode, 0);
ma_device_start(&device);
printf("Press Enter to quit...\n");
getchar();
/* It's important that we stop the device first or else we'll uninitialize the graph from under the device. */
ma_device_stop(&device);
/*done3:*/ ma_data_source_node_uninit(&g_dataSupplyNode, NULL);
done2: ma_delay_node_uninit(&g_delayNode, NULL);
done1: ma_node_graph_uninit(&g_nodeGraph, NULL);
done0: ma_device_uninit(&device);
(void)argc;
(void)argv;
return 0;
}
#include "../../../../extras/nodes/ma_reverb_node/ma_reverb_node.c"
#define VERBLIB_IMPLEMENTATION \ No newline at end of file
#include "ma_reverb_node.h"
MA_API ma_reverb_node_config ma_reverb_node_config_init(ma_uint32 channels, ma_uint32 sampleRate)
{
ma_reverb_node_config config;
MA_ZERO_OBJECT(&config);
config.nodeConfig = ma_node_config_init(); /* Input and output channels will be set in ma_reverb_node_init(). */
config.channels = channels;
config.sampleRate = sampleRate;
config.roomSize = verblib_initialroom;
config.damping = verblib_initialdamp;
config.width = verblib_initialwidth;
config.wetVolume = verblib_initialwet;
config.dryVolume = verblib_initialdry;
config.mode = verblib_initialmode;
return config;
}
static void ma_reverb_node_process_pcm_frames(ma_node* pNode, const float** ppFramesIn, ma_uint32* pFrameCountIn, float** ppFramesOut, ma_uint32* pFrameCountOut)
{
ma_reverb_node* pReverbNode = (ma_reverb_node*)pNode;
(void)pFrameCountIn;
verblib_process(&pReverbNode->reverb, ppFramesIn[0], ppFramesOut[0], *pFrameCountOut);
}
static ma_node_vtable g_ma_reverb_node_vtable =
{
ma_reverb_node_process_pcm_frames,
NULL,
1, /* 1 input channels. */
1, /* 1 output channel. */
MA_NODE_FLAG_CONTINUOUS_PROCESSING /* Reverb requires continuous processing to ensure the tail get's processed. */
};
MA_API ma_result ma_reverb_node_init(ma_node_graph* pNodeGraph, const ma_reverb_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_reverb_node* pReverbNode)
{
ma_result result;
ma_node_config baseConfig;
if (pReverbNode == NULL) {
return MA_INVALID_ARGS;
}
MA_ZERO_OBJECT(pReverbNode);
if (pConfig == NULL) {
return MA_INVALID_ARGS;
}
if (verblib_initialize(&pReverbNode->reverb, (unsigned long)pConfig->sampleRate, (unsigned int)pConfig->channels) == 0) {
return MA_INVALID_ARGS;
}
baseConfig = pConfig->nodeConfig;
baseConfig.vtable = &g_ma_reverb_node_vtable;
baseConfig.pInputChannels = &pConfig->channels;
baseConfig.pOutputChannels = &pConfig->channels;
result = ma_node_init(pNodeGraph, &baseConfig, pAllocationCallbacks, &pReverbNode->baseNode);
if (result != MA_SUCCESS) {
return result;
}
return MA_SUCCESS;
}
MA_API void ma_reverb_node_uninit(ma_reverb_node* pReverbNode, const ma_allocation_callbacks* pAllocationCallbacks)
{
/* The base node is always uninitialized first. */
ma_node_uninit(pReverbNode, pAllocationCallbacks);
}
/* Include ma_reverb_node.h after miniaudio.h */ #include "../../../../extras/nodes/ma_reverb_node/ma_reverb_node.h"
#ifndef ma_reverb_node_h \ No newline at end of file
#define ma_reverb_node_h
#include "verblib.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
The reverb node has one input and one output.
*/
typedef struct
{
ma_node_config nodeConfig;
ma_uint32 channels; /* The number of channels of the source, which will be the same as the output. Must be 1 or 2. The excite bus must always have one channel. */
ma_uint32 sampleRate;
float roomSize;
float damping;
float width;
float wetVolume;
float dryVolume;
float mode;
} ma_reverb_node_config;
MA_API ma_reverb_node_config ma_reverb_node_config_init(ma_uint32 channels, ma_uint32 sampleRate);
typedef struct
{
ma_node_base baseNode;
verblib reverb;
} ma_reverb_node;
MA_API ma_result ma_reverb_node_init(ma_node_graph* pNodeGraph, const ma_reverb_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_reverb_node* pReverbNode);
MA_API void ma_reverb_node_uninit(ma_reverb_node* pReverbNode, const ma_allocation_callbacks* pAllocationCallbacks);
#ifdef __cplusplus
}
#endif
#endif /* ma_reverb_node_h */
#define MINIAUDIO_IMPLEMENTATION #include "../../../../extras/nodes/ma_reverb_node/ma_reverb_node_example.c"
#include "../../../../miniaudio.h" \ No newline at end of file
#include "../../../miniaudio_engine.h"
#include "ma_reverb_node.c"
#include <stdio.h>
#define DEVICE_FORMAT ma_format_f32 /* Must always be f32 for this example because the node graph system only works with this. */
#define DEVICE_CHANNELS 1 /* For this example, always set to 1. */
#define DEVICE_SAMPLE_RATE 48000 /* Cannot be less than 22050 for this example. */
static ma_audio_buffer_ref g_dataSupply; /* The underlying data source of the source node. */
static ma_data_source_node g_dataSupplyNode; /* The node that will sit at the root level. Will be reading data from g_dataSupply. */
static ma_reverb_node g_reverbNode; /* The reverb node. */
static ma_node_graph g_nodeGraph; /* The main node graph that we'll be feeding data through. */
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
MA_ASSERT(pDevice->capture.format == pDevice->playback.format && pDevice->capture.format == ma_format_f32);
MA_ASSERT(pDevice->capture.channels == pDevice->playback.channels);
/*
The node graph system is a pulling style of API. At the lowest level of the chain will be a
node acting as a data source for the purpose of delivering the initial audio data. In our case,
the data source is our `pInput` buffer. We need to update the underlying data source so that it
read data from `pInput`.
*/
ma_audio_buffer_ref_set_data(&g_dataSupply, pInput, frameCount);
/* With the source buffer configured we can now read directly from the node graph. */
ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
}
int main(int argc, char** argv)
{
ma_result result;
ma_device_config deviceConfig;
ma_device device;
ma_node_graph_config nodeGraphConfig;
ma_reverb_node_config reverbNodeConfig;
ma_data_source_node_config dataSupplyNodeConfig;
deviceConfig = ma_device_config_init(ma_device_type_duplex);
deviceConfig.capture.pDeviceID = NULL;
deviceConfig.capture.format = DEVICE_FORMAT;
deviceConfig.capture.channels = DEVICE_CHANNELS;
deviceConfig.capture.shareMode = ma_share_mode_shared;
deviceConfig.playback.pDeviceID = NULL;
deviceConfig.playback.format = DEVICE_FORMAT;
deviceConfig.playback.channels = DEVICE_CHANNELS;
deviceConfig.sampleRate = DEVICE_SAMPLE_RATE;
deviceConfig.dataCallback = data_callback;
result = ma_device_init(NULL, &deviceConfig, &device);
if (result != MA_SUCCESS) {
return result;
}
/* Node graph. */
nodeGraphConfig = ma_node_graph_config_init(device.capture.channels);
result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
if (result != MA_SUCCESS) {
printf("Failed to initialize node graph.");
goto done0;
}
/* Reverb. Attached straight to the endpoint. */
reverbNodeConfig = ma_reverb_node_config_init(device.capture.channels, device.sampleRate);
result = ma_reverb_node_init(&g_nodeGraph, &reverbNodeConfig, NULL, &g_reverbNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize reverb node.");
goto done1;
}
ma_node_attach_output_bus(&g_reverbNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
/* Data supply. Attached to input bus 0 of the reverb node. */
result = ma_audio_buffer_ref_init(device.capture.format, device.capture.channels, NULL, 0, &g_dataSupply);
if (result != MA_SUCCESS) {
printf("Failed to initialize audio buffer for source.");
goto done2;
}
dataSupplyNodeConfig = ma_data_source_node_config_init(&g_dataSupply, MA_FALSE);
result = ma_data_source_node_init(&g_nodeGraph, &dataSupplyNodeConfig, NULL, &g_dataSupplyNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize source node.");
goto done2;
}
ma_node_attach_output_bus(&g_dataSupplyNode, 0, &g_reverbNode, 0);
/* Now we just start the device and wait for the user to terminate the program. */
ma_device_start(&device);
printf("Press Enter to quit...\n");
getchar();
/* It's important that we stop the device first or else we'll uninitialize the graph from under the device. */
ma_device_stop(&device);
/*done3:*/ ma_data_source_node_uninit(&g_dataSupplyNode, NULL);
done2: ma_reverb_node_uninit(&g_reverbNode, NULL);
done1: ma_node_graph_uninit(&g_nodeGraph, NULL);
done0: ma_device_uninit(&device);
(void)argc;
(void)argv;
return 0;
}
\ No newline at end of file
#include "../../../../extras/nodes/ma_vocoder_node/ma_vocoder_node.c"
#define VOCLIB_IMPLEMENTATION \ No newline at end of file
#include "ma_vocoder_node.h"
MA_API ma_vocoder_node_config ma_vocoder_node_config_init(ma_uint32 channels, ma_uint32 sampleRate)
{
ma_vocoder_node_config config;
MA_ZERO_OBJECT(&config);
config.nodeConfig = ma_node_config_init(); /* Input and output channels will be set in ma_vocoder_node_init(). */
config.channels = channels;
config.sampleRate = sampleRate;
config.bands = 16;
config.filtersPerBand = 6;
return config;
}
static void ma_vocoder_node_process_pcm_frames(ma_node* pNode, const float** ppFramesIn, ma_uint32* pFrameCountIn, float** ppFramesOut, ma_uint32* pFrameCountOut)
{
ma_vocoder_node* pVocoderNode = (ma_vocoder_node*)pNode;
(void)pFrameCountIn;
voclib_process(&pVocoderNode->voclib, ppFramesIn[0], ppFramesIn[1], ppFramesOut[0], *pFrameCountOut);
}
static ma_node_vtable g_ma_vocoder_node_vtable =
{
ma_vocoder_node_process_pcm_frames,
NULL,
2, /* 2 input channels. */
1, /* 1 output channel. */
0
};
MA_API ma_result ma_vocoder_node_init(ma_node_graph* pNodeGraph, const ma_vocoder_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_vocoder_node* pVocoderNode)
{
ma_result result;
ma_node_config baseConfig;
ma_uint32 inputChannels[2];
ma_uint32 outputChannels[1];
if (pVocoderNode == NULL) {
return MA_INVALID_ARGS;
}
MA_ZERO_OBJECT(pVocoderNode);
if (pConfig == NULL) {
return MA_INVALID_ARGS;
}
if (voclib_initialize(&pVocoderNode->voclib, (unsigned char)pConfig->bands, (unsigned char)pConfig->filtersPerBand, (unsigned int)pConfig->sampleRate, (unsigned char)pConfig->channels) == 0) {
return MA_INVALID_ARGS;
}
inputChannels [0] = pConfig->channels; /* Source/carrier. */
inputChannels [1] = 1; /* Excite/modulator. Must always be single channel. */
outputChannels[0] = pConfig->channels; /* Output channels is always the same as the source/carrier. */
baseConfig = pConfig->nodeConfig;
baseConfig.vtable = &g_ma_vocoder_node_vtable;
baseConfig.pInputChannels = inputChannels;
baseConfig.pOutputChannels = outputChannels;
result = ma_node_init(pNodeGraph, &baseConfig, pAllocationCallbacks, &pVocoderNode->baseNode);
if (result != MA_SUCCESS) {
return result;
}
return MA_SUCCESS;
}
MA_API void ma_vocoder_node_uninit(ma_vocoder_node* pVocoderNode, const ma_allocation_callbacks* pAllocationCallbacks)
{
/* The base node must always be initialized first. */
ma_node_uninit(pVocoderNode, pAllocationCallbacks);
}
/* Include ma_vocoder_node.h after miniaudio.h */ #include "../../../../extras/nodes/ma_vocoder_node/ma_vocoder_node.h"
#ifndef ma_vocoder_node_h \ No newline at end of file
#define ma_vocoder_node_h
#include "voclib.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
The vocoder node has two inputs and one output. Inputs:
Input Bus 0: The source/carrier stream.
Input Bus 1: The excite/modulator stream.
The source (input bus 0) and output must have the same channel count, and is restricted to 1 or 2.
The excite (input bus 1) is restricted to 1 channel.
*/
typedef struct
{
ma_node_config nodeConfig;
ma_uint32 channels; /* The number of channels of the source, which will be the same as the output. Must be 1 or 2. The excite bus must always have one channel. */
ma_uint32 sampleRate;
ma_uint32 bands; /* Defaults to 16. */
ma_uint32 filtersPerBand; /* Defaults to 6. */
} ma_vocoder_node_config;
MA_API ma_vocoder_node_config ma_vocoder_node_config_init(ma_uint32 channels, ma_uint32 sampleRate);
typedef struct
{
ma_node_base baseNode;
voclib_instance voclib;
} ma_vocoder_node;
MA_API ma_result ma_vocoder_node_init(ma_node_graph* pNodeGraph, const ma_vocoder_node_config* pConfig, const ma_allocation_callbacks* pAllocationCallbacks, ma_vocoder_node* pVocoderNode);
MA_API void ma_vocoder_node_uninit(ma_vocoder_node* pVocoderNode, const ma_allocation_callbacks* pAllocationCallbacks);
#ifdef __cplusplus
}
#endif
#endif /* ma_vocoder_node_h */
/* #include "../../../../extras/nodes/ma_vocoder_node/ma_vocoder_node_example.c"
Demonstrates how to apply an effect to a duplex stream using the node graph system. \ No newline at end of file
This example applies a vocoder effect to the input stream before outputting it. A custom node
called `ma_vocoder_node` is used to achieve the effect which can be found in the extras folder in
the miniaudio repository. The vocoder node uses https://github.com/blastbay/voclib to achieve the
effect.
*/
#define MINIAUDIO_IMPLEMENTATION
#include "../../../../miniaudio.h"
#include "../../../miniaudio_engine.h"
#include "ma_vocoder_node.c"
#include <stdio.h>
#define DEVICE_FORMAT ma_format_f32 /* Must always be f32 for this example because the node graph system only works with this. */
#define DEVICE_CHANNELS 1 /* For this example, always set to 1. */
static ma_waveform g_sourceData; /* The underlying data source of the excite node. */
static ma_audio_buffer_ref g_exciteData; /* The underlying data source of the source node. */
static ma_data_source_node g_sourceNode; /* A data source node containing the source data we'll be sending through to the vocoder. This will be routed into the first bus of the vocoder node. */
static ma_data_source_node g_exciteNode; /* A data source node containing the excite data we'll be sending through to the vocoder. This will be routed into the second bus of the vocoder node. */
static ma_vocoder_node g_vocoderNode; /* The vocoder node. */
static ma_node_graph g_nodeGraph;
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
{
MA_ASSERT(pDevice->capture.format == pDevice->playback.format);
MA_ASSERT(pDevice->capture.channels == pDevice->playback.channels);
/*
The node graph system is a pulling style of API. At the lowest level of the chain will be a
node acting as a data source for the purpose of delivering the initial audio data. In our case,
the data source is our `pInput` buffer. We need to update the underlying data source so that it
read data from `pInput`.
*/
ma_audio_buffer_ref_set_data(&g_exciteData, pInput, frameCount);
/* With the source buffer configured we can now read directly from the node graph. */
ma_node_graph_read_pcm_frames(&g_nodeGraph, pOutput, frameCount, NULL);
}
int main(int argc, char** argv)
{
ma_result result;
ma_device_config deviceConfig;
ma_device device;
ma_node_graph_config nodeGraphConfig;
ma_vocoder_node_config vocoderNodeConfig;
ma_data_source_node_config sourceNodeConfig;
ma_data_source_node_config exciteNodeConfig;
ma_waveform_config waveformConfig;
deviceConfig = ma_device_config_init(ma_device_type_duplex);
deviceConfig.capture.pDeviceID = NULL;
deviceConfig.capture.format = DEVICE_FORMAT;
deviceConfig.capture.channels = DEVICE_CHANNELS;
deviceConfig.capture.shareMode = ma_share_mode_shared;
deviceConfig.playback.pDeviceID = NULL;
deviceConfig.playback.format = DEVICE_FORMAT;
deviceConfig.playback.channels = DEVICE_CHANNELS;
deviceConfig.dataCallback = data_callback;
result = ma_device_init(NULL, &deviceConfig, &device);
if (result != MA_SUCCESS) {
return result;
}
/* Now we can setup our node graph. */
nodeGraphConfig = ma_node_graph_config_init(device.capture.channels);
result = ma_node_graph_init(&nodeGraphConfig, NULL, &g_nodeGraph);
if (result != MA_SUCCESS) {
printf("Failed to initialize node graph.");
goto done0;
}
/* Vocoder. Attached straight to the endpoint. */
vocoderNodeConfig = ma_vocoder_node_config_init(device.capture.channels, device.sampleRate);
result = ma_vocoder_node_init(&g_nodeGraph, &vocoderNodeConfig, NULL, &g_vocoderNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize vocoder node.");
goto done1;
}
ma_node_attach_output_bus(&g_vocoderNode, 0, ma_node_graph_get_endpoint(&g_nodeGraph), 0);
/* Amplify the volume of the vocoder output because in my testing it is a bit quiet. */
ma_node_set_output_bus_volume(&g_vocoderNode, 0, 4);
/* Source/carrier. Attached to input bus 0 of the vocoder node. */
waveformConfig = ma_waveform_config_init(device.capture.format, device.capture.channels, device.sampleRate, ma_waveform_type_sawtooth, 1.0, 50);
result = ma_waveform_init(&waveformConfig, &g_sourceData);
if (result != MA_SUCCESS) {
printf("Failed to initialize waveform for excite node.");
goto done3;
}
sourceNodeConfig = ma_data_source_node_config_init(&g_sourceData, MA_FALSE);
result = ma_data_source_node_init(&g_nodeGraph, &sourceNodeConfig, NULL, &g_sourceNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize excite node.");
goto done3;
}
ma_node_attach_output_bus(&g_sourceNode, 0, &g_vocoderNode, 0);
/* Excite/modulator. Attached to input bus 1 of the vocoder node. */
result = ma_audio_buffer_ref_init(device.capture.format, device.capture.channels, NULL, 0, &g_exciteData);
if (result != MA_SUCCESS) {
printf("Failed to initialize audio buffer for source.");
goto done2;
}
exciteNodeConfig = ma_data_source_node_config_init(&g_exciteData, MA_FALSE);
result = ma_data_source_node_init(&g_nodeGraph, &exciteNodeConfig, NULL, &g_exciteNode);
if (result != MA_SUCCESS) {
printf("Failed to initialize source node.");
goto done2;
}
ma_node_attach_output_bus(&g_exciteNode, 0, &g_vocoderNode, 1);
ma_device_start(&device);
printf("Press Enter to quit...\n");
getchar();
/* It's important that we stop the device first or else we'll uninitialize the graph from under the device. */
ma_device_stop(&device);
/*done4:*/ ma_data_source_node_uninit(&g_exciteNode, NULL);
done3: ma_data_source_node_uninit(&g_sourceNode, NULL);
done2: ma_vocoder_node_uninit(&g_vocoderNode, NULL);
done1: ma_node_graph_uninit(&g_nodeGraph, NULL);
done0: ma_device_uninit(&device);
(void)argc;
(void)argv;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment