Beep Sound Efffects...

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Lprogster
Member
Member
Posts: 174
Joined: Tue Nov 14, 2006 11:59 am

Beep Sound Efffects...

Post 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
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Beep Sound Efffects...

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Lprogster
Member
Member
Posts: 174
Joined: Tue Nov 14, 2006 11:59 am

Post 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
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post 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
Last edited by AndrewAPrice on Sun Jul 15, 2007 4:00 am, edited 1 time in total.
My OS is Perception.
Lprogster
Member
Member
Posts: 174
Joined: Tue Nov 14, 2006 11:59 am

Post 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
Lprogster
Member
Member
Posts: 174
Joined: Tue Nov 14, 2006 11:59 am

Post by Lprogster »

I think I have done it! I really appreciate your help; thanks!
User avatar
mutex
Member
Member
Posts: 131
Joined: Sat Jul 07, 2007 7:49 pm

Pulse Width Modulation

Post 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
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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....
Lprogster
Member
Member
Posts: 174
Joined: Tue Nov 14, 2006 11:59 am

Post 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
User avatar
mutex
Member
Member
Posts: 131
Joined: Sat Jul 07, 2007 7:49 pm

Pcspeaker with pcm playback.. :)

Post 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
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post 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
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
Lprogster
Member
Member
Posts: 174
Joined: Tue Nov 14, 2006 11:59 am

Post by Lprogster »

...

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

Thanks,
Lster
Last edited by Lprogster on Tue Oct 23, 2007 10:57 am, edited 1 time in total.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post 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.
Lprogster
Member
Member
Posts: 174
Joined: Tue Nov 14, 2006 11:59 am

Post by Lprogster »

Does Qemu emulate a sound card? If so, would and AC97 driver work with that?
Lprogster
Member
Member
Posts: 174
Joined: Tue Nov 14, 2006 11:59 am

Post 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
Post Reply