home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Acorn User 10
/
AU_CD10.iso
/
Archived
/
Updates
/
Flash
/
writeflash
/
!MakeFlash
/
c
/
sound
< prev
Wrap
Text File
|
2000-06-04
|
4KB
|
156 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//
#include "proto.h"
#include "bucket.h"
#include "main.h"
#include "flash.h"
#include "sound.h"
static int write_file(char *file);
int sound_create(SOUND **sound) {
*sound = malloc(sizeof(SOUND));
if (!*sound) return 1;
memset(*sound, 0, sizeof(SOUND));
return 0;
}
int sound_write(SOUND *sound) {
int filesize;
U32 ptr, ptr2;
U8 *buf;
if (flush_bucket()) return 1;
ptr = read_position(NULL);
if (write_ushort(0)) return 1;
if (write_ushort(sound->id)) return 1;
if ((sound->format == SOUNDFORMAT_LIN8) ||
(sound->format == SOUNDFORMAT_LIN16)) {
if (write_ubits(4, 0)) return 1;
} else
if (write_ubits(4, 1)) return 1;
switch (sound->freq) {
case 5:
if (write_ubits(2, 0)) return 1;
break;
case 11:
if (write_ubits(2, 1)) return 1;
break;
case 22:
if (write_ubits(2, 2)) return 1;
break;
case 44:
if (write_ubits(2, 3)) return 1;
break;
default:
if (write_ubits(2, 2)) return 1;
break;
}
if (sound->format == SOUNDFORMAT_LIN8) {
if (write_ubits(2, 0)) return 1;
} else {
if (write_ubits(2, 1)) return 1;
}
if (sound->stereo) {
if (write_ubits(2, 1)) return 1;
} else {
if (write_ubits(2, 0)) return 1;
}
if (flush_bucket()) return 1;
ptr2 = read_position(&buf);
if (write_uint(0)) return 1;
filesize = write_file(sound->file);
if (filesize <= 0) return 1;
filesize *= 8; // bits
switch (sound->format) {
case SOUNDFORMAT_LIN8:
filesize /= 8;
break;
case SOUNDFORMAT_LIN16:
filesize /= 16;
break;
case SOUNDFORMAT_ADPCM2:
filesize = (filesize - 24)/2;
break;
case SOUNDFORMAT_ADPCM3:
filesize = (filesize - 24)/3;
break;
case SOUNDFORMAT_ADPCM4:
filesize = (filesize - 24)/4;
break;
case SOUNDFORMAT_ADPCM5:
filesize = (filesize - 24)/5;
break;
}
if (sound->stereo) filesize /= 2;
buf[ptr2+0] = filesize & 0xff;
buf[ptr2+1] = (filesize>> 8) & 0xff;
buf[ptr2+2] = (filesize>>16) & 0xff;
buf[ptr2+3] = (filesize>>24) & 0xff;
return update_record_header(stagDefineSound, ptr);
}
int write_play_sound(PLAYSOUND *play) {
U32 ptr;
if (flush_bucket()) return 1;
ptr = read_position(NULL);
if (write_ushort(0)) return 1;
if (write_ushort(play->id)) return 1;
if (write_ubits(4, 0)) return 1;
if (write_ubits(1, 0)) return 1;
if (play->loops > 1) {
if (write_ubits(1, 1)) return 1;
} else {
if (write_ubits(1, 0)) return 1;
}
if (write_ubits(1, 0)) return 1;
if (write_ubits(1, 0)) return 1;
if (play->loops > 1) {
if (write_ushort(play->loops)) return 1;
}
return update_record_header(stagStartSound, ptr);
}
// ----------------------------------------------------
int write_file(char *file) {
FILE *fh;
U32 i, filesize;
fh = fopen(file, "rb");
if (!fh) return -1;
fseek(fh, 0, SEEK_END);
filesize = (U32)ftell(fh);
fseek(fh, 0, SEEK_SET);
for (i = 0; i < filesize; i++)
if (write_ubyte(fgetc(fh))) return -1;
fclose(fh);
return filesize;
}