Speaker Beep

Programming, for all ages and all languages.
Post Reply
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Speaker Beep

Post by Omega »

Beep, Beep. What's up guys? I thought this might be fun for your OS. I think I am going to use it for errors (error codes in beeps), you know like Morse code, except mine will generate natural voices through sophisticated alien like programming skills, but that is for the books and this is for you:

Code: Select all

void StartSound(unsigned int Frequency)
{
	unsigned int Divisor = 1193180 / Frequency;

	outb(0x43, 0xB6);
	outb(0x42, Divisor & 0xFF);
	outb(0x42, Divisor >> 8);

	outb(0x61, inb(0x61) | 3);
}

void StopSound()
{
	outb(0x61, inb(0x61) & ~3);
}

void beep(unsigned int Frequency, unsigned int Duration)
{
	StartSound(Frequency);
	delay(Duration);
	StopSound();
}
Happy Coding!
Free energy is indeed evil for it absorbs the light.
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Re: Speaker Beep

Post by Omega »

For those listening, here is an example of how to use this:

Code: Select all

	beep(440.000,1); //A4
	beep(493.883,1); //B4
	beep(261.626,1); //C4
	beep(293.665,1); //D4
	beep(329.628,1); //E4
	beep(349.228,1); //F4
	beep(391.995,1); //G4
Now if you get bored you can generate your favorite monophonic instrumental (it does play through the speakers btw). Here is mine:

Code: Select all

beep(391.995,1); //G4
beep(2349.32,1); //D7
beep(391.995,1); //G4
Mary Had a Little Lamb. :)
Free energy is indeed evil for it absorbs the light.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Speaker Beep

Post by JamesM »

Hi there,

Sorry to piss on your parade yet again, but do you notice that you're passing single-precision floating point numbers to a function that takes an unsigned int argument? That definately won't work. (I assume when you tested it you changed the function argument lists to contain floats instead of unsigned ints).

Not only that but how many small hobby OS's actually save the FPU state on task switch?... ;)
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Re: Speaker Beep

Post by Omega »

No, your great. I have learned a lot from you JamesM and I am glad that you take the time to comment, because it makes me better. So, I suppose I best fix that; round it? And, when I post code I do it for the longterm broadness of it. I know that at some point someone will come from Google wanting to know how to do this very thing and here it will be for them buggy, yet complete. :)

It's beta code, I post it seconds after I write it, so what can I say. . . I'm impatient?
Free energy is indeed evil for it absorbs the light.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Speaker Beep

Post by JamesM »

The rule I tend to go by is that if I'm posting example code, make it exemplary (to the best of my ability).
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Re: Speaker Beep

Post by Omega »

Well, that is you and this is me, so I suppose that is life. Don't you think?
Free energy is indeed evil for it absorbs the light.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Re: Speaker Beep

Post by inflater »

Good, you have the beeping routines. Now try this http://forum.osdev.org/viewtopic.php?f=13&t=17293 :twisted:
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
Omega
Member
Member
Posts: 250
Joined: Sun May 25, 2008 2:04 am
Location: United States
Contact:

Re: Speaker Beep

Post by Omega »

If I can find a use for this in my project I certainly will incorporate the best of it. Thanks for the link.
Free energy is indeed evil for it absorbs the light.
Post Reply