tools/openwav2bor/source/bor2wav.c
/**
 * Reads the stream metadata and encoded ADPCM stream from a BOR music file on 
 * the filesystem.
 * @param filename the path to the BOR file
 * @param buf structure where the ADPCM stream and its metadata is stored
 * @return true on success, false on failure
 */
Reads the stream metadata and encoded ADPCM stream from a BOR music file on
the filesystem.
@param filename the path to the BOR file
@param buf structure where the ADPCM stream and its metadata is stored
@return true on success, false on failure
Reads the stream metadata and encoded ADPCM stream from a BOR music file on the filesystem . @param filename the path to the BOR file @param buf structure where the ADPCM stream and its metadata is stored @return true on success false on failure
bool readbor(char *filename, samplestruct *buf)
{
	struct {
		char           identifier[16];
		char           artist[64];
		char           title[64];
		uint32_t       version;
		int32_t        frequency;
		int32_t        channels;
		int32_t        datastart;
	} borheader;
	int i=0, len;
	FILE *fd = fopen(filename, "rb");
	if(fd == NULL) ioerror(strerror(errno));
	if(fread(&borheader, sizeof(borheader), 1, fd) != 1) ioerror(strerror(errno));
	borheader.version = SwapLSB32(borheader.version);
	borheader.frequency = SwapLSB32(borheader.frequency);
	borheader.channels = SwapLSB32(borheader.channels);
	borheader.datastart = SwapLSB32(borheader.datastart);
	if((strcmp(borheader.identifier, BOR_IDENTIFIER) != 0) ||
		(borheader.version != OLD_BOR_VERSION && borheader.version != NEW_BOR_VERSION) ||
		(borheader.channels != 1 && borheader.channels != 2) ||
		(borheader.version == OLD_BOR_VERSION && borheader.channels == 2) ||
		(borheader.datastart != SwapLSB32(sizeof(borheader))))
	{
		ioerror("invalid BOR header");
	}
	if(*borheader.artist) printf("Artist: %s\n", borheader.artist);
	if(*borheader.title)  printf("Title: %s\n", borheader.title);
	if(*borheader.artist || *borheader.title) printf("\n"); 
	if (fseek(fd, 0, SEEK_END) != 0) ioerror(strerror(errno));
	len = ftell(fd) - borheader.datastart;
	buf->soundlen = len;
	fseek(fd, borheader.datastart, SEEK_SET);
	buf->bits = 16;
	buf->frequency = borheader.frequency;
	buf->channels = borheader.channels;
	buf->sampleptr = malloc(len);
	if(buf->sampleptr == NULL) ioerror(strerror(errno));
	if(fseek(fd, borheader.datastart, SEEK_SET) != 0) ioerror(strerror(errno));
	if(fread(buf->sampleptr, 1, len, fd) != len) ioerror(strerror(errno));
	fclose(fd);
	return true;
error:
	if(buf->sampleptr) { free(buf->sampleptr); buf->sampleptr = NULL; }
	if(fd) fclose(fd);
	return false;
}
bool readbor ( char * filename , samplestruct * buf ) { struct { char identifier [ 16 ] ; char artist [ 64 ] ; char title [ 64 ] ; uint32_t version ; int32_t frequency ; int32_t channels ; int32_t datastart ; } borheader ; int i = 0 , len ; FILE * fd = fopen ( filename , " " ) ; if ( fd == NULL ) ioerror ( strerror ( errno ) ) ; if ( fread ( & borheader , sizeof ( borheader ) , 1 , fd ) != 1 ) ioerror ( strerror ( errno ) ) ; borheader . version = SwapLSB32 ( borheader . version ) ; borheader . frequency = SwapLSB32 ( borheader . frequency ) ; borheader . channels = SwapLSB32 ( borheader . channels ) ; borheader . datastart = SwapLSB32 ( borheader . datastart ) ; if ( ( strcmp ( borheader . identifier , BOR_IDENTIFIER ) != 0 ) || ( borheader . version != OLD_BOR_VERSION && borheader . version != NEW_BOR_VERSION ) || ( borheader . channels != 1 && borheader . channels != 2 ) || ( borheader . version == OLD_BOR_VERSION && borheader . channels == 2 ) || ( borheader . datastart != SwapLSB32 ( sizeof ( borheader ) ) ) ) { ioerror ( " " ) ; } if ( * borheader . artist ) printf ( " \n " , borheader . artist ) ; if ( * borheader . title ) printf ( " \n " , borheader . title ) ; if ( * borheader . artist || * borheader . title ) printf ( " \n " ) ; if ( fseek ( fd , 0 , SEEK_END ) != 0 ) ioerror ( strerror ( errno ) ) ; len = ftell ( fd ) - borheader . datastart ; buf -> soundlen = len ; fseek ( fd , borheader . datastart , SEEK_SET ) ; buf -> bits = 16 ; buf -> frequency = borheader . frequency ; buf -> channels = borheader . channels ; buf -> sampleptr = malloc ( len ) ; if ( buf -> sampleptr == NULL ) ioerror ( strerror ( errno ) ) ; if ( fseek ( fd , borheader . datastart , SEEK_SET ) != 0 ) ioerror ( strerror ( errno ) ) ; if ( fread ( buf -> sampleptr , 1 , len , fd ) != len ) ioerror ( strerror ( errno ) ) ; fclose ( fd ) ; return true ; error : if ( buf -> sampleptr ) { free ( buf -> sampleptr ) ; buf -> sampleptr = NULL ; } if ( fd ) fclose ( fd ) ; return false ; }
Reads the stream metadata and encoded ADPCM stream from a BOR music file on
 the filesystem.
Reads the stream metadata and encoded ADPCM stream from a BOR music file on the filesystem .
// read header //assert(ftell(fd) == borheader.datastart); // ordinarily true, but not required by the BOR format // check header validity // print artist and title // to make the output neater // get ADPCM stream size in bytes // convert header info to stream info used for encoding // read ADPCM stream