tools/openwav2bor/source/bor2wav.c
/**
 * Decodes the ADPCM to PCM and saves it as a WAV file.
 * @param filename the path to the output WAV file
 * @param buf structure containing the ADPCM stream and its metadata
 * @return true on success, false on error
 */
Decodes the ADPCM to PCM and saves it as a WAV file.
@param filename the path to the output WAV file
@param buf structure containing the ADPCM stream and its metadata
@return true on success, false on error
Decodes the ADPCM to PCM and saves it as a WAV file . @param filename the path to the output WAV file @param buf structure containing the ADPCM stream and its metadata @return true on success false on error
bool writewav(char *filename, samplestruct *buf)
{
	struct {
		uint32_t riff;
		uint32_t size;
		uint32_t type;
	} riffheader;
	struct {
		uint32_t tag;
		uint32_t size;
	} rifftag;
	struct {
		uint16_t format;	 
		uint16_t channels;	 
		uint32_t samplerate; 
		uint32_t bps;		 
		uint16_t blockalign;
		uint16_t samplebits; 
	} fmt;
	int i=0, len = buf->soundlen;
	unsigned char *adpcm = buf->sampleptr;
	short *pcm = malloc(ADPCM_CHUNK_SIZE * 4);
	FILE *fd = fopen(filename, "wb");
	if(fd == NULL) ioerror(strerror(errno));
	riffheader.riff = SwapLSB32(HEX_RIFF);
	riffheader.size = SwapLSB32(sizeof(riffheader) - 8 + 2*sizeof(rifftag) + sizeof(fmt) + 4*buf->soundlen);
	riffheader.type = SwapLSB32(HEX_WAVE);
	if(fwrite(&riffheader, sizeof(riffheader), 1, fd) != 1) ioerror("can't write to file");
	rifftag.tag = SwapLSB32(HEX_fmt);
	rifftag.size = SwapLSB32(sizeof(fmt));
	fmt.format = SwapLSB16(FMT_PCM);
	fmt.channels = SwapLSB16(buf->channels);
	fmt.samplerate = SwapLSB32(buf->frequency);
	fmt.bps = SwapLSB32(buf->frequency * buf->channels * 2);
	fmt.blockalign = SwapLSB16(buf->channels * 2);
	fmt.samplebits = SwapLSB16(16);
	if(fwrite(&rifftag, sizeof(rifftag), 1, fd) != 1) ioerror(strerror(errno));
	if(fwrite(&fmt, sizeof(fmt), 1, fd) != 1) ioerror(strerror(errno));
	rifftag.tag = SwapLSB32(HEX_data);
	rifftag.size = SwapLSB32(buf->soundlen * 4);
	if(fwrite(&rifftag, sizeof(rifftag), 1, fd) != 1) ioerror(strerror(errno));
	while(len)
	{
		int adpcmsize = len < ADPCM_CHUNK_SIZE ? len : ADPCM_CHUNK_SIZE;
		int pcmsize = adpcmsize * 4;
		if(adpcm_decode(adpcm, pcm, adpcmsize, buf->channels) != pcmsize) ioerror("can't decode ADPCM data");
		if(fwrite(pcm, 1, pcmsize, fd) != pcmsize) ioerror(strerror(errno));
		adpcm += adpcmsize;
		i += pcmsize;
		printf("\rConverting... %09d", i);
		len -= adpcmsize;
	}
	printf("\n");
	free(buf->sampleptr);
	buf->sampleptr = NULL;
	free(pcm);
	fclose(fd);
	return true;
error:
	free(buf->sampleptr);
	buf->sampleptr = NULL;
	if(pcm) free(pcm);
	if(fd) fclose(fd);
	return false;
}
bool writewav ( char * filename , samplestruct * buf ) { struct { uint32_t riff ; uint32_t size ; uint32_t type ; } riffheader ; struct { uint32_t tag ; uint32_t size ; } rifftag ; struct { uint16_t format ; uint16_t channels ; uint32_t samplerate ; uint32_t bps ; uint16_t blockalign ; uint16_t samplebits ; } fmt ; int i = 0 , len = buf -> soundlen ; unsigned char * adpcm = buf -> sampleptr ; short * pcm = malloc ( ADPCM_CHUNK_SIZE * 4 ) ; FILE * fd = fopen ( filename , " " ) ; if ( fd == NULL ) ioerror ( strerror ( errno ) ) ; riffheader . riff = SwapLSB32 ( HEX_RIFF ) ; riffheader . size = SwapLSB32 ( sizeof ( riffheader ) - 8 + 2 * sizeof ( rifftag ) + sizeof ( fmt ) + 4 * buf -> soundlen ) ; riffheader . type = SwapLSB32 ( HEX_WAVE ) ; if ( fwrite ( & riffheader , sizeof ( riffheader ) , 1 , fd ) != 1 ) ioerror ( " " ) ; rifftag . tag = SwapLSB32 ( HEX_fmt ) ; rifftag . size = SwapLSB32 ( sizeof ( fmt ) ) ; fmt . format = SwapLSB16 ( FMT_PCM ) ; fmt . channels = SwapLSB16 ( buf -> channels ) ; fmt . samplerate = SwapLSB32 ( buf -> frequency ) ; fmt . bps = SwapLSB32 ( buf -> frequency * buf -> channels * 2 ) ; fmt . blockalign = SwapLSB16 ( buf -> channels * 2 ) ; fmt . samplebits = SwapLSB16 ( 16 ) ; if ( fwrite ( & rifftag , sizeof ( rifftag ) , 1 , fd ) != 1 ) ioerror ( strerror ( errno ) ) ; if ( fwrite ( & fmt , sizeof ( fmt ) , 1 , fd ) != 1 ) ioerror ( strerror ( errno ) ) ; rifftag . tag = SwapLSB32 ( HEX_data ) ; rifftag . size = SwapLSB32 ( buf -> soundlen * 4 ) ; if ( fwrite ( & rifftag , sizeof ( rifftag ) , 1 , fd ) != 1 ) ioerror ( strerror ( errno ) ) ; while ( len ) { int adpcmsize = len < ADPCM_CHUNK_SIZE ? len : ADPCM_CHUNK_SIZE ; int pcmsize = adpcmsize * 4 ; if ( adpcm_decode ( adpcm , pcm , adpcmsize , buf -> channels ) != pcmsize ) ioerror ( " " ) ; if ( fwrite ( pcm , 1 , pcmsize , fd ) != pcmsize ) ioerror ( strerror ( errno ) ) ; adpcm += adpcmsize ; i += pcmsize ; printf ( " \r " , i ) ; len -= adpcmsize ; } printf ( " \n " ) ; free ( buf -> sampleptr ) ; buf -> sampleptr = NULL ; free ( pcm ) ; fclose ( fd ) ; return true ; error : free ( buf -> sampleptr ) ; buf -> sampleptr = NULL ; if ( pcm ) free ( pcm ) ; if ( fd ) fclose ( fd ) ; return false ; }
Decodes the ADPCM to PCM and saves it as a WAV file.
Decodes the ADPCM to PCM and saves it as a WAV file .
// 1 = PCM // Mono, stereo // 11025, 22050, 44100 // Bytes/second // 8, 12, 16 // write RIFF header // write format chunk // write data chunk header // decode ADPCM stream to PCM and write to WAV file