diff --git a/examples/sound-c/main.c b/examples/sound-c/main.c index fecf2fb..ebb0623 100644 --- a/examples/sound-c/main.c +++ b/examples/sound-c/main.c @@ -20,25 +20,26 @@ static int run() { .height = H, .buf = buf, }; - struct fenster_audio fa = {0}; + struct fenster_audio fa = {0}; fenster_open(&f); - fenster_audio_open(&fa); + fenster_audio_open(&fa); uint32_t t, u = 0; - float audio[FENSTER_AUDIO_BUFSZ]; + float audio[FENSTER_AUDIO_BUFSZ]; int64_t now = fenster_time(); while (fenster_loop(&f) == 0) { t++; - int n = fenster_audio_available(&fa); - if (n > 0) { - for (int i = 0; i < n; i++) { - u++; - /*audio[i] = (rand() & 0xff)/256.f;*/ - int x = u * 80/441; - audio[i] = ((((x >> 10) & 42) * x)&0xff)/256.f; - } - fenster_audio_write(&fa, audio, n); - } - if (f.keys[27]) break; + int n = fenster_audio_available(&fa); + if (n > 0) { + for (int i = 0; i < n; i++) { + u++; + /*audio[i] = (rand() & 0xff)/256.f;*/ + int x = u * 80 / 441; + audio[i] = ((((x >> 10) & 42) * x) & 0xff) / 256.f; + } + fenster_audio_write(&fa, audio, n); + } + if (f.keys[27]) + break; for (int i = 0; i < 320; i++) { for (int j = 0; j < 240; j++) { @@ -54,11 +55,11 @@ static int run() { } int64_t time = fenster_time(); if (time - now < 1000 / 60) { - fenster_sleep(time - now); + fenster_sleep(time - now); } now = time; } - fenster_audio_close(&fa); + fenster_audio_close(&fa); fenster_close(&f); return 0; } diff --git a/fenster_audio.h b/fenster_audio.h index 0af7f44..f9598ec 100644 --- a/fenster_audio.h +++ b/fenster_audio.h @@ -100,21 +100,26 @@ int snd_pcm_writei(void *, const void *, unsigned long); int snd_pcm_recover(void *, int, int); int snd_pcm_close(void *); FENSTER_API int fenster_audio_open(struct fenster_audio *fa) { - if (snd_pcm_open(&fa->pcm, "default", 0, 0)) return -1; - int fmt = (*(unsigned char *)(&(uint16_t){1}))?14:15; + if (snd_pcm_open(&fa->pcm, "default", 0, 0)) + return -1; + int fmt = (*(unsigned char *)(&(uint16_t){1})) ? 14 : 15; return snd_pcm_set_params(fa->pcm, fmt, 3, 1, FENSTER_SAMPLE_RATE, 1, 100000); } FENSTER_API int fenster_audio_available(struct fenster_audio *fa) { - int n = snd_pcm_avail(fa->pcm); - if (n < 0) snd_pcm_recover(fa->pcm, n, 0); + int n = snd_pcm_avail(fa->pcm); + if (n < 0) + snd_pcm_recover(fa->pcm, n, 0); return n; } FENSTER_API void fenster_audio_write(struct fenster_audio *fa, float *buf, size_t n) { int r = snd_pcm_writei(fa->pcm, buf, n); - if (r < 0) snd_pcm_recover(fa->pcm, r, 0); + if (r < 0) + snd_pcm_recover(fa->pcm, r, 0); +} +FENSTER_API void fenster_audio_close(struct fenster_audio *fa) { + snd_pcm_close(fa->pcm); } -FENSTER_API void fenster_audio_close(struct fenster_audio *fa) { snd_pcm_close(fa->pcm); } #endif #endif /* FENSTER_HEADER */