L
linuxcici
Unregistered / Unconfirmed
GUEST, unregistred user!
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<dos.h>
#include<alloc.h>
#define NumberOfTimes1 50
#define NumberOfTimes2 100
#define OldTimerInt 0x60
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
unsigned long Counter;
unsigned CounterInt8,fpI8;
unsigned Port=0x210;
char Found=0;
unsigned Cnt1,Cnt2;
FILE *fp;
long num;
int smp1;
unsigned char a[64000];
struct wavhead
{
char riffid[4];
long cksize;
char fcctype[4];
char fmtid[4];
long ck16;
unsigned fortag;
unsigned nchan;
long nsamps;
long navgbgs;
unsigned nblocka;
unsigned wbpsam;
char dataid[4];
long datalen;
}wfhead;
void Testsb();
void PlayWavBg();
void interrupt NewTimer(__CPPARGS);
void SetTimer(void interrupt(*Rout)(__CPPARGS),unsigned freq);
void RestoreTimer();
void main(int argc,char *argv[])
{
argc=2;
argv[1]="a.wav";
if(argc<2)
{
printf("/nUsage:display filename.wav!!!"
exit(0);
}
if(!(strstr(argv[1],".wav"||strstr(argv[1],".WAV"))
{
printf("/ntype of the file is wrong!please insert *.wav/n"
exit(0);
}
if((fp=fopen(argv[1],"r+b")==NULL)
{
printf("cannot open the file/n"
exit(0);
}
fseek(fp,22L,0);
fread(&wfhead.nchan,sizeof(int),1,fp);
fseek(fp,24L,0);
fread(&wfhead.nsamps,sizeof(long),1,fp);
fseek(fp,34L,0);
fread(&wfhead.wbpsam,sizeof(int),1,fp);
fseek(fp,40L,0);
fread(&num,sizeof(long),1,fp);
fread(a, sizeof(unsigned char), 64000, fp);
Testsb();
PlayWavBg();
while(!kbhit())
printf("."
fclose(fp);
RestoreTimer();
}
void Testsb()
{
Cnt1=NumberOfTimes1;
while((Port<=0x260)&&!Found)
{
outportb(Port+0x6,1);
outportb(Port+0x6,0);
Cnt2=NumberOfTimes2;
while((Cnt2>0)&&(inportb(Port+0xE)<128))
--Cnt2;
if((Cnt2==0)||(inportb(Port+0xA)!=0xAA))
{
--Cnt1;
if(Cnt1==0)
{
Cnt1=NumberOfTimes1;
Port=Port+0x10;
}
}
else
Found=1;
}
if(!Found)
{
printf("Nobase port found!/n"
printf("/nDSP not reseted!/n"
exit(0);
}
}
void PlayWavBg()
{
smp1=44100/wfhead.nsamps;
Counter=1;
fseek(fp,44L,0);
if(wfhead.wbpsam==16)
num=num/2;
SetTimer(NewTimer,44100);
}
void SetTimer(void interrupt(*Rout)(__CPPARGS),unsigned freq)
{
int ICnt;
unsigned char intreg21;
fpI8=(freq+9)/18;
intreg21=inportb(0x21);
outportb(0x21,255);
ICnt=1193180/freq;
outportb(0x43,0x36);
outportb(0x40,ICnt&255);
outportb(0x40,ICnt>>8);
setvect(OldTimerInt,getvect(8));
setvect(8,Rout);
outportb(0x21,intreg21);
}
void RestoreTimer()
{
unsigned char intreg21;
intreg21=inportb(0x21);
outportb(0x21,255);
outportb(0x43,0x36);
outportb(0x40,0);
outportb(0x40,0);
setvect(8,getvect(OldTimerInt));
outportb(0x21,intreg21);
}
void interrupt NewTimer(__CPPARGS)
{
struct REGPACK R;
static long i=0;
Counter++;
if((Counter>=smp1&&i<num)||i==0)
{
i++;
outportb(Port+0xC,0x10);
while(inportb(Port+0xC)&0x80);
outportb(Port+0xC,a[i%64000]);
Counter=0;
}
if(--CounterInt8==0)
{
intr(OldTimerInt,&R);
CounterInt8=fpI8;
}
else outportb(0x20,0x20);
}
以上程序是<C游戏从入门到高手>复过来的,效果就是播放背景音乐,
例用时间中断令程序和音乐可以同部播放,但程序没有效果.
我学了一个学期的C,对硬件不太熟,可以利用你保贵的时间给我调一调吗?
小弟万分感谢了.
#include "ALLOC.H"
#include "DOS.H"
#include "CONIO.H"
#include "STDIO.H"
#include<graphics.h>
void playwav(char wavefile[14],float delaytime);
struct WaveData {
unsigned int SoundLength, Frequency;
char *Sample;
};
struct HeaderType {
long RIFF;
char NI1 [18];
unsigned int Channels;
long Frequency;
char NI2 [6];
char BitRes;
char NI3 [12];
} Header;
struct WaveData Voice;
unsigned int Base;
char WaveFile [25];
int ResetDSP(unsigned int Test)
{
outportb (Test + 0x6, 1);
delay(10);
outportb (Test + 0x6, 0);
delay(10);
if ((inportb(Test + 0xE) & 0x80 == 0x80) && (inportb(Test + 0xA) == 0xAA))
{
Base = Test;
return (1);
}
else
return (0);
}
void WriteDSP(unsigned char Value)
{
while ((inportb(Base + 0xC) & 0x80) == 0x80);
outportb (Base + 0xC, Value);
}
void PlayBack (struct WaveData *Wave)
{
long LinearAddress;
unsigned int Page, OffSet;
unsigned char TimeConstant;
TimeConstant = (65536 - (256000000 / Wave->Frequency)) >> 8;
WriteDSP(0x40);
WriteDSP(TimeConstant);
LinearAddress = FP_SEG (Wave->Sample);
LinearAddress = (LinearAddress << 4) + FP_OFF (Wave->Sample);
Page = LinearAddress >> 16;
OffSet = LinearAddress & 0xFFFF;
outportb (0x0A, 5);
outportb (0x0C, 0);
outportb (0x0B, 0x49);
outportb (0x02, OffSet & 0x100);
outportb (0x02, OffSet >> 8);
outportb (0x83, Page);
outportb (0x03, Wave->SoundLength & 0x100);
outportb (0x03, Wave->SoundLength >> 8);
outportb (0x0A, 1);
WriteDSP(0x14);
WriteDSP(Wave->SoundLength & 0xFF);
WriteDSP(Wave->SoundLength >> 8);
}
int LoadVoice (struct WaveData *Voice, char *FileName)
{
FILE *WAVFile;
WAVFile = fopen(FileName, "rb"
if (WAVFile == NULL) {
return (0);
}
fseek(WAVFile, 0L, SEEK_END);
Voice->SoundLength = ftell (WAVFile) - 48;
fseek(WAVFile, 0L, SEEK_SET);
if (Voice->SoundLength > 32000) {
if (Voice->SoundLength > 64000) {
Voice->SoundLength = 64000;
}
}
free(Voice->Sample);
Voice->Sample = (char *)malloc(Voice->SoundLength);
if (!Voice->Sample) {
return (0);
}
fread(&Header, 46, 1, WAVFile);
if (Header.RIFF != 0x46464952) {
printf ("Not a wave file/n"
return (0);
}
if (Header.Channels != 1) {
printf ("Not a mono wave file/n"
return (0);
}
if (Header.BitRes != 8) {
printf ("Not an 8-bit wave file/n"
return (0);
}
Voice->Frequency = Header.Frequency;
fread(Voice->Sample, Voice->SoundLength + 2, 1, WAVFile);
fclose (WAVFile);
return (1);
}
void playwav(char wavefile[14], float delaytime)
{
if (ResetDSP (0x220)) {
printf (""
} else {
if (ResetDSP (0x240)) {
printf (""
} else {
printf (""
return;
}
}
if (LoadVoice (&Voice, wavefile)) {
PlayBack (&Voice);
delay(delaytime*1000);
WriteDSP (0xD0);
}
}
main()
{
clrscr();
playwav("why1.wav",90000);
}
以上程序是网上找的,可以播放WAV,但是就不能够同步播放
#include<string.h>
#include<process.h>
#include<dos.h>
#include<alloc.h>
#define NumberOfTimes1 50
#define NumberOfTimes2 100
#define OldTimerInt 0x60
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
unsigned long Counter;
unsigned CounterInt8,fpI8;
unsigned Port=0x210;
char Found=0;
unsigned Cnt1,Cnt2;
FILE *fp;
long num;
int smp1;
unsigned char a[64000];
struct wavhead
{
char riffid[4];
long cksize;
char fcctype[4];
char fmtid[4];
long ck16;
unsigned fortag;
unsigned nchan;
long nsamps;
long navgbgs;
unsigned nblocka;
unsigned wbpsam;
char dataid[4];
long datalen;
}wfhead;
void Testsb();
void PlayWavBg();
void interrupt NewTimer(__CPPARGS);
void SetTimer(void interrupt(*Rout)(__CPPARGS),unsigned freq);
void RestoreTimer();
void main(int argc,char *argv[])
{
argc=2;
argv[1]="a.wav";
if(argc<2)
{
printf("/nUsage:display filename.wav!!!"
exit(0);
}
if(!(strstr(argv[1],".wav"||strstr(argv[1],".WAV"))
{
printf("/ntype of the file is wrong!please insert *.wav/n"
exit(0);
}
if((fp=fopen(argv[1],"r+b")==NULL)
{
printf("cannot open the file/n"
exit(0);
}
fseek(fp,22L,0);
fread(&wfhead.nchan,sizeof(int),1,fp);
fseek(fp,24L,0);
fread(&wfhead.nsamps,sizeof(long),1,fp);
fseek(fp,34L,0);
fread(&wfhead.wbpsam,sizeof(int),1,fp);
fseek(fp,40L,0);
fread(&num,sizeof(long),1,fp);
fread(a, sizeof(unsigned char), 64000, fp);
Testsb();
PlayWavBg();
while(!kbhit())
printf("."
fclose(fp);
RestoreTimer();
}
void Testsb()
{
Cnt1=NumberOfTimes1;
while((Port<=0x260)&&!Found)
{
outportb(Port+0x6,1);
outportb(Port+0x6,0);
Cnt2=NumberOfTimes2;
while((Cnt2>0)&&(inportb(Port+0xE)<128))
--Cnt2;
if((Cnt2==0)||(inportb(Port+0xA)!=0xAA))
{
--Cnt1;
if(Cnt1==0)
{
Cnt1=NumberOfTimes1;
Port=Port+0x10;
}
}
else
Found=1;
}
if(!Found)
{
printf("Nobase port found!/n"
printf("/nDSP not reseted!/n"
exit(0);
}
}
void PlayWavBg()
{
smp1=44100/wfhead.nsamps;
Counter=1;
fseek(fp,44L,0);
if(wfhead.wbpsam==16)
num=num/2;
SetTimer(NewTimer,44100);
}
void SetTimer(void interrupt(*Rout)(__CPPARGS),unsigned freq)
{
int ICnt;
unsigned char intreg21;
fpI8=(freq+9)/18;
intreg21=inportb(0x21);
outportb(0x21,255);
ICnt=1193180/freq;
outportb(0x43,0x36);
outportb(0x40,ICnt&255);
outportb(0x40,ICnt>>8);
setvect(OldTimerInt,getvect(8));
setvect(8,Rout);
outportb(0x21,intreg21);
}
void RestoreTimer()
{
unsigned char intreg21;
intreg21=inportb(0x21);
outportb(0x21,255);
outportb(0x43,0x36);
outportb(0x40,0);
outportb(0x40,0);
setvect(8,getvect(OldTimerInt));
outportb(0x21,intreg21);
}
void interrupt NewTimer(__CPPARGS)
{
struct REGPACK R;
static long i=0;
Counter++;
if((Counter>=smp1&&i<num)||i==0)
{
i++;
outportb(Port+0xC,0x10);
while(inportb(Port+0xC)&0x80);
outportb(Port+0xC,a[i%64000]);
Counter=0;
}
if(--CounterInt8==0)
{
intr(OldTimerInt,&R);
CounterInt8=fpI8;
}
else outportb(0x20,0x20);
}
以上程序是<C游戏从入门到高手>复过来的,效果就是播放背景音乐,
例用时间中断令程序和音乐可以同部播放,但程序没有效果.
我学了一个学期的C,对硬件不太熟,可以利用你保贵的时间给我调一调吗?
小弟万分感谢了.
#include "ALLOC.H"
#include "DOS.H"
#include "CONIO.H"
#include "STDIO.H"
#include<graphics.h>
void playwav(char wavefile[14],float delaytime);
struct WaveData {
unsigned int SoundLength, Frequency;
char *Sample;
};
struct HeaderType {
long RIFF;
char NI1 [18];
unsigned int Channels;
long Frequency;
char NI2 [6];
char BitRes;
char NI3 [12];
} Header;
struct WaveData Voice;
unsigned int Base;
char WaveFile [25];
int ResetDSP(unsigned int Test)
{
outportb (Test + 0x6, 1);
delay(10);
outportb (Test + 0x6, 0);
delay(10);
if ((inportb(Test + 0xE) & 0x80 == 0x80) && (inportb(Test + 0xA) == 0xAA))
{
Base = Test;
return (1);
}
else
return (0);
}
void WriteDSP(unsigned char Value)
{
while ((inportb(Base + 0xC) & 0x80) == 0x80);
outportb (Base + 0xC, Value);
}
void PlayBack (struct WaveData *Wave)
{
long LinearAddress;
unsigned int Page, OffSet;
unsigned char TimeConstant;
TimeConstant = (65536 - (256000000 / Wave->Frequency)) >> 8;
WriteDSP(0x40);
WriteDSP(TimeConstant);
LinearAddress = FP_SEG (Wave->Sample);
LinearAddress = (LinearAddress << 4) + FP_OFF (Wave->Sample);
Page = LinearAddress >> 16;
OffSet = LinearAddress & 0xFFFF;
outportb (0x0A, 5);
outportb (0x0C, 0);
outportb (0x0B, 0x49);
outportb (0x02, OffSet & 0x100);
outportb (0x02, OffSet >> 8);
outportb (0x83, Page);
outportb (0x03, Wave->SoundLength & 0x100);
outportb (0x03, Wave->SoundLength >> 8);
outportb (0x0A, 1);
WriteDSP(0x14);
WriteDSP(Wave->SoundLength & 0xFF);
WriteDSP(Wave->SoundLength >> 8);
}
int LoadVoice (struct WaveData *Voice, char *FileName)
{
FILE *WAVFile;
WAVFile = fopen(FileName, "rb"
if (WAVFile == NULL) {
return (0);
}
fseek(WAVFile, 0L, SEEK_END);
Voice->SoundLength = ftell (WAVFile) - 48;
fseek(WAVFile, 0L, SEEK_SET);
if (Voice->SoundLength > 32000) {
if (Voice->SoundLength > 64000) {
Voice->SoundLength = 64000;
}
}
free(Voice->Sample);
Voice->Sample = (char *)malloc(Voice->SoundLength);
if (!Voice->Sample) {
return (0);
}
fread(&Header, 46, 1, WAVFile);
if (Header.RIFF != 0x46464952) {
printf ("Not a wave file/n"
return (0);
}
if (Header.Channels != 1) {
printf ("Not a mono wave file/n"
return (0);
}
if (Header.BitRes != 8) {
printf ("Not an 8-bit wave file/n"
return (0);
}
Voice->Frequency = Header.Frequency;
fread(Voice->Sample, Voice->SoundLength + 2, 1, WAVFile);
fclose (WAVFile);
return (1);
}
void playwav(char wavefile[14], float delaytime)
{
if (ResetDSP (0x220)) {
printf (""
} else {
if (ResetDSP (0x240)) {
printf (""
} else {
printf (""
return;
}
}
if (LoadVoice (&Voice, wavefile)) {
PlayBack (&Voice);
delay(delaytime*1000);
WriteDSP (0xD0);
}
}
main()
{
clrscr();
playwav("why1.wav",90000);
}
以上程序是网上找的,可以播放WAV,但是就不能够同步播放