dont hide structs behind typedefs

master
Ahmet Inan 2012-06-01 23:41:04 +02:00
rodzic c377692be5
commit 7bc72d4acf
19 zmienionych plików z 134 dodań i 134 usunięć

38
alsa.c
Wyświetl plik

@ -11,44 +11,44 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
#include "alsa.h" #include "alsa.h"
typedef struct { struct alsa {
pcm_t base; struct pcm base;
snd_pcm_t *pcm; snd_pcm_t *pcm;
int index; int index;
int frames; int frames;
int r; int r;
int c; int c;
} alsa_t; };
void close_alsa(pcm_t *pcm) void close_alsa(struct pcm *pcm)
{ {
alsa_t *alsa = (alsa_t *)(pcm->data); struct alsa *alsa = (struct alsa *)(pcm->data);
snd_pcm_drain(alsa->pcm); snd_pcm_drain(alsa->pcm);
snd_pcm_close(alsa->pcm); snd_pcm_close(alsa->pcm);
free(alsa); free(alsa);
} }
void info_alsa(pcm_t *pcm) void info_alsa(struct pcm *pcm)
{ {
alsa_t *alsa = (alsa_t *)(pcm->data); struct alsa *alsa = (struct alsa *)(pcm->data);
if (alsa->frames) if (alsa->frames)
fprintf(stderr, "%d channel(s), %d rate, %.2f seconds\n", alsa->c, alsa->r, (float)alsa->frames / (float)alsa->r); fprintf(stderr, "%d channel(s), %d rate, %.2f seconds\n", alsa->c, alsa->r, (float)alsa->frames / (float)alsa->r);
else else
fprintf(stderr, "%d channel(s), %d rate\n", alsa->c, alsa->r); fprintf(stderr, "%d channel(s), %d rate\n", alsa->c, alsa->r);
} }
int rate_alsa(pcm_t *pcm) int rate_alsa(struct pcm *pcm)
{ {
alsa_t *alsa = (alsa_t *)(pcm->data); struct alsa *alsa = (struct alsa *)(pcm->data);
return alsa->r; return alsa->r;
} }
int channels_alsa(pcm_t *pcm) int channels_alsa(struct pcm *pcm)
{ {
alsa_t *alsa = (alsa_t *)(pcm->data); struct alsa *alsa = (struct alsa *)(pcm->data);
return alsa->c; return alsa->c;
} }
int read_alsa(pcm_t *pcm, short *buff, int frames) int read_alsa(struct pcm *pcm, short *buff, int frames)
{ {
alsa_t *alsa = (alsa_t *)(pcm->data); struct alsa *alsa = (struct alsa *)(pcm->data);
int got = 0; int got = 0;
while (0 < frames) { while (0 < frames) {
while ((got = snd_pcm_readi(alsa->pcm, buff, frames)) < 0) while ((got = snd_pcm_readi(alsa->pcm, buff, frames)) < 0)
@ -60,9 +60,9 @@ int read_alsa(pcm_t *pcm, short *buff, int frames)
return 1; return 1;
} }
int write_alsa(pcm_t *pcm, short *buff, int frames) int write_alsa(struct pcm *pcm, short *buff, int frames)
{ {
alsa_t *alsa = (alsa_t *)(pcm->data); struct alsa *alsa = (struct alsa *)(pcm->data);
if (alsa->frames && (alsa->index + frames) > alsa->frames) if (alsa->frames && (alsa->index + frames) > alsa->frames)
return 0; return 0;
alsa->index += frames; alsa->index += frames;
@ -77,9 +77,9 @@ int write_alsa(pcm_t *pcm, short *buff, int frames)
return 1; return 1;
} }
int open_alsa_read(pcm_t **p, char *name) int open_alsa_read(struct pcm **p, char *name)
{ {
alsa_t *alsa = (alsa_t *)malloc(sizeof(alsa_t)); struct alsa *alsa = (struct alsa *)malloc(sizeof(struct alsa));
alsa->base.close = close_alsa; alsa->base.close = close_alsa;
alsa->base.info = info_alsa; alsa->base.info = info_alsa;
alsa->base.rate = rate_alsa; alsa->base.rate = rate_alsa;
@ -153,9 +153,9 @@ int open_alsa_read(pcm_t **p, char *name)
return 1; return 1;
} }
int open_alsa_write(pcm_t **p, char *name, int rate, int channels, float seconds) int open_alsa_write(struct pcm **p, char *name, int rate, int channels, float seconds)
{ {
alsa_t *alsa = (alsa_t *)malloc(sizeof(alsa_t)); struct alsa *alsa = (struct alsa *)malloc(sizeof(struct alsa));
alsa->base.close = close_alsa; alsa->base.close = close_alsa;
alsa->base.info = info_alsa; alsa->base.info = info_alsa;
alsa->base.rate = rate_alsa; alsa->base.rate = rate_alsa;

4
alsa.h
Wyświetl plik

@ -9,7 +9,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#ifndef ALSA_H #ifndef ALSA_H
#define ALSA_H #define ALSA_H
#include "pcm.h" #include "pcm.h"
int open_alsa_read(pcm_t **, char *); int open_alsa_read(struct pcm **, char *);
int open_alsa_write(pcm_t **, char *, int, int, float); int open_alsa_write(struct pcm **, char *, int, int, float);
#endif #endif

8
ddc.c
Wyświetl plik

@ -12,7 +12,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include "window.h" #include "window.h"
#include "ddc.h" #include "ddc.h"
void do_ddc(ddc_t *ddc, float *input, float complex *output) void do_ddc(struct ddc *ddc, float *input, float complex *output)
{ {
// this works only for L <= M // this works only for L <= M
for (int k = 0, last = ddc->last, in = 0; k < ddc->L; k++) { for (int k = 0, last = ddc->last, in = 0; k < ddc->L; k++) {
@ -38,11 +38,11 @@ void do_ddc(ddc_t *ddc, float *input, float complex *output)
// ddc->osc /= cabsf(ddc->osc); // not really needed // ddc->osc /= cabsf(ddc->osc); // not really needed
} }
} }
ddc_t *alloc_ddc(float freq, float bw, float step, int taps, int L, int M, float (*window)(float, float, float), float a) struct ddc *alloc_ddc(float freq, float bw, float step, int taps, int L, int M, float (*window)(float, float, float), float a)
{ {
float lstep = step / (float)L; float lstep = step / (float)L;
float ostep = step * (float)M / (float)L; float ostep = step * (float)M / (float)L;
ddc_t *ddc = malloc(sizeof(ddc_t)); struct ddc *ddc = malloc(sizeof(struct ddc));
ddc->taps = taps; ddc->taps = taps;
ddc->samples = (taps + L - 1) / L; ddc->samples = (taps + L - 1) / L;
ddc->b = malloc(sizeof(float complex) * ddc->taps); ddc->b = malloc(sizeof(float complex) * ddc->taps);
@ -73,7 +73,7 @@ ddc_t *alloc_ddc(float freq, float bw, float step, int taps, int L, int M, float
ddc->b[i] /= sum; ddc->b[i] /= sum;
return ddc; return ddc;
} }
void free_ddc(ddc_t *ddc) void free_ddc(struct ddc *ddc)
{ {
free(ddc->b); free(ddc->b);
free(ddc->s); free(ddc->s);

10
ddc.h
Wyświetl plik

@ -10,7 +10,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#define DDC_H #define DDC_H
#include "window.h" #include "window.h"
typedef struct { struct ddc {
float complex *b; float complex *b;
float *s; float *s;
float complex osc; float complex osc;
@ -22,11 +22,11 @@ typedef struct {
int samples; int samples;
int L; int L;
int M; int M;
} ddc_t; };
void do_ddc(ddc_t *, float *, float complex *); void do_ddc(struct ddc *, float *, float complex *);
ddc_t *alloc_ddc(float, float, float, int, int, int, float (*)(float, float, float), float); struct ddc *alloc_ddc(float, float, float, int, int, int, float (*)(float, float, float), float);
void free_ddc(ddc_t *); void free_ddc(struct ddc *);
#endif #endif

12
debug.c
Wyświetl plik

@ -22,7 +22,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
pcm_t *pcm; struct pcm *pcm;
char *pcm_name = "default"; char *pcm_name = "default";
char *img_name = 0; char *img_name = 0;
if (argc != 1) if (argc != 1)
@ -112,11 +112,11 @@ int main(int argc, char **argv)
float complex *cnt_q = malloc(sizeof(float complex) * factor_L); float complex *cnt_q = malloc(sizeof(float complex) * factor_L);
float complex *dat_q = malloc(sizeof(float complex) * factor_L); float complex *dat_q = malloc(sizeof(float complex) * factor_L);
// same factor to keep life simple and have accurate horizontal sync // same factor to keep life simple and have accurate horizontal sync
ddc_t *cnt_ddc = alloc_ddc(1200.0, 200.0, step, cnt_taps, factor_L, factor_M, kaiser, 2.0); struct ddc *cnt_ddc = alloc_ddc(1200.0, 200.0, step, cnt_taps, factor_L, factor_M, kaiser, 2.0);
ddc_t *dat_ddc = alloc_ddc(1900.0, 800.0, step, dat_taps, factor_L, factor_M, kaiser, 2.0); struct ddc *dat_ddc = alloc_ddc(1900.0, 800.0, step, dat_taps, factor_L, factor_M, kaiser, 2.0);
// delay input by phase shift of other filter to synchronize outputs // delay input by phase shift of other filter to synchronize outputs
delay_t *cnt_delay = alloc_delay((dat_taps - 1) / (2 * factor_L)); struct delay *cnt_delay = alloc_delay((dat_taps - 1) / (2 * factor_L));
delay_t *dat_delay = alloc_delay((cnt_taps - 1) / (2 * factor_L)); struct delay *dat_delay = alloc_delay((cnt_taps - 1) / (2 * factor_L));
short *buff = (short *)malloc(sizeof(short) * channels * factor_M); short *buff = (short *)malloc(sizeof(short) * channels * factor_M);
@ -130,7 +130,7 @@ int main(int argc, char **argv)
const int width = (0.150 + 3.0 * sync_porch_len) * drate + 20; const int width = (0.150 + 3.0 * sync_porch_len) * drate + 20;
const int height = 256; const int height = 256;
img_t *img = 0; struct img *img = 0;
int hor_ticks = 0; int hor_ticks = 0;
int y_pixel_x = 0; int y_pixel_x = 0;

Wyświetl plik

@ -156,7 +156,7 @@ int cal_header(float cnt_freq, float dat_freq, float drate)
return 0; return 0;
} }
int decode(int *reset, img_t **img, char *img_name, float cnt_freq, float dat_freq, float drate) int decode(int *reset, struct img **img, char *img_name, float cnt_freq, float dat_freq, float drate)
{ {
const int width = 320; const int width = 320;
const int height = 240; const int height = 240;
@ -281,7 +281,7 @@ int decode(int *reset, img_t **img, char *img_name, float cnt_freq, float dat_fr
uv_pixel[uv_pixel_x++ + odd * uv_width] = fclampf(255.0 * (dat_freq - 1500.0) / 800.0, 0.0, 255.0); uv_pixel[uv_pixel_x++ + odd * uv_width] = fclampf(255.0 * (dat_freq - 1500.0) / 800.0, 0.0, 255.0);
return 0; return 0;
} }
int demodulate(pcm_t *pcm, float *cnt_freq, float *dat_freq, float *drate) int demodulate(struct pcm *pcm, float *cnt_freq, float *dat_freq, float *drate)
{ {
static float rate; static float rate;
static int channels; static int channels;
@ -295,10 +295,10 @@ int demodulate(pcm_t *pcm, float *cnt_freq, float *dat_freq, float *drate)
static float *dat_amp; static float *dat_amp;
static float complex *cnt_q; static float complex *cnt_q;
static float complex *dat_q; static float complex *dat_q;
static ddc_t *cnt_ddc; static struct ddc *cnt_ddc;
static ddc_t *dat_ddc; static struct ddc *dat_ddc;
static delay_t *cnt_delay; static struct delay *cnt_delay;
static delay_t *dat_delay; static struct delay *dat_delay;
static short *buff; static short *buff;
static int init = 0; static int init = 0;
@ -382,7 +382,7 @@ int demodulate(pcm_t *pcm, float *cnt_freq, float *dat_freq, float *drate)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
pcm_t *pcm; struct pcm *pcm;
char *pcm_name = "default"; char *pcm_name = "default";
char *img_name = 0; char *img_name = 0;
if (argc != 1) if (argc != 1)
@ -410,7 +410,7 @@ int main(int argc, char **argv)
int vis_reset = 0; int vis_reset = 0;
int dat_reset = 0; int dat_reset = 0;
img_t *img = 0; struct img *img = 0;
float cnt_freq = 0.0; float cnt_freq = 0.0;
float dat_freq = 0.0; float dat_freq = 0.0;
float drate = 0.0; float drate = 0.0;

Wyświetl plik

@ -9,17 +9,17 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include "delay.h" #include "delay.h"
#include <stdlib.h> #include <stdlib.h>
float do_delay(delay_t *d, float input) float do_delay(struct delay *d, float input)
{ {
d->s[d->last] = input; d->s[d->last] = input;
d->last = (d->last + 1) < d->len ? d->last + 1 : 0; d->last = (d->last + 1) < d->len ? d->last + 1 : 0;
return d->s[d->last]; return d->s[d->last];
} }
delay_t *alloc_delay(int samples) struct delay *alloc_delay(int samples)
{ {
int len = samples + 1; int len = samples + 1;
delay_t *d = malloc(sizeof(delay_t)); struct delay *d = malloc(sizeof(struct delay));
d->s = malloc(sizeof(float) * len); d->s = malloc(sizeof(float) * len);
d->last = 0; d->last = 0;
d->len = len; d->len = len;
@ -27,7 +27,7 @@ delay_t *alloc_delay(int samples)
d->s[i] = 0.0; d->s[i] = 0.0;
return d; return d;
} }
void free_delay(delay_t *delay) void free_delay(struct delay *delay)
{ {
free(delay->s); free(delay->s);
free(delay); free(delay);

10
delay.h
Wyświetl plik

@ -8,14 +8,14 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#ifndef DELAY_H #ifndef DELAY_H
#define DELAY_H #define DELAY_H
typedef struct { struct delay {
float *s; float *s;
int last; int last;
int len; int len;
} delay_t; };
float do_delay(delay_t *, float); float do_delay(struct delay *, float);
delay_t *alloc_delay(int); struct delay *alloc_delay(int);
void free_delay(delay_t *); void free_delay(struct delay *);
#endif #endif

Wyświetl plik

@ -17,8 +17,8 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include "pcm.h" #include "pcm.h"
#include "img.h" #include "img.h"
img_t *img; struct img *img;
pcm_t *pcm; struct pcm *pcm;
complex float nco; complex float nco;
float hz2rad; float hz2rad;
int channels; int channels;

6
img.c
Wyświetl plik

@ -11,19 +11,19 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include "ppm.h" #include "ppm.h"
#include "sdl.h" #include "sdl.h"
void close_img(img_t *img) void close_img(struct img *img)
{ {
img->close(img); img->close(img);
} }
int open_img_read(img_t **p, char *name) int open_img_read(struct img **p, char *name)
{ {
if (strstr(name, ".ppm") == (name + (strlen(name) - strlen(".ppm")))) if (strstr(name, ".ppm") == (name + (strlen(name) - strlen(".ppm"))))
return open_ppm_read(p, name); return open_ppm_read(p, name);
return 0; return 0;
} }
int open_img_write(img_t **p, char *name, int width, int height) int open_img_write(struct img **p, char *name, int width, int height)
{ {
if (strstr(name, ".ppm") == (name + (strlen(name) - strlen(".ppm")))) if (strstr(name, ".ppm") == (name + (strlen(name) - strlen(".ppm"))))
return open_ppm_write(p, name, width, height); return open_ppm_write(p, name, width, height);

10
img.h
Wyświetl plik

@ -11,17 +11,17 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include <stdint.h> #include <stdint.h>
typedef struct img { struct img {
void (*close)(struct img *); void (*close)(struct img *);
uint8_t *pixel; uint8_t *pixel;
void *data; void *data;
int width; int width;
int height; int height;
} img_t; };
void close_img(img_t *); void close_img(struct img *);
int open_img_read(img_t **, char *); int open_img_read(struct img **, char *);
int open_img_write(img_t **, char *, int, int); int open_img_write(struct img **, char *, int, int);
#endif #endif

16
pcm.c
Wyświetl plik

@ -13,37 +13,37 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include "alsa.h" #include "alsa.h"
#include "wav.h" #include "wav.h"
void close_pcm(pcm_t *pcm) void close_pcm(struct pcm *pcm)
{ {
pcm->close(pcm); pcm->close(pcm);
} }
void info_pcm(pcm_t *pcm) void info_pcm(struct pcm *pcm)
{ {
pcm->info(pcm); pcm->info(pcm);
} }
int rate_pcm(pcm_t *pcm) int rate_pcm(struct pcm *pcm)
{ {
return pcm->rate(pcm); return pcm->rate(pcm);
} }
int channels_pcm(pcm_t *pcm) int channels_pcm(struct pcm *pcm)
{ {
return pcm->channels(pcm); return pcm->channels(pcm);
} }
int read_pcm(pcm_t *pcm, short *buff, int frames) int read_pcm(struct pcm *pcm, short *buff, int frames)
{ {
return pcm->rw(pcm, buff, frames); return pcm->rw(pcm, buff, frames);
} }
int write_pcm(pcm_t *pcm, short *buff, int frames) int write_pcm(struct pcm *pcm, short *buff, int frames)
{ {
return pcm->rw(pcm, buff, frames); return pcm->rw(pcm, buff, frames);
} }
int open_pcm_read(pcm_t **p, char *name) int open_pcm_read(struct pcm **p, char *name)
{ {
if (strstr(name, "plughw:") == name || strstr(name, "hw:") == name || strstr(name, "default") == name) if (strstr(name, "plughw:") == name || strstr(name, "hw:") == name || strstr(name, "default") == name)
return open_alsa_read(p, name); return open_alsa_read(p, name);
@ -52,7 +52,7 @@ int open_pcm_read(pcm_t **p, char *name)
return 0; return 0;
} }
int open_pcm_write(pcm_t **p, char *name, int rate, int channels, float seconds) int open_pcm_write(struct pcm **p, char *name, int rate, int channels, float seconds)
{ {
if (strstr(name, "plughw:") == name || strstr(name, "hw:") == name || strstr(name, "default") == name) if (strstr(name, "plughw:") == name || strstr(name, "hw:") == name || strstr(name, "default") == name)
return open_alsa_write(p, name, rate, channels, seconds); return open_alsa_write(p, name, rate, channels, seconds);

20
pcm.h
Wyświetl plik

@ -9,23 +9,23 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#ifndef PCM_H #ifndef PCM_H
#define PCM_H #define PCM_H
typedef struct pcm { struct pcm {
void (*close)(struct pcm *); void (*close)(struct pcm *);
void (*info)(struct pcm *); void (*info)(struct pcm *);
int (*rate)(struct pcm *); int (*rate)(struct pcm *);
int (*channels)(struct pcm *); int (*channels)(struct pcm *);
int (*rw)(struct pcm *, short *, int); int (*rw)(struct pcm *, short *, int);
void *data; void *data;
} pcm_t; };
void close_pcm(pcm_t *); void close_pcm(struct pcm *);
void info_pcm(pcm_t *); void info_pcm(struct pcm *);
int rate_pcm(pcm_t *); int rate_pcm(struct pcm *);
int channels_pcm(pcm_t *); int channels_pcm(struct pcm *);
int read_pcm(pcm_t *, short *, int); int read_pcm(struct pcm *, short *, int);
int write_pcm(pcm_t *, short *, int); int write_pcm(struct pcm *, short *, int);
int open_pcm_read(pcm_t **, char *); int open_pcm_read(struct pcm **, char *);
int open_pcm_write(pcm_t **, char *, int, int, float); int open_pcm_write(struct pcm **, char *, int, int, float);
#endif #endif

18
ppm.c
Wyświetl plik

@ -12,21 +12,21 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include "mmap_file.h" #include "mmap_file.h"
#include "img.h" #include "img.h"
typedef struct { struct ppm {
img_t base; struct img base;
void *p; void *p;
size_t size; size_t size;
} ppm_t; };
void close_ppm(img_t *img) void close_ppm(struct img *img)
{ {
ppm_t *ppm = (ppm_t *)(img->data); struct ppm *ppm = (struct ppm *)(img->data);
munmap_file(ppm->p, ppm->size); munmap_file(ppm->p, ppm->size);
free(ppm); free(ppm);
} }
int open_ppm_read(img_t **p, char *name) { int open_ppm_read(struct img **p, char *name) {
ppm_t *ppm = (ppm_t *)malloc(sizeof(ppm_t)); struct ppm *ppm = (struct ppm *)malloc(sizeof(struct ppm));
ppm->base.close = close_ppm; ppm->base.close = close_ppm;
ppm->base.data = (void *)ppm; ppm->base.data = (void *)ppm;
@ -89,9 +89,9 @@ int open_ppm_read(img_t **p, char *name) {
return 1; return 1;
} }
int open_ppm_write(img_t **p, char *name, int width, int height) int open_ppm_write(struct img **p, char *name, int width, int height)
{ {
ppm_t *ppm = (ppm_t *)malloc(sizeof(ppm_t)); struct ppm *ppm = (struct ppm *)malloc(sizeof(struct ppm));
ppm->base.close = close_ppm; ppm->base.close = close_ppm;
ppm->base.data = (void *)ppm; ppm->base.data = (void *)ppm;

4
ppm.h
Wyświetl plik

@ -9,6 +9,6 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#ifndef PPM_H #ifndef PPM_H
#define PPM_H #define PPM_H
#include "img.h" #include "img.h"
int open_ppm_read(img_t **, char *); int open_ppm_read(struct img **, char *);
int open_ppm_write(img_t **, char *, int, int); int open_ppm_write(struct img **, char *, int, int);
#endif #endif

26
sdl.c
Wyświetl plik

@ -12,7 +12,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include <SDL.h> #include <SDL.h>
#include "img.h" #include "img.h"
typedef struct { struct data {
uint8_t *pixel; uint8_t *pixel;
int quit; int quit;
int stop; int stop;
@ -20,12 +20,12 @@ typedef struct {
int okay; int okay;
SDL_Surface *screen; SDL_Surface *screen;
SDL_Thread *thread; SDL_Thread *thread;
} data_t; };
typedef struct { struct sdl {
img_t base; struct img base;
data_t *data; struct data *data;
} sdl_t; };
void handle_events() void handle_events()
{ {
@ -56,7 +56,7 @@ void handle_events()
int update_sdl(void *ptr) int update_sdl(void *ptr)
{ {
data_t *data = (data_t *)ptr; struct data *data = (struct data *)ptr;
while (!data->quit) { while (!data->quit) {
if (data->stop) { if (data->stop) {
data->busy = 0; data->busy = 0;
@ -81,9 +81,9 @@ int update_sdl(void *ptr)
return 0; return 0;
} }
void close_sdl(img_t *img) void close_sdl(struct img *img)
{ {
sdl_t *sdl = (sdl_t *)(img->data); struct sdl *sdl = (struct sdl *)(img->data);
sdl->data->okay = 0; sdl->data->okay = 0;
sdl->data->stop = 1; sdl->data->stop = 1;
while (sdl->data->busy) while (sdl->data->busy)
@ -93,15 +93,15 @@ void close_sdl(img_t *img)
free(sdl); free(sdl);
} }
int open_sdl_write(img_t **p, char *name, int width, int height) int open_sdl_write(struct img **p, char *name, int width, int height)
{ {
sdl_t *sdl = (sdl_t *)malloc(sizeof(sdl_t)); struct sdl *sdl = (struct sdl *)malloc(sizeof(struct sdl));
sdl->base.close = close_sdl; sdl->base.close = close_sdl;
sdl->base.data = (void *)sdl; sdl->base.data = (void *)sdl;
static data_t *data = 0; static struct data *data = 0;
if (!data) { if (!data) {
data = (data_t *)malloc(sizeof(data_t)); data = (struct data *)malloc(sizeof(struct data));
data->quit = 0; data->quit = 0;
data->stop = 1; data->stop = 1;
data->okay = 0; data->okay = 0;

2
sdl.h
Wyświetl plik

@ -9,5 +9,5 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#ifndef SDL_H #ifndef SDL_H
#define SDL_H #define SDL_H
#include "img.h" #include "img.h"
int open_sdl_write(img_t **, char *, int, int); int open_sdl_write(struct img **, char *, int, int);
#endif #endif

52
wav.c
Wyświetl plik

@ -13,7 +13,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include "wav.h" #include "wav.h"
#include "mmap_file.h" #include "mmap_file.h"
typedef struct { struct wav_head {
uint32_t ChunkID; uint32_t ChunkID;
uint32_t ChunkSize; uint32_t ChunkSize;
uint32_t Format; uint32_t Format;
@ -27,10 +27,10 @@ typedef struct {
uint16_t BitsPerSample; uint16_t BitsPerSample;
uint32_t Subchunk2ID; uint32_t Subchunk2ID;
uint32_t Subchunk2Size; uint32_t Subchunk2Size;
} wav_head_t; };
typedef struct { struct wav {
pcm_t base; struct pcm base;
void *p; void *p;
short *b; short *b;
size_t size; size_t size;
@ -38,42 +38,42 @@ typedef struct {
int frames; int frames;
int r; int r;
int c; int c;
} wav_t; };
void close_wav(pcm_t *pcm) void close_wav(struct pcm *pcm)
{ {
wav_t *wav = (wav_t *)(pcm->data); struct wav *wav = (struct wav *)(pcm->data);
munmap_file(wav->p, wav->size); munmap_file(wav->p, wav->size);
free(wav); free(wav);
} }
void info_wav(pcm_t *pcm) void info_wav(struct pcm *pcm)
{ {
wav_t *wav = (wav_t *)(pcm->data); struct wav *wav = (struct wav *)(pcm->data);
fprintf(stderr, "%d channel(s), %d rate, %.2f seconds\n", wav->c, wav->r, (float)wav->frames / (float)wav->r); 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) int rate_wav(struct pcm *pcm)
{ {
wav_t *wav = (wav_t *)(pcm->data); struct wav *wav = (struct wav *)(pcm->data);
return wav->r; return wav->r;
} }
int channels_wav(pcm_t *pcm) int channels_wav(struct pcm *pcm)
{ {
wav_t *wav = (wav_t *)(pcm->data); struct wav *wav = (struct wav *)(pcm->data);
return wav->c; return wav->c;
} }
int read_wav(pcm_t *pcm, short *buff, int frames) int read_wav(struct pcm *pcm, short *buff, int frames)
{ {
wav_t *wav = (wav_t *)(pcm->data); struct wav *wav = (struct wav *)(pcm->data);
if ((wav->index + frames) > wav->frames) if ((wav->index + frames) > wav->frames)
return 0; return 0;
memcpy(buff, wav->b + wav->index * wav->c, sizeof(short) * frames * wav->c); memcpy(buff, wav->b + wav->index * wav->c, sizeof(short) * frames * wav->c);
wav->index += frames; wav->index += frames;
return 1; return 1;
} }
int write_wav(pcm_t *pcm, short *buff, int frames) int write_wav(struct pcm *pcm, short *buff, int frames)
{ {
wav_t *wav = (wav_t *)(pcm->data); struct wav *wav = (struct wav *)(pcm->data);
if ((wav->index + frames) > wav->frames) if ((wav->index + frames) > wav->frames)
return 0; return 0;
memcpy(wav->b + wav->index * wav->c, buff, sizeof(short) * frames * wav->c); memcpy(wav->b + wav->index * wav->c, buff, sizeof(short) * frames * wav->c);
@ -81,9 +81,9 @@ int write_wav(pcm_t *pcm, short *buff, int frames)
return 1; return 1;
} }
int open_wav_read(pcm_t **p, char *name) int open_wav_read(struct pcm **p, char *name)
{ {
wav_t *wav = (wav_t *)malloc(sizeof(wav_t)); struct wav *wav = (struct wav *)malloc(sizeof(struct wav));
wav->base.close = close_wav; wav->base.close = close_wav;
wav->base.info = info_wav; wav->base.info = info_wav;
wav->base.rate = rate_wav; wav->base.rate = rate_wav;
@ -95,8 +95,8 @@ int open_wav_read(pcm_t **p, char *name)
free(wav); free(wav);
return 0; return 0;
} }
wav_head_t *head = (wav_head_t *)wav->p; struct wav_head *head = (struct wav_head *)wav->p;
wav->b = (short *)(wav->p + sizeof(wav_head_t)); wav->b = (short *)(wav->p + sizeof(struct wav_head));
if (head->ChunkID != 0x46464952 || head->Format != 0x45564157 || if (head->ChunkID != 0x46464952 || head->Format != 0x45564157 ||
head->Subchunk1ID != 0x20746d66 || head->Subchunk1Size != 16 || head->Subchunk1ID != 0x20746d66 || head->Subchunk1Size != 16 ||
@ -120,9 +120,9 @@ int open_wav_read(pcm_t **p, char *name)
return 1; return 1;
} }
int open_wav_write(pcm_t **p, char *name, int rate, int channels, float seconds) int open_wav_write(struct pcm **p, char *name, int rate, int channels, float seconds)
{ {
wav_t *wav = (wav_t *)malloc(sizeof(wav_t)); struct wav *wav = (struct wav *)malloc(sizeof(struct wav));
wav->base.close = close_wav; wav->base.close = close_wav;
wav->base.info = info_wav; wav->base.info = info_wav;
wav->base.rate = rate_wav; wav->base.rate = rate_wav;
@ -130,14 +130,14 @@ int open_wav_write(pcm_t **p, char *name, int rate, int channels, float seconds)
wav->base.rw = write_wav; wav->base.rw = write_wav;
wav->base.data = (void *)wav; wav->base.data = (void *)wav;
int frames = seconds * rate; int frames = seconds * rate;
wav->size = frames * channels * sizeof(short) + sizeof(wav_head_t); wav->size = frames * channels * sizeof(short) + sizeof(struct wav_head);
if (!mmap_file_rw(&wav->p, name, wav->size)) { if (!mmap_file_rw(&wav->p, name, wav->size)) {
fprintf(stderr, "couldnt open wav file %s!\n", name); fprintf(stderr, "couldnt open wav file %s!\n", name);
free(wav); free(wav);
return 0; return 0;
} }
wav_head_t *head = (wav_head_t *)wav->p; struct wav_head *head = (struct wav_head *)wav->p;
wav->b = (short *)(wav->p + sizeof(wav_head_t)); wav->b = (short *)(wav->p + sizeof(struct wav_head));
head->ChunkID = 0x46464952; head->ChunkID = 0x46464952;
head->ChunkSize = 36 + frames * sizeof(short) * channels; head->ChunkSize = 36 + frames * sizeof(short) * channels;

4
wav.h
Wyświetl plik

@ -9,6 +9,6 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#ifndef WAV_H #ifndef WAV_H
#define WAV_H #define WAV_H
#include "pcm.h" #include "pcm.h"
int open_wav_read(pcm_t **, char *); int open_wav_read(struct pcm **, char *);
int open_wav_write(pcm_t **, char *, int, int, float); int open_wav_write(struct pcm **, char *, int, int, float);
#endif #endif