分享
 
 
 

播放WAV文件的c程序

王朝厨房·作者佚名  2007-01-04
窄屏简体版  字體: |||超大  

#include "ALLOC.H"

#include "DOS.H"

#include "CONIO.H"

#include "STDIO.H"

void playwav(char wavefile[14],float delaytime);

struct WaveData {

unsigned int SoundLength, Frequency;

char *Sample;

};

struct HeaderType {

long RIFF; //RIFF header

char NI1 [18]; //not important

unsigned int Channels; //channels 1 = mono; 2 = stereo

long Frequency; //sample frequency

char NI2 [6]; //not important

char BitRes; //bit resolution 8/16 bit

char NI3 [12]; //not important

} Header;

struct WaveData Voice; //Pointer to wave file

unsigned int Base; //Sound Blaster base address

char WaveFile [25]; //File name for the wave file to be played

/****************************************************************************

** Checks to see if a Sound Blaster exists at a given address, returns **

** true if Sound Blaster found, false if not. **

****************************************************************************/

int ResetDSP(unsigned int Test)

{

//Reset the DSP

outportb (Test + 0x6, 1);

delay(10);

outportb (Test + 0x6, 0);

delay(10);

//Check if (reset was succesfull

if ((inportb(Test + 0xE) & 0x80 == 0x80) && (inportb(Test + 0xA) == 0xAA))

{

//DSP was found

Base = Test;

return (1);

}

else

//No DSP was found

return (0);

}

/****************************************************************************

** Send a byte to the DSP (Digital Signal Processor) on the Sound Blaster **

****************************************************************************/

void WriteDSP(unsigned char Value)

{

//Wait for the DSP to be ready to accept data

while ((inportb(Base + 0xC) & 0x80) == 0x80);

//Send byte

outportb (Base + 0xC, Value);

}

/****************************************************************************

** Plays a part of the memory **

****************************************************************************/

void PlayBack (struct WaveData *Wave)

{

long LinearAddress;

unsigned int Page, OffSet;

unsigned char TimeConstant;

TimeConstant = (65536 - (256000000 / Wave->Frequency)) >> 8;

WriteDSP(0x40); //DSP-command 40h - Set sample frequency

WriteDSP(TimeConstant); //Write time constant

//Convert pointer to linear address

LinearAddress = FP_SEG (Wave->Sample);

LinearAddress = (LinearAddress << 4) + FP_OFF (Wave->Sample);

Page = LinearAddress >> 16; //Calculate page

OffSet = LinearAddress & 0xFFFF; //Calculate offset in the page

/*

Note - this procedure only works with DMA channel 1

*/

outportb (0x0A, 5); //Mask DMA channel 1

outportb (0x0C, 0); //Clear byte pointer

outportb (0x0B, 0x49); //Set mode

/*

The mode consists of the following:

0x49 = binary 01 00 10 01

| | | |

| | | +- DMA channel 01

| | +---- Read operation (the DSP reads from memory)

| +------- Single cycle mode

+---------- Block mode

*/

outportb (0x02, OffSet & 0x100); //Write the offset to the DMA controller

outportb (0x02, OffSet >> 8);

outportb (0x83, Page); //Write the page to the DMA controller

outportb (0x03, Wave->SoundLength & 0x100);

outportb (0x03, Wave->SoundLength >> 8);

outportb (0x0A, 1); //Unmask DMA channel

WriteDSP(0x14); // DSP-command 14h - Single cycle playback

WriteDSP(Wave->SoundLength & 0xFF);

WriteDSP(Wave->SoundLength >> 8);

}

/****************************************************************************

** Loads a wave file into memory. **

** This procedure expects a _very_ standard wave header. **

** It doesn’t perform much error checking. **

****************************************************************************/

int LoadVoice (struct WaveData *Voice, char *FileName)

{

FILE *WAVFile;

//If it can’t be opened...

WAVFile = fopen(FileName, "rb");

if (WAVFile == NULL) {

//..display error message

return (0);

}

//Return length of file for sound length minus 48 bytes for .WAV header

fseek(WAVFile, 0L, SEEK_END);

Voice->SoundLength = ftell (WAVFile) - 48;

fseek(WAVFile, 0L, SEEK_SET);

//Check RIFF header

if (Voice->SoundLength > 32000) {

if (Voice->SoundLength > 64000) {

Voice->SoundLength = 64000;

}

}

free(Voice->Sample);

Voice->Sample = (char *)malloc(Voice->SoundLength); //Assign memory

if (!Voice->Sample) {

return (0);

}

//Load the sample data

fread(&Header, 46, 1, WAVFile);

//Check RIFF header

if (Header.RIFF != 0x46464952) {

printf ("Not a wave file\n");

return (0);

}

//Check channels

if (Header.Channels != 1) {

printf ("Not a mono wave file\n");

return (0);

}

//Check bit resolution

if (Header.BitRes != 8) {

printf ("Not an 8-bit wave file\n");

return (0);

}

Voice->Frequency = Header.Frequency;

//Load the sample data

fread(Voice->Sample, Voice->SoundLength + 2, 1, WAVFile);

fclose (WAVFile); //Close the file

return (1);

}

void playwav (char wavefile[14], float delaytime = 1.0 )

{

if (ResetDSP (0x220)) {

//at 220h

printf ("");

} else {

if (ResetDSP (0x240)) {

//at 240h

printf ("");

} else {

//or none at all

printf ("");

return;

}

}

//Load wave file

if (LoadVoice (&Voice, wavefile)) {

//Start playback

PlayBack (&Voice);

delay(delaytime*1000);

//Stops DMA-transfer

WriteDSP (0xD0);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有