/*

		wav2snd.exe : WAV->SND converter
		
		copyright (c) by bye

		※ステレオデータの場合は，左の音だけを変換します

*/

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<time.h>

FILE	*fin,*fout;
unsigned char	buf[30000];

void	main(int argc,char *argv[])
{
	int		i,c;
	int		channel,sps,bps;
	/*	channel : 0=モノラル，1=ステレオ
		sps : samples per second
		bps : bits per sample		*/
	
	long	size,l,p,q;
	
	srand(time(NULL));
	
	if(argc!=3)
		{
		puts("wav2snd.exe : WAV->SND converter");
		puts("copyright (c) by bye");
		puts("");
		puts("usage : wav2snd infile(.wav) outfile(.snd)");
		return;
		}
	
	/*	ファイルのオープン	*/
	if((fin=fopen(argv[1],"rb"))==NULL)
		{
		printf("%s cannot open.",argv[1]);
		return;
		}
	
	if((fout=fopen(argv[2],"wb"))==NULL)
		{
		printf("%s cannot open.",argv[2]);
		return;
		}
	
	/*	RIFFチャンクの確認	*/
	fread(buf,1,12,fin);
	if(!(strncmp(buf,"RIFF",4)==0 && strncmp(buf+8,"WAVE",4)==0))
		{
		puts("cannot find RIFF chunk.");
		return;
		}
	
	/*	fmtチャンクの確認	*/
	fread(buf,1,24,fin);
	if(!(strncmp(buf,"fmt ",4)==0))
		{
		puts("cannot find fmt chunk.");
		return;
		}
	
	if(!(*((unsigned short *)(buf+8))==1))
		{
		puts("illegal waveFormatType.");
		return;
		}
	
	channel=*((unsigned short *)(buf+10));
	sps=*((unsigned long *)(buf+12));
	bps=*((unsigned short *)(buf+22));
	
	/*	dataチャンクの確認	*/
	fread(buf,1,8,fin);
	if(!(strncmp(buf,"data",4)==0))
		{
		puts("cannot find data chunk.");
		return;
		}
	
	size=*((unsigned long *)(buf+4));
	
	/*	データ内容の表示	*/
	printf("%s : %dHz(%dbit/",argv[1],sps,bps);
	if(channel==1)		printf("monaural");
	else				printf("stereo");
	printf(")  %ld bytes ",size);
	
	for(i=0;i<(int)((size+16384L-1L)/16384L);i++)
		{
		putchar('o');
		}
	
	for(i=0;i<(int)((size+16384L-1L)/16384L);i++)
		{
		putchar(8);
		}
	
	/*	SNDのヘッダを作成	*/
	strncpy(buf,"        ",8);				/*	名前			*/
	
	*((unsigned short *)(buf+8))=rand();	/*	サウンドID		*/
	*((unsigned short *)(buf+10))=rand();
	
	*((unsigned long *)(buf+12))=size;		/*	データ幅		*/
	
	*((unsigned long *)(buf+16))=0;			/*	ループポイント	*/
	*((unsigned long *)(buf+20))=0;			/*	ループレングス	*/
	
	*((unsigned short *)(buf+24))=(unsigned short)((long)sps*0x62L/1000L);
											/*	サンプリング周波数	*/
	*((unsigned short *)(buf+26))=0;		/*	補正値			*/
	*((unsigned char *)(buf+28))=0x3C;		/*	基本音階		*/
	
	*((unsigned char *)(buf+29))=0;			/*	リザーブ		*/
	*((unsigned char *)(buf+30))=0;			/*	同上			*/
	
	fwrite(buf,1,32,fout);
	
	/*	データコンバート	*/
	p=0;	/*	read pointer	*/
	q=0;	/*	write pointer	*/
	c=16384;
	fread(buf,1,30000,fin);
	putchar('.');
	
	/*	8 bit の場合	*/
	if(bps==8)
		{
		for(l=0;l<size;l++)
			{
			i=*((unsigned char *)(buf+p));	/*	i はWAVの 8 bit 形式	*/
			p+=channel;
			if(i<128)		i=128-i;		/*	SNDの 8 bit 形式に変換	*/
			buf[q++]=i;
			
			if(p>=30000)
				{
				fwrite(buf,1,q,fout);
				fread(buf,1,30000,fin);
				p=0;
				q=0;
				}
			
			if((--c)==0)
				{
				c=16384;
				putchar('.');
				}
			}
		}
	else{
		for(l=0;l<size;l++)
			{
			i=(int)(((long)*((signed short *)(buf+p))+32768L)/256L);
			p+=channel*2;					/*	i はWAVの 8 bit 形式	*/
			if(i<128)		i=128-i;		/*	SNDの 8 bit 形式に変換	*/
			buf[q++]=i;
			
			if(p>=30000)
				{
				fwrite(buf,1,q,fout);
				fread(buf,1,30000,fin);
				p=0;
				q=0;
				}
			
			if((--c)==0)
				{
				c=16384;
				putchar('.');
				}
			}
		}
	fwrite(buf,1,q,fout);
	
	return;
}
