Show datsamp.c syntax highlighted
/* ______ ___ ___
* /\ _ \ /\_ \ /\_ \
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
* /\____/
* \_/__/
*
* Grabber plugin for managing sample objects.
*
* By Shawn Hargreaves.
*
* See readme.txt for copyright information.
*/
#include <stdio.h>
#include "allegro.h"
#include "../datedit.h"
/* creates a new sample object */
static void *makenew_sample(long *size)
{
return al_create_sample(8, FALSE, 11025, 1024);
}
/* displays a sample in the grabber object view window */
static void plot_sample(AL_CONST AL_DATAFILE *dat, int x, int y)
{
al_put_text(al_screen, al_font_8x8, "Double-click in the item list to play it", x, y+32, al_gui_fg_color);
}
/* handles double-clicking on a sample in the grabber */
static int dclick_sample(AL_DATAFILE *dat)
{
al_play_sample(dat->dat, 255, 127, 1000, FALSE);
return D_O_K;
}
/* returns an information string describing a sample object */
static void get_sample_desc(AL_CONST AL_DATAFILE *dat, char *s)
{
AL_SAMPLE *sample = (AL_SAMPLE *)dat->dat;
long sec = (sample->len + sample->freq/2) * 10 / MAX(sample->freq, 1);
char *type = (sample->stereo) ? "stereo" : "mono";
sprintf(s, "sample (%d bit %s, %d, %ld.%ld sec)", sample->bits, type, sample->freq, sec/10, sec%10);
}
/* exports a sample into an external file */
static int export_sample(AL_CONST AL_DATAFILE *dat, AL_CONST char *filename)
{
AL_SAMPLE *spl = (AL_SAMPLE *)dat->dat;
int bps = spl->bits/8 * ((spl->stereo) ? 2 : 1);
int len = spl->len * bps;
int i;
signed short s;
AL_PACKFILE *f;
f = al_pack_fopen(filename, F_WRITE);
if (f) {
pack_fputs("RIFF", f); /* RIFF header */
pack_iputl(36+len, f); /* size of RIFF chunk */
pack_fputs("WAVE", f); /* WAV definition */
pack_fputs("fmt ", f); /* format chunk */
pack_iputl(16, f); /* size of format chunk */
pack_iputw(1, f); /* PCM data */
pack_iputw((spl->stereo) ? 2 : 1, f); /* mono/stereo data */
pack_iputl(spl->freq, f); /* sample frequency */
pack_iputl(spl->freq*bps, f); /* avg. bytes per sec */
pack_iputw(bps, f); /* block alignment */
pack_iputw(spl->bits, f); /* bits per sample */
pack_fputs("data", f); /* data chunk */
pack_iputl(len, f); /* actual data length */
if (spl->bits == 8) {
pack_fwrite(spl->data, len, f); /* write the data */
}
else {
for (i=0; i < (int)spl->len * ((spl->stereo) ? 2 : 1); i++) {
s = ((signed short *)spl->data)[i];
pack_iputw(s^0x8000, f);
}
}
pack_fclose(f);
}
return (errno == 0);
}
/* imports a sample from an external file */
static void *grab_sample(AL_CONST char *filename, long *size, int x, int y, int w, int h, int depth)
{
return al_load_sample(filename);
}
/* saves a sample into the datafile format */
static void save_sample(AL_DATAFILE *dat, int packed, int packkids, int strip, int verbose, int extra, AL_PACKFILE *f)
{
AL_SAMPLE *spl = (AL_SAMPLE *)dat->dat;
pack_mputw((spl->stereo) ? -spl->bits : spl->bits, f);
pack_mputw(spl->freq, f);
pack_mputl(spl->len, f);
if (spl->bits == 8) {
pack_fwrite(spl->data, spl->len * ((spl->stereo) ? 2 : 1), f);
}
else {
int i;
for (i=0; i < (int)spl->len * ((spl->stereo) ? 2 : 1); i++) {
pack_iputw(((unsigned short *)spl->data)[i], f);
}
}
}
/* returns a description string for a GUS patch object */
static void get_patch_desc(AL_CONST AL_DATAFILE *dat, char *s)
{
sprintf(s, "AL_MIDI instrument (%ld bytes)", dat->size);
}
/* plugin interface header */
DATEDIT_OBJECT_INFO datpatch_info =
{
DAT_PATCH,
"GUS patch",
get_patch_desc,
NULL,
NULL,
NULL,
NULL,
NULL
};
DATEDIT_GRABBER_INFO datpatch_grabber =
{
DAT_PATCH,
"pat",
"pat",
NULL,
NULL
};
/* plugin interface header */
DATEDIT_OBJECT_INFO datsample_info =
{
DAT_SAMPLE,
"Sample",
get_sample_desc,
makenew_sample,
save_sample,
plot_sample,
dclick_sample,
NULL
};
DATEDIT_GRABBER_INFO datsample_grabber =
{
DAT_SAMPLE,
"voc;wav",
"wav",
grab_sample,
export_sample
};
See more files for this project here