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>