aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2014-09-02 18:11:30 +0000
committerDimitri Sokolyuk <demon@dim13.org>2014-09-02 18:11:30 +0000
commite5400522430da40e678270f50a2c9eaa12fb8c1b (patch)
tree687e26c8c104a4d92aadf408ac0a47dd6e6045ba
parent56b9322a73bd3e4f9974b80fdc64bcb863913073 (diff)
simplify fft, switch to assert by malloc
-rw-r--r--alsa.c8
-rw-r--r--fft.c49
-rw-r--r--sio.c9
-rw-r--r--spectrogram.c10
4 files changed, 23 insertions, 53 deletions
diff --git a/alsa.c b/alsa.c
index bc307ed..9511f4a 100644
--- a/alsa.c
+++ b/alsa.c
@@ -15,6 +15,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <assert.h>
#include <err.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
@@ -38,8 +39,7 @@ init_sio(unsigned int round)
sio = malloc(sizeof(struct sio));
- if (!sio)
- errx(1, "malloc failed");
+ assert(sio);
rc = snd_pcm_open(&sio->handle, "default", SND_PCM_STREAM_CAPTURE, 0);
if (rc < 0)
@@ -63,9 +63,7 @@ init_sio(unsigned int round)
sio->bufsz = sio->frames * RCHAN * sizeof(int16_t);
sio->buffer = malloc(sio->bufsz);
-
- if (!sio->buffer)
- errx(1, "malloc failed");
+ assert(sio->buffer);
return sio;
}
diff --git a/fft.c b/fft.c
index 55b35f4..12ef65c 100644
--- a/fft.c
+++ b/fft.c
@@ -15,7 +15,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <err.h>
+#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
@@ -37,8 +37,7 @@ hamming(size_t n)
int i;
w = calloc(n, sizeof(double));
- if (!w)
- errx(1, "malloc failed");
+ assert(w);
for (i = 0; i < n; i++)
w[i] = 0.54 - 0.46 * cos((2 * M_PI * i) / (n - 1));
@@ -46,44 +45,22 @@ hamming(size_t n)
return w;
}
-static double *
-resize(double *p, size_t n)
-{
- if (p)
- fftw_free(p);
- p = fftw_malloc(n * sizeof(double));
- if (!p)
- errx(1, "malloc failed");
-
- return p;
-}
-
-struct fft *
-resize_fft(struct fft *p, size_t n)
-{
- if (n != p->n) {
- p->n = n;
-
- p->in = resize(p->in, p->n);
- p->out = resize(p->out, p->n);
-
- if (p->window)
- free(p->window);
- p->window = hamming(p->n);
-
- p->plan = fftw_plan_r2r_1d(p->n, p->in, p->out,
- FFTW_R2HC, FFTW_MEASURE);
- }
- return p;
-}
-
struct fft *
init_fft(size_t n)
{
struct fft *p;
- p = calloc(1, sizeof(struct fft));
- p = resize_fft(p, n);
+ p = malloc(sizeof(struct fft));
+ assert(p);
+
+ p->n = n;
+ p->in = fftw_alloc_real(p->n);
+ p->out = fftw_alloc_real(p->n);
+ assert(p->in && p->out);
+
+ p->window = hamming(p->n);
+ p->plan = fftw_plan_r2r_1d(p->n, p->in, p->out,
+ FFTW_R2HC, FFTW_MEASURE);
return p;
}
diff --git a/sio.c b/sio.c
index ab5b5e8..1f24ffe 100644
--- a/sio.c
+++ b/sio.c
@@ -15,6 +15,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <assert.h>
#include <err.h>
#include <stdlib.h>
#include <sndio.h>
@@ -39,11 +40,9 @@ init_sio(unsigned int round)
size_t bufsz;
sio = malloc(sizeof(struct sio));
- if (!sio)
- errx(1, "malloc failed");
+ assert(sio);
sio->sio = sio_open(SIO_DEVANY, SIO_REC, 0);
-
if (!sio->sio)
errx(1, "cannot connect to sound server, is it running?");
@@ -73,9 +72,7 @@ init_sio(unsigned int round)
bufsz += sio->par.round;
sio->bufsz = bufsz * sio->par.rchan * sizeof(int16_t);
sio->buffer = malloc(sio->bufsz);
-
- if (!sio->buffer)
- errx(1, "malloc failed");
+ assert(sio->buffer);
sio_start(sio->sio);
diff --git a/spectrogram.c b/spectrogram.c
index 62812e3..e5232c7 100644
--- a/spectrogram.c
+++ b/spectrogram.c
@@ -22,6 +22,7 @@
#include <sys/types.h>
#include <sys/time.h>
+#include <assert.h>
#include <err.h>
#include <math.h>
#include <signal.h>
@@ -103,8 +104,7 @@ init_palette(Display *d, struct palette pal, int n)
int i;
p = calloc(n, sizeof(unsigned long));
- if (!p)
- errx(1, "malloc failed");
+ assert(p);
hstep = (pal.to.h - pal.from.h) / n;
sstep = (pal.to.s - pal.from.s) / n;
@@ -239,12 +239,10 @@ init_panel(Display *d, Window win, int x, int y, int w, int h, int mirror)
unsigned long *palette;
p = malloc(sizeof(struct panel));
- if (!p)
- errx(1, "malloc failed");
+ assert(p);
p->data = calloc(w, sizeof(double));
- if (!p->data)
- errx(1, "malloc failed");
+ assert(p->data);
/* main panel window */
p->win = XCreateSimpleWindow(d, win, x, y, w, h, 0, white, gray);