mirror of
https://github.com/mackron/miniaudio.git
synced 2026-04-22 00:06:59 +02:00
Add syntax highlighting to documentation.
This commit is contained in:
@@ -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 "../miniaudio.h"
|
||||
<span style="color:#666666">#define</span> MINIAUDIO_IMPLEMENTATION
|
||||
<span style="color:#666666">#include</span> <span style="color:#cc3300">"../miniaudio.h"</span>
|
||||
|
||||
#include <stdio.h>
|
||||
<span style="color:#666666">#include</span> <span style="color:#cc3300"><stdio.h></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 /* <-- 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">/* <-- 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("frameCount=%d\n", frameCount);
|
||||
*/</span>
|
||||
printf(<span style="color:#cc3300">"frameCount=%d\n"</span>, frameCount);
|
||||
|
||||
ma_waveform_read_pcm_frames(&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'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->playback.channels == DEVICE_CHANNELS);
|
||||
|
||||
/*
|
||||
<span style="color:#009900">/*
|
||||
The first thing to do is check if there'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's enough, making sure we only fill the ring buffer in chunks of PCM_FRAME_CHUNK_SIZE.
|
||||
*/
|
||||
while (pcmFramesProcessed < frameCount) { /* Keep going until we've filled the output buffer. */
|
||||
ma_uint32 framesRemaining = frameCount - pcmFramesProcessed;
|
||||
*/</span>
|
||||
<span style="color:#0033ff">while</span> (pcmFramesProcessed < frameCount) { <span style="color:#009900">/* Keep going until we've filled the output buffer. */</span>
|
||||
<span style="color:#0099cc">ma_uint32</span> framesRemaining = frameCount - pcmFramesProcessed;
|
||||
|
||||
pcmFramesAvailableInRB = ma_pcm_rb_available_read(&g_rb);
|
||||
if (pcmFramesAvailableInRB > 0) {
|
||||
ma_uint32 framesToRead = (framesRemaining < pcmFramesAvailableInRB) ? framesRemaining : pcmFramesAvailableInRB;
|
||||
void* pReadBuffer;
|
||||
<span style="color:#0033ff">if</span> (pcmFramesAvailableInRB > 0) {
|
||||
<span style="color:#0099cc">ma_uint32</span> framesToRead = (framesRemaining < pcmFramesAvailableInRB) ? framesRemaining : pcmFramesAvailableInRB;
|
||||
<span style="color:#0033ff">void</span>* pReadBuffer;
|
||||
|
||||
ma_pcm_rb_acquire_read(&g_rb, &framesToRead, &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->playback.format, pDevice->playback.channels);
|
||||
pcmFramesProcessed += framesToRead;
|
||||
} else {
|
||||
/*
|
||||
} <span style="color:#0033ff">else</span> {
|
||||
<span style="color:#009900">/*
|
||||
There'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(&g_rb);
|
||||
ma_pcm_rb_acquire_write(&g_rb, &framesToWrite, &pWriteBuffer);
|
||||
{
|
||||
MA_ASSERT(framesToWrite == PCM_FRAME_CHUNK_SIZE); /* <-- 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">/* <-- 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(&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(&waveformConfig, &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; /* <-- Set this to a pointer to the ring buffer if you don't want it in global scope. */
|
||||
deviceConfig.pUserData = NULL; <span style="color:#009900">/* <-- Set this to a pointer to the ring buffer if you don't want it in global scope. */</span>
|
||||
|
||||
if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) {
|
||||
printf("Failed to open playback device.\n");
|
||||
<span style="color:#0033ff">if</span> (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) {
|
||||
printf(<span style="color:#cc3300">"Failed to open playback device.\n"</span>);
|
||||
ma_pcm_rb_uninit(&g_rb);
|
||||
return -4;
|
||||
<span style="color:#0033ff">return</span> -4;
|
||||
}
|
||||
|
||||
printf("Device Name: %s\n", device.playback.name);
|
||||
printf(<span style="color:#cc3300">"Device Name: %s\n"</span>, device.playback.name);
|
||||
|
||||
if (ma_device_start(&device) != MA_SUCCESS) {
|
||||
printf("Failed to start playback device.\n");
|
||||
<span style="color:#0033ff">if</span> (ma_device_start(&device) != MA_SUCCESS) {
|
||||
printf(<span style="color:#cc3300">"Failed to start playback device.\n"</span>);
|
||||
ma_pcm_rb_uninit(&g_rb);
|
||||
ma_device_uninit(&device);
|
||||
return -5;
|
||||
<span style="color:#0033ff">return</span> -5;
|
||||
}
|
||||
|
||||
printf("Press Enter to quit...\n");
|
||||
printf(<span style="color:#cc3300">"Press Enter to quit...\n"</span>);
|
||||
getchar();
|
||||
|
||||
ma_device_uninit(&device);
|
||||
ma_pcm_rb_uninit(&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>
|
||||
|
||||
Reference in New Issue
Block a user