Add syntax highlighting to documentation.

This commit is contained in:
David Reid
2020-07-16 21:32:01 +10:00
parent aba471bbe9
commit ba57831c18
10 changed files with 469 additions and 469 deletions
+53 -53
View File
@@ -262,58 +262,58 @@ around as user data for the device (device.pUserData).
This example only works for output devices, but can be implemented for input devices by simply swapping the direction
of data movement.</p>
<div style="font-family:monospace; border:solid 1px #003800; border-left:solid 0.5em #003800; margin:1em 0em;"><pre style="margin:0.5em 1em; padding:0; line-height:125%">
#define MINIAUDIO_IMPLEMENTATION
#include &quot;../miniaudio.h&quot;
<span style="color:#666666">#define</span> MINIAUDIO_IMPLEMENTATION
<span style="color:#666666">#include</span> <span style="color:#cc3300">&quot;../miniaudio.h&quot;</span>
#include &lt;stdio.h&gt;
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;stdio.h&gt;</span>
#define DEVICE_FORMAT ma_format_f32
#define DEVICE_CHANNELS 1
#define DEVICE_SAMPLE_RATE 48000
<span style="color:#666666">#define</span> DEVICE_FORMAT ma_format_f32
<span style="color:#666666">#define</span> DEVICE_CHANNELS 1
<span style="color:#666666">#define</span> DEVICE_SAMPLE_RATE 48000
#define PCM_FRAME_CHUNK_SIZE 1234 /* &lt;-- Play around with this to control your fixed sized buffer. */
<span style="color:#666666">#define</span> PCM_FRAME_CHUNK_SIZE 1234 <span style="color:#009900">/* &lt;-- Play around with this to control your fixed sized buffer. */</span>
ma_waveform g_sineWave;
ma_pcm_rb g_rb; /* The ring buffer. */
<span style="color:#0099cc">ma_waveform</span> g_sineWave;
<span style="color:#0099cc">ma_pcm_rb</span> g_rb; <span style="color:#009900">/* The ring buffer. */</span>
void data_callback_fixed(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
<span style="color:#0033ff">void</span> data_callback_fixed(<span style="color:#0099cc">ma_device</span>* pDevice, <span style="color:#0033ff">void</span>* pOutput, <span style="color:#0033ff">const</span> <span style="color:#0033ff">void</span>* pInput, <span style="color:#0099cc">ma_uint32</span> frameCount)
{
/*
<span style="color:#009900">/*
This callback will have a guaranteed and consistent size for frameCount. In this example we just fill the output buffer with a sine wave. This
is where you would handle the callback just like normal, only now you can assume frameCount is a fixed size.
*/
printf(&quot;frameCount=%d\n&quot;, frameCount);
*/</span>
printf(<span style="color:#cc3300">&quot;frameCount=%d\n&quot;</span>, frameCount);
ma_waveform_read_pcm_frames(&amp;g_sineWave, pOutput, frameCount);
/* Unused in this example. */
(void)pDevice;
(void)pInput;
<span style="color:#009900">/* Unused in this example. */</span>
(<span style="color:#0033ff">void</span>)pDevice;
(<span style="color:#0033ff">void</span>)pInput;
}
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
<span style="color:#0033ff">void</span> data_callback(<span style="color:#0099cc">ma_device</span>* pDevice, <span style="color:#0033ff">void</span>* pOutput, <span style="color:#0033ff">const</span> <span style="color:#0033ff">void</span>* pInput, <span style="color:#0099cc">ma_uint32</span> frameCount)
{
/*
<span style="color:#009900">/*
This is the device&#39;s main data callback. This will handle all of the fixed sized buffer management for you and will call data_callback_fixed()
for you. You should do all of your normal callback stuff in data_callback_fixed().
*/
ma_uint32 pcmFramesAvailableInRB;
ma_uint32 pcmFramesProcessed = 0;
ma_uint8* pRunningOutput = (ma_uint8*)pOutput;
*/</span>
<span style="color:#0099cc">ma_uint32</span> pcmFramesAvailableInRB;
<span style="color:#0099cc">ma_uint32</span> pcmFramesProcessed = 0;
<span style="color:#0099cc">ma_uint8</span>* pRunningOutput = (<span style="color:#0099cc">ma_uint8</span>*)pOutput;
MA_ASSERT(pDevice-&gt;playback.channels == DEVICE_CHANNELS);
/*
<span style="color:#009900">/*
The first thing to do is check if there&#39;s enough data available in the ring buffer. If so we can read from it. Otherwise we need to keep filling
the ring buffer until there&#39;s enough, making sure we only fill the ring buffer in chunks of PCM_FRAME_CHUNK_SIZE.
*/
while (pcmFramesProcessed &lt; frameCount) { /* Keep going until we&#39;ve filled the output buffer. */
ma_uint32 framesRemaining = frameCount - pcmFramesProcessed;
*/</span>
<span style="color:#0033ff">while</span> (pcmFramesProcessed &lt; frameCount) { <span style="color:#009900">/* Keep going until we&#39;ve filled the output buffer. */</span>
<span style="color:#0099cc">ma_uint32</span> framesRemaining = frameCount - pcmFramesProcessed;
pcmFramesAvailableInRB = ma_pcm_rb_available_read(&amp;g_rb);
if (pcmFramesAvailableInRB &gt; 0) {
ma_uint32 framesToRead = (framesRemaining &lt; pcmFramesAvailableInRB) ? framesRemaining : pcmFramesAvailableInRB;
void* pReadBuffer;
<span style="color:#0033ff">if</span> (pcmFramesAvailableInRB &gt; 0) {
<span style="color:#0099cc">ma_uint32</span> framesToRead = (framesRemaining &lt; pcmFramesAvailableInRB) ? framesRemaining : pcmFramesAvailableInRB;
<span style="color:#0033ff">void</span>* pReadBuffer;
ma_pcm_rb_acquire_read(&amp;g_rb, &amp;framesToRead, &amp;pReadBuffer);
{
@@ -323,35 +323,35 @@ void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uin
pRunningOutput += framesToRead * ma_get_bytes_per_frame(pDevice-&gt;playback.format, pDevice-&gt;playback.channels);
pcmFramesProcessed += framesToRead;
} else {
/*
} <span style="color:#0033ff">else</span> {
<span style="color:#009900">/*
There&#39;s nothing in the buffer. Fill it with more data from the callback. We reset the buffer first so that the read and write pointers
are reset back to the start so we can fill the ring buffer in chunks of PCM_FRAME_CHUNK_SIZE which is what we initialized it with. Note
that this is not how you would want to do it in a multi-threaded environment. In this case you would want to seek the write pointer
forward via the producer thread and the read pointer forward via the consumer thread (this thread).
*/
ma_uint32 framesToWrite = PCM_FRAME_CHUNK_SIZE;
void* pWriteBuffer;
*/</span>
<span style="color:#0099cc">ma_uint32</span> framesToWrite = PCM_FRAME_CHUNK_SIZE;
<span style="color:#0033ff">void</span>* pWriteBuffer;
ma_pcm_rb_reset(&amp;g_rb);
ma_pcm_rb_acquire_write(&amp;g_rb, &amp;framesToWrite, &amp;pWriteBuffer);
{
MA_ASSERT(framesToWrite == PCM_FRAME_CHUNK_SIZE); /* &lt;-- This should always work in this example because we just reset the ring buffer. */
MA_ASSERT(framesToWrite == PCM_FRAME_CHUNK_SIZE); <span style="color:#009900">/* &lt;-- This should always work in this example because we just reset the ring buffer. */</span>
data_callback_fixed(pDevice, pWriteBuffer, NULL, framesToWrite);
}
ma_pcm_rb_commit_write(&amp;g_rb, framesToWrite, pWriteBuffer);
}
}
/* Unused in this example. */
(void)pInput;
<span style="color:#009900">/* Unused in this example. */</span>
(<span style="color:#0033ff">void</span>)pInput;
}
int main(int argc, char** argv)
<span style="color:#0033ff">int</span> main(<span style="color:#0033ff">int</span> argc, <span style="color:#0033ff">char</span>** argv)
{
ma_waveform_config waveformConfig;
ma_device_config deviceConfig;
ma_device device;
<span style="color:#0099cc">ma_waveform_config</span> waveformConfig;
<span style="color:#0099cc">ma_device_config</span> deviceConfig;
<span style="color:#0099cc">ma_device</span> device;
waveformConfig = ma_waveform_config_init(DEVICE_FORMAT, DEVICE_CHANNELS, DEVICE_SAMPLE_RATE, ma_waveform_type_sine, 0.1, 220);
ma_waveform_init(&amp;waveformConfig, &amp;g_sineWave);
@@ -363,32 +363,32 @@ int main(int argc, char** argv)
deviceConfig.playback.channels = DEVICE_CHANNELS;
deviceConfig.sampleRate = DEVICE_SAMPLE_RATE;
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = NULL; /* &lt;-- Set this to a pointer to the ring buffer if you don&#39;t want it in global scope. */
deviceConfig.pUserData = NULL; <span style="color:#009900">/* &lt;-- Set this to a pointer to the ring buffer if you don&#39;t want it in global scope. */</span>
if (ma_device_init(NULL, &amp;deviceConfig, &amp;device) != MA_SUCCESS) {
printf(&quot;Failed to open playback device.\n&quot;);
<span style="color:#0033ff">if</span> (ma_device_init(NULL, &amp;deviceConfig, &amp;device) != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to open playback device.\n&quot;</span>);
ma_pcm_rb_uninit(&amp;g_rb);
return -4;
<span style="color:#0033ff">return</span> -4;
}
printf(&quot;Device Name: %s\n&quot;, device.playback.name);
printf(<span style="color:#cc3300">&quot;Device Name: %s\n&quot;</span>, device.playback.name);
if (ma_device_start(&amp;device) != MA_SUCCESS) {
printf(&quot;Failed to start playback device.\n&quot;);
<span style="color:#0033ff">if</span> (ma_device_start(&amp;device) != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to start playback device.\n&quot;</span>);
ma_pcm_rb_uninit(&amp;g_rb);
ma_device_uninit(&amp;device);
return -5;
<span style="color:#0033ff">return</span> -5;
}
printf(&quot;Press Enter to quit...\n&quot;);
printf(<span style="color:#cc3300">&quot;Press Enter to quit...\n&quot;</span>);
getchar();
ma_device_uninit(&amp;device);
ma_pcm_rb_uninit(&amp;g_rb);
(void)argc;
(void)argv;
return 0;
(<span style="color:#0033ff">void</span>)argc;
(<span style="color:#0033ff">void</span>)argv;
<span style="color:#0033ff">return</span> 0;
}
</pre></div></td>
</tr></table>
+27 -27
View File
@@ -255,40 +255,40 @@ Capturing works in a very similar way to playback. The only difference is the di
the application sending data to the device, the device will send data to the application. This example just writes the
data received by the microphone straight to a WAV file.</p>
<div style="font-family:monospace; border:solid 1px #003800; border-left:solid 0.5em #003800; margin:1em 0em;"><pre style="margin:0.5em 1em; padding:0; line-height:125%">
#define MINIAUDIO_IMPLEMENTATION
#include &quot;../miniaudio.h&quot;
<span style="color:#666666">#define</span> MINIAUDIO_IMPLEMENTATION
<span style="color:#666666">#include</span> <span style="color:#cc3300">&quot;../miniaudio.h&quot;</span>
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;stdlib.h&gt;</span>
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;stdio.h&gt;</span>
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
<span style="color:#0033ff">void</span> data_callback(<span style="color:#0099cc">ma_device</span>* pDevice, <span style="color:#0033ff">void</span>* pOutput, <span style="color:#0033ff">const</span> <span style="color:#0033ff">void</span>* pInput, <span style="color:#0099cc">ma_uint32</span> frameCount)
{
ma_encoder* pEncoder = (ma_encoder*)pDevice-&gt;pUserData;
<span style="color:#0099cc">ma_encoder</span>* pEncoder = (<span style="color:#0099cc">ma_encoder</span>*)pDevice-&gt;pUserData;
MA_ASSERT(pEncoder != NULL);
ma_encoder_write_pcm_frames(pEncoder, pInput, frameCount);
(void)pOutput;
(<span style="color:#0033ff">void</span>)pOutput;
}
int main(int argc, char** argv)
<span style="color:#0033ff">int</span> main(<span style="color:#0033ff">int</span> argc, <span style="color:#0033ff">char</span>** argv)
{
ma_result result;
ma_encoder_config encoderConfig;
ma_encoder encoder;
ma_device_config deviceConfig;
ma_device device;
<span style="color:#0099cc">ma_result</span> result;
<span style="color:#0099cc">ma_encoder_config</span> encoderConfig;
<span style="color:#0099cc">ma_encoder</span> encoder;
<span style="color:#0099cc">ma_device_config</span> deviceConfig;
<span style="color:#0099cc">ma_device</span> device;
if (argc &lt; 2) {
printf(&quot;No input file.\n&quot;);
return -1;
<span style="color:#0033ff">if</span> (argc &lt; 2) {
printf(<span style="color:#cc3300">&quot;No input file.\n&quot;</span>);
<span style="color:#0033ff">return</span> -1;
}
encoderConfig = ma_encoder_config_init(ma_resource_format_wav, ma_format_f32, 2, 44100);
if (ma_encoder_init_file(argv[1], &amp;encoderConfig, &amp;encoder) != MA_SUCCESS) {
printf(&quot;Failed to initialize output file.\n&quot;);
return -1;
<span style="color:#0033ff">if</span> (ma_encoder_init_file(argv[1], &amp;encoderConfig, &amp;encoder) != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to initialize output file.\n&quot;</span>);
<span style="color:#0033ff">return</span> -1;
}
deviceConfig = ma_device_config_init(ma_device_type_capture);
@@ -299,25 +299,25 @@ int main(int argc, char** argv)
deviceConfig.pUserData = &amp;encoder;
result = ma_device_init(NULL, &amp;deviceConfig, &amp;device);
if (result != MA_SUCCESS) {
printf(&quot;Failed to initialize capture device.\n&quot;);
return -2;
<span style="color:#0033ff">if</span> (result != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to initialize capture device.\n&quot;</span>);
<span style="color:#0033ff">return</span> -2;
}
result = ma_device_start(&amp;device);
if (result != MA_SUCCESS) {
<span style="color:#0033ff">if</span> (result != MA_SUCCESS) {
ma_device_uninit(&amp;device);
printf(&quot;Failed to start device.\n&quot;);
return -3;
printf(<span style="color:#cc3300">&quot;Failed to start device.\n&quot;</span>);
<span style="color:#0033ff">return</span> -3;
}
printf(&quot;Press Enter to stop recording...\n&quot;);
printf(<span style="color:#cc3300">&quot;Press Enter to stop recording...\n&quot;</span>);
getchar();
ma_device_uninit(&amp;device);
ma_encoder_uninit(&amp;encoder);
return 0;
<span style="color:#0033ff">return</span> 0;
}
</pre></div></td>
</tr></table>
+23 -23
View File
@@ -257,31 +257,31 @@ glitching which the backend may not be able to recover from. For this reason, mi
sample rate for both capture and playback. If internally the native sample rates differ, miniaudio will perform the
sample rate conversion for you automatically.</p>
<div style="font-family:monospace; border:solid 1px #003800; border-left:solid 0.5em #003800; margin:1em 0em;"><pre style="margin:0.5em 1em; padding:0; line-height:125%">
#define MINIAUDIO_IMPLEMENTATION
#include &quot;../miniaudio.h&quot;
<span style="color:#666666">#define</span> MINIAUDIO_IMPLEMENTATION
<span style="color:#666666">#include</span> <span style="color:#cc3300">&quot;../miniaudio.h&quot;</span>
#include &lt;stdio.h&gt;
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;stdio.h&gt;</span>
#ifdef __EMSCRIPTEN__
void main_loop__em()
<span style="color:#666666">#ifdef</span> __EMSCRIPTEN__
<span style="color:#0033ff">void</span> main_loop__em()
{
}
#endif
<span style="color:#666666">#endif</span>
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
<span style="color:#0033ff">void</span> data_callback(<span style="color:#0099cc">ma_device</span>* pDevice, <span style="color:#0033ff">void</span>* pOutput, <span style="color:#0033ff">const</span> <span style="color:#0033ff">void</span>* pInput, <span style="color:#0099cc">ma_uint32</span> frameCount)
{
MA_ASSERT(pDevice-&gt;capture.format == pDevice-&gt;playback.format);
MA_ASSERT(pDevice-&gt;capture.channels == pDevice-&gt;playback.channels);
/* In this example the format and channel count are the same for both input and output which means we can just memcpy(). */
<span style="color:#009900">/* In this example the format and channel count are the same for both input and output which means we can just memcpy(). */</span>
MA_COPY_MEMORY(pOutput, pInput, frameCount * ma_get_bytes_per_frame(pDevice-&gt;capture.format, pDevice-&gt;capture.channels));
}
int main(int argc, char** argv)
<span style="color:#0033ff">int</span> main(<span style="color:#0033ff">int</span> argc, <span style="color:#0033ff">char</span>** argv)
{
ma_result result;
ma_device_config deviceConfig;
ma_device device;
<span style="color:#0099cc">ma_result</span> result;
<span style="color:#0099cc">ma_device_config</span> deviceConfig;
<span style="color:#0099cc">ma_device</span> device;
deviceConfig = ma_device_config_init(ma_device_type_duplex);
deviceConfig.capture.pDeviceID = NULL;
@@ -293,28 +293,28 @@ int main(int argc, char** argv)
deviceConfig.playback.channels = 2;
deviceConfig.dataCallback = data_callback;
result = ma_device_init(NULL, &amp;deviceConfig, &amp;device);
if (result != MA_SUCCESS) {
return result;
<span style="color:#0033ff">if</span> (result != MA_SUCCESS) {
<span style="color:#0033ff">return</span> result;
}
#ifdef __EMSCRIPTEN__
<span style="color:#666666">#ifdef</span> __EMSCRIPTEN__
getchar();
#endif
<span style="color:#666666">#endif</span>
ma_device_start(&amp;device);
#ifdef __EMSCRIPTEN__
<span style="color:#666666">#ifdef</span> __EMSCRIPTEN__
emscripten_set_main_loop(main_loop__em, 0, 1);
#else
printf(&quot;Press Enter to quit...\n&quot;);
<span style="color:#666666">#else</span>
printf(<span style="color:#cc3300">&quot;Press Enter to quit...\n&quot;</span>);
getchar();
#endif
<span style="color:#666666">#endif</span>
ma_device_uninit(&amp;device);
(void)argc;
(void)argv;
return 0;
(<span style="color:#0033ff">void</span>)argc;
(<span style="color:#0033ff">void</span>)argv;
<span style="color:#0033ff">return</span> 0;
}
</pre></div></td>
</tr></table>
+27 -27
View File
@@ -254,50 +254,50 @@ context sits above a device. You can have many devices to one context.
If you use device enumeration, you should explicitly specify the same context you used for enumeration in the call to
<span style="font-family:monospace;">ma_device_init()</span> when you initialize your devices.</p>
<div style="font-family:monospace; border:solid 1px #003800; border-left:solid 0.5em #003800; margin:1em 0em;"><pre style="margin:0.5em 1em; padding:0; line-height:125%">
#define MINIAUDIO_IMPLEMENTATION
#include &quot;../miniaudio.h&quot;
<span style="color:#666666">#define</span> MINIAUDIO_IMPLEMENTATION
<span style="color:#666666">#include</span> <span style="color:#cc3300">&quot;../miniaudio.h&quot;</span>
#include &lt;stdio.h&gt;
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;stdio.h&gt;</span>
int main(int argc, char** argv)
<span style="color:#0033ff">int</span> main(<span style="color:#0033ff">int</span> argc, <span style="color:#0033ff">char</span>** argv)
{
ma_result result;
ma_context context;
ma_device_info* pPlaybackDeviceInfos;
ma_uint32 playbackDeviceCount;
ma_device_info* pCaptureDeviceInfos;
ma_uint32 captureDeviceCount;
ma_uint32 iDevice;
<span style="color:#0099cc">ma_result</span> result;
<span style="color:#0099cc">ma_context</span> context;
<span style="color:#0099cc">ma_device_info</span>* pPlaybackDeviceInfos;
<span style="color:#0099cc">ma_uint32</span> playbackDeviceCount;
<span style="color:#0099cc">ma_device_info</span>* pCaptureDeviceInfos;
<span style="color:#0099cc">ma_uint32</span> captureDeviceCount;
<span style="color:#0099cc">ma_uint32</span> iDevice;
if (ma_context_init(NULL, 0, NULL, &amp;context) != MA_SUCCESS) {
printf(&quot;Failed to initialize context.\n&quot;);
return -2;
<span style="color:#0033ff">if</span> (ma_context_init(NULL, 0, NULL, &amp;context) != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to initialize context.\n&quot;</span>);
<span style="color:#0033ff">return</span> -2;
}
result = ma_context_get_devices(&amp;context, &amp;pPlaybackDeviceInfos, &amp;playbackDeviceCount, &amp;pCaptureDeviceInfos, &amp;captureDeviceCount);
if (result != MA_SUCCESS) {
printf(&quot;Failed to retrieve device information.\n&quot;);
return -3;
<span style="color:#0033ff">if</span> (result != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to retrieve device information.\n&quot;</span>);
<span style="color:#0033ff">return</span> -3;
}
printf(&quot;Playback Devices\n&quot;);
for (iDevice = 0; iDevice &lt; playbackDeviceCount; ++iDevice) {
printf(&quot; %u: %s\n&quot;, iDevice, pPlaybackDeviceInfos[iDevice].name);
printf(<span style="color:#cc3300">&quot;Playback Devices\n&quot;</span>);
<span style="color:#0033ff">for</span> (iDevice = 0; iDevice &lt; playbackDeviceCount; ++iDevice) {
printf(<span style="color:#cc3300">&quot; %u: %s\n&quot;</span>, iDevice, pPlaybackDeviceInfos[iDevice].name);
}
printf(&quot;\n&quot;);
printf(<span style="color:#cc3300">&quot;\n&quot;</span>);
printf(&quot;Capture Devices\n&quot;);
for (iDevice = 0; iDevice &lt; captureDeviceCount; ++iDevice) {
printf(&quot; %u: %s\n&quot;, iDevice, pCaptureDeviceInfos[iDevice].name);
printf(<span style="color:#cc3300">&quot;Capture Devices\n&quot;</span>);
<span style="color:#0033ff">for</span> (iDevice = 0; iDevice &lt; captureDeviceCount; ++iDevice) {
printf(<span style="color:#cc3300">&quot; %u: %s\n&quot;</span>, iDevice, pCaptureDeviceInfos[iDevice].name);
}
ma_context_uninit(&amp;context);
(void)argc;
(void)argv;
return 0;
(<span style="color:#0033ff">void</span>)argc;
(<span style="color:#0033ff">void</span>)argv;
<span style="color:#0033ff">return</span> 0;
}
</pre></div></td>
</tr></table>
+30 -30
View File
@@ -259,75 +259,75 @@ used indirectly with PulseAudio by choosing the appropriate loopback device afte
To use loopback mode you just need to set the device type to ma_device_type_loopback and set the capture device config
properties. The output buffer in the callback will be null whereas the input buffer will be valid.</p>
<div style="font-family:monospace; border:solid 1px #003800; border-left:solid 0.5em #003800; margin:1em 0em;"><pre style="margin:0.5em 1em; padding:0; line-height:125%">
#define MINIAUDIO_IMPLEMENTATION
#include &quot;../miniaudio.h&quot;
<span style="color:#666666">#define</span> MINIAUDIO_IMPLEMENTATION
<span style="color:#666666">#include</span> <span style="color:#cc3300">&quot;../miniaudio.h&quot;</span>
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;stdlib.h&gt;</span>
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;stdio.h&gt;</span>
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
<span style="color:#0033ff">void</span> data_callback(<span style="color:#0099cc">ma_device</span>* pDevice, <span style="color:#0033ff">void</span>* pOutput, <span style="color:#0033ff">const</span> <span style="color:#0033ff">void</span>* pInput, <span style="color:#0099cc">ma_uint32</span> frameCount)
{
ma_encoder* pEncoder = (ma_encoder*)pDevice-&gt;pUserData;
<span style="color:#0099cc">ma_encoder</span>* pEncoder = (<span style="color:#0099cc">ma_encoder</span>*)pDevice-&gt;pUserData;
MA_ASSERT(pEncoder != NULL);
ma_encoder_write_pcm_frames(pEncoder, pInput, frameCount);
(void)pOutput;
(<span style="color:#0033ff">void</span>)pOutput;
}
int main(int argc, char** argv)
<span style="color:#0033ff">int</span> main(<span style="color:#0033ff">int</span> argc, <span style="color:#0033ff">char</span>** argv)
{
ma_result result;
ma_encoder_config encoderConfig;
ma_encoder encoder;
ma_device_config deviceConfig;
ma_device device;
<span style="color:#0099cc">ma_result</span> result;
<span style="color:#0099cc">ma_encoder_config</span> encoderConfig;
<span style="color:#0099cc">ma_encoder</span> encoder;
<span style="color:#0099cc">ma_device_config</span> deviceConfig;
<span style="color:#0099cc">ma_device</span> device;
/* Loopback mode is currently only supported on WASAPI. */
<span style="color:#009900">/* Loopback mode is currently only supported on WASAPI. */</span>
ma_backend backends[] = {
ma_backend_wasapi
};
if (argc &lt; 2) {
printf(&quot;No input file.\n&quot;);
return -1;
<span style="color:#0033ff">if</span> (argc &lt; 2) {
printf(<span style="color:#cc3300">&quot;No input file.\n&quot;</span>);
<span style="color:#0033ff">return</span> -1;
}
encoderConfig = ma_encoder_config_init(ma_resource_format_wav, ma_format_f32, 2, 44100);
if (ma_encoder_init_file(argv[1], &amp;encoderConfig, &amp;encoder) != MA_SUCCESS) {
printf(&quot;Failed to initialize output file.\n&quot;);
return -1;
<span style="color:#0033ff">if</span> (ma_encoder_init_file(argv[1], &amp;encoderConfig, &amp;encoder) != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to initialize output file.\n&quot;</span>);
<span style="color:#0033ff">return</span> -1;
}
deviceConfig = ma_device_config_init(ma_device_type_loopback);
deviceConfig.capture.pDeviceID = NULL; /* Use default device for this example. Set this to the ID of a _playback_ device if you want to capture from a specific device. */
deviceConfig.capture.pDeviceID = NULL; <span style="color:#009900">/* Use default device for this example. Set this to the ID of a _playback_ device if you want to capture from a specific device. */</span>
deviceConfig.capture.format = encoder.config.format;
deviceConfig.capture.channels = encoder.config.channels;
deviceConfig.sampleRate = encoder.config.sampleRate;
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = &amp;encoder;
result = ma_device_init_ex(backends, sizeof(backends)/sizeof(backends[0]), NULL, &amp;deviceConfig, &amp;device);
if (result != MA_SUCCESS) {
printf(&quot;Failed to initialize loopback device.\n&quot;);
return -2;
result = ma_device_init_ex(backends, <span style="color:#0033ff">sizeof</span>(backends)/<span style="color:#0033ff">sizeof</span>(backends[0]), NULL, &amp;deviceConfig, &amp;device);
<span style="color:#0033ff">if</span> (result != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to initialize loopback device.\n&quot;</span>);
<span style="color:#0033ff">return</span> -2;
}
result = ma_device_start(&amp;device);
if (result != MA_SUCCESS) {
<span style="color:#0033ff">if</span> (result != MA_SUCCESS) {
ma_device_uninit(&amp;device);
printf(&quot;Failed to start device.\n&quot;);
return -3;
printf(<span style="color:#cc3300">&quot;Failed to start device.\n&quot;</span>);
<span style="color:#0033ff">return</span> -3;
}
printf(&quot;Press Enter to stop recording...\n&quot;);
printf(<span style="color:#cc3300">&quot;Press Enter to stop recording...\n&quot;</span>);
getchar();
ma_device_uninit(&amp;device);
ma_encoder_uninit(&amp;encoder);
return 0;
<span style="color:#0033ff">return</span> 0;
}
</pre></div></td>
</tr></table>
+29 -29
View File
@@ -250,44 +250,44 @@ This example uses a decoder as the data source. Decoders can be used with the <s
supports looping via the <span style="font-family:monospace;">ma_data_source_read_pcm_frames()</span> API. To use it, all you need to do is pass a pointer to the
decoder straight into <span style="font-family:monospace;">ma_data_source_read_pcm_frames()</span> and it will just work.</p>
<div style="font-family:monospace; border:solid 1px #003800; border-left:solid 0.5em #003800; margin:1em 0em;"><pre style="margin:0.5em 1em; padding:0; line-height:125%">
#define MINIAUDIO_IMPLEMENTATION
#include &quot;../miniaudio.h&quot;
<span style="color:#666666">#define</span> MINIAUDIO_IMPLEMENTATION
<span style="color:#666666">#include</span> <span style="color:#cc3300">&quot;../miniaudio.h&quot;</span>
#include &lt;stdio.h&gt;
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;stdio.h&gt;</span>
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
<span style="color:#0033ff">void</span> data_callback(<span style="color:#0099cc">ma_device</span>* pDevice, <span style="color:#0033ff">void</span>* pOutput, <span style="color:#0033ff">const</span> <span style="color:#0033ff">void</span>* pInput, <span style="color:#0099cc">ma_uint32</span> frameCount)
{
ma_bool32 isLooping = MA_TRUE;
<span style="color:#0099cc">ma_bool32</span> isLooping = MA_TRUE;
ma_decoder* pDecoder = (ma_decoder*)pDevice-&gt;pUserData;
if (pDecoder == NULL) {
return;
<span style="color:#0099cc">ma_decoder</span>* pDecoder = (<span style="color:#0099cc">ma_decoder</span>*)pDevice-&gt;pUserData;
<span style="color:#0033ff">if</span> (pDecoder == NULL) {
<span style="color:#0033ff">return</span>;
}
/*
<span style="color:#009900">/*
A decoder is a data source which means you can seemlessly plug it into the ma_data_source API. We can therefore take advantage
of the &quot;loop&quot; parameter of ma_data_source_read_pcm_frames() to handle looping for us.
*/
*/</span>
ma_data_source_read_pcm_frames(pDecoder, pOutput, frameCount, NULL, isLooping);
(void)pInput;
(<span style="color:#0033ff">void</span>)pInput;
}
int main(int argc, char** argv)
<span style="color:#0033ff">int</span> main(<span style="color:#0033ff">int</span> argc, <span style="color:#0033ff">char</span>** argv)
{
ma_result result;
ma_decoder decoder;
ma_device_config deviceConfig;
ma_device device;
<span style="color:#0099cc">ma_result</span> result;
<span style="color:#0099cc">ma_decoder</span> decoder;
<span style="color:#0099cc">ma_device_config</span> deviceConfig;
<span style="color:#0099cc">ma_device</span> device;
if (argc &lt; 2) {
printf(&quot;No input file.\n&quot;);
return -1;
<span style="color:#0033ff">if</span> (argc &lt; 2) {
printf(<span style="color:#cc3300">&quot;No input file.\n&quot;</span>);
<span style="color:#0033ff">return</span> -1;
}
result = ma_decoder_init_file(argv[1], NULL, &amp;decoder);
if (result != MA_SUCCESS) {
return -2;
<span style="color:#0033ff">if</span> (result != MA_SUCCESS) {
<span style="color:#0033ff">return</span> -2;
}
deviceConfig = ma_device_config_init(ma_device_type_playback);
@@ -297,26 +297,26 @@ int main(int argc, char** argv)
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = &amp;decoder;
if (ma_device_init(NULL, &amp;deviceConfig, &amp;device) != MA_SUCCESS) {
printf(&quot;Failed to open playback device.\n&quot;);
<span style="color:#0033ff">if</span> (ma_device_init(NULL, &amp;deviceConfig, &amp;device) != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to open playback device.\n&quot;</span>);
ma_decoder_uninit(&amp;decoder);
return -3;
<span style="color:#0033ff">return</span> -3;
}
if (ma_device_start(&amp;device) != MA_SUCCESS) {
printf(&quot;Failed to start playback device.\n&quot;);
<span style="color:#0033ff">if</span> (ma_device_start(&amp;device) != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to start playback device.\n&quot;</span>);
ma_device_uninit(&amp;device);
ma_decoder_uninit(&amp;decoder);
return -4;
<span style="color:#0033ff">return</span> -4;
}
printf(&quot;Press Enter to quit...&quot;);
printf(<span style="color:#cc3300">&quot;Press Enter to quit...&quot;</span>);
getchar();
ma_device_uninit(&amp;device);
ma_decoder_uninit(&amp;decoder);
return 0;
<span style="color:#0033ff">return</span> 0;
}
</pre></div></td>
</tr></table>
+84 -84
View File
@@ -258,139 +258,139 @@ is when sample are clampled to their minimum and maximum range, which for floati
Usage: simple_mixing [input file 0] [input file 1] ... [input file n]
Example: simple_mixing file1.wav file2.flac
</pre></div><div style="font-family:monospace; border:solid 1px #003800; border-left:solid 0.5em #003800; margin:1em 0em;"><pre style="margin:0.5em 1em; padding:0; line-height:125%">
#define MINIAUDIO_IMPLEMENTATION
#include &quot;../miniaudio.h&quot;
<span style="color:#666666">#define</span> MINIAUDIO_IMPLEMENTATION
<span style="color:#666666">#include</span> <span style="color:#cc3300">&quot;../miniaudio.h&quot;</span>
#include &lt;stdio.h&gt;
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;stdio.h&gt;</span>
/*
<span style="color:#009900">/*
For simplicity, this example requires the device to use floating point samples.
*/
#define SAMPLE_FORMAT ma_format_f32
#define CHANNEL_COUNT 2
#define SAMPLE_RATE 48000
*/</span>
<span style="color:#666666">#define</span> SAMPLE_FORMAT ma_format_f32
<span style="color:#666666">#define</span> CHANNEL_COUNT 2
<span style="color:#666666">#define</span> SAMPLE_RATE 48000
ma_uint32 g_decoderCount;
ma_decoder* g_pDecoders;
ma_bool32* g_pDecodersAtEnd;
<span style="color:#0099cc">ma_uint32</span> g_decoderCount;
<span style="color:#0099cc">ma_decoder</span>* g_pDecoders;
<span style="color:#0099cc">ma_bool32</span>* g_pDecodersAtEnd;
ma_event g_stopEvent; /* &lt;-- Signaled by the audio thread, waited on by the main thread. */
<span style="color:#0099cc">ma_event</span> g_stopEvent; <span style="color:#009900">/* &lt;-- Signaled by the audio thread, waited on by the main thread. */</span>
ma_bool32 are_all_decoders_at_end()
<span style="color:#0099cc">ma_bool32</span> are_all_decoders_at_end()
{
ma_uint32 iDecoder;
for (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
if (g_pDecodersAtEnd[iDecoder] == MA_FALSE) {
return MA_FALSE;
<span style="color:#0099cc">ma_uint32</span> iDecoder;
<span style="color:#0033ff">for</span> (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
<span style="color:#0033ff">if</span> (g_pDecodersAtEnd[iDecoder] == MA_FALSE) {
<span style="color:#0033ff">return</span> MA_FALSE;
}
}
return MA_TRUE;
<span style="color:#0033ff">return</span> MA_TRUE;
}
ma_uint32 read_and_mix_pcm_frames_f32(ma_decoder* pDecoder, float* pOutputF32, ma_uint32 frameCount)
<span style="color:#0099cc">ma_uint32</span> read_and_mix_pcm_frames_f32(<span style="color:#0099cc">ma_decoder</span>* pDecoder, <span style="color:#0033ff">float</span>* pOutputF32, <span style="color:#0099cc">ma_uint32</span> frameCount)
{
/*
<span style="color:#009900">/*
The way mixing works is that we just read into a temporary buffer, then take the contents of that buffer and mix it with the
contents of the output buffer by simply adding the samples together. You could also clip the samples to -1..+1, but I&#39;m not
doing that in this example.
*/
float temp[4096];
ma_uint32 tempCapInFrames = ma_countof(temp) / CHANNEL_COUNT;
ma_uint32 totalFramesRead = 0;
*/</span>
<span style="color:#0033ff">float</span> temp[4096];
<span style="color:#0099cc">ma_uint32</span> tempCapInFrames = ma_countof(temp) / CHANNEL_COUNT;
<span style="color:#0099cc">ma_uint32</span> totalFramesRead = 0;
while (totalFramesRead &lt; frameCount) {
ma_uint32 iSample;
ma_uint32 framesReadThisIteration;
ma_uint32 totalFramesRemaining = frameCount - totalFramesRead;
ma_uint32 framesToReadThisIteration = tempCapInFrames;
if (framesToReadThisIteration &gt; totalFramesRemaining) {
<span style="color:#0033ff">while</span> (totalFramesRead &lt; frameCount) {
<span style="color:#0099cc">ma_uint32</span> iSample;
<span style="color:#0099cc">ma_uint32</span> framesReadThisIteration;
<span style="color:#0099cc">ma_uint32</span> totalFramesRemaining = frameCount - totalFramesRead;
<span style="color:#0099cc">ma_uint32</span> framesToReadThisIteration = tempCapInFrames;
<span style="color:#0033ff">if</span> (framesToReadThisIteration &gt; totalFramesRemaining) {
framesToReadThisIteration = totalFramesRemaining;
}
framesReadThisIteration = (ma_uint32)ma_decoder_read_pcm_frames(pDecoder, temp, framesToReadThisIteration);
if (framesReadThisIteration == 0) {
break;
framesReadThisIteration = (<span style="color:#0099cc">ma_uint32</span>)ma_decoder_read_pcm_frames(pDecoder, temp, framesToReadThisIteration);
<span style="color:#0033ff">if</span> (framesReadThisIteration == 0) {
<span style="color:#0033ff">break</span>;
}
/* Mix the frames together. */
for (iSample = 0; iSample &lt; framesReadThisIteration*CHANNEL_COUNT; ++iSample) {
<span style="color:#009900">/* Mix the frames together. */</span>
<span style="color:#0033ff">for</span> (iSample = 0; iSample &lt; framesReadThisIteration*CHANNEL_COUNT; ++iSample) {
pOutputF32[totalFramesRead*CHANNEL_COUNT + iSample] += temp[iSample];
}
totalFramesRead += framesReadThisIteration;
if (framesReadThisIteration &lt; framesToReadThisIteration) {
break; /* Reached EOF. */
<span style="color:#0033ff">if</span> (framesReadThisIteration &lt; framesToReadThisIteration) {
<span style="color:#0033ff">break</span>; <span style="color:#009900">/* Reached EOF. */</span>
}
}
return totalFramesRead;
<span style="color:#0033ff">return</span> totalFramesRead;
}
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
<span style="color:#0033ff">void</span> data_callback(<span style="color:#0099cc">ma_device</span>* pDevice, <span style="color:#0033ff">void</span>* pOutput, <span style="color:#0033ff">const</span> <span style="color:#0033ff">void</span>* pInput, <span style="color:#0099cc">ma_uint32</span> frameCount)
{
float* pOutputF32 = (float*)pOutput;
ma_uint32 iDecoder;
<span style="color:#0033ff">float</span>* pOutputF32 = (<span style="color:#0033ff">float</span>*)pOutput;
<span style="color:#0099cc">ma_uint32</span> iDecoder;
MA_ASSERT(pDevice-&gt;playback.format == SAMPLE_FORMAT); /* &lt;-- Important for this example. */
MA_ASSERT(pDevice-&gt;playback.format == SAMPLE_FORMAT); <span style="color:#009900">/* &lt;-- Important for this example. */</span>
for (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
if (!g_pDecodersAtEnd[iDecoder]) {
ma_uint32 framesRead = read_and_mix_pcm_frames_f32(&amp;g_pDecoders[iDecoder], pOutputF32, frameCount);
if (framesRead &lt; frameCount) {
<span style="color:#0033ff">for</span> (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
<span style="color:#0033ff">if</span> (!g_pDecodersAtEnd[iDecoder]) {
<span style="color:#0099cc">ma_uint32</span> framesRead = read_and_mix_pcm_frames_f32(&amp;g_pDecoders[iDecoder], pOutputF32, frameCount);
<span style="color:#0033ff">if</span> (framesRead &lt; frameCount) {
g_pDecodersAtEnd[iDecoder] = MA_TRUE;
}
}
}
/*
<span style="color:#009900">/*
If at the end all of our decoders are at the end we need to stop. We cannot stop the device in the callback. Instead we need to
signal an event to indicate that it&#39;s stopped. The main thread will be waiting on the event, after which it will stop the device.
*/
if (are_all_decoders_at_end()) {
*/</span>
<span style="color:#0033ff">if</span> (are_all_decoders_at_end()) {
ma_event_signal(&amp;g_stopEvent);
}
(void)pInput;
(<span style="color:#0033ff">void</span>)pInput;
}
int main(int argc, char** argv)
<span style="color:#0033ff">int</span> main(<span style="color:#0033ff">int</span> argc, <span style="color:#0033ff">char</span>** argv)
{
ma_result result;
ma_decoder_config decoderConfig;
ma_device_config deviceConfig;
ma_device device;
ma_uint32 iDecoder;
<span style="color:#0099cc">ma_result</span> result;
<span style="color:#0099cc">ma_decoder_config</span> decoderConfig;
<span style="color:#0099cc">ma_device_config</span> deviceConfig;
<span style="color:#0099cc">ma_device</span> device;
<span style="color:#0099cc">ma_uint32</span> iDecoder;
if (argc &lt; 2) {
printf(&quot;No input files.\n&quot;);
return -1;
<span style="color:#0033ff">if</span> (argc &lt; 2) {
printf(<span style="color:#cc3300">&quot;No input files.\n&quot;</span>);
<span style="color:#0033ff">return</span> -1;
}
g_decoderCount = argc-1;
g_pDecoders = (ma_decoder*)malloc(sizeof(*g_pDecoders) * g_decoderCount);
g_pDecodersAtEnd = (ma_bool32*) malloc(sizeof(*g_pDecodersAtEnd) * g_decoderCount);
g_pDecoders = (<span style="color:#0099cc">ma_decoder</span>*)malloc(<span style="color:#0033ff">sizeof</span>(*g_pDecoders) * g_decoderCount);
g_pDecodersAtEnd = (<span style="color:#0099cc">ma_bool32</span>*) malloc(<span style="color:#0033ff">sizeof</span>(*g_pDecodersAtEnd) * g_decoderCount);
/* In this example, all decoders need to have the same output format. */
<span style="color:#009900">/* In this example, all decoders need to have the same output format. */</span>
decoderConfig = ma_decoder_config_init(SAMPLE_FORMAT, CHANNEL_COUNT, SAMPLE_RATE);
for (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
<span style="color:#0033ff">for</span> (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
result = ma_decoder_init_file(argv[1+iDecoder], &amp;decoderConfig, &amp;g_pDecoders[iDecoder]);
if (result != MA_SUCCESS) {
ma_uint32 iDecoder2;
for (iDecoder2 = 0; iDecoder2 &lt; iDecoder; ++iDecoder2) {
<span style="color:#0033ff">if</span> (result != MA_SUCCESS) {
<span style="color:#0099cc">ma_uint32</span> iDecoder2;
<span style="color:#0033ff">for</span> (iDecoder2 = 0; iDecoder2 &lt; iDecoder; ++iDecoder2) {
ma_decoder_uninit(&amp;g_pDecoders[iDecoder2]);
}
free(g_pDecoders);
free(g_pDecodersAtEnd);
printf(&quot;Failed to load %s.\n&quot;, argv[1+iDecoder]);
return -3;
printf(<span style="color:#cc3300">&quot;Failed to load %s.\n&quot;</span>, argv[1+iDecoder]);
<span style="color:#0033ff">return</span> -3;
}
g_pDecodersAtEnd[iDecoder] = MA_FALSE;
}
/* Create only a single device. The decoders will be mixed together in the callback. In this example the data format needs to be the same as the decoders. */
<span style="color:#009900">/* Create only a single device. The decoders will be mixed together in the callback. In this example the data format needs to be the same as the decoders. */</span>
deviceConfig = ma_device_config_init(ma_device_type_playback);
deviceConfig.playback.format = SAMPLE_FORMAT;
deviceConfig.playback.channels = CHANNEL_COUNT;
@@ -398,50 +398,50 @@ int main(int argc, char** argv)
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = NULL;
if (ma_device_init(NULL, &amp;deviceConfig, &amp;device) != MA_SUCCESS) {
for (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
<span style="color:#0033ff">if</span> (ma_device_init(NULL, &amp;deviceConfig, &amp;device) != MA_SUCCESS) {
<span style="color:#0033ff">for</span> (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
ma_decoder_uninit(&amp;g_pDecoders[iDecoder]);
}
free(g_pDecoders);
free(g_pDecodersAtEnd);
printf(&quot;Failed to open playback device.\n&quot;);
return -3;
printf(<span style="color:#cc3300">&quot;Failed to open playback device.\n&quot;</span>);
<span style="color:#0033ff">return</span> -3;
}
/*
<span style="color:#009900">/*
We can&#39;t stop in the audio thread so we instead need to use an event. We wait on this thread in the main thread, and signal it in the audio thread. This
needs to be done before starting the device. We need a context to initialize the event, which we can get from the device. Alternatively you can initialize
a context separately, but we don&#39;t need to do that for this example.
*/
*/</span>
ma_event_init(&amp;g_stopEvent);
/* Now we start playback and wait for the audio thread to tell us to stop. */
if (ma_device_start(&amp;device) != MA_SUCCESS) {
<span style="color:#009900">/* Now we start playback and wait for the audio thread to tell us to stop. */</span>
<span style="color:#0033ff">if</span> (ma_device_start(&amp;device) != MA_SUCCESS) {
ma_device_uninit(&amp;device);
for (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
<span style="color:#0033ff">for</span> (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
ma_decoder_uninit(&amp;g_pDecoders[iDecoder]);
}
free(g_pDecoders);
free(g_pDecodersAtEnd);
printf(&quot;Failed to start playback device.\n&quot;);
return -4;
printf(<span style="color:#cc3300">&quot;Failed to start playback device.\n&quot;</span>);
<span style="color:#0033ff">return</span> -4;
}
printf(&quot;Waiting for playback to complete...\n&quot;);
printf(<span style="color:#cc3300">&quot;Waiting for playback to complete...\n&quot;</span>);
ma_event_wait(&amp;g_stopEvent);
/* Getting here means the audio thread has signaled that the device should be stopped. */
<span style="color:#009900">/* Getting here means the audio thread has signaled that the device should be stopped. */</span>
ma_device_uninit(&amp;device);
for (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
<span style="color:#0033ff">for</span> (iDecoder = 0; iDecoder &lt; g_decoderCount; ++iDecoder) {
ma_decoder_uninit(&amp;g_pDecoders[iDecoder]);
}
free(g_pDecoders);
free(g_pDecodersAtEnd);
return 0;
<span style="color:#0033ff">return</span> 0;
}
</pre></div></td>
</tr></table>
+26 -26
View File
@@ -257,38 +257,38 @@ device and can be used independently of it. This example only plays back a singl
back multiple files by simple loading multiple decoders and mixing them (do not create multiple devices to do this). See
the simple_mixing example for how best to do this.</p>
<div style="font-family:monospace; border:solid 1px #003800; border-left:solid 0.5em #003800; margin:1em 0em;"><pre style="margin:0.5em 1em; padding:0; line-height:125%">
#define MINIAUDIO_IMPLEMENTATION
#include &quot;../miniaudio.h&quot;
<span style="color:#666666">#define</span> MINIAUDIO_IMPLEMENTATION
<span style="color:#666666">#include</span> <span style="color:#cc3300">&quot;../miniaudio.h&quot;</span>
#include &lt;stdio.h&gt;
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;stdio.h&gt;</span>
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
<span style="color:#0033ff">void</span> data_callback(<span style="color:#0099cc">ma_device</span>* pDevice, <span style="color:#0033ff">void</span>* pOutput, <span style="color:#0033ff">const</span> <span style="color:#0033ff">void</span>* pInput, <span style="color:#0099cc">ma_uint32</span> frameCount)
{
ma_decoder* pDecoder = (ma_decoder*)pDevice-&gt;pUserData;
if (pDecoder == NULL) {
return;
<span style="color:#0099cc">ma_decoder</span>* pDecoder = (<span style="color:#0099cc">ma_decoder</span>*)pDevice-&gt;pUserData;
<span style="color:#0033ff">if</span> (pDecoder == NULL) {
<span style="color:#0033ff">return</span>;
}
ma_decoder_read_pcm_frames(pDecoder, pOutput, frameCount);
(void)pInput;
(<span style="color:#0033ff">void</span>)pInput;
}
int main(int argc, char** argv)
<span style="color:#0033ff">int</span> main(<span style="color:#0033ff">int</span> argc, <span style="color:#0033ff">char</span>** argv)
{
ma_result result;
ma_decoder decoder;
ma_device_config deviceConfig;
ma_device device;
<span style="color:#0099cc">ma_result</span> result;
<span style="color:#0099cc">ma_decoder</span> decoder;
<span style="color:#0099cc">ma_device_config</span> deviceConfig;
<span style="color:#0099cc">ma_device</span> device;
if (argc &lt; 2) {
printf(&quot;No input file.\n&quot;);
return -1;
<span style="color:#0033ff">if</span> (argc &lt; 2) {
printf(<span style="color:#cc3300">&quot;No input file.\n&quot;</span>);
<span style="color:#0033ff">return</span> -1;
}
result = ma_decoder_init_file(argv[1], NULL, &amp;decoder);
if (result != MA_SUCCESS) {
return -2;
<span style="color:#0033ff">if</span> (result != MA_SUCCESS) {
<span style="color:#0033ff">return</span> -2;
}
deviceConfig = ma_device_config_init(ma_device_type_playback);
@@ -298,26 +298,26 @@ int main(int argc, char** argv)
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = &amp;decoder;
if (ma_device_init(NULL, &amp;deviceConfig, &amp;device) != MA_SUCCESS) {
printf(&quot;Failed to open playback device.\n&quot;);
<span style="color:#0033ff">if</span> (ma_device_init(NULL, &amp;deviceConfig, &amp;device) != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to open playback device.\n&quot;</span>);
ma_decoder_uninit(&amp;decoder);
return -3;
<span style="color:#0033ff">return</span> -3;
}
if (ma_device_start(&amp;device) != MA_SUCCESS) {
printf(&quot;Failed to start playback device.\n&quot;);
<span style="color:#0033ff">if</span> (ma_device_start(&amp;device) != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to start playback device.\n&quot;</span>);
ma_device_uninit(&amp;device);
ma_decoder_uninit(&amp;decoder);
return -4;
<span style="color:#0033ff">return</span> -4;
}
printf(&quot;Press Enter to quit...&quot;);
printf(<span style="color:#cc3300">&quot;Press Enter to quit...&quot;</span>);
getchar();
ma_device_uninit(&amp;device);
ma_decoder_uninit(&amp;decoder);
return 0;
<span style="color:#0033ff">return</span> 0;
}
</pre></div></td>
</tr></table>
+35 -35
View File
@@ -263,45 +263,45 @@ the <span style="font-family:monospace;">ma_waveform_read_pcm_frames()</span> AP
This example works with Emscripten.</p>
<div style="font-family:monospace; border:solid 1px #003800; border-left:solid 0.5em #003800; margin:1em 0em;"><pre style="margin:0.5em 1em; padding:0; line-height:125%">
#define MA_NO_DECODING
#define MA_NO_ENCODING
#define MINIAUDIO_IMPLEMENTATION
#include &quot;../miniaudio.h&quot;
<span style="color:#666666">#define</span> MA_NO_DECODING
<span style="color:#666666">#define</span> MA_NO_ENCODING
<span style="color:#666666">#define</span> MINIAUDIO_IMPLEMENTATION
<span style="color:#666666">#include</span> <span style="color:#cc3300">&quot;../miniaudio.h&quot;</span>
#include &lt;stdio.h&gt;
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;stdio.h&gt;</span>
#ifdef __EMSCRIPTEN__
#include &lt;emscripten.h&gt;
<span style="color:#666666">#ifdef</span> __EMSCRIPTEN__
<span style="color:#666666">#include</span> <span style="color:#cc3300">&lt;emscripten.h&gt;</span>
void main_loop__em()
<span style="color:#0033ff">void</span> main_loop__em()
{
}
#endif
<span style="color:#666666">#endif</span>
#define DEVICE_FORMAT ma_format_f32
#define DEVICE_CHANNELS 2
#define DEVICE_SAMPLE_RATE 48000
<span style="color:#666666">#define</span> DEVICE_FORMAT ma_format_f32
<span style="color:#666666">#define</span> DEVICE_CHANNELS 2
<span style="color:#666666">#define</span> DEVICE_SAMPLE_RATE 48000
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
<span style="color:#0033ff">void</span> data_callback(<span style="color:#0099cc">ma_device</span>* pDevice, <span style="color:#0033ff">void</span>* pOutput, <span style="color:#0033ff">const</span> <span style="color:#0033ff">void</span>* pInput, <span style="color:#0099cc">ma_uint32</span> frameCount)
{
ma_waveform* pSineWave;
<span style="color:#0099cc">ma_waveform</span>* pSineWave;
MA_ASSERT(pDevice-&gt;playback.channels == DEVICE_CHANNELS);
pSineWave = (ma_waveform*)pDevice-&gt;pUserData;
pSineWave = (<span style="color:#0099cc">ma_waveform</span>*)pDevice-&gt;pUserData;
MA_ASSERT(pSineWave != NULL);
ma_waveform_read_pcm_frames(pSineWave, pOutput, frameCount);
(void)pInput; /* Unused. */
(<span style="color:#0033ff">void</span>)pInput; <span style="color:#009900">/* Unused. */</span>
}
int main(int argc, char** argv)
<span style="color:#0033ff">int</span> main(<span style="color:#0033ff">int</span> argc, <span style="color:#0033ff">char</span>** argv)
{
ma_waveform sineWave;
ma_device_config deviceConfig;
ma_device device;
ma_waveform_config sineWaveConfig;
<span style="color:#0099cc">ma_waveform</span> sineWave;
<span style="color:#0099cc">ma_device_config</span> deviceConfig;
<span style="color:#0099cc">ma_device</span> device;
<span style="color:#0099cc">ma_waveform_config</span> sineWaveConfig;
sineWaveConfig = ma_waveform_config_init(DEVICE_FORMAT, DEVICE_CHANNELS, DEVICE_SAMPLE_RATE, ma_waveform_type_sine, 0.2, 220);
ma_waveform_init(&amp;sineWaveConfig, &amp;sineWave);
@@ -313,31 +313,31 @@ int main(int argc, char** argv)
deviceConfig.dataCallback = data_callback;
deviceConfig.pUserData = &amp;sineWave;
if (ma_device_init(NULL, &amp;deviceConfig, &amp;device) != MA_SUCCESS) {
printf(&quot;Failed to open playback device.\n&quot;);
return -4;
<span style="color:#0033ff">if</span> (ma_device_init(NULL, &amp;deviceConfig, &amp;device) != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to open playback device.\n&quot;</span>);
<span style="color:#0033ff">return</span> -4;
}
printf(&quot;Device Name: %s\n&quot;, device.playback.name);
printf(<span style="color:#cc3300">&quot;Device Name: %s\n&quot;</span>, device.playback.name);
if (ma_device_start(&amp;device) != MA_SUCCESS) {
printf(&quot;Failed to start playback device.\n&quot;);
<span style="color:#0033ff">if</span> (ma_device_start(&amp;device) != MA_SUCCESS) {
printf(<span style="color:#cc3300">&quot;Failed to start playback device.\n&quot;</span>);
ma_device_uninit(&amp;device);
return -5;
<span style="color:#0033ff">return</span> -5;
}
#ifdef __EMSCRIPTEN__
<span style="color:#666666">#ifdef</span> __EMSCRIPTEN__
emscripten_set_main_loop(main_loop__em, 0, 1);
#else
printf(&quot;Press Enter to quit...\n&quot;);
<span style="color:#666666">#else</span>
printf(<span style="color:#cc3300">&quot;Press Enter to quit...\n&quot;</span>);
getchar();
#endif
<span style="color:#666666">#endif</span>
ma_device_uninit(&amp;device);
(void)argc;
(void)argv;
return 0;
(<span style="color:#0033ff">void</span>)argc;
(<span style="color:#0033ff">void</span>)argv;
<span style="color:#0033ff">return</span> 0;
}
</pre></div></td>
</tr></table>