pvrecorder/src/miniaudio/examples/simple_duplex.c
Go to the documentation of this file.
1 /*
2 Demonstrates duplex mode which is where data is captured from a microphone and then output to a speaker device.
3 
4 This example captures audio from the default microphone and then outputs it straight to the default playback device
5 without any kind of modification. If you wanted to, you could also apply filters and effects to the input stream
6 before outputting to the playback device.
7 
8 Note that the microphone and playback device must run in lockstep. Any kind of timing deviation will result in audible
9 glitching which the backend may not be able to recover from. For this reason, miniaudio forces you to use the same
10 sample rate for both capture and playback. If internally the native sample rates differ, miniaudio will perform the
11 sample rate conversion for you automatically.
12 */
13 #define MINIAUDIO_IMPLEMENTATION
14 #include "../miniaudio.h"
15 
16 #include <stdio.h>
17 
18 #ifdef __EMSCRIPTEN__
19 void main_loop__em()
20 {
21 }
22 #endif
23 
24 void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)
25 {
26  MA_ASSERT(pDevice->capture.format == pDevice->playback.format);
27  MA_ASSERT(pDevice->capture.channels == pDevice->playback.channels);
28 
29  /* In this example the format and channel count are the same for both input and output which means we can just memcpy(). */
30  MA_COPY_MEMORY(pOutput, pInput, frameCount * ma_get_bytes_per_frame(pDevice->capture.format, pDevice->capture.channels));
31 }
32 
33 int main(int argc, char** argv)
34 {
35  ma_result result;
36  ma_device_config deviceConfig;
38 
40  deviceConfig.capture.pDeviceID = NULL;
41  deviceConfig.capture.format = ma_format_s16;
42  deviceConfig.capture.channels = 2;
43  deviceConfig.capture.shareMode = ma_share_mode_shared;
44  deviceConfig.playback.pDeviceID = NULL;
45  deviceConfig.playback.format = ma_format_s16;
46  deviceConfig.playback.channels = 2;
47  deviceConfig.dataCallback = data_callback;
48  result = ma_device_init(NULL, &deviceConfig, &device);
49  if (result != MA_SUCCESS) {
50  return result;
51  }
52 
53 #ifdef __EMSCRIPTEN__
54  getchar();
55 #endif
56 
58 
59 #ifdef __EMSCRIPTEN__
60  emscripten_set_main_loop(main_loop__em, 0, 1);
61 #else
62  printf("Press Enter to quit...\n");
63  getchar();
64 #endif
65 
67 
68  (void)argc;
69  (void)argv;
70  return 0;
71 }
ma_get_bytes_per_frame
static MA_INLINE ma_uint32 ma_get_bytes_per_frame(ma_format format, ma_uint32 channels)
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:2673
ma_device_config::pDeviceID
ma_device_id * pDeviceID
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:3127
ma_device_config::shareMode
ma_share_mode shareMode
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:3131
ma_device_uninit
void ma_device_uninit(ma_device *pDevice)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:27425
ma_device_start
ma_result ma_device_start(ma_device *pDevice)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:27485
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
ma_device::channels
ma_uint32 channels
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:3626
ma_device::format
ma_format format
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:3625
data_callback
void data_callback(ma_device *pDevice, void *pOutput, const void *pInput, ma_uint32 frameCount)
Definition: pvrecorder/src/miniaudio/examples/simple_duplex.c:24
ma_device_config::dataCallback
ma_device_callback_proc dataCallback
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:3110
device
ma_device device
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_deviceio/ma_test_deviceio.c:57
ma_device_config::capture
struct ma_device_config::@98 capture
MA_COPY_MEMORY
#define MA_COPY_MEMORY(dst, src, sz)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:754
ma_result
int ma_result
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1658
ma_share_mode_shared
@ ma_share_mode_shared
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:3020
main
int main(int argc, char **argv)
Definition: pvrecorder/src/miniaudio/examples/simple_duplex.c:33
ma_format_s16
@ ma_format_s16
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1787
MA_ASSERT
#define MA_ASSERT(condition)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:770
ma_uint32
uint32_t ma_uint32
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1503
ma_device_config::format
ma_format format
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:3128
ma_device_config_init
ma_device_config ma_device_config_init(ma_device_type deviceType)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:27034
ma_device_config::channels
ma_uint32 channels
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:3129
python.test_porcupine.argv
argv
Definition: test_porcupine.py:158
ma_device_init
ma_result ma_device_init(ma_context *pContext, const ma_device_config *pConfig, ma_device *pDevice)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:27048
ma_device
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:3584
ma_device_config
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:3100
ma_device::capture
struct ma_device::@116 capture
MA_SUCCESS
#define MA_SUCCESS
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:1659
ma_device_config::playback
struct ma_device_config::@97 playback
ma_device_type_duplex
@ ma_device_type_duplex
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/miniaudio.h:3014
ma_device::playback
struct ma_device::@115 playback


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50