aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2014-09-04 11:40:25 +0000
committerDimitri Sokolyuk <demon@dim13.org>2014-09-04 11:40:25 +0000
commit967d75bb678cc3b4140462830851e28762726115 (patch)
treea8f9d5d8facff5e4b318c55f74a634b719a37e5b
parent86e7bc7358f0b926846ff9adc60cd545718bce56 (diff)
fix fps, request optimal samples number (more then required);
prepare for window resize capability;
-rw-r--r--alsa.c13
-rw-r--r--sio.c51
-rw-r--r--sio.h4
-rw-r--r--spectrogram.c4
4 files changed, 38 insertions, 34 deletions
diff --git a/alsa.c b/alsa.c
index 19e19f2..051ab59 100644
--- a/alsa.c
+++ b/alsa.c
@@ -34,7 +34,7 @@ struct sio {
};
struct sio *
-init_sio(unsigned int round)
+init_sio(void)
{
struct sio *sio;
int rc;
@@ -52,21 +52,20 @@ init_sio(unsigned int round)
SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(sio->handle, sio->params,
SND_PCM_FORMAT_S16_LE);
- snd_pcm_hw_params_set_rate(sio->handle, sio->params, RATE, 0);
snd_pcm_hw_params_set_channels(sio->handle, sio->params, STEREO);
- snd_pcm_hw_params_set_period_size(sio->handle, sio->params, round, 0);
+ //snd_pcm_hw_params_set_rate(sio->handle, sio->params, RATE, 0);
+ //snd_pcm_hw_params_set_period_size(sio->handle, sio->params, round, 0);
rc = snd_pcm_hw_params(sio->handle, sio->params);
if (rc < 0)
errx(1, "unable to set hw parameters: %s", snd_strerror(rc));
+ /* FIXME */
snd_pcm_hw_params_get_period_size(sio->params, &sio->round, NULL);
+ //snd_pcm_hw_params_get_rate(sio->handle, sio->params, &sio->rate, 0);
snd_pcm_hw_params_free(sio->params);
snd_pcm_prepare(sio->handle);
- if (sio->round != round)
- warnx("requested %d frames, got %d", round, sio->round);
-
sio->bufsz = sio->round * STEREO * sizeof(int16_t);
sio->buffer = malloc(sio->bufsz);
assert(sio->buffer);
@@ -75,7 +74,7 @@ init_sio(unsigned int round)
}
int16_t *
-read_sio(struct sio *sio)
+read_sio(struct sio *sio, unsigned int n)
{
int rc;
diff --git a/sio.c b/sio.c
index e57f8be..e0d9eaf 100644
--- a/sio.c
+++ b/sio.c
@@ -25,21 +25,19 @@
#define STEREO 2
#define BITS 16
#define SIGNED 1
-#define FPS 24
+#define FPS 25
struct sio {
struct sio_hdl *sio;
struct sio_par par;
int16_t *buffer;
- size_t bufsz;
- size_t round;
+ unsigned int samples;
};
struct sio *
-init_sio(unsigned int round)
+init_sio(void)
{
struct sio *sio;
- size_t bufsz;
sio = malloc(sizeof(struct sio));
assert(sio);
@@ -66,14 +64,9 @@ init_sio(unsigned int round)
sio->par.sig != SIGNED)
errx(1, "unsupported audio params");
- sio->round = round;
-
- 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);
+ sio->samples = sio->par.rate / FPS;
+ sio->samples -= sio->samples % sio->par.round;
+ sio->buffer = calloc(sio->samples * sio->par.rchan, sizeof(int16_t));
assert(sio->buffer);
sio_start(sio->sio);
@@ -81,27 +74,39 @@ init_sio(unsigned int round)
return sio;
}
+unsigned int
+max_samples_sio(struct sio *sio)
+{
+ /*
+ * maximal number of samples we're willing to provide
+ * with 1920 at 25 fps and 48000 Hz or
+ * with 1764 at 25 fps and 44100 Hz it shall fit on most screens
+ */
+ return sio->samples;
+}
+
int16_t *
-read_sio(struct sio *sio)
+read_sio(struct sio *sio, unsigned int n)
{
- int done = 0;
+ int done;
char *buffer = (char *)sio->buffer;
- size_t sz = sio->bufsz;
- size_t roundsz = sio->round * sio->par.rchan * sizeof(int16_t);
+ size_t bufsz = sio->samples * sio->par.rchan * sizeof(int16_t);
+ size_t rndsz = n * sio->par.rchan * sizeof(int16_t);
+
+ if (rndsz > bufsz)
+ rndsz = bufsz;
- do {
- done = sio_read(sio->sio, buffer, sz);
+ for (done = 0; bufsz > 0; buffer += done, bufsz -= done) {
+ done = sio_read(sio->sio, buffer, bufsz);
if (sio_eof(sio->sio))
errx(1, "SIO EOF");
- buffer += done;
- sz -= done;
- } while (sz);
+ }
/*
* return a pointer to the latest ROUND samples (the most recent
* ones) to minimize latency between picture and sound
*/
- return (int16_t *)(buffer - roundsz);
+ return (int16_t *)(buffer - rndsz);
}
void
diff --git a/sio.h b/sio.h
index b9df7a8..29dfea8 100644
--- a/sio.h
+++ b/sio.h
@@ -21,8 +21,8 @@
struct sio;
__BEGIN_DECLS
-struct sio *init_sio(unsigned int);
-int16_t *read_sio(struct sio *);
+struct sio *init_sio(void);
+int16_t *read_sio(struct sio *, unsigned int);
void free_sio(struct sio *);
__END_DECLS
diff --git a/spectrogram.c b/spectrogram.c
index 02e574f..c3576db 100644
--- a/spectrogram.c
+++ b/spectrogram.c
@@ -464,7 +464,7 @@ main(int argc, char **argv)
fflag = 1;
}
- sio = init_sio(round);
+ sio = init_sio();
scr = DefaultScreen(dsp);
white = WhitePixel(dsp, scr);
@@ -518,7 +518,7 @@ main(int argc, char **argv)
}
while (!die) {
- buffer = read_sio(sio);
+ buffer = read_sio(sio, round);
exec_fft(fft, buffer, left->data, FFT_LEFT);
exec_fft(fft, buffer, right->data, FFT_RIGHT);