aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sio.c31
-rw-r--r--sio.h2
-rw-r--r--spectrogram.c17
3 files changed, 39 insertions, 11 deletions
diff --git a/sio.c b/sio.c
index 1758c40..9080878 100644
--- a/sio.c
+++ b/sio.c
@@ -22,18 +22,23 @@
#define RCHAN 2
#define BITS 16
#define SIGNED 1
+#define ROUND 512 /* FFT is fastest with powers of two */
+#define FPS 24
struct sio {
struct sio_hdl *sio;
struct sio_par par;
int16_t *buffer;
size_t bufsz;
+ size_t round;
+ size_t roundsz;
};
struct sio *
-init_sio(void)
+init_sio(int factor)
{
struct sio *sio;
+ size_t bufsz;
sio = malloc(sizeof(struct sio));
if (!sio)
@@ -62,7 +67,17 @@ init_sio(void)
sio->par.sig != SIGNED)
errx(1, "unsupported audio params");
- sio->bufsz = sio->par.rchan * sio->par.round * sizeof(int16_t);
+ if (factor < 0)
+ sio->round = ROUND >> -factor;
+ else
+ sio->round = ROUND << factor;
+ sio->roundsz = sio->round * sio->par.rchan * sizeof(int16_t);
+
+ bufsz = sio->par.rate / FPS; /* 24 pictures/second */
+ bufsz -= bufsz % sio->par.round; /* round to block size */
+ while (bufsz < sio->round) /* not less than block size */
+ bufsz += sio->par.round;
+ sio->bufsz = bufsz * sio->par.rchan * sizeof(int16_t);
sio->buffer = malloc(sio->bufsz);
if (!sio->buffer)
@@ -76,25 +91,29 @@ init_sio(void)
unsigned int
get_round(struct sio *sio)
{
- return sio->par.round;
+ return sio->round;
}
int16_t *
read_sio(struct sio *sio)
{
int done = 0;
- int16_t *buffer = sio->buffer;
+ char *buffer = (char *)sio->buffer;
size_t sz = sio->bufsz;
do {
- done += sio_read(sio->sio, buffer, sz);
+ done = sio_read(sio->sio, buffer, sz);
if (sio_eof(sio->sio))
errx(1, "SIO EOF");
buffer += done;
sz -= done;
} while (sz);
- return sio->buffer;
+ /*
+ * return a pointer to the latest ROUND samples (the most recent
+ * ones) to minimize latency between picture and sound
+ */
+ return (int16_t *)(buffer - sio->roundsz);
}
void
diff --git a/sio.h b/sio.h
index 40d7793..12e3d29 100644
--- a/sio.h
+++ b/sio.h
@@ -20,7 +20,7 @@
struct sio;
-struct sio *init_sio(void);
+struct sio *init_sio(int);
unsigned int get_round(struct sio *);
int16_t *read_sio(struct sio *);
void del_sio(struct sio *);
diff --git a/spectrogram.c b/spectrogram.c
index 2dd0fb2..8bc7956 100644
--- a/spectrogram.c
+++ b/spectrogram.c
@@ -122,9 +122,11 @@ catch(int notused)
void
usage(void)
{
- fprintf(stderr, "Usage: %s [-hd]\n", __progname);
- fprintf(stderr, "\t-h\tthis help\n");
+ fprintf(stderr, "Usage: %s [-12dh]\n", __progname);
+ fprintf(stderr, "\t-1\thalf size\n");
+ fprintf(stderr, "\t-2\tdouble size\n");
fprintf(stderr, "\t-d\tdaemonize\n");
+ fprintf(stderr, "\t-h\tthis help\n");
exit(0);
}
@@ -332,6 +334,7 @@ main(int argc, char **argv)
Atom nhints;
XSizeHints *hints;
int scr;
+ int factor = 0;
struct panel *left, *right;
struct sio *sio;
@@ -343,8 +346,14 @@ main(int argc, char **argv)
int width, height;
unsigned long black, white;
- while ((ch = getopt(argc, argv, "hd")) != -1)
+ while ((ch = getopt(argc, argv, "12hd")) != -1)
switch (ch) {
+ case '1':
+ --factor;
+ break;
+ case '2':
+ ++factor;
+ break;
case 'd':
dflag = 1;
break;
@@ -362,7 +371,7 @@ main(int argc, char **argv)
if (!dsp)
errx(1, "Cannot connect to X11 server");
- sio = init_sio();
+ sio = init_sio(factor);
if (dflag)
daemon(0, 0);