Page 1 of 1

Speaker Beep

Posted: Fri Jul 25, 2008 9:08 pm
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!

Re: Speaker Beep

Posted: Fri Jul 25, 2008 10:26 pm
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. :)

Re: Speaker Beep

Posted: Sat Jul 26, 2008 2:33 am
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?... ;)

Re: Speaker Beep

Posted: Sat Jul 26, 2008 2:42 am
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?

Re: Speaker Beep

Posted: Sat Jul 26, 2008 2:58 am
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).

Re: Speaker Beep

Posted: Sat Jul 26, 2008 3:03 am
by Omega
Well, that is you and this is me, so I suppose that is life. Don't you think?

Re: Speaker Beep

Posted: Sat Jul 26, 2008 4:49 am
by inflater
Good, you have the beeping routines. Now try this http://forum.osdev.org/viewtopic.php?f=13&t=17293 :twisted:

Re: Speaker Beep

Posted: Sat Jul 26, 2008 12:47 pm
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.