I'm trying to make a wav player without using any libraries just solid C and x86 assembly which is compatible with DOS PC's.I am using Turbo C compiler (version 3.0) pcm and stereo aren't supported in this example (<- Because I don't know compression) Here's the code: #include #define BUFFER 43
unsigned int sd = 0;
unsigned short f = 0;
main(char *argv[]){
FILE *wav = fopen(argv[1], "r");
unsigned int datalenght,samplerate,filelenght,lenght = 0;
unsigned short pcmordd,monoorstereo,samplebits = 0;
char byte[BUFFER], *chunk;
if(wav < 0){
printf("Can't open file!");
exit(-1);
}else{
if(fgets(byte, BUFFER, wav) == EOF){
printf("Unexcepted EOF");
}
if(byte[0] != 0x52){
printf("File is not RIFF!");
exit(1);
}
if(byte[1] != 0x49){
printf("File is not RIFF!");
exit(1);
}
if(byte[2] != 0x46){
printf("File is not RIFF!");
exit(1);
}
if(byte[3] != 0x46){
printf("File is not RIFF!");
exit(1);
}
filelenght = (byte[4] * 16777216) + (byte[5] * 65536) + (byte[6] * 256) + byte[7];
if(byte[8] != 0x57){
printf("Not a valid WAVE file");
exit(1);
}
if(byte[9] != 0x41){
printf("Not a valid WAVE file");
exit(1);
}
if(byte[10] != 0x56){
printf("Not a valid WAVE file");
exit(1);
}
if(byte[11] != 0x45){
printf("Not a valid WAVE file");
exit(1);
}
if(byte[12] != 0x66){
printf("Only fmt supported");
exit(1);
}
if(byte[13] != 0x6D){
printf("Only fmt supported");
exit(1);
}
if(byte[14] != 0x74){
printf("Only fmt supported");
exit(1);
}
if(byte[15] != 0x20){
printf("Only fmt supported");
exit(1);
}
pcmordd = (byte[20] * 256) + byte[21];
if(pcmordd != 1){
printf("Only PCM supported");
exit(1);
}
monoorstereo = (byte[22] * 256) + byte[23];
if(monoorstereo != 1){
printf("Stereo is not supported");
exit(1);
}
samplerate = (byte[24] * 16777216) + (byte[25] * 65536) + (byte[26] * 256) + byte[27];
samplebits = (byte[34] * 256) + byte[35];
datalenght = (byte[40] * 16777216) + (byte[41] * 65536) + (byte[42] * 256) + byte[43];
lenght = datalenght / (samplerate * (samplebits / 8));
printf("File size is %d", filelenght);
printf("The file is %f seconds long", lenght);
sd = (65535*25) / samplerate;
while(1){
if(fgets(chunk,(samplebits / 2), wav) == EOF){
printf("EOF reported (end of the song)");
fclose(wav);
exit(0);
}
f = (chunk[0] * 256) + byte[1];
makesound();
}
}
return 0;
}
makesound(){
asm{
mov al, 182
out 43h, al
mov ax, f
out 42h, al
mov al, ah
out 42h, al
in al, 61h
or al, 00000011b
out 61h, al
mov cx, sd
}
pause1:
asm{
dec cx
jne pause1
in al, 61h
and al, 11111100b
out 61h, al
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire