Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to run wave files (.wav) thru' C? Urgent..... 1

Status
Not open for further replies.

arbadhe

MIS
May 17, 2001
2
0
0
IN
Is it possible to run wave files (.wav) thru' C?
If yes, How? pls give me some details of it.
If No, what are other sound files which can be run thru' C? What is the proc. of doing the same.
 
Ok... As per your e-mail here's code of how to do it in DOS:
Note: The commands to access IO ports may be different on your compiler.

Code:
/* Code to playback an 8bit *.wav file */

#include <stdio.h>
#define PORT 0x220  /* Soundblaster Base Port, change to 0x240 or whatever */
		    /* for different hardware configuration */

void writedsp( int byte, int bp );
int samplebyte( int bp );
void sbreset( int bp );
unsigned long getspeed(void);

int main(void)
{

int delay, speed;
FILE *fp;

speed = getspeed();
printf (&quot;Speed = %u\n&quot;, speed);
sbreset(PORT);  /* Reset the Soundblaster card */
if( fp = fopen( &quot;c:\\windows\\hal.wav&quot;,&quot;rb&quot; ) )
  {
   printf( &quot;Now Playing...&quot; );
   writedsp(0xD1, PORT);  /* Turn speaker on */
   while (feof(fp)== 0)
	 {
	  writedsp(0x10, PORT);
	  writedsp(fgetc(fp), PORT);
	  for (delay = 0 ; delay < speed; delay++) {;}
	 }
   writedsp(0xD3, PORT);  /* Turn speaker off */
   }
else
   { printf( &quot;Error in opening file\n&quot; ); }
return 0;
}


void writedsp( int byte, int bp )
{
/* Writes to the Soundblaster's DSP Command Channel -
*  call with bp% = SB base port (normally 0x220)
*  byte = byte to write to DSP
*/

int t, dspcmd, x;
dspcmd = bp + 12;
for (t = 1; t < 8; t++)
    { x = inp(dspcmd); }  /* Delay to give SB time to process code */
outp (dspcmd, byte);
}

int samplebyte( int bp )
{
/* Samples a byte from the SB's ADC, and returns
*  the resultant byte.  Call with BP = SB base port
*  (normally 0x220)
*/

int port;
writedsp(0x20, bp); /* Command to sample one byte */
port = bp + 14;
while ( inp(port) & 0x80 == 0 ) {;}
port = bp + 10;
return (inp(port));
}

void sbreset( int bp )
{
/* Resets the Soundblaster chip -
*  call with bp = Base Port (normally 0x220)
*/

int port, t, x;
port = bp + 6;
outp (port, 1);
for (t = 1; t < 10; t++)
    { x = inp(port); }  /* Delay loop, give SB time to reset */
outp ( port, 0 );
port = bp + 10;
for (t = 1; t < 10; t++)
    { x = inp(port); }
}

unsigned long getspeed(void)
  {
   unsigned long temp, count = 0;
   temp = clock() + 800;
   while( (clock() < temp ) )
	{ count++; }
  return (count >> 4);
  }


Kim_Christensen@telus.net
 
In Windows it is different. I haven't done any wave playback in Win32. In Win16 you could just use sndPlaySound(lpszSoundName, wFlags) where lpszSoundName is a string depicting the wavefile name and wFlags is either SND_SYNC or SND_ASYNC. Other options, such as playing a WAV file from a resource are also possible.
In Win32 you have to use the wave/out device class for playback of a wave file. You'll have to refer to the SDK for more info on that.



Kim_Christensen@telus.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top