9front - general discussion about 9front
 help / color / mirror / Atom feed
* games/midi: erratic write delays cause stammering and cracking
@ 2016-06-16 11:16 qwx
  0 siblings, 0 replies; only message in thread
From: qwx @ 2016-06-16 11:16 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 1793 bytes --]

games/midi: erratic write delays cause stammering and cracking

run:samp can vary greatly, anywhere from 1 to over 12000. the size of each write
to ofd and the resulting delay between writes throws /dev/audio off and causes
stammering and cracking during playback.

the inlined patch uses bio to segment writes evenly.
attached are 2 midi files with problematic playback. tested with hda and ac97
only.



diff -r ee777ca6b0a0 sys/src/games/midi.c
--- a/sys/src/games/midi.c	Fri Jun 10 12:14:02 2016 +0200
+++ b/sys/src/games/midi.c	Thu Jun 16 10:50:59 2016 +0300
@@ -1,5 +1,6 @@
 #include <u.h>
 #include <libc.h>
+#include <bio.h>
 
 enum { SAMPLE = 44100 };
 
@@ -13,7 +14,8 @@
 
 typedef struct Tracker Tracker;
 
-int fd, ofd, div, tempo = 500000, ntrack;
+Biobuf *ofd;
+int fd, div, tempo = 500000, ntrack;
 uvlong T;
 int freq[128];
 
@@ -110,8 +112,8 @@
 void
 run(uvlong n)
 {
-	int samp, j, k, l, no[128];
-	uchar *s;
+	int samp, j, k, no[128];
+	uchar s[4];
 	int t, f;
 	short u;
 	Tracker *x;
@@ -127,20 +129,18 @@
 			for(k = 0; k < 128; k++)
 				no[k] += x->notes[j][k];
 	}
-	s = emallocz(samp * 4);
-	for(l = 0; l < samp; l++){
+	while(samp--){
 		t = 0;
 		for(k = 0; k < 128; k++){
 			f = (T % freq[k]) >= freq[k]/2 ? 1 : 0;
 			t += f * no[k];
 		}
 		u = t*10;
-		s[4 * l] = s[4 * l + 2] = u;
-		s[4 * l + 1] = s[4 * l + 3] = u >> 8;
+		s[0] = s[2] = u;
+		s[1] = s[3] = u >> 8;
+		Bwrite(ofd, s, sizeof s);
 		T++;
 	}
-	write(ofd, s, samp * 4);
-	free(s);
 }
 
 void
@@ -213,8 +213,8 @@
 	fd = open(argv[1], OREAD);
 	if(fd < 0)
 		sysfatal("open: %r");
-	ofd = open("/dev/audio", OWRITE);
-	if(ofd < 0)
+	ofd = Bopen("/dev/audio", OWRITE);
+	if(ofd == nil)
 		sysfatal("ofd: %r");
 	if(get32(nil) != 0x4D546864 || get32(nil) != 6)
 		sysfatal("invalid file header");

[-- Attachment #2: d_romero.mid --]
[-- Type: audio/midi, Size: 45441 bytes --]

[-- Attachment #3: u5.lord.blackthorn.mid --]
[-- Type: audio/midi, Size: 8286 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2016-06-16  8:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-16 11:16 games/midi: erratic write delays cause stammering and cracking qwx

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).