| 1 |
#include "../osaudio.h" |
| 2 |
|
| 3 |
|
| 4 |
#define MINIAUDIO_IMPLEMENTATION |
| 5 |
#include "../../../miniaudio.h" |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#define MODE_PLAYBACK 0 |
| 12 |
#define MODE_CAPTURE 1 |
| 13 |
#define MODE_DUPLEX 2 |
| 14 |
|
| 15 |
void enumerate_devices() |
| 16 |
{ |
| 17 |
int result; |
| 18 |
unsigned int iDevice; |
| 19 |
unsigned int count; |
| 20 |
osaudio_info_t* pDeviceInfos; |
| 21 |
|
| 22 |
result = osaudio_enumerate(&count, &pDeviceInfos); |
| 23 |
if (result != OSAUDIO_SUCCESS) { |
| 24 |
printf("Failed to enumerate audio devices.\n"); |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
for (iDevice = 0; iDevice < count; iDevice += 1) { |
| 29 |
printf("(%s) %s\n", (pDeviceInfos[iDevice].direction == OSAUDIO_OUTPUT) ? "Playback" : "Capture", pDeviceInfos[iDevice].name); |
| 30 |
} |
| 31 |
|
| 32 |
free(pDeviceInfos); |
| 33 |
} |
| 34 |
|
| 35 |
osaudio_t open_device(int direction) |
| 36 |
{ |
| 37 |
int result; |
| 38 |
osaudio_t audio; |
| 39 |
osaudio_config_t config; |
| 40 |
|
| 41 |
osaudio_config_init(&config, direction); |
| 42 |
config.format = OSAUDIO_FORMAT_F32; |
| 43 |
config.channels = 2; |
| 44 |
config.rate = 48000; |
| 45 |
config.flags = OSAUDIO_FLAG_REPORT_XRUN; |
| 46 |
|
| 47 |
result = osaudio_open(&audio, &config); |
| 48 |
if (result != OSAUDIO_SUCCESS) { |
| 49 |
printf("Failed to open audio device.\n"); |
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
return audio; |
| 54 |
} |
| 55 |
|
| 56 |
void do_playback(int argc, char** argv) |
| 57 |
{ |
| 58 |
int result; |
| 59 |
osaudio_t audio; |
| 60 |
const osaudio_config_t* config; |
| 61 |
const char* pFilePath = NULL; |
| 62 |
ma_result resultMA; |
| 63 |
ma_decoder_config decoderConfig; |
| 64 |
ma_decoder decoder; |
| 65 |
|
| 66 |
audio = open_device(OSAUDIO_OUTPUT); |
| 67 |
if (audio == NULL) { |
| 68 |
printf("Failed to open audio device.\n"); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
config = &osaudio_get_info(audio)->configs[0]; |
| 73 |
|
| 74 |
|
| 75 |
if (config->format == OSAUDIO_FORMAT_F32) { |
| 76 |
if (argc > 1) { |
| 77 |
pFilePath = argv[1]; |
| 78 |
|
| 79 |
decoderConfig = ma_decoder_config_init(ma_format_f32, (ma_uint32)config->channels, (ma_uint32)config->rate); |
| 80 |
|
| 81 |
resultMA = ma_decoder_init_file(pFilePath, &decoderConfig, &decoder); |
| 82 |
if (resultMA == MA_SUCCESS) { |
| 83 |
|
| 84 |
for (;;) { |
| 85 |
float frames[1024]; |
| 86 |
ma_uint64 frameCount; |
| 87 |
|
| 88 |
resultMA = ma_decoder_read_pcm_frames(&decoder, frames, ma_countof(frames) / config->channels, &frameCount); |
| 89 |
if (resultMA != MA_SUCCESS) { |
| 90 |
break; |
| 91 |
} |
| 92 |
|
| 93 |
result = osaudio_write(audio, frames, (unsigned int)frameCount); |
| 94 |
if (result != OSAUDIO_SUCCESS && result != OSAUDIO_XRUN) { |
| 95 |
printf("Error writing to audio device."); |
| 96 |
break; |
| 97 |
} |
| 98 |
|
| 99 |
if (result == OSAUDIO_XRUN) { |
| 100 |
printf("WARNING: An xrun occurred while writing to the playback device.\n"); |
| 101 |
} |
| 102 |
} |
| 103 |
} else { |
| 104 |
printf("Failed to open file: %s\n", pFilePath); |
| 105 |
} |
| 106 |
} else { |
| 107 |
printf("No input file.\n"); |
| 108 |
} |
| 109 |
} else { |
| 110 |
printf("Unsupported device format.\n"); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
osaudio_close(audio); |
| 115 |
} |
| 116 |
|
| 117 |
void do_duplex() |
| 118 |
{ |
| 119 |
int result; |
| 120 |
osaudio_t capture; |
| 121 |
osaudio_t playback; |
| 122 |
|
| 123 |
capture = open_device(OSAUDIO_INPUT); |
| 124 |
if (capture == NULL) { |
| 125 |
printf("Failed to open capture device.\n"); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
playback = open_device(OSAUDIO_OUTPUT); |
| 130 |
if (playback == NULL) { |
| 131 |
osaudio_close(capture); |
| 132 |
printf("Failed to open playback device.\n"); |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
for (;;) { |
| 137 |
float frames[1024]; |
| 138 |
unsigned int frameCount; |
| 139 |
|
| 140 |
frameCount = ma_countof(frames) / osaudio_get_info(capture)->configs[0].channels; |
| 141 |
|
| 142 |
|
| 143 |
result = osaudio_read(capture, frames, frameCount); |
| 144 |
if (result != OSAUDIO_SUCCESS && result != OSAUDIO_XRUN) { |
| 145 |
printf("Error reading from capture device.\n"); |
| 146 |
break; |
| 147 |
} |
| 148 |
|
| 149 |
if (result == OSAUDIO_XRUN) { |
| 150 |
printf("WARNING: An xrun occurred while reading from the capture device.\n"); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
result = osaudio_write(playback, frames, frameCount); |
| 156 |
if (result != OSAUDIO_SUCCESS && result != OSAUDIO_XRUN) { |
| 157 |
printf("Error writing to playback device.\n"); |
| 158 |
break; |
| 159 |
} |
| 160 |
|
| 161 |
if (result == OSAUDIO_XRUN) { |
| 162 |
printf("WARNING: An xrun occurred while writing to the playback device.\n"); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
osaudio_close(capture); |
| 167 |
osaudio_close(playback); |
| 168 |
} |
| 169 |
|
| 170 |
int main(int argc, char** argv) |
| 171 |
{ |
| 172 |
int mode = MODE_PLAYBACK; |
| 173 |
int iarg; |
| 174 |
|
| 175 |
enumerate_devices(); |
| 176 |
|
| 177 |
for (iarg = 0; iarg < argc; iarg += 1) { |
| 178 |
if (strcmp(argv[iarg], "capture") == 0) { |
| 179 |
mode = MODE_CAPTURE; |
| 180 |
} else if (strcmp(argv[iarg], "duplex") == 0) { |
| 181 |
mode = MODE_DUPLEX; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
switch (mode) |
| 186 |
{ |
| 187 |
case MODE_PLAYBACK: do_playback(argc, argv); break; |
| 188 |
case MODE_CAPTURE: break; |
| 189 |
case MODE_DUPLEX: do_duplex(); break; |
| 190 |
} |
| 191 |
|
| 192 |
(void)argc; |
| 193 |
(void)argv; |
| 194 |
|
| 195 |
return 0; |
| 196 |
} |