Merge branch 'master' of github.com:xdsopl/robot36

master
Ahmet Inan 2012-05-11 20:11:09 +02:00
commit c377692be5
8 zmienionych plików z 70 dodań i 73 usunięć

45
alsa.c
Wyświetl plik

@ -12,11 +12,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include "alsa.h" #include "alsa.h"
typedef struct { typedef struct {
void (*close)(pcm_t *); pcm_t base;
void (*info)(pcm_t *);
int (*rate)(pcm_t *);
int (*channels)(pcm_t *);
int (*rw)(pcm_t *, short *, int);
snd_pcm_t *pcm; snd_pcm_t *pcm;
int index; int index;
int frames; int frames;
@ -26,14 +22,15 @@ typedef struct {
void close_alsa(pcm_t *pcm) 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_drain(alsa->pcm);
snd_pcm_close(alsa->pcm); snd_pcm_close(alsa->pcm);
free(alsa);
} }
void info_alsa(pcm_t *pcm) void info_alsa(pcm_t *pcm)
{ {
alsa_t *alsa = (alsa_t *)pcm; alsa_t *alsa = (alsa_t *)(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
@ -41,17 +38,17 @@ void info_alsa(pcm_t *pcm)
} }
int rate_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; return alsa->r;
} }
int channels_alsa(pcm_t *pcm) int channels_alsa(pcm_t *pcm)
{ {
alsa_t *alsa = (alsa_t *)pcm; alsa_t *alsa = (alsa_t *)(pcm->data);
return alsa->c; return alsa->c;
} }
int read_alsa(pcm_t *pcm, short *buff, int frames) 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; 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)
@ -65,7 +62,7 @@ int read_alsa(pcm_t *pcm, short *buff, int frames)
int write_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) if (alsa->frames && (alsa->index + frames) > alsa->frames)
return 0; return 0;
alsa->index += frames; 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) int open_alsa_read(pcm_t **p, char *name)
{ {
alsa_t *alsa = (alsa_t *)malloc(sizeof(alsa_t)); alsa_t *alsa = (alsa_t *)malloc(sizeof(alsa_t));
alsa->close = close_alsa; alsa->base.close = close_alsa;
alsa->info = info_alsa; alsa->base.info = info_alsa;
alsa->rate = rate_alsa; alsa->base.rate = rate_alsa;
alsa->channels = channels_alsa; alsa->base.channels = channels_alsa;
alsa->rw = read_alsa; alsa->base.rw = read_alsa;
alsa->base.data = (void *)alsa;
snd_pcm_t *pcm; snd_pcm_t *pcm;
snd_pcm_hw_params_t *params; snd_pcm_hw_params_t *params;
@ -151,18 +149,19 @@ int open_alsa_read(pcm_t **p, char *name)
alsa->r = rate; alsa->r = rate;
alsa->c = channels; alsa->c = channels;
alsa->frames = 0; alsa->frames = 0;
*p = (pcm_t *)alsa; *p = &(alsa->base);
return 1; return 1;
} }
int open_alsa_write(pcm_t **p, char *name, int rate, int channels, float seconds) 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_t *alsa = (alsa_t *)malloc(sizeof(alsa_t));
alsa->close = close_alsa; alsa->base.close = close_alsa;
alsa->info = info_alsa; alsa->base.info = info_alsa;
alsa->rate = rate_alsa; alsa->base.rate = rate_alsa;
alsa->channels = channels_alsa; alsa->base.channels = channels_alsa;
alsa->rw = write_alsa; alsa->base.rw = write_alsa;
alsa->base.data = (void *)alsa;
snd_pcm_t *pcm; snd_pcm_t *pcm;
snd_pcm_hw_params_t *params; 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->c = channels;
alsa->frames = seconds * rate; alsa->frames = seconds * rate;
alsa->index = 0; alsa->index = 0;
*p = (pcm_t *)alsa; *p = &(alsa->base);
return 1; return 1;
} }

1
img.c
Wyświetl plik

@ -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) void close_img(img_t *img)
{ {
img->close(img); img->close(img);
free(img);
} }
int open_img_read(img_t **p, char *name) int open_img_read(img_t **p, char *name)

1
img.h
Wyświetl plik

@ -14,6 +14,7 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
typedef struct img { typedef struct img {
void (*close)(struct img *); void (*close)(struct img *);
uint8_t *pixel; uint8_t *pixel;
void *data;
int width; int width;
int height; int height;
} img_t; } img_t;

1
pcm.c
Wyświetl plik

@ -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) void close_pcm(pcm_t *pcm)
{ {
pcm->close(pcm); pcm->close(pcm);
free(pcm);
} }
void info_pcm(pcm_t *pcm) void info_pcm(pcm_t *pcm)

1
pcm.h
Wyświetl plik

@ -15,6 +15,7 @@ typedef 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;
} pcm_t; } pcm_t;
void close_pcm(pcm_t *); void close_pcm(pcm_t *);

28
ppm.c
Wyświetl plik

@ -13,23 +13,22 @@ You should have received a copy of the CC0 Public Domain Dedication along with t
#include "img.h" #include "img.h"
typedef struct { typedef struct {
void (*close)(img_t *); img_t base;
uint8_t *pixel;
int width;
int height;
void *p; void *p;
size_t size; size_t size;
} ppm_t; } ppm_t;
void close_ppm(img_t *img) 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); munmap_file(ppm->p, ppm->size);
free(ppm);
} }
int open_ppm_read(img_t **p, char *name) { int open_ppm_read(img_t **p, char *name) {
ppm_t *ppm = (ppm_t *)malloc(sizeof(ppm_t)); 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))) { if (!mmap_file_ro(&(ppm->p), name, &(ppm->size))) {
fprintf(stderr, "couldnt open image file %s\n", name); fprintf(stderr, "couldnt open image file %s\n", name);
@ -80,12 +79,12 @@ int open_ppm_read(img_t **p, char *name) {
free(ppm); free(ppm);
return 0; return 0;
} }
ppm->width = integer[0]; ppm->base.width = integer[0];
ppm->height = integer[1]; 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; 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) int open_ppm_write(img_t **p, char *name, int width, int height)
{ {
ppm_t *ppm = (ppm_t *)malloc(sizeof(ppm_t)); 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]; char head[32];
snprintf(head, 32, "P6 %d %d 255\n", width, height); 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)); memcpy(ppm->p, head, strlen(head));
ppm->pixel = (uint8_t *)ppm->p + strlen(head); ppm->base.pixel = (uint8_t *)ppm->p + strlen(head);
memset(ppm->pixel, 0, width * height * 3); memset(ppm->base.pixel, 0, width * height * 3);
*p = (img_t *)ppm; *p = &(ppm->base);
return 1; return 1;
} }

21
sdl.c
Wyświetl plik

@ -23,10 +23,7 @@ typedef struct {
} data_t; } data_t;
typedef struct { typedef struct {
void (*close)(img_t *); img_t base;
uint8_t *pixel;
int width;
int height;
data_t *data; data_t *data;
} sdl_t; } sdl_t;
@ -86,19 +83,21 @@ int update_sdl(void *ptr)
void close_sdl(img_t *img) 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->okay = 0;
sdl->data->stop = 1; sdl->data->stop = 1;
while (sdl->data->busy) while (sdl->data->busy)
SDL_Delay(50); SDL_Delay(50);
sdl->data->stop = 0; 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) int open_sdl_write(img_t **p, char *name, int width, int height)
{ {
sdl_t *sdl = (sdl_t *)malloc(sizeof(sdl_t)); 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; static data_t *data = 0;
if (!data) { 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->pixel, 0, width * height * 3);
memset(data->screen->pixels, 0, width * height * 4); memset(data->screen->pixels, 0, width * height * 4);
sdl->width = width; sdl->base.width = width;
sdl->height = height; sdl->base.height = height;
sdl->data = data; sdl->data = data;
sdl->pixel = data->pixel; sdl->base.pixel = data->pixel;
data->okay = 1; data->okay = 1;
data->stop = 0; data->stop = 0;
*p = (img_t *)sdl; *p = &(sdl->base);
return 1; return 1;
} }

45
wav.c
Wyświetl plik

@ -30,11 +30,7 @@ typedef struct {
} wav_head_t; } wav_head_t;
typedef struct { typedef struct {
void (*close)(pcm_t *); pcm_t base;
void (*info)(pcm_t *);
int (*rate)(pcm_t *);
int (*channels)(pcm_t *);
int (*rw)(pcm_t *, short *, int);
void *p; void *p;
short *b; short *b;
size_t size; size_t size;
@ -46,28 +42,29 @@ typedef struct {
void close_wav(pcm_t *pcm) 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); munmap_file(wav->p, wav->size);
free(wav);
} }
void info_wav(pcm_t *pcm) 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); 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(pcm_t *pcm)
{ {
wav_t *wav = (wav_t *)pcm; wav_t *wav = (wav_t *)(pcm->data);
return wav->r; return wav->r;
} }
int channels_wav(pcm_t *pcm) int channels_wav(pcm_t *pcm)
{ {
wav_t *wav = (wav_t *)pcm; wav_t *wav = (wav_t *)(pcm->data);
return wav->c; return wav->c;
} }
int read_wav(pcm_t *pcm, short *buff, int frames) 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) 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);
@ -76,7 +73,7 @@ int read_wav(pcm_t *pcm, short *buff, int frames)
} }
int write_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) 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);
@ -87,11 +84,12 @@ int write_wav(pcm_t *pcm, short *buff, int frames)
int open_wav_read(pcm_t **p, char *name) int open_wav_read(pcm_t **p, char *name)
{ {
wav_t *wav = (wav_t *)malloc(sizeof(wav_t)); wav_t *wav = (wav_t *)malloc(sizeof(wav_t));
wav->close = close_wav; wav->base.close = close_wav;
wav->info = info_wav; wav->base.info = info_wav;
wav->rate = rate_wav; wav->base.rate = rate_wav;
wav->channels = channels_wav; wav->base.channels = channels_wav;
wav->rw = read_wav; wav->base.rw = read_wav;
wav->base.data = (void *)wav;
if (!mmap_file_ro(&wav->p, name, &wav->size)) { if (!mmap_file_ro(&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);
@ -118,18 +116,19 @@ int open_wav_read(pcm_t **p, char *name)
wav->frames = head->Subchunk2Size / (sizeof(short) * head->NumChannels); wav->frames = head->Subchunk2Size / (sizeof(short) * head->NumChannels);
wav->r = head->SampleRate; wav->r = head->SampleRate;
wav->c = head->NumChannels; wav->c = head->NumChannels;
*p = (pcm_t *)wav; *p = &(wav->base);
return 1; return 1;
} }
int open_wav_write(pcm_t **p, char *name, int rate, int channels, float seconds) 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_t *wav = (wav_t *)malloc(sizeof(wav_t));
wav->close = close_wav; wav->base.close = close_wav;
wav->info = info_wav; wav->base.info = info_wav;
wav->rate = rate_wav; wav->base.rate = rate_wav;
wav->channels = channels_wav; wav->base.channels = channels_wav;
wav->rw = write_wav; wav->base.rw = write_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(wav_head_t);
if (!mmap_file_rw(&wav->p, name, wav->size)) { 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->frames = frames;
wav->index = 0; wav->index = 0;
*p = (pcm_t *)wav; *p = &(wav->base);
return 1; return 1;
} }