Home

Forums

Web development

 

 

 

 
     
 
dna88 Web development and Technology Forum
 
Profile   Register   Memberlist   Usergroups   FAQ   Search  Log in
Make sound with C++

 
Post new topic   Reply to topic    dna88 Forum Index -> Programming in Java, C, C#, VB, .NET Discussion Forum
Author Message
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: Make sound with C++ Reply with quote

Anyone knows how to produce sound at pc speaker using C++?
It should be fairly common but I cannot seem to find how to do it. Turbo C / Visual C.

For a starter I need some direction or code as to how produce a sound. Any sound with C.
_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Sat Jun 26, 04 7:20 am
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: making sound in PC speaker with C Reply with quote

I did little research about making sound in PC speaker with C. And found a sample code. Anyone interested can give this a try.

Quote:
EXTERN void DoSound( unsigned int , unsigned int );

#if defined(USE_WIN32)
void DoSound( unsigned int freq, unsigned int time )
{
Beep( freq, time );
return;
}
#elif defined(USE_DOS)
#if defined(__DJGPP__)
#undef nosound

void nosound(void)
{
sound(0);
return;
}

#define SLEEP(t) delay(t/2)

void DoSound(unsigned int freq, unsigned int time)
{
sound(freq);
SLEEP(time);
nosound();
return;
}
#elif defined(HAVE_SOUND)
void DoSound(unsigned int freq, unsigned int time)
{
sound(freq);
sleep(time);
nosound();
return;
}
#else
#ifndef LOBYTE
#define LOBYTE(w) ((BYTE) (w))
#endif

#ifndef HIBYTE
#define HIBYTE(w) ((BYTE) (((USHORT) (w) >> 8) & 0xFF))
#endif

EXTERN void HWSound( unsigned );
EXTERN void HWNoSound( void );
EXTERN void HWDelay( unsigned );

void HWSound(unsigned int freq)
{
USHORT counter = 1193280 / freq; /* cycle counter */

outp(0x43, 0xB6); /* prepare timer */
outp(0x42, LOBYTE(counter)); /* send low byte */
outp(0x42, HIBYTE(counter)); /* send high byte */
outp(0x61, inp(0x61) | 0x03); /* turn speaker ON */
return;
}

void HWNoSound(void)
{
outp(0x61, inp(0x61) & 0xFC); /* turn speaker OFF */
return;
}

void HWDelay(unsigned int cc)
{
time_t delay;
delay = clock() + cc;
while (delay > clock());
return;
}

void DoSound(unsigned int freq, unsigned int time)
{
HWSound(freq);
HWDelay(time*1000);
HWNoSound();
return;
}

#endif
#else
#include <asm/io.h>
#include <signal.h>
#include <stdio.h>

#define outp outb
#define inp inb

#ifndef LOBYTE
#define LOBYTE(w) ((BYTE) (w))
#endif

#ifndef HIBYTE
#define HIBYTE(w) ((BYTE) (((USHORT) (w) >> 8) & 0xFF))
#endif

EXTERN void HWSound( unsigned );
EXTERN void HWNoSound( void );

void HWSound(unsigned int freq)
{
USHORT counter = 1193280 / freq; /* cycle counter */

outp(0x43, 0xB6); /* prepare timer */
outp(0x42, LOBYTE(counter)); /* send low byte */
outp(0x42, HIBYTE(counter)); /* send high byte */
outp(0x61, inp(0x61) | 0x03); /* turn speaker ON */
return;
}

void HWNoSound(void)
{
outp(0x61, inp(0x61) & 0xFC); /* turn speaker OFF */
return;
}

void DoSound(unsigned int freq, unsigned int time)
{
HWSound(freq);
usleep(time*1000);
HWNoSound();
return;
}
#endif


If anyone else has any more reference or functions or sample codes on this sound issue with C, please reply.

Thanks.
_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Sun Jun 27, 04 5:12 am
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
Display posts from previous:   
Post new topic   Reply to topic    dna88 Forum Index -> Programming in Java, C, C#, VB, .NET Discussion Forum All times are GMT - 7 Hours
Page 1 of 1

 

Partners and Resources

Bangladesh hosting company

Bangladesh web design

Driven by phpBB © phpBB Group