# HG changeset patch # User pouya@nohup.io # Date 1641846719 -3600 # Mon Jan 10 21:31:59 2022 +0100 # Node ID 41e1bd38c2e4818d324d22b3dbef8348a5e1ba48 # Parent 0000000000000000000000000000000000000000 initial commit diff --git a/Makefile b/Makefile new file mode 100644 --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +CC=pcc +LD=pcc + +CCFLAGS=-O +LDFLAGS=-static + +TARG=beep +OFILES=main.o + +all: ${TARG} + +clean: + rm -f *.o ${TARG} + +.SUFFIXES: .c .o + +.c.o: + ${CC} ${CCFLAGS} -c ${.IMPSRC} -o ${.TARGET} + +${TARG}: ${OFILES} + ${LD} ${LDFLAGS} ${.ALLSRC} -o ${.TARGET} -lm + +.PHONY: all clean diff --git a/main.c b/main.c new file mode 100644 --- /dev/null +++ b/main.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include + +#define PREC 8 +#define ENC AUDIO_ENCODING_PCM8 + +#define DEFAULT_RATE 8000 +#define DEFAULT_FREQ 1000 +#define DEFAULT_MSEC 1000 + +typedef struct Ctx Ctx; +struct Ctx { + int rate, freq, msec; +} ctx; + +int beep(int rate, int freq, int msec); +int init(int argc, char **argv); + +int +beep(int rate, int freq, int msec) { + audio_info_t aufmt = {0}; + uint8_t s; + double step; + int fd, e, max; + long i, n; + + // init + if((fd = open("/dev/audioctl", O_WRONLY)) < 0) return fd; + if((e = ioctl(fd, AUDIO_GETINFO, &aufmt)) < 0) return e; + aufmt.mode = AUMODE_PLAY; + aufmt.play.encoding = ENC; + aufmt.play.precision = PREC; + aufmt.play.channels = 1; + aufmt.play.sample_rate = rate; + if((e = ioctl(fd, AUDIO_SETINFO, &aufmt)) < 0) return e; + + close(fd); + + // play + if((fd = open("/dev/sound", O_WRONLY)) < 0) return fd; + + n = msec * rate / 1000; + max = 1<