mirror of
https://github.com/zserge/fenster.git
synced 2025-04-22 09:28:39 +03:00
add audio example
This commit is contained in:
15
examples/sound-c/Makefile
Normal file
15
examples/sound-c/Makefile
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
CFLAGS ?= -Wall -Wextra -std=c99
|
||||||
|
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
LDFLAGS = -lgdi32
|
||||||
|
else
|
||||||
|
UNAME_S := $(shell uname -s)
|
||||||
|
ifeq ($(UNAME_S),Darwin)
|
||||||
|
LDFLAGS = -framework Cocoa -framework AudioToolbox
|
||||||
|
else
|
||||||
|
LDFLAGS = -lX11
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
main: main.c ../../fenster.h
|
||||||
|
$(CC) main.c -I../.. -o $@ $(CFLAGS) $(LDFLAGS)
|
74
examples/sound-c/main.c
Normal file
74
examples/sound-c/main.c
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#include "fenster.h"
|
||||||
|
#include "fenster_audio.h"
|
||||||
|
|
||||||
|
#define W 320
|
||||||
|
#define H 240
|
||||||
|
|
||||||
|
/* ============================================================
|
||||||
|
* A very minimal example of a Fenster app:
|
||||||
|
* - Opens a window
|
||||||
|
* - Starts a loop
|
||||||
|
* - Changes pixel colours based on some "shader" formula
|
||||||
|
* - Sleeps if needed to maintain a frame rate of 60 FPS
|
||||||
|
* - Closes a window
|
||||||
|
* ============================================================ */
|
||||||
|
static int run() {
|
||||||
|
uint32_t buf[W * H];
|
||||||
|
struct fenster f = {
|
||||||
|
.title = "hello",
|
||||||
|
.width = W,
|
||||||
|
.height = H,
|
||||||
|
.buf = buf,
|
||||||
|
};
|
||||||
|
struct fenster_audio fa = {0};
|
||||||
|
fenster_open(&f);
|
||||||
|
fenster_audio_open(&fa);
|
||||||
|
uint32_t t, u = 0;
|
||||||
|
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;
|
||||||
|
|
||||||
|
for (int i = 0; i < 320; i++) {
|
||||||
|
for (int j = 0; j < 240; j++) {
|
||||||
|
/* White noise: */
|
||||||
|
/* fenster_pixel(&f, i, j) = (rand() << 16) ^ (rand() << 8) ^ rand(); */
|
||||||
|
|
||||||
|
/* Colourful and moving: */
|
||||||
|
/* fenster_pixel(&f, i, j) = i * j * t; */
|
||||||
|
|
||||||
|
/* Munching squares: */
|
||||||
|
fenster_pixel(&f, i, j) = i ^ j ^ t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int64_t time = fenster_time();
|
||||||
|
if (time - now < 1000 / 60) {
|
||||||
|
fenster_sleep(time - now);
|
||||||
|
}
|
||||||
|
now = time;
|
||||||
|
}
|
||||||
|
fenster_audio_close(&fa);
|
||||||
|
fenster_close(&f);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
|
||||||
|
int nCmdShow) {
|
||||||
|
(void)hInstance, (void)hPrevInstance, (void)pCmdLine, (void)nCmdShow;
|
||||||
|
return run();
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
int main() { return run(); }
|
||||||
|
#endif
|
Reference in New Issue
Block a user