Debian Codegraph @

extras/osaudio/tests/osaudio_sine.c

raw symbols SCIP
1 #include "../osaudio.h"
2 #include "../../decoders/litewav/litewav.c"
3
4 #include <stdio.h>
5 #include <stdlib.h> /* free() */
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 /* Sine wave generation. */
64 #include <math.h>
65
66 #if defined(OSAUDIO_DOS)
67 /* For farmalloc(). */
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 //float sinePhase = 0;
149 //float sinePhaseIncrement = 0;
150 //float sineVolume = 0.2f;
151 //
152 //static void sine_init()
153 //{
154 // sinePhase = 0;
155 // sinePhaseIncrement = 2 * 3.14159265f * 440.0f / 44100.0f;
156 //}
157 //
158 //static void sine_u8(unsigned char* dst, unsigned int frameCount, unsigned int channels)
159 //{
160 // unsigned int iFrame;
161 //
162 // for (iFrame = 0; iFrame < frameCount; iFrame += 1) {
163 // unsigned int iChannel;
164 // float sample = (float)sin(sinePhase) * sineVolume;
165 // sample = (sample + 1.0f) * 127.5f;
166 //
167 // for (iChannel = 0; iChannel < channels; iChannel += 1) {
168 // dst[iChannel] = (unsigned char)sample;
169 // }
170 //
171 // dst += channels;
172 // sinePhase += sinePhaseIncrement;
173 // }
174 //}
175 //
176 //
177 //unsigned char data[4096];
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 //printf("sizeof(void*) = %u\n", (unsigned int)sizeof(void far *));
204
205 /* 5 seconds. */
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 /*unsigned int framesToSilence = config.rate;
221 while (framesToSilence > 0) {
222 unsigned int framesToWrite;
223 char silence[256];
224 memset(silence, 128, sizeof(silence));
225
226 framesToWrite = framesToSilence;
227 if (framesToWrite > sizeof(silence) / config.channels) {
228 framesToWrite = sizeof(silence) / config.channels;
229 }
230
231 osaudio_write(audio, silence, framesToWrite);
232 framesToSilence -= framesToWrite;
233 }*/
234
235 while (sineWaveCursor < sineWaveFrameCount) {
236 unsigned long framesToWrite = sineWaveFrameCount - sineWaveCursor;
237 if (framesToWrite > 0xFFFF) {
238 framesToWrite = 0xFFFF;
239 }
240
241 //printf("Writing sine wave: %u\n", (unsigned int)framesToWrite);
242
243 osaudio_write(audio, (char OSAUDIO_FAR*)pSineWave + (sineWaveCursor * config.channels), (unsigned int)framesToWrite);
244 sineWaveCursor += framesToWrite;
245
246 //printf("TRACE 0\n");
247 //sine_u8(data, frameCount, config.channels);
248 //printf("TRACE: %d\n", frameCount);
249 //osaudio_write(audio, data, frameCount);
250 //printf("DONE LOOP\n");
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 /* Temporary. Just spinning here to ensure the program stays active. */
268 //delay(1);
269 if (g_TESTING > 0) {
270 //printf("TESTING: %d\n", g_TESTING);
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 }