mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 15:28:30 +00:00
Productionize polished cosmoaudio library
This change introduces comsoaudio v1. We're using a new strategy when it comes to dynamic linking of dso files and building miniaudio device code which I think will be fast and stable in the long run. You now have your choice of reading/writing to the internal ring buffer abstraction or you can specify a device-driven callback function instead. It's now possible to not open the microphone when you don't need it since touching the mic causes security popups to happen. The DLL is now built statically, so it only needs to depend on kernel32. Our NES terminal emulator now uses the cosmoaudio library and is confirmed to be working on Windows, Mac, Linux
This commit is contained in:
parent
c66abd7260
commit
dc579b79cd
13 changed files with 640 additions and 470 deletions
|
@ -1,5 +1,12 @@
|
|||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#if 0
|
||||
/*─────────────────────────────────────────────────────────────────╗
|
||||
│ To the extent possible under law, Justine Tunney has waived │
|
||||
│ all copyright and related or neighboring rights to this file, │
|
||||
│ as it is written in the following disclaimers: │
|
||||
│ • http://unlicense.org/ │
|
||||
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
||||
╚─────────────────────────────────────────────────────────────────*/
|
||||
#endif
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
@ -9,28 +16,43 @@
|
|||
#define M_PIf 3.14159265358979323846f
|
||||
#endif
|
||||
|
||||
int g_hz = 44100;
|
||||
int g_channels = 2;
|
||||
int g_generation = 0;
|
||||
int g_freq = 440;
|
||||
|
||||
void data_callback(struct CosmoAudio *ca, float *outputSamples,
|
||||
const float *inputSamples, int frameCount, int channels,
|
||||
void *argument) {
|
||||
for (int i = 0; i < frameCount; i++) {
|
||||
float t = (float)g_generation++ / g_hz;
|
||||
if (g_generation == g_hz)
|
||||
g_generation = 0;
|
||||
float s = sinf(2 * M_PIf * g_freq * t);
|
||||
for (int j = 0; j < channels; j++)
|
||||
outputSamples[i * channels + j] = s;
|
||||
}
|
||||
(void)inputSamples;
|
||||
(void)argument;
|
||||
(void)ca;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int hz = 44100;
|
||||
int channels = 2;
|
||||
struct CosmoAudioOpenOptions cao = {};
|
||||
cao.sizeofThis = sizeof(struct CosmoAudioOpenOptions);
|
||||
cao.deviceType = kCosmoAudioDeviceTypePlayback;
|
||||
cao.sampleRate = g_hz;
|
||||
cao.channels = g_channels;
|
||||
cao.dataCallback = data_callback;
|
||||
|
||||
struct CosmoAudio *ca;
|
||||
if (cosmoaudio_open(&ca, hz, channels) != COSMOAUDIO_SUCCESS) {
|
||||
fprintf(stderr, "%s: failed to open audio\n", argv[0]);
|
||||
if (cosmoaudio_open(&ca, &cao) != COSMOAUDIO_SUCCESS) {
|
||||
fprintf(stderr, "failed to open audio\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int n = 1000;
|
||||
int sample = 0;
|
||||
float *buf = (float *)malloc(sizeof(float) * channels * n);
|
||||
for (;;) {
|
||||
for (int i = 0; i < 128; i++) {
|
||||
float freq = 440;
|
||||
float t = (float)sample++ / hz;
|
||||
if (sample == hz)
|
||||
sample = 0;
|
||||
buf[i * channels] = sinf(freq * 2.f * M_PIf * t);
|
||||
buf[i * channels + 1] = sinf(freq * 2.f * M_PIf * t);
|
||||
}
|
||||
cosmoaudio_write(ca, buf, 128);
|
||||
}
|
||||
fgetc(stdin);
|
||||
|
||||
cosmoaudio_close(ca);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue