From ecdc38a28868c8b37e904c5660e78a5fd8a63fbd Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Fri, 2 Mar 2012 10:19:02 +0100 Subject: [PATCH] cleaned up code to make adding to the interfaces easier. --- alsa.c | 45 ++++++++++++++++++++++----------------------- img.c | 1 - img.h | 1 + pcm.c | 1 - pcm.h | 1 + ppm.c | 28 ++++++++++++++-------------- sdl.c | 21 ++++++++++----------- wav.c | 45 ++++++++++++++++++++++----------------------- 8 files changed, 70 insertions(+), 73 deletions(-) diff --git a/alsa.c b/alsa.c index c582e25..b621abe 100644 --- a/alsa.c +++ b/alsa.c @@ -12,11 +12,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #include "alsa.h" typedef struct { - void (*close)(pcm_t *); - void (*info)(pcm_t *); - int (*rate)(pcm_t *); - int (*channels)(pcm_t *); - int (*rw)(pcm_t *, short *, int); + pcm_t base; snd_pcm_t *pcm; int index; int frames; @@ -26,14 +22,15 @@ typedef struct { void close_alsa(pcm_t *pcm) { - alsa_t *alsa = (alsa_t *)pcm; + alsa_t *alsa = (alsa_t *)(pcm->data); snd_pcm_drain(alsa->pcm); snd_pcm_close(alsa->pcm); + free(alsa); } void info_alsa(pcm_t *pcm) { - alsa_t *alsa = (alsa_t *)pcm; + alsa_t *alsa = (alsa_t *)(pcm->data); if (alsa->frames) fprintf(stderr, "%d channel(s), %d rate, %.2f seconds\n", alsa->c, alsa->r, (float)alsa->frames / (float)alsa->r); else @@ -41,17 +38,17 @@ void info_alsa(pcm_t *pcm) } int rate_alsa(pcm_t *pcm) { - alsa_t *alsa = (alsa_t *)pcm; + alsa_t *alsa = (alsa_t *)(pcm->data); return alsa->r; } int channels_alsa(pcm_t *pcm) { - alsa_t *alsa = (alsa_t *)pcm; + alsa_t *alsa = (alsa_t *)(pcm->data); return alsa->c; } int read_alsa(pcm_t *pcm, short *buff, int frames) { - alsa_t *alsa = (alsa_t *)pcm; + alsa_t *alsa = (alsa_t *)(pcm->data); int got = 0; while (0 < frames) { while ((got = snd_pcm_readi(alsa->pcm, buff, frames)) < 0) @@ -65,7 +62,7 @@ int read_alsa(pcm_t *pcm, short *buff, int frames) int write_alsa(pcm_t *pcm, short *buff, int frames) { - alsa_t *alsa = (alsa_t *)pcm; + alsa_t *alsa = (alsa_t *)(pcm->data); if (alsa->frames && (alsa->index + frames) > alsa->frames) return 0; alsa->index += frames; @@ -83,11 +80,12 @@ int write_alsa(pcm_t *pcm, short *buff, int frames) int open_alsa_read(pcm_t **p, char *name) { alsa_t *alsa = (alsa_t *)malloc(sizeof(alsa_t)); - alsa->close = close_alsa; - alsa->info = info_alsa; - alsa->rate = rate_alsa; - alsa->channels = channels_alsa; - alsa->rw = read_alsa; + alsa->base.close = close_alsa; + alsa->base.info = info_alsa; + alsa->base.rate = rate_alsa; + alsa->base.channels = channels_alsa; + alsa->base.rw = read_alsa; + alsa->base.data = (void *)alsa; snd_pcm_t *pcm; snd_pcm_hw_params_t *params; @@ -151,18 +149,19 @@ int open_alsa_read(pcm_t **p, char *name) alsa->r = rate; alsa->c = channels; alsa->frames = 0; - *p = (pcm_t *)alsa; + *p = &(alsa->base); return 1; } int open_alsa_write(pcm_t **p, char *name, int rate, int channels, float seconds) { alsa_t *alsa = (alsa_t *)malloc(sizeof(alsa_t)); - alsa->close = close_alsa; - alsa->info = info_alsa; - alsa->rate = rate_alsa; - alsa->channels = channels_alsa; - alsa->rw = write_alsa; + alsa->base.close = close_alsa; + alsa->base.info = info_alsa; + alsa->base.rate = rate_alsa; + alsa->base.channels = channels_alsa; + alsa->base.rw = write_alsa; + alsa->base.data = (void *)alsa; snd_pcm_t *pcm; snd_pcm_hw_params_t *params; @@ -223,6 +222,6 @@ int open_alsa_write(pcm_t **p, char *name, int rate, int channels, float seconds alsa->c = channels; alsa->frames = seconds * rate; alsa->index = 0; - *p = (pcm_t *)alsa; + *p = &(alsa->base); return 1; } diff --git a/img.c b/img.c index 3b67054..1aa65f0 100644 --- a/img.c +++ b/img.c @@ -14,7 +14,6 @@ You should have received a copy of the CC0 Public Domain Dedication along with t void close_img(img_t *img) { img->close(img); - free(img); } int open_img_read(img_t **p, char *name) diff --git a/img.h b/img.h index c31117e..ee29486 100644 --- a/img.h +++ b/img.h @@ -14,6 +14,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t typedef struct img { void (*close)(struct img *); uint8_t *pixel; + void *data; int width; int height; } img_t; diff --git a/pcm.c b/pcm.c index 5515761..3a12cdc 100644 --- a/pcm.c +++ b/pcm.c @@ -16,7 +16,6 @@ You should have received a copy of the CC0 Public Domain Dedication along with t void close_pcm(pcm_t *pcm) { pcm->close(pcm); - free(pcm); } void info_pcm(pcm_t *pcm) diff --git a/pcm.h b/pcm.h index ca0a8b4..afc2bb3 100644 --- a/pcm.h +++ b/pcm.h @@ -15,6 +15,7 @@ typedef struct pcm { int (*rate)(struct pcm *); int (*channels)(struct pcm *); int (*rw)(struct pcm *, short *, int); + void *data; } pcm_t; void close_pcm(pcm_t *); diff --git a/ppm.c b/ppm.c index 8dfee4d..21ac5b0 100644 --- a/ppm.c +++ b/ppm.c @@ -13,23 +13,22 @@ You should have received a copy of the CC0 Public Domain Dedication along with t #include "img.h" typedef struct { - void (*close)(img_t *); - uint8_t *pixel; - int width; - int height; + img_t base; void *p; size_t size; } ppm_t; void close_ppm(img_t *img) { - ppm_t *ppm = (ppm_t *)img; + ppm_t *ppm = (ppm_t *)(img->data); munmap_file(ppm->p, ppm->size); + free(ppm); } int open_ppm_read(img_t **p, char *name) { ppm_t *ppm = (ppm_t *)malloc(sizeof(ppm_t)); - ppm->close = close_ppm; + ppm->base.close = close_ppm; + ppm->base.data = (void *)ppm; if (!mmap_file_ro(&(ppm->p), name, &(ppm->size))) { fprintf(stderr, "couldnt open image file %s\n", name); @@ -80,12 +79,12 @@ int open_ppm_read(img_t **p, char *name) { free(ppm); return 0; } - ppm->width = integer[0]; - ppm->height = integer[1]; + ppm->base.width = integer[0]; + ppm->base.height = integer[1]; - ppm->pixel = (uint8_t *)ppm->p + index; + ppm->base.pixel = (uint8_t *)ppm->p + index; - *p = (img_t *)ppm; + *p = &(ppm->base); return 1; } @@ -93,7 +92,8 @@ int open_ppm_read(img_t **p, char *name) { int open_ppm_write(img_t **p, char *name, int width, int height) { ppm_t *ppm = (ppm_t *)malloc(sizeof(ppm_t)); - ppm->close = close_ppm; + ppm->base.close = close_ppm; + ppm->base.data = (void *)ppm; char head[32]; snprintf(head, 32, "P6 %d %d 255\n", width, height); @@ -106,10 +106,10 @@ int open_ppm_write(img_t **p, char *name, int width, int height) } memcpy(ppm->p, head, strlen(head)); - ppm->pixel = (uint8_t *)ppm->p + strlen(head); - memset(ppm->pixel, 0, width * height * 3); + ppm->base.pixel = (uint8_t *)ppm->p + strlen(head); + memset(ppm->base.pixel, 0, width * height * 3); - *p = (img_t *)ppm; + *p = &(ppm->base); return 1; } diff --git a/sdl.c b/sdl.c index 9704561..e4f3bab 100644 --- a/sdl.c +++ b/sdl.c @@ -23,10 +23,7 @@ typedef struct { } data_t; typedef struct { - void (*close)(img_t *); - uint8_t *pixel; - int width; - int height; + img_t base; data_t *data; } sdl_t; @@ -86,19 +83,21 @@ int update_sdl(void *ptr) void close_sdl(img_t *img) { - sdl_t *sdl = (sdl_t *)img; + sdl_t *sdl = (sdl_t *)(img->data); sdl->data->okay = 0; sdl->data->stop = 1; while (sdl->data->busy) SDL_Delay(50); sdl->data->stop = 0; - free(sdl->pixel); + free(sdl->base.pixel); + free(sdl); } int open_sdl_write(img_t **p, char *name, int width, int height) { sdl_t *sdl = (sdl_t *)malloc(sizeof(sdl_t)); - sdl->close = close_sdl; + sdl->base.close = close_sdl; + sdl->base.data = (void *)sdl; static data_t *data = 0; if (!data) { @@ -148,15 +147,15 @@ int open_sdl_write(img_t **p, char *name, int width, int height) memset(data->pixel, 0, width * height * 3); memset(data->screen->pixels, 0, width * height * 4); - sdl->width = width; - sdl->height = height; + sdl->base.width = width; + sdl->base.height = height; sdl->data = data; - sdl->pixel = data->pixel; + sdl->base.pixel = data->pixel; data->okay = 1; data->stop = 0; - *p = (img_t *)sdl; + *p = &(sdl->base); return 1; } diff --git a/wav.c b/wav.c index f46153d..fea95ca 100644 --- a/wav.c +++ b/wav.c @@ -30,11 +30,7 @@ typedef struct { } wav_head_t; typedef struct { - void (*close)(pcm_t *); - void (*info)(pcm_t *); - int (*rate)(pcm_t *); - int (*channels)(pcm_t *); - int (*rw)(pcm_t *, short *, int); + pcm_t base; void *p; short *b; size_t size; @@ -46,28 +42,29 @@ typedef struct { void close_wav(pcm_t *pcm) { - wav_t *wav = (wav_t *)pcm; + wav_t *wav = (wav_t *)(pcm->data); munmap_file(wav->p, wav->size); + free(wav); } void info_wav(pcm_t *pcm) { - wav_t *wav = (wav_t *)pcm; + wav_t *wav = (wav_t *)(pcm->data); fprintf(stderr, "%d channel(s), %d rate, %.2f seconds\n", wav->c, wav->r, (float)wav->frames / (float)wav->r); } int rate_wav(pcm_t *pcm) { - wav_t *wav = (wav_t *)pcm; + wav_t *wav = (wav_t *)(pcm->data); return wav->r; } int channels_wav(pcm_t *pcm) { - wav_t *wav = (wav_t *)pcm; + wav_t *wav = (wav_t *)(pcm->data); return wav->c; } int read_wav(pcm_t *pcm, short *buff, int frames) { - wav_t *wav = (wav_t *)pcm; + wav_t *wav = (wav_t *)(pcm->data); if ((wav->index + frames) > wav->frames) return 0; memcpy(buff, wav->b + wav->index * wav->c, sizeof(short) * frames * wav->c); @@ -76,7 +73,7 @@ int read_wav(pcm_t *pcm, short *buff, int frames) } int write_wav(pcm_t *pcm, short *buff, int frames) { - wav_t *wav = (wav_t *)pcm; + wav_t *wav = (wav_t *)(pcm->data); if ((wav->index + frames) > wav->frames) return 0; memcpy(wav->b + wav->index * wav->c, buff, sizeof(short) * frames * wav->c); @@ -87,11 +84,12 @@ int write_wav(pcm_t *pcm, short *buff, int frames) int open_wav_read(pcm_t **p, char *name) { wav_t *wav = (wav_t *)malloc(sizeof(wav_t)); - wav->close = close_wav; - wav->info = info_wav; - wav->rate = rate_wav; - wav->channels = channels_wav; - wav->rw = read_wav; + wav->base.close = close_wav; + wav->base.info = info_wav; + wav->base.rate = rate_wav; + wav->base.channels = channels_wav; + wav->base.rw = read_wav; + wav->base.data = (void *)wav; if (!mmap_file_ro(&wav->p, name, &wav->size)) { fprintf(stderr, "couldnt open wav file %s!\n", name); free(wav); @@ -118,18 +116,19 @@ int open_wav_read(pcm_t **p, char *name) wav->frames = head->Subchunk2Size / (sizeof(short) * head->NumChannels); wav->r = head->SampleRate; wav->c = head->NumChannels; - *p = (pcm_t *)wav; + *p = &(wav->base); return 1; } int open_wav_write(pcm_t **p, char *name, int rate, int channels, float seconds) { wav_t *wav = (wav_t *)malloc(sizeof(wav_t)); - wav->close = close_wav; - wav->info = info_wav; - wav->rate = rate_wav; - wav->channels = channels_wav; - wav->rw = write_wav; + wav->base.close = close_wav; + wav->base.info = info_wav; + wav->base.rate = rate_wav; + wav->base.channels = channels_wav; + wav->base.rw = write_wav; + wav->base.data = (void *)wav; int frames = seconds * rate; wav->size = frames * channels * sizeof(short) + sizeof(wav_head_t); if (!mmap_file_rw(&wav->p, name, wav->size)) { @@ -159,6 +158,6 @@ int open_wav_write(pcm_t **p, char *name, int rate, int channels, float seconds) wav->frames = frames; wav->index = 0; - *p = (pcm_t *)wav; + *p = &(wav->base); return 1; }