Commit 9ed608a4 authored by David Reid's avatar David Reid

Update simple playback example.

parent 08e21aae
......@@ -9,7 +9,7 @@ Features
- A simple build system.
- It should Just Work out of the box, without the need to download and install any dependencies.
- A simple API.
- Supports both playback and capture on all backends.
- Supports playback, capture and full-duplex.
- Data conversion.
- Sample format conversion, with optional dithering.
- Sample rate conversion.
......@@ -74,15 +74,16 @@ Simple Playback Example
#include <stdio.h>
// This is the function that's used for sending more data to the device for playback.
mal_uint32 on_send_frames_to_device(mal_device* pDevice, mal_uint32 frameCount, void* pSamples)
void data_callback(mal_device* pDevice, void* pOutput, const void* pInput, mal_uint32 frameCount)
{
mal_decoder* pDecoder = (mal_decoder*)pDevice->pUserData;
if (pDecoder == NULL) {
return 0;
return;
}
return (mal_uint32)mal_decoder_read_pcm_frames(pDecoder, frameCount, pSamples);
mal_decoder_read_pcm_frames(pDecoder, frameCount, pOutput);
(void)pInput;
}
int main(int argc, char** argv)
......@@ -98,15 +99,16 @@ int main(int argc, char** argv)
return -2;
}
mal_device_config config = mal_device_config_init_playback(
decoder.outputFormat,
decoder.outputChannels,
decoder.outputSampleRate,
on_send_frames_to_device,
&decoder);
mal_device_config config = mal_device_config_init(mal_device_type_playback);
config.playback.pDeviceID = NULL;
config.playback.format = decoder.outputFormat;
config.playback.channels = decoder.outputChannels;
config.sampleRate = decoder.outputSampleRate;
config.dataCallback = data_callback;
config.pUserData = &decoder;
mal_device device;
if (mal_device_init(NULL, mal_device_type_playback, NULL, &config, &device) != MAL_SUCCESS) {
if (mal_device_init(NULL, &config, &device) != MAL_SUCCESS) {
printf("Failed to open playback device.\n");
mal_decoder_uninit(&decoder);
return -3;
......@@ -126,7 +128,7 @@ int main(int argc, char** argv)
mal_decoder_uninit(&decoder);
return 0;
}
}}
```
......
......@@ -10,7 +10,7 @@
#include <stdio.h>
void on_send_frames_to_device(mal_device* pDevice, void* pOutput, const void* pInput, mal_uint32 frameCount)
void data_callback(mal_device* pDevice, void* pOutput, const void* pInput, mal_uint32 frameCount)
{
mal_decoder* pDecoder = (mal_decoder*)pDevice->pUserData;
if (pDecoder == NULL) {
......@@ -37,11 +37,11 @@ int main(int argc, char** argv)
mal_device_config config = mal_device_config_init(mal_device_type_playback);
config.playback.pDeviceID = NULL;
config.playback.format = decoder.outputFormat;
config.playback.channels = decoder.outputChannels;
config.sampleRate = decoder.outputSampleRate;
config.dataCallback = on_send_frames_to_device;
config.pUserData = &decoder;
config.playback.format = decoder.outputFormat;
config.playback.channels = decoder.outputChannels;
config.sampleRate = decoder.outputSampleRate;
config.dataCallback = data_callback;
config.pUserData = &decoder;
mal_device device;
if (mal_device_init(NULL, &config, &device) != MAL_SUCCESS) {
......
......@@ -50,24 +50,29 @@ of capture.
Playback Example
----------------
mal_uint32 on_send_samples(mal_device* pDevice, mal_uint32 frameCount, void* pSamples)
void data_callback(mal_device* pDevice, void* pOutput, const void* pInput, mal_uint32 frameCount)
{
// This callback is set at initialization time and will be called when a playback device needs more
// data. You need to write as many frames as you can to pSamples (but no more than frameCount) and
// then return the number of frames you wrote.
//
// The user data (pDevice->pUserData) is set by mal_device_init().
return (mal_uint32)mal_decoder_read_pcm_frames((mal_decoder*)pDevice->pUserData, frameCount, pSamples);
mal_decoder* pDecoder = (mal_decoder*)pDevice->pUserData;
if (pDecoder == NULL) {
return;
}
mal_decoder_read_pcm_frames(pDecoder, frameCount, pOutput);
}
...
mal_device_config config = mal_device_config_init_playback(decoder.outputFormat, decoder.outputChannels, decoder.outputSampleRate, on_send_frames_to_device, &decoder);
mal_device_config config = mal_device_config_init(mal_device_type_playback);
config.playback.pDeviceID = NULL;
config.playback.format = decoder.outputFormat;
config.playback.channels = decoder.outputChannels;
config.sampleRate = decoder.outputSampleRate;
config.dataCallback = data_callback;
config.pUserData = &decoder;
mal_device device;
mal_result result = mal_device_init(NULL, mal_device_type_playback, NULL, &config, &device);
if (result != MAL_SUCCESS) {
return -1;
if (mal_device_init(NULL, &config, &device) != MAL_SUCCESS) {
... An error occurred ...
}
mal_device_start(&device); // The device is sleeping by default so you'll need to start it manually.
......@@ -287,12 +287,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\examples\simple_playback.c">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\research\tests\mal_resampler_test_0.c">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
......@@ -359,12 +359,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="mal_test_0.c">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="mal_test_0.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
......
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