| 1 |
#include "../osaudio.h" |
| 2 |
#include "../../decoders/litewav/litewav.c" |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
|
| 7 |
#if defined(__MSDOS__) || defined(__DOS__) |
| 8 |
#include <dos.h> |
| 9 |
#define OSAUDIO_DOS |
| 10 |
#endif |
| 11 |
|
| 12 |
const char* format_to_string(osaudio_format_t format) |
| 13 |
{ |
| 14 |
switch (format) |
| 15 |
{ |
| 16 |
case OSAUDIO_FORMAT_F32: return "F32"; |
| 17 |
case OSAUDIO_FORMAT_U8: return "U8"; |
| 18 |
case OSAUDIO_FORMAT_S16: return "S16"; |
| 19 |
case OSAUDIO_FORMAT_S24: return "S24"; |
| 20 |
case OSAUDIO_FORMAT_S32: return "S32"; |
| 21 |
default: return "Unknown Format"; |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
void enumerate_devices() |
| 26 |
{ |
| 27 |
osaudio_result_t result; |
| 28 |
osaudio_info_t* pDeviceInfos; |
| 29 |
unsigned int deviceCount; |
| 30 |
unsigned int iDevice; |
| 31 |
|
| 32 |
result = osaudio_enumerate(&deviceCount, &pDeviceInfos); |
| 33 |
if (result != OSAUDIO_SUCCESS) { |
| 34 |
printf("Failed to enumerate devices."); |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
for (iDevice = 0; iDevice < deviceCount; iDevice += 1) { |
| 39 |
osaudio_info_t* pDeviceInfo = &pDeviceInfos[iDevice]; |
| 40 |
|
| 41 |
printf("Device %u: [%s] %s\n", iDevice, (pDeviceInfo->direction == OSAUDIO_OUTPUT) ? "Playback" : "Capture", pDeviceInfo->name); |
| 42 |
|
| 43 |
#if 0 |
| 44 |
{ |
| 45 |
unsigned int iFormat; |
| 46 |
|
| 47 |
printf(" Native Formats\n"); |
| 48 |
for (iFormat = 0; iFormat < pDeviceInfo->config_count; iFormat += 1) { |
| 49 |
osaudio_config_t* pConfig = &pDeviceInfo->configs[iFormat]; |
| 50 |
printf(" %s %uHz %u channels\n", format_to_string(pConfig->format), pConfig->rate, pConfig->channels); |
| 51 |
} |
| 52 |
} |
| 53 |
#endif |
| 54 |
} |
| 55 |
|
| 56 |
free(pDeviceInfos); |
| 57 |
} |
| 58 |
|
| 59 |
extern int g_TESTING; |
| 60 |
|
| 61 |
#include <string.h> |
| 62 |
|
| 63 |
|
| 64 |
#include <math.h> |
| 65 |
|
| 66 |
#if defined(OSAUDIO_DOS) |
| 67 |
|
| 68 |
static void OSAUDIO_FAR* far_malloc(unsigned int sz) |
| 69 |
{ |
| 70 |
unsigned int segment; |
| 71 |
unsigned int err; |
| 72 |
|
| 73 |
err = _dos_allocmem(sz >> 4, &segment); |
| 74 |
if (err == 0) { |
| 75 |
return MK_FP(segment, 0); |
| 76 |
} else { |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
} |
| 80 |
#else |
| 81 |
#define far_malloc malloc |
| 82 |
#endif |
| 83 |
|
| 84 |
static char OSAUDIO_FAR* gen_sine_u8(unsigned long frameCount, unsigned int channels, unsigned int sampleRate) |
| 85 |
{ |
| 86 |
float phase = 0; |
| 87 |
float phaseIncrement = 2 * 3.14159265f * 220.0f / 44100.0f; |
| 88 |
unsigned long iFrame; |
| 89 |
char OSAUDIO_FAR* pData; |
| 90 |
char OSAUDIO_FAR* pRunningData; |
| 91 |
|
| 92 |
pData = (char OSAUDIO_FAR*)far_malloc(frameCount * channels); |
| 93 |
if (pData == NULL) { |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
|
| 97 |
pRunningData = pData; |
| 98 |
|
| 99 |
for (iFrame = 0; iFrame < frameCount; iFrame += 1) { |
| 100 |
unsigned int iChannel; |
| 101 |
float sample = (float)sin(phase) * 0.2f; |
| 102 |
sample = (sample + 1.0f) * 127.5f; |
| 103 |
|
| 104 |
for (iChannel = 0; iChannel < channels; iChannel += 1) { |
| 105 |
pRunningData[iChannel] = (unsigned char)sample; |
| 106 |
} |
| 107 |
|
| 108 |
pRunningData += channels; |
| 109 |
phase += phaseIncrement; |
| 110 |
} |
| 111 |
|
| 112 |
return pData; |
| 113 |
} |
| 114 |
|
| 115 |
static short OSAUDIO_FAR* gen_sine_s16(unsigned long frameCount, unsigned int channels, unsigned int sampleRate) |
| 116 |
{ |
| 117 |
float phase = 0; |
| 118 |
float phaseIncrement = 2 * 3.14159265f * 220.0f / 44100.0f; |
| 119 |
unsigned long iFrame; |
| 120 |
short OSAUDIO_FAR* pData; |
| 121 |
short OSAUDIO_FAR* pRunningData; |
| 122 |
|
| 123 |
pData = (short OSAUDIO_FAR*)far_malloc(frameCount * channels * sizeof(short)); |
| 124 |
if (pData == NULL) { |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
|
| 128 |
pRunningData = pData; |
| 129 |
|
| 130 |
for (iFrame = 0; iFrame < frameCount; iFrame += 1) { |
| 131 |
unsigned int iChannel; |
| 132 |
float sample = (float)sin(phase) * 0.2f; |
| 133 |
sample = sample * 32767.5f; |
| 134 |
|
| 135 |
for (iChannel = 0; iChannel < channels; iChannel += 1) { |
| 136 |
pRunningData[iChannel] = (short)sample; |
| 137 |
} |
| 138 |
|
| 139 |
pRunningData += channels; |
| 140 |
phase += phaseIncrement; |
| 141 |
} |
| 142 |
|
| 143 |
return pData; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
int main(int argc, char** argv) |
| 180 |
{ |
| 181 |
osaudio_result_t result; |
| 182 |
osaudio_t audio; |
| 183 |
osaudio_config_t config; |
| 184 |
void OSAUDIO_FAR* pSineWave; |
| 185 |
unsigned long sineWaveFrameCount; |
| 186 |
unsigned long sineWaveCursor = 0; |
| 187 |
|
| 188 |
enumerate_devices(); |
| 189 |
|
| 190 |
osaudio_config_init(&config, OSAUDIO_OUTPUT); |
| 191 |
config.format = OSAUDIO_FORMAT_S16; |
| 192 |
config.channels = 2; |
| 193 |
config.rate = 44100; |
| 194 |
|
| 195 |
result = osaudio_open(&audio, &config); |
| 196 |
if (result != OSAUDIO_SUCCESS) { |
| 197 |
printf("Failed to initialize audio.\n"); |
| 198 |
return -1; |
| 199 |
} |
| 200 |
|
| 201 |
printf("Device: %s (%s %uHz %u channels)\n", osaudio_get_info(audio)->name, format_to_string(config.format), config.rate, config.channels); |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
sineWaveFrameCount = config.rate * 1; |
| 207 |
|
| 208 |
if (config.format == OSAUDIO_FORMAT_U8) { |
| 209 |
pSineWave = gen_sine_u8(sineWaveFrameCount, config.channels, config.rate); |
| 210 |
} else { |
| 211 |
pSineWave = gen_sine_s16(sineWaveFrameCount, config.channels, config.rate); |
| 212 |
} |
| 213 |
|
| 214 |
if (pSineWave == NULL) { |
| 215 |
printf("Failed to generate sine wave.\n"); |
| 216 |
return -1; |
| 217 |
} |
| 218 |
|
| 219 |
if (config.format == OSAUDIO_FORMAT_U8) { |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
while (sineWaveCursor < sineWaveFrameCount) { |
| 236 |
unsigned long framesToWrite = sineWaveFrameCount - sineWaveCursor; |
| 237 |
if (framesToWrite > 0xFFFF) { |
| 238 |
framesToWrite = 0xFFFF; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
osaudio_write(audio, (char OSAUDIO_FAR*)pSineWave + (sineWaveCursor * config.channels), (unsigned int)framesToWrite); |
| 244 |
sineWaveCursor += framesToWrite; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
} |
| 252 |
} else if (config.format == OSAUDIO_FORMAT_S16) { |
| 253 |
while (sineWaveCursor < sineWaveFrameCount) { |
| 254 |
unsigned long framesToWrite = sineWaveFrameCount - sineWaveCursor; |
| 255 |
if (framesToWrite > 0xFFFF) { |
| 256 |
framesToWrite = 0xFFFF; |
| 257 |
} |
| 258 |
|
| 259 |
osaudio_write(audio, (short OSAUDIO_FAR*)pSineWave + (sineWaveCursor * config.channels), (unsigned int)framesToWrite); |
| 260 |
sineWaveCursor += framesToWrite; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
#if defined(OSAUDIO_DOS) |
| 265 |
printf("Processing...\n"); |
| 266 |
for (;;) { |
| 267 |
|
| 268 |
|
| 269 |
if (g_TESTING > 0) { |
| 270 |
|
| 271 |
} |
| 272 |
} |
| 273 |
#endif |
| 274 |
|
| 275 |
printf("Shutting down... "); |
| 276 |
osaudio_close(audio); |
| 277 |
printf("Done.\n"); |
| 278 |
|
| 279 |
(void)argc; |
| 280 |
(void)argv; |
| 281 |
|
| 282 |
return 0; |
| 283 |
} |