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

Update simple playback example.

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