shithub: drawterm

Download patch

ref: cdcc2bf6944fa9f0765a5c3c990eb6b0b12b605d
parent: 86573f1aaf078c1c2fcd51beda36eaeaa06cfbf6
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Feb 28 23:14:15 EST 2016

devaudio-win32: quick and dirty audio playback driver for windows

--- a/Make.win32
+++ b/Make.win32
@@ -3,6 +3,7 @@
 # on another platform.  Otherwise the binaries are just
 # named gcc, etc.
 
+AUDIO=win32
 MING=i686-w64-mingw32-
 #MING=
 AR=$(MING)ar
@@ -17,7 +18,7 @@
 OS=win32
 GUI=win32
 LDFLAGS=-mwindows
-LDADD=-lkernel32 -ladvapi32 -lgdi32 -lmpr -lwsock32 -lmsvcrt -lmingw32
+LDADD=-lkernel32 -ladvapi32 -lgdi32 -lmpr -lwsock32 -lmsvcrt -lmingw32 -lwinmm
 TARG=drawterm.exe
 XOFILES=glenda-t.$O
 
--- /dev/null
+++ b/kern/devaudio-win32.c
@@ -1,0 +1,106 @@
+#include	<windows.h>
+#include	<mmsystem.h>
+
+#include	"u.h"
+#include	"lib.h"
+#include	"dat.h"
+#include	"fns.h"
+#include	"error.h"
+#include	"devaudio.h"
+
+enum
+{
+	Channels = 2,
+	Rate = 44100,
+	Bits = 16,
+};
+
+typedef struct Waveblock Waveblock;
+struct Waveblock {
+	WAVEHDR	h;
+	uchar	s[2048];
+};
+
+static HWAVEOUT waveout;
+static Waveblock blk[16];
+static uint blkidx;
+
+void
+audiodevopen(void)
+{
+	WAVEFORMATEX f;
+
+	memset(&f, 0, sizeof(f));
+	f.nSamplesPerSec = Rate;
+	f.wBitsPerSample = Bits;
+	f.nChannels = Channels;
+	f.cbSize = 0;
+	f.wFormatTag = WAVE_FORMAT_PCM;
+	f.nBlockAlign = (f.wBitsPerSample/8) * f.nChannels;
+	f.nAvgBytesPerSec = f.nBlockAlign * f.nSamplesPerSec;
+	if(waveOutOpen(&waveout, WAVE_MAPPER, &f, 0, 0, CALLBACK_NULL) != MMSYSERR_NOERROR)
+		oserror();
+}
+
+void
+audiodevclose(void)
+{
+	waveOutClose(waveout);
+	waveout = 0;
+}
+
+void
+audiodevsetvol(int what, int left, int right)
+{
+	USED(what);
+	USED(left);
+	USED(right);
+	error("not supported");
+}
+
+void
+audiodevgetvol(int what, int *left, int *right)
+{
+	USED(what);
+	USED(left);
+	USED(right);
+	error("not supported");
+}
+
+int
+audiodevwrite(void *v, int n)
+{
+	Waveblock *b;
+	int m;
+
+	m = 0;
+	while(n > sizeof(b->s)){
+		audiodevwrite(v, sizeof(b->s));
+		v = (uchar*)v + sizeof(b->s);
+		n -= sizeof(b->s);
+		m += sizeof(b->s);
+	}
+
+	b = &blk[blkidx++ % nelem(blk)];
+	if(b->h.dwFlags & WHDR_PREPARED){
+		while(waveOutUnprepareHeader(waveout, &b->h, sizeof(b->h)) == WAVERR_STILLPLAYING)
+			osyield();
+	}
+	memmove(b->s, v, n);
+	b->h.lpData = (void*)b->s;
+	b->h.dwBufferLength = n;
+	waveOutPrepareHeader(waveout, &b->h, sizeof(b->h));
+	waveOutWrite(waveout, &b->h, sizeof(b->h));
+
+	return m + n;
+}
+
+int
+audiodevread(void *v, int n)
+{
+	USED(v);
+	USED(n);
+
+	error("no reading");
+	return -1;
+}
--- a/kern/fns.h
+++ b/kern/fns.h
@@ -367,7 +367,7 @@
 int		xmerge(void*, void*);
 void*		xspanalloc(ulong, int, ulong);
 void		xsummary(void);
-void		yield(void);
+void		osyield(void);
 Segment*	data2txt(Segment*);
 Segment*	dupseg(Segment**, int, int);
 Segment*	newseg(int, ulong, ulong);