wav24to16.zip/wav24to16.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment (lib,"Comdlg32.lib")
int convert(char *in,char *out){
FILE *fp,*wfp;
int size;
int i;
char data[0x28];
fp = fopen(in,"rb");
fread(data,1,0x28,fp);
fread(&size,4,1,fp);
size /= 3;
*(int*)&data[4] = 0x2C+size*2-8;
if(*(short*)&data[34]!=24){
printf("24bit WAVではありません\n");
fclose(fp);
fclose(wfp);
return 0;
}
*(short*)&data[34] = 16;
*(short*)&data[32] = 2*(*(short*)&data[22]);
*(int*)&data[28] = *(int*)&data[24]*(*(short*)&data[32]);
wfp = fopen(out,"wb");
fwrite(data,1,0x28,wfp);
size *= 2;
fwrite(&size,4,1,wfp);
size /= 2;
i = 0;
data[3] = 0x00;
while(i < size){
fread(data,1,3,fp);
fwrite(&data[1],1,2,wfp);
i++;
}
fclose(fp);
fclose(wfp);
printf("%sファイルの変換が完了しました\n",in);
return 0;
}
void GetAppDir(char *str){
int i;
GetModuleFileName(NULL,str,256);
i = strlen(str)-1;
while(str[i] != '\\' && str[i] != '/' && i > 0)i--;
str[i] = '\0';
}
int Open(char *s){
char fn[256]="",str[256];
char dir[256]="";
OPENFILENAME ofn;
if(s == NULL || strlen(s)==0){
GetAppDir(dir);
memset(&ofn,0,sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFilter = "WAVファイル(*.wav)\0*.wav\0All files(*.*)\0*.*\0\0";
ofn.lpstrFile = fn;
ofn.nMaxFile = 256;
ofn.lpstrInitialDir = dir;
ofn.Flags = OFN_FILEMUSTEXIST;
ofn.lpstrTitle = "ファイルを開く";
ofn.lpstrDefExt = "wav";
if(!GetOpenFileName(&ofn)){
return 1;
}
} else {
strcpy(fn,s);
}
strcpy(str,fn);
if(strcmp(&fn[strlen(fn)-4],".wav")!=0){
printf("不正な拡張子です(%s)\n",fn);
} else {
strcpy(&fn[strlen(fn)-4],"_16.wav");
convert(str,fn);
}
return 0;
}
int main(int argc,char *argv[]){
int i;
char str[256];
if(argc < 2){
Open(NULL);
} else {
i = 1;
while(i < argc){
Open(argv[i]);
i++;
}
}
printf("全ての処理が終了しました、終了する場合はENTERを押してください\n");
getchar();
return 0;
}