Page 1 of 2

Beep Sound Efffects...

Posted: Sun Jul 15, 2007 2:06 am
by Lprogster
Hi guys :)

I've been coding fine for a while now - everything is just going great. I've now been thinking about upgrading my beep ( ) function to play multiple tones. Currently it produces a note for a certain amount of time.

I would really like to change this so I could play a variety of notes, possibly at different volumes. Is this possible? If so, a code example would be brilliant.

Thanks again,
Lster

Re: Beep Sound Efffects...

Posted: Sun Jul 15, 2007 2:58 am
by Brendan
Hi,
Lprogster wrote:I would really like to change this so I could play a variety of notes, possibly at different volumes. Is this possible? If so, a code example would be brilliant.
Generating single notes at different frequencies is easy. Anything more than that (e.g. 2 notes at same time, volume control, etc) is not.

In theory, it'd be possible to use an insane amount of CPU time and "pulse width modulation" to gain a large amount of control over the PC speaker (including multiple different notes at the same time, volume control, and perhaps even playing several different digitized sounds at the same time with different volumes).

In practice, the PC speaker has very non-standard acoustic qualities. The same speaker in a small computer case has a different sound to the same speaker in a large computer case, different computers have different speakers, and some computers even use piezos instead of the 3.5 inch paper cone speaker that's fairly common. I'd assume the speaker driver electronics can also vary in quality (and effect the resulting sound), and might even include a low bandpass filter that makes pulse width modulation almost impossible. Lastly, it's usually impractical - an IRQ or SMI could create distortion and you'd need to disable task switches until all sound is completed.

Despite all of this, the idea is to generate a high frequency square wave where the duty cycle of the square wave is used to to control the "average" voltage applied to the speaker (and therefore the position of the speaker).

For example, for a sine wave you could send a stream like this to the speaker (where '1' is speaker output high and '0' is speaker output low):

1111 1110 1100 1000 0000 1000 11000 11110 1111

You could also take into account hysterisis. For example, if you want a square wave at 50% amplitude you could try sending a stream like this:

000000000000101010101010101010000000000

The problem is that the speaker takes time to move, and you'd end up with output that looks more like "________xxXXXXXXXXxx________". To reduce the effect of this you could try sending a stream like this:

000000000000111111101010101010000000000

Where the extra "ones" help to push the speaker to "half way" faster, and give you something closer to "________xXXXXXXXXXxx________". It's still not perfect, but it is slightly less imperfect... ;)

To improve this you can use bias, so that silence is:

01010101010101010101010101010101

And the square wave at 50% amplitude looks like this:

0101010111111110110110110110110000000101010

This allows you to take into account hysterisis in both directions (where the "1111111" helps push the speaker to "75%" faster, and the "000000" helps to drop the speaker back to "25%" faster...


Cheers,

Brendan

Posted: Sun Jul 15, 2007 3:19 am
by Lprogster
Thanks for the info. I don't want to do pulse modulation though, but I can still play different notes, right?

What port IO do I need to do to play a certain note. I'm really looking for an example of port IO to produce different notes...

Thanks lots,
Lster

Posted: Sun Jul 15, 2007 3:37 am
by AndrewAPrice
The most useful link I've found so far is:
http://www.gamedev.net/reference/articl ... cle442.asp

And another great article (I just like the table at the bottom :P)
http://fly.cc.fer.hr/GDM/articles/sndmus/speaker1.html

Posted: Sun Jul 15, 2007 3:50 am
by Lprogster
Err... What's so funny? The information is still useful as it explains differences and I may change my mind and use pulse modulation later on... I'm really not sure what I'm doing with sound - anything and everything is useful!

Lster

Posted: Sun Jul 15, 2007 6:53 am
by Lprogster
I think I have done it! I really appreciate your help; thanks!

Pulse Width Modulation

Posted: Sun Jul 15, 2007 8:16 am
by mutex
You wont use that mouch cpu time for a pwm signal to the pcspeaker. I remember in win3.1 that you could install a pcspeaker driver that "emulated" a soundblaster. So playing wav etc was possible. It used pwm and was running smooth on 386 dx33mhz with 4mb ram :) I also remember Psyco Pinball and Pinball Fantasies for dos did this..

I have'nt done any maths on it, but it should be quite possible to implement. Just link in a 8000khz mono wav and do a routine to play it on the speaker.. Would be VERY cool :D

-
Thomas

Posted: Sun Jul 15, 2007 8:32 am
by earlz
I have read an article that told how to do that without using a lot of CPU, don't remember it now though....

Posted: Sun Jul 15, 2007 8:57 am
by Lprogster
That sounds really good - I will search for tomorrow. That would be like software rendering, wouldn't it? How would a modern OS like Windows Xp play high-quality sound without hardware? Would they use this method still?

I am very intrigued...

Thanks,
Lster

Pcspeaker with pcm playback.. :)

Posted: Sun Jul 15, 2007 9:42 am
by mutex
http://en.wikipedia.org/wiki/PC_speaker

Take a look at the info here and check out the links at the end..

-
Thomas

Posted: Sun Jul 15, 2007 10:05 am
by inflater
Space Racer had a beautiful intro music coming out from the PC speaker:

http://www.oldskool.org/sound/pc/exampl ... rIntro.mp3

(Acutally no, this isn't Soundblaster. ;))

I actually downloaded this game from an abandonware site (I think legally, for god's sake, it's a game from 1987 or so :D) and it played this same through a normal PC speaker! :)

inflater

Posted: Sun Jul 15, 2007 10:08 am
by Lprogster
...

I guess that would be the next step... Actually using a dedicated card...

Thanks,
Lster

Posted: Sun Jul 15, 2007 12:07 pm
by Dex
At first the AC97 looks like a good replacement for the old SB, but there are problems, as in you can not use the same driver for all vendors, as they seem to need setting up differently. Some can use the same driver, but it's a pain just to get over 30% working.

Posted: Sun Jul 15, 2007 12:15 pm
by Lprogster
Does Qemu emulate a sound card? If so, would and AC97 driver work with that?

Posted: Sun Jul 15, 2007 12:19 pm
by Lprogster
And, if so, can you recommend any resources. So far I have only found the "OS Dever Cottontail" page which has an archived copy of "Audio Codec '97 Revision 2.3" by Intel.

Would that be good to read?

Thanks,
Lster