Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
miniaudio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
miniaudio
Commits
32c64703
Commit
32c64703
authored
Feb 09, 2019
by
David Reid
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial working implementation of full-duplex on WASAPI.
parent
934c7d5e
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
300 additions
and
148 deletions
+300
-148
examples/simple_capture.c
examples/simple_capture.c
+41
-75
mini_al.h
mini_al.h
+205
-49
tests/mal_duplex.c
tests/mal_duplex.c
+38
-12
tests/mal_test_0.vcxproj
tests/mal_test_0.vcxproj
+13
-12
tests/mal_test_0.vcxproj.filters
tests/mal_test_0.vcxproj.filters
+3
-0
No files found.
examples/simple_capture.c
View file @
32c64703
// This example simply captures data from your default microphone until you press Enter, after
// This example simply captures data from your default microphone until you press Enter. The output is saved to the file specified on the command line.
// which it plays back the captured audio.
#define MINI_AL_IMPLEMENTATION
#define MINI_AL_IMPLEMENTATION
#include "../mini_al.h"
#include "../mini_al.h"
#define DR_WAV_IMPLEMENTATION
#include "../extras/dr_wav.h"
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
void
data_callback
(
mal_device
*
pDevice
,
void
*
pOutput
,
const
void
*
pInput
,
mal_uint32
frameCount
)
mal_uint32
capturedSampleCount
=
0
;
mal_int16
*
pCapturedSamples
=
NULL
;
mal_uint32
playbackSample
=
0
;
void
on_recv_frames
(
mal_device
*
pDevice
,
void
*
pOutput
,
const
void
*
pInput
,
mal_uint32
frameCount
)
{
{
mal_uint32
sampleCount
=
frameCount
*
pDevice
->
channels
;
mal_uint32
newCapturedSampleCount
=
capturedSampleCount
+
sampleCount
;
mal_int16
*
pNewCapturedSamples
=
(
mal_int16
*
)
realloc
(
pCapturedSamples
,
newCapturedSampleCount
*
sizeof
(
mal_int16
));
if
(
pNewCapturedSamples
==
NULL
)
{
return
;
}
memcpy
(
pNewCapturedSamples
+
capturedSampleCount
,
pInput
,
sampleCount
*
sizeof
(
mal_int16
));
pCapturedSamples
=
pNewCapturedSamples
;
capturedSampleCount
=
newCapturedSampleCount
;
(
void
)
pOutput
;
(
void
)
pOutput
;
drwav
*
pWav
=
(
drwav
*
)
pDevice
->
pUserData
;
mal_assert
(
pWav
!=
NULL
);
drwav_write_pcm_frames
(
pWav
,
frameCount
,
pInput
);
}
}
void
on_send_frames
(
mal_device
*
pDevice
,
void
*
pOutput
,
const
void
*
pInput
,
mal_uint32
frameCount
)
int
main
(
int
argc
,
char
**
argv
)
{
{
mal_uint32
samplesToRead
=
frameCount
*
pDevice
->
channels
;
if
(
argc
<
2
)
{
if
(
samplesToRead
>
capturedSampleCount
-
playbackSample
)
{
printf
(
"No input file.
\n
"
);
samplesToRead
=
capturedSampleCount
-
playbackSample
;
return
-
1
;
}
if
(
samplesToRead
==
0
)
{
return
;
}
}
memcpy
(
pOutput
,
pCapturedSamples
+
playbackSample
,
samplesToRead
*
sizeof
(
mal_int16
));
mal_result
result
;
playbackSample
+=
samplesToRead
;
(
void
)
pInput
;
}
int
main
()
drwav_data_format
wavFormat
;
{
wavFormat
.
container
=
drwav_container_riff
;
mal_device_config
config
;
wavFormat
.
format
=
DR_WAVE_FORMAT_IEEE_FLOAT
;
wavFormat
.
channels
=
2
;
wavFormat
.
sampleRate
=
44100
;
wavFormat
.
bitsPerSample
=
32
;
mal_context
context
;
drwav
wav
;
if
(
mal_context_init
(
NULL
,
0
,
NULL
,
&
context
)
!=
MAL_SUCCESS
)
{
if
(
drwav_init_file_write
(
&
wav
,
argv
[
1
],
&
wavFormat
)
==
DRWAV_FALSE
)
{
printf
(
"Failed to initialize
context.
"
);
printf
(
"Failed to initialize
output file.
\n
"
);
return
-
1
;
return
-
1
;
}
}
printf
(
"Recording...
\n
"
);
mal_device_config
config
=
mal_device_config_init
(
mal_device_type_capture
);
config
=
mal_device_config_init
(
mal_format_s16
,
2
,
48000
,
on_recv_frames
,
NULL
);
config
.
capture
.
format
=
mal_format_f32
;
mal_device
captureDevice
;
config
.
capture
.
channels
=
wavFormat
.
channels
;
if
(
mal_device_init
(
&
context
,
mal_device_type_capture
,
NULL
,
&
config
,
&
captureDevice
)
!=
MAL_SUCCESS
)
{
config
.
sampleRate
=
wavFormat
.
sampleRate
;
mal_context_uninit
(
&
context
);
config
.
dataCallback
=
data_callback
;
config
.
pUserData
=
&
wav
;
mal_device
device
;
result
=
mal_device_init
(
NULL
,
&
config
,
&
device
);
if
(
result
!=
MAL_SUCCESS
)
{
printf
(
"Failed to initialize capture device.
\n
"
);
printf
(
"Failed to initialize capture device.
\n
"
);
return
-
2
;
return
-
2
;
}
}
if
(
mal_device_start
(
&
captureDevice
)
!=
MAL_SUCCESS
)
{
result
=
mal_device_start
(
&
device
);
mal_device_uninit
(
&
captureDevice
);
if
(
result
!=
MAL_SUCCESS
)
{
mal_
context_uninit
(
&
context
);
mal_
device_uninit
(
&
device
);
printf
(
"Failed to start
capture
device.
\n
"
);
printf
(
"Failed to start device.
\n
"
);
return
-
3
;
return
-
3
;
}
}
printf
(
"Press Enter to stop recording...
\n
"
);
printf
(
"Press Enter to stop recording...
\n
"
);
getchar
();
getchar
();
mal_device_uninit
(
&
device
);
drwav_uninit
(
&
wav
);
mal_device_uninit
(
&
captureDevice
);
printf
(
"Playing...
\n
"
);
config
=
mal_device_config_init
(
mal_format_s16
,
2
,
48000
,
on_send_frames
,
NULL
);
mal_device
playbackDevice
;
if
(
mal_device_init
(
&
context
,
mal_device_type_playback
,
NULL
,
&
config
,
&
playbackDevice
)
!=
MAL_SUCCESS
)
{
mal_context_uninit
(
&
context
);
printf
(
"Failed to initialize playback device.
\n
"
);
return
-
4
;
}
if
(
mal_device_start
(
&
playbackDevice
)
!=
MAL_SUCCESS
)
{
mal_device_uninit
(
&
playbackDevice
);
mal_context_uninit
(
&
context
);
printf
(
"Failed to start playback device.
\n
"
);
return
-
5
;
}
printf
(
"Press Enter to quit...
\n
"
);
getchar
();
mal_device_uninit
(
&
playbackDevice
);
mal_context_uninit
(
&
context
);
return
0
;
return
0
;
}
}
\ No newline at end of file
mini_al.h
View file @
32c64703
...
@@ -2052,6 +2052,8 @@ MAL_ALIGNED_STRUCT(MAL_SIMD_ALIGNMENT) mal_device
...
@@ -2052,6 +2052,8 @@ MAL_ALIGNED_STRUCT(MAL_SIMD_ALIGNMENT) mal_device
mal_uint32 internalBufferSizeInFrames;
mal_uint32 internalBufferSizeInFrames;
mal_uint32 internalPeriods;
mal_uint32 internalPeriods;
mal_pcm_converter converter;
mal_pcm_converter converter;
mal_uint32 _dspFrameCount; // Internal use only. Used as the data source when reading from the device.
const mal_uint8* _dspFrames; // ^^^ AS ABOVE ^^^
} playback;
} playback;
struct
struct
{
{
...
@@ -2072,6 +2074,8 @@ MAL_ALIGNED_STRUCT(MAL_SIMD_ALIGNMENT) mal_device
...
@@ -2072,6 +2074,8 @@ MAL_ALIGNED_STRUCT(MAL_SIMD_ALIGNMENT) mal_device
mal_uint32 internalBufferSizeInFrames;
mal_uint32 internalBufferSizeInFrames;
mal_uint32 internalPeriods;
mal_uint32 internalPeriods;
mal_pcm_converter converter;
mal_pcm_converter converter;
mal_uint32 _dspFrameCount; // Internal use only. Used as the data source when reading from the device.
const mal_uint8* _dspFrames; // ^^^ AS ABOVE ^^^
} capture;
} capture;
union
union
...
@@ -4623,6 +4627,53 @@ mal_uint32 mal_device__on_read_from_device(mal_pcm_converter* pDSP, mal_uint32 f
...
@@ -4623,6 +4627,53 @@ mal_uint32 mal_device__on_read_from_device(mal_pcm_converter* pDSP, mal_uint32 f
return framesToRead;
return framesToRead;
}
}
/* The PCM converter callback for reading from a buffer. */
mal_uint32 mal_device__pcm_converter__on_read_from_buffer_capture(mal_pcm_converter* pConverter, mal_uint32 frameCount, void* pFramesOut, void* pUserData)
{
mal_device* pDevice = (mal_device*)pUserData;
mal_assert(pDevice != NULL);
if (pDevice->capture._dspFrameCount == 0) {
return 0; // Nothing left.
}
mal_uint32 framesToRead = frameCount;
if (framesToRead > pDevice->capture._dspFrameCount) {
framesToRead = pDevice->capture._dspFrameCount;
}
mal_uint32 bytesToRead = framesToRead * mal_get_bytes_per_frame(pConverter->formatConverterIn.config.formatIn, pConverter->channelRouter.config.channelsIn);
mal_copy_memory(pFramesOut, pDevice->capture._dspFrames, bytesToRead);
pDevice->capture._dspFrameCount -= framesToRead;
pDevice->capture._dspFrames += bytesToRead;
return framesToRead;
}
mal_uint32 mal_device__pcm_converter__on_read_from_buffer_playback(mal_pcm_converter* pConverter, mal_uint32 frameCount, void* pFramesOut, void* pUserData)
{
mal_device* pDevice = (mal_device*)pUserData;
mal_assert(pDevice != NULL);
if (pDevice->playback._dspFrameCount == 0) {
return 0; // Nothing left.
}
mal_uint32 framesToRead = frameCount;
if (framesToRead > pDevice->playback._dspFrameCount) {
framesToRead = pDevice->playback._dspFrameCount;
}
mal_uint32 bytesToRead = framesToRead * mal_get_bytes_per_frame(pConverter->formatConverterIn.config.formatIn, pConverter->channelRouter.config.channelsIn);
mal_copy_memory(pFramesOut, pDevice->playback._dspFrames, bytesToRead);
pDevice->playback._dspFrameCount -= framesToRead;
pDevice->playback._dspFrames += bytesToRead;
return framesToRead;
}
// A helper function for reading sample data from the client. Returns the number of samples read from the client. Remaining samples
// A helper function for reading sample data from the client. Returns the number of samples read from the client. Remaining samples
// are filled with silence.
// are filled with silence.
static MAL_INLINE mal_uint32 mal_device__read_frames_from_client(mal_device* pDevice, mal_uint32 frameCount, void* pSamples)
static MAL_INLINE mal_uint32 mal_device__read_frames_from_client(mal_device* pDevice, mal_uint32 frameCount, void* pSamples)
...
@@ -7156,10 +7207,10 @@ mal_result mal_device_init__wasapi(mal_context* pContext, const mal_device_confi
...
@@ -7156,10 +7207,10 @@ mal_result mal_device_init__wasapi(mal_context* pContext, const mal_device_confi
if (pConfig->deviceType == mal_device_type_capture || pConfig->deviceType == mal_device_type_duplex) {
if (pConfig->deviceType == mal_device_type_capture || pConfig->deviceType == mal_device_type_duplex) {
mal_device_init_internal_data__wasapi data;
mal_device_init_internal_data__wasapi data;
data.formatIn = pConfig->format;
data.formatIn = pConfig->
capture.
format;
data.channelsIn = pConfig->channels;
data.channelsIn = pConfig->c
apture.c
hannels;
data.sampleRateIn = pConfig->sampleRate;
data.sampleRateIn = pConfig->sampleRate;
mal_copy_memory(data.channelMapIn, pConfig->c
hannelMap, sizeof(pConfig->
channelMap));
mal_copy_memory(data.channelMapIn, pConfig->c
apture.channelMap, sizeof(pConfig->capture.
channelMap));
data.usingDefaultFormat = pDevice->usingDefaultFormat;
data.usingDefaultFormat = pDevice->usingDefaultFormat;
data.usingDefaultChannels = pDevice->usingDefaultChannels;
data.usingDefaultChannels = pDevice->usingDefaultChannels;
data.usingDefaultSampleRate = pDevice->usingDefaultSampleRate;
data.usingDefaultSampleRate = pDevice->usingDefaultSampleRate;
...
@@ -7207,25 +7258,18 @@ mal_result mal_device_init__wasapi(mal_context* pContext, const mal_device_confi
...
@@ -7207,25 +7258,18 @@ mal_result mal_device_init__wasapi(mal_context* pContext, const mal_device_confi
if (pConfig->deviceType == mal_device_type_playback || pConfig->deviceType == mal_device_type_duplex) {
if (pConfig->deviceType == mal_device_type_playback || pConfig->deviceType == mal_device_type_duplex) {
mal_device_init_internal_data__wasapi data;
mal_device_init_internal_data__wasapi data;
data.formatIn
= pConfig->
format;
data.formatIn
= pConfig->playback.
format;
data.channelsIn
= pConfig->
channels;
data.channelsIn
= pConfig->playback.
channels;
data.sampleRateIn = pConfig->sampleRate;
data.sampleRateIn
= pConfig->sampleRate;
mal_copy_memory(data.channelMapIn, pConfig->channelMap, sizeof(pConfig->channelMap));
mal_copy_memory(data.channelMapIn, pConfig->channelMap, sizeof(pConfig->channelMap));
data.usingDefaultFormat = pDevice->usingDefaultFormat;
data.usingDefaultFormat = pDevice->usingDefaultFormat;
data.usingDefaultChannels = pDevice->usingDefaultChannels;
data.usingDefaultChannels = pDevice->usingDefaultChannels;
data.usingDefaultSampleRate = pDevice->usingDefaultSampleRate;
data.usingDefaultSampleRate = pDevice->usingDefaultSampleRate;
data.usingDefaultChannelMap = pDevice->usingDefaultChannelMap;
data.usingDefaultChannelMap = pDevice->usingDefaultChannelMap;
data.shareMode = pConfig->playback.shareMode;
data.shareMode = pConfig->playback.shareMode;
data.bufferSizeInFramesIn = pConfig->bufferSizeInFrames;
// In duplex mode we want the playback device to have the same buffer size and period count as the capture device.
data.bufferSizeInMillisecondsIn = pConfig->bufferSizeInMilliseconds;
if (pConfig->deviceType == mal_device_type_duplex) {
data.periodsIn = pConfig->periods;
data.bufferSizeInFramesIn = pDevice->capture.internalBufferSizeInFrames;
data.periodsIn = pDevice->capture.internalPeriods;
} else {
data.bufferSizeInFramesIn = pConfig->bufferSizeInFrames;
data.bufferSizeInMillisecondsIn = pConfig->bufferSizeInMilliseconds;
data.periodsIn = pConfig->periods;
}
result = mal_device_init_internal__wasapi(pDevice->pContext, mal_device_type_playback, pConfig->playback.pDeviceID, &data);
result = mal_device_init_internal__wasapi(pDevice->pContext, mal_device_type_playback, pConfig->playback.pDeviceID, &data);
if (result != MAL_SUCCESS) {
if (result != MAL_SUCCESS) {
...
@@ -7446,7 +7490,7 @@ mal_result mal_device__get_available_frames__wasapi(mal_device* pDevice, mal_IAu
...
@@ -7446,7 +7490,7 @@ mal_result mal_device__get_available_frames__wasapi(mal_device* pDevice, mal_IAu
*pFrameCount = paddingFramesCount;
*pFrameCount = paddingFramesCount;
} else {
} else {
if ((mal_ptr)pAudioClient == pDevice->wasapi.pAudioClientPlayback) {
if ((mal_ptr)pAudioClient == pDevice->wasapi.pAudioClientPlayback) {
*pFrameCount =
pDevice->bufferSizeInFrames
- paddingFramesCount;
*pFrameCount =
(pDevice->playback.internalBufferSizeInFrames/pDevice->playback.internalPeriods)
- paddingFramesCount;
} else {
} else {
*pFrameCount = paddingFramesCount;
*pFrameCount = paddingFramesCount;
}
}
...
@@ -8616,11 +8660,6 @@ mal_result mal_device_init__dsound(mal_context* pContext, const mal_device_confi
...
@@ -8616,11 +8660,6 @@ mal_result mal_device_init__dsound(mal_context* pContext, const mal_device_confi
mal_assert(pDevice != NULL);
mal_assert(pDevice != NULL);
mal_zero_object(&pDevice->dsound);
mal_zero_object(&pDevice->dsound);
/* Full-duplex is not yet implemented. */
if (pConfig->deviceType == mal_device_type_duplex) {
return MAL_INVALID_ARGS;
}
/* DirectSound should use a latency of about 20ms per period for low latency mode. */
/* DirectSound should use a latency of about 20ms per period for low latency mode. */
if (pDevice->usingDefaultBufferSize) {
if (pDevice->usingDefaultBufferSize) {
if (pConfig->performanceProfile == mal_performance_profile_low_latency) {
if (pConfig->performanceProfile == mal_performance_profile_low_latency) {
...
@@ -9717,11 +9756,6 @@ mal_result mal_device_init__winmm(mal_context* pContext, const mal_device_config
...
@@ -9717,11 +9756,6 @@ mal_result mal_device_init__winmm(mal_context* pContext, const mal_device_config
mal_assert(pDevice != NULL);
mal_assert(pDevice != NULL);
mal_zero_object(&pDevice->winmm);
mal_zero_object(&pDevice->winmm);
/* Full-duplex is not yet implemented. */
if (pConfig->deviceType == mal_device_type_duplex) {
return MAL_INVALID_ARGS;
}
/* No exlusive mode with WinMM. */
/* No exlusive mode with WinMM. */
if (((pConfig->deviceType == mal_device_type_playback || pConfig->deviceType == mal_device_type_duplex) && pConfig->playback.shareMode == mal_share_mode_exclusive) ||
if (((pConfig->deviceType == mal_device_type_playback || pConfig->deviceType == mal_device_type_duplex) && pConfig->playback.shareMode == mal_share_mode_exclusive) ||
((pConfig->deviceType == mal_device_type_capture || pConfig->deviceType == mal_device_type_duplex) && pConfig->capture.shareMode == mal_share_mode_exclusive)) {
((pConfig->deviceType == mal_device_type_capture || pConfig->deviceType == mal_device_type_duplex) && pConfig->capture.shareMode == mal_share_mode_exclusive)) {
...
@@ -20657,6 +20691,50 @@ void mal_device__post_init_setup(mal_device* pDevice, mal_device_type deviceType
...
@@ -20657,6 +20691,50 @@ void mal_device__post_init_setup(mal_device* pDevice, mal_device_type deviceType
dspConfig.onRead = mal_device__on_read_from_device;
dspConfig.onRead = mal_device__on_read_from_device;
mal_pcm_converter_init(&dspConfig, &pDevice->dsp);
mal_pcm_converter_init(&dspConfig, &pDevice->dsp);
}
}
/* PCM converters. */
if (deviceType == mal_device_type_capture || deviceType == mal_device_type_duplex) {
/* Converting from internal device format to public format. */
mal_pcm_converter_config converterConfig = mal_pcm_converter_config_init_new();
converterConfig.neverConsumeEndOfInput = MAL_TRUE;
converterConfig.pUserData = pDevice;
converterConfig.formatIn = pDevice->capture.internalFormat;
converterConfig.channelsIn = pDevice->capture.internalChannels;
converterConfig.sampleRateIn = pDevice->capture.internalSampleRate;
mal_channel_map_copy(converterConfig.channelMapIn, pDevice->capture.internalChannelMap, pDevice->capture.internalChannels);
converterConfig.formatOut = pDevice->capture.format;
converterConfig.channelsOut = pDevice->capture.channels;
converterConfig.sampleRateOut = pDevice->sampleRate;
mal_channel_map_copy(converterConfig.channelMapOut, pDevice->capture.channelMap, pDevice->capture.channels);
if (deviceType == mal_device_type_capture) {
converterConfig.onRead = mal_device__on_read_from_device;
} else {
converterConfig.onRead = mal_device__pcm_converter__on_read_from_buffer_capture;
}
mal_pcm_converter_init(&converterConfig, &pDevice->capture.converter);
}
if (deviceType == mal_device_type_playback || deviceType == mal_device_type_duplex) {
/* Converting from public format to device format. */
mal_pcm_converter_config converterConfig = mal_pcm_converter_config_init_new();
converterConfig.neverConsumeEndOfInput = MAL_TRUE;
converterConfig.pUserData = pDevice;
converterConfig.formatIn = pDevice->playback.format;
converterConfig.channelsIn = pDevice->playback.channels;
converterConfig.sampleRateIn = pDevice->sampleRate;
mal_channel_map_copy(converterConfig.channelMapIn, pDevice->playback.channelMap, pDevice->playback.channels);
converterConfig.formatOut = pDevice->playback.internalFormat;
converterConfig.channelsOut = pDevice->playback.internalChannels;
converterConfig.sampleRateOut = pDevice->playback.internalSampleRate;
mal_channel_map_copy(converterConfig.channelMapOut, pDevice->playback.internalChannelMap, pDevice->playback.internalChannels);
if (deviceType == mal_device_type_playback) {
converterConfig.onRead = mal_device__on_read_from_client;
} else {
converterConfig.onRead = mal_device__pcm_converter__on_read_from_buffer_playback;
}
mal_pcm_converter_init(&converterConfig, &pDevice->playback.converter);
}
}
}
...
@@ -20700,7 +20778,13 @@ mal_thread_result MAL_THREADCALL mal_worker_thread(void* pData)
...
@@ -20700,7 +20778,13 @@ mal_thread_result MAL_THREADCALL mal_worker_thread(void* pData)
(pDevice->type == mal_device_type_duplex && pDevice->pContext->onDeviceWrite != NULL && pDevice->pContext->onDeviceRead != NULL)
(pDevice->type == mal_device_type_duplex && pDevice->pContext->onDeviceWrite != NULL && pDevice->pContext->onDeviceRead != NULL)
);
);
mal_uint32 periodSizeInFrames = pDevice->bufferSizeInFrames / pDevice->periods;
mal_uint32 periodSizeInFrames;
if (pDevice->type == mal_device_type_capture || pDevice->type == mal_device_type_duplex) {
periodSizeInFrames = pDevice->capture.internalBufferSizeInFrames / pDevice->capture.internalPeriods;
} else {
periodSizeInFrames = pDevice->playback.internalBufferSizeInFrames / pDevice->playback.internalPeriods;
}
/*
/*
With the blocking API, the device is started automatically in read()/write(). All we need to do is enter the loop and just keep reading
With the blocking API, the device is started automatically in read()/write(). All we need to do is enter the loop and just keep reading
...
@@ -20718,25 +20802,97 @@ mal_thread_result MAL_THREADCALL mal_worker_thread(void* pData)
...
@@ -20718,25 +20802,97 @@ mal_thread_result MAL_THREADCALL mal_worker_thread(void* pData)
while (mal_device__get_state(pDevice) == MAL_STATE_STARTED) {
while (mal_device__get_state(pDevice) == MAL_STATE_STARTED) {
mal_result result = MAL_SUCCESS;
mal_result result = MAL_SUCCESS;
mal_uint32 totalFramesProcessed = 0;
mal_uint32 totalFramesProcessed = 0;
mal_uint8 buffer[4096];
mal_uint32 bufferSizeInFrames = sizeof(buffer) / mal_get_bytes_per_frame(pDevice->internalFormat, pDevice->internalChannels);
while (totalFramesProcessed < periodSizeInFrames) {
mal_uint32 framesRemaining = periodSizeInFrames - totalFramesProcessed;
mal_uint32 framesToProcess = framesRemaining;
if (framesToProcess > bufferSizeInFrames) {
framesToProcess = bufferSizeInFrames;
}
if (pDevice->type == mal_device_type_playback) {
if (pDevice->type == mal_device_type_duplex) {
mal_device__read_frames_from_client(pDevice, framesToProcess, buffer);
/* The process is device_read -> convert -> callback -> convert -> device_write. */
result = pDevice->pContext->onDeviceWrite(pDevice, buffer, framesToProcess);
mal_uint8 captureDeviceData[4096];
} else {
mal_uint32 captureDeviceDataCapInFrames = sizeof(captureDeviceData) / mal_get_bytes_per_frame(pDevice->capture.internalFormat, pDevice->capture.internalChannels);
result = pDevice->pContext->onDeviceRead(pDevice, buffer, framesToProcess);
mal_device__send_frames_to_client(pDevice, framesToProcess, buffer);
while (totalFramesProcessed < periodSizeInFrames) {
mal_uint32 framesRemaining = periodSizeInFrames - totalFramesProcessed;
mal_uint32 framesToProcess = framesRemaining;
if (framesToProcess > captureDeviceDataCapInFrames) {
framesToProcess = captureDeviceDataCapInFrames;
}
result = pDevice->pContext->onDeviceRead(pDevice, captureDeviceData, framesToProcess);
if (result != MAL_SUCCESS) {
break;
}
mal_device_callback_proc onData = pDevice->onData;
if (onData != NULL) {
pDevice->capture._dspFrameCount = framesToProcess;
pDevice->capture._dspFrames = captureDeviceData;
/* We need to process every input frame. */
for (;;) {
mal_uint8 capturedData[4096]; // In capture.format/channels format
mal_uint8 playbackData[4096]; // In playback.format/channels format
mal_uint32 capturedDataCapInFrames = sizeof(capturedData) / mal_get_bytes_per_frame(pDevice->capture.format, pDevice->capture.channels);
mal_uint32 playbackDataCapInFrames = sizeof(playbackData) / mal_get_bytes_per_frame(pDevice->playback.format, pDevice->playback.channels);
mal_uint32 capturedFramesToTryProcessing = mal_min(capturedDataCapInFrames, playbackDataCapInFrames);
mal_uint32 capturedFramesToProcess = (mal_uint32)mal_pcm_converter_read(&pDevice->capture.converter, capturedFramesToTryProcessing, capturedData, pDevice->capture.converter.pUserData);
if (capturedFramesToProcess == 0) {
break; /* Don't fire the data callback with zero frames. */
}
onData(pDevice, playbackData, capturedData, capturedFramesToProcess);
/* At this point the playbackData buffer should be holding data that needs to be written to the device. */
pDevice->playback._dspFrameCount = capturedFramesToProcess;
pDevice->playback._dspFrames = playbackData;
for (;;) {
mal_uint8 playbackDeviceData[4096];
mal_uint32 playbackDeviceDataCapInFrames = sizeof(playbackDeviceData) / mal_get_bytes_per_frame(pDevice->playback.internalFormat, pDevice->playback.internalChannels);
mal_uint32 playbackDeviceFramesCount = (mal_uint32)mal_pcm_converter_read(&pDevice->playback.converter, playbackDeviceDataCapInFrames, playbackDeviceData, pDevice->playback.converter.pUserData);
if (playbackDeviceFramesCount == 0) {
break;
}
result = pDevice->pContext->onDeviceWrite(pDevice, playbackDeviceData, playbackDeviceFramesCount);
if (result != MAL_SUCCESS) {
break;
}
}
if (capturedFramesToProcess < capturedFramesToTryProcessing) {
break;
}
/* In case an error happened from onDeviceWrite()... */
if (result != MAL_SUCCESS) {
break;
}
}
}
totalFramesProcessed += framesToProcess;
}
}
} else {
mal_uint8 buffer[4096];
mal_uint32 bufferSizeInFrames = sizeof(buffer) / mal_get_bytes_per_frame(pDevice->internalFormat, pDevice->internalChannels);
while (totalFramesProcessed < periodSizeInFrames) {
mal_uint32 framesRemaining = periodSizeInFrames - totalFramesProcessed;
mal_uint32 framesToProcess = framesRemaining;
if (framesToProcess > bufferSizeInFrames) {
framesToProcess = bufferSizeInFrames;
}
if (pDevice->type == mal_device_type_playback) {
mal_device__read_frames_from_client(pDevice, framesToProcess, buffer);
result = pDevice->pContext->onDeviceWrite(pDevice, buffer, framesToProcess);
} else {
result = pDevice->pContext->onDeviceRead(pDevice, buffer, framesToProcess);
mal_device__send_frames_to_client(pDevice, framesToProcess, buffer);
}
totalFramesProcessed += framesToProcess;
totalFramesProcessed += framesToProcess;
}
}
}
/* Get out of the loop if read()/write() returned an error. It probably means the device has been stopped. */
/* Get out of the loop if read()/write() returned an error. It probably means the device has been stopped. */
tests/mal_duplex.c
View file @
32c64703
...
@@ -3,6 +3,9 @@
...
@@ -3,6 +3,9 @@
#define MINI_AL_IMPLEMENTATION
#define MINI_AL_IMPLEMENTATION
#include "../mini_al.h"
#include "../mini_al.h"
#define DR_WAV_IMPLEMENTATION
#include "../extras/dr_wav.h"
void
log_callback
(
mal_context
*
pContext
,
mal_device
*
pDevice
,
mal_uint32
logLevel
,
const
char
*
message
)
void
log_callback
(
mal_context
*
pContext
,
mal_device
*
pDevice
,
mal_uint32
logLevel
,
const
char
*
message
)
{
{
(
void
)
pContext
;
(
void
)
pContext
;
...
@@ -21,14 +24,31 @@ void data_callback(mal_device* pDevice, void* pOutput, const void* pInput, mal_u
...
@@ -21,14 +24,31 @@ void data_callback(mal_device* pDevice, void* pOutput, const void* pInput, mal_u
{
{
/* In this test the format and channel count are the same for both input and output which means we can just memcpy(). */
/* In this test the format and channel count are the same for both input and output which means we can just memcpy(). */
mal_copy_memory
(
pOutput
,
pInput
,
frameCount
*
mal_get_bytes_per_frame
(
pDevice
->
format
,
pDevice
->
channels
));
mal_copy_memory
(
pOutput
,
pInput
,
frameCount
*
mal_get_bytes_per_frame
(
pDevice
->
format
,
pDevice
->
channels
));
/* Also write to a wav file for debugging. */
drwav
*
pWav
=
(
drwav
*
)
pDevice
->
pUserData
;
mal_assert
(
pWav
!=
NULL
);
drwav_write_pcm_frames
(
pWav
,
frameCount
,
pInput
);
}
}
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
mal_result
result
;
mal_result
result
;
(
void
)
argc
;
drwav_data_format
wavFormat
;
(
void
)
argv
;
wavFormat
.
container
=
drwav_container_riff
;
wavFormat
.
format
=
DR_WAVE_FORMAT_IEEE_FLOAT
;
wavFormat
.
channels
=
2
;
wavFormat
.
sampleRate
=
44100
;
wavFormat
.
bitsPerSample
=
32
;
drwav
wav
;
if
(
drwav_init_file_write
(
&
wav
,
"output.wav"
,
&
wavFormat
)
==
DRWAV_FALSE
)
{
printf
(
"Failed to initialize output file.
\n
"
);
return
-
1
;
}
mal_backend
backend
=
mal_backend_wasapi
;
mal_backend
backend
=
mal_backend_wasapi
;
...
@@ -43,16 +63,18 @@ int main(int argc, char** argv)
...
@@ -43,16 +63,18 @@ int main(int argc, char** argv)
}
}
mal_device_config
deviceConfig
=
mal_device_config_init
(
mal_device_type_duplex
);
mal_device_config
deviceConfig
=
mal_device_config_init
(
mal_device_type_duplex
);
deviceConfig
.
pPlaybackDeviceID
=
NULL
;
deviceConfig
.
capture
.
pDeviceID
=
NULL
;
deviceConfig
.
format
=
mal_format_f32
;
deviceConfig
.
capture
.
format
=
mal_format_f32
;
deviceConfig
.
channels
=
2
;
deviceConfig
.
capture
.
channels
=
2
;
deviceConfig
.
sampleRate
=
44100
;
deviceConfig
.
playback
.
pDeviceID
=
NULL
;
deviceConfig
.
bufferSizeInMilliseconds
=
50
;
deviceConfig
.
playback
.
format
=
mal_format_f32
;
deviceConfig
.
periods
=
3
;
deviceConfig
.
playback
.
channels
=
2
;
deviceConfig
.
shareMode
=
mal_share_mode_shared
;
deviceConfig
.
sampleRate
=
44100
;
deviceConfig
.
dataCallback
=
data_callback
;
deviceConfig
.
bufferSizeInMilliseconds
=
1000
;
deviceConfig
.
stopCallback
=
stop_callback
;
deviceConfig
.
periods
=
3
;
deviceConfig
.
pUserData
=
NULL
;
deviceConfig
.
dataCallback
=
data_callback
;
deviceConfig
.
stopCallback
=
stop_callback
;
deviceConfig
.
pUserData
=
&
wav
;
mal_device
device
;
mal_device
device
;
result
=
mal_device_init
(
&
context
,
&
deviceConfig
,
&
device
);
result
=
mal_device_init
(
&
context
,
&
deviceConfig
,
&
device
);
...
@@ -66,5 +88,9 @@ int main(int argc, char** argv)
...
@@ -66,5 +88,9 @@ int main(int argc, char** argv)
getchar
();
getchar
();
mal_device_uninit
(
&
device
);
mal_device_uninit
(
&
device
);
drwav_uninit
(
&
wav
);
(
void
)
argc
;
(
void
)
argv
;
return
0
;
return
0
;
}
}
\ No newline at end of file
tests/mal_test_0.vcxproj
View file @
32c64703
...
@@ -272,11 +272,11 @@
...
@@ -272,11 +272,11 @@
</ClCompile>
</ClCompile>
<ClCompile
Include=
"..\examples\simple_capture.c"
>
<ClCompile
Include=
"..\examples\simple_capture.c"
>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
true
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
true
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
tru
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
fals
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|ARM'"
>
tru
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|ARM'"
>
fals
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|ARM'"
>
tru
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|ARM'"
>
fals
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
tru
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
fals
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
tru
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
fals
e
</ExcludedFromBuild>
</ClCompile>
</ClCompile>
<ClCompile
Include=
"..\examples\simple_enumeration.c"
>
<ClCompile
Include=
"..\examples\simple_enumeration.c"
>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
true
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
true
</ExcludedFromBuild>
...
@@ -319,7 +319,7 @@
...
@@ -319,7 +319,7 @@
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
true
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
true
</ExcludedFromBuild>
</ClCompile>
</ClCompile>
<ClCompile
Include=
"mal_duplex.c"
>
<ClCompile
Include=
"mal_duplex.c"
>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
tru
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
fals
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
true
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
true
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|ARM'"
>
true
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|ARM'"
>
true
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|ARM'"
>
true
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|ARM'"
>
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'"
>
fals
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|Win32'"
>
tru
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|ARM'"
>
fals
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|ARM'"
>
tru
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
fals
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|Win32'"
>
tru
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|ARM'"
>
fals
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|ARM'"
>
tru
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
fals
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Debug|x64'"
>
tru
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
fals
e
</ExcludedFromBuild>
<ExcludedFromBuild
Condition=
"'$(Configuration)|$(Platform)'=='Release|x64'"
>
tru
e
</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>
...
@@ -386,6 +386,7 @@
...
@@ -386,6 +386,7 @@
<ItemGroup>
<ItemGroup>
<ClInclude
Include=
"..\mini_al.h"
/>
<ClInclude
Include=
"..\mini_al.h"
/>
<ClInclude
Include=
"..\research\mal_resampler.h"
/>
<ClInclude
Include=
"..\research\mal_resampler.h"
/>
<ClInclude
Include=
"..\research\mal_ring_buffer.h"
/>
</ItemGroup>
</ItemGroup>
<Import
Project=
"$(VCTargetsPath)\Microsoft.Cpp.targets"
/>
<Import
Project=
"$(VCTargetsPath)\Microsoft.Cpp.targets"
/>
<ImportGroup
Label=
"ExtensionTargets"
>
<ImportGroup
Label=
"ExtensionTargets"
>
...
...
tests/mal_test_0.vcxproj.filters
View file @
32c64703
...
@@ -68,5 +68,8 @@
...
@@ -68,5 +68,8 @@
<ClInclude
Include=
"..\research\mal_resampler.h"
>
<ClInclude
Include=
"..\research\mal_resampler.h"
>
<Filter>
Source Files
</Filter>
<Filter>
Source Files
</Filter>
</ClInclude>
</ClInclude>
<ClInclude
Include=
"..\research\mal_ring_buffer.h"
>
<Filter>
Source Files
</Filter>
</ClInclude>
</ItemGroup>
</ItemGroup>
</Project>
</Project>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment