Skip to contentSkip to conversation

minimal PC speaker output

PATCH In review Joachim Henke 7 participants 21 messages Jan 9, 2006 – Jan 29, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 minimal PC speaker output

Ok, although this ugly hack has nothing to do with real PC speaker emulation, it's probably the most portable way to have some noise anyway. I just post this for people (like me) who are missing their guest operating system beeps. For this case the patch below is "better than nothing" (c:

Greets
Jo.

pc.c +5 -0
@@ -276,6 +276,11 @@
 static void speaker_ioport_write(void *opaque, uint32_t addr, uint32_t val)
 {
+    int spk_off = speaker_data_on ^ 1;
+
     speaker_data_on = (val >> 1) & 1;
     pit_set_gate(pit, 2, val & 1);
+    /* ring terminal bell, if speaker is switched on */
+    if (spk_off & speaker_data_on)
+        puts("*beep*\a");
 }
 
Signature · 2 lines
Joachim Henke
http://he-jo.net/
#1 lore raw

Hi,

I think it would not be difficult to add a real speaker emulation with the audio API written by malc, at least to simulate tone generation using the PIT.

Regards,

Fabrice.

Quoted 23 lines · Joachim Henke

Ok, although this ugly hack has nothing to do with real PC speaker emulation, it's probably the most portable way to have some noise anyway. I just post this for people (like me) who are missing their guest operating system beeps. For this case the patch below is "better than nothing" (c:

Greets
Jo.

pc.c +5 -0
@@ -276,6 +276,11 @@
 static void speaker_ioport_write(void *opaque, uint32_t addr, uint32_t val)
 {
+    int spk_off = speaker_data_on ^ 1;
+
     speaker_data_on = (val >> 1) & 1;
     pit_set_gate(pit, 2, val & 1);
+    /* ring terminal bell, if speaker is switched on */
+    if (spk_off & speaker_data_on)
+        puts("*beep*\a");
 }
 
#2 lore raw

Excellent idea. Thanks for your hint! The adlib code is probably a good point to start from. I'll have a look at it, when I find some time.

Jo.

Quoted 9 lines

Hi,

I think it would not be difficult to add a real speaker emulation with the audio API written by malc, at least to simulate tone generation using the PIT.

Regards,

Fabrice.

Signature · 2 lines
DSL-Aktion wegen großer Nachfrage bis 28.2.2006 verlängert:
GMX DSL-Flatrate 1 Jahr kostenlos* http://www.gmx.net/de/go/dsl
#3 lore raw

PATCH 1/1 PC speaker emulation

In reply to Fabrice Bellard

Ok, here is it - my first attempt for emulating the PC speaker using the audio API. This needs some testing, tough it seems to work well with full system emulation on my iMac G5. Would be nice if someone could test this in a different environment (on a PC, in user mode, ...).

With my patch, QEMU plays a sine wave, that is calculated on base of the count value in PIT channel 2. So it only supports tone generation (typical for system beeps), but no sample playing using two PITs (I mean that noisy sound from old DOS programs :) - It would probably be not that easy to emulate this in realtime.

The attached zip file also includes a DOS program, that came with an old computer book. It plays a long tone scale, each tone half a second. You can use this for testing, but maybe you have some more interesting applications.

The patch is probably a bit too hackish to be included yet. As I am a bit busy these days, I just sent it as a request for comments. I would prefer to use a sample rate of 32000 instead of 44100 Hz, but when playing with sample rates of 22050 or 32000 Hz, QEMU becomes so slow and unusable, that I need to kill it. I don't know if this is something Mac specific. malc, could you please have a look, if I use your audio API correctly?

Thanks
Jo.

Quoted 7 lines · Fabrice Bellard

Hi,

I think it would not be difficult to add a real speaker emulation with the audio API written by malc, at least to simulate tone generation using the PIT.

Regards,

Fabrice.

Signature · 2 lines
Joachim Henke
http://he-jo.net/
#4 lore raw

[Qemu-devel] Re: [PATCH] PC speaker emulation

I forgot to mention, that you must call QEMU with the switch '-soundhw pcspk' to enable PC speaker emulation.

Sorry
Jo.

Quoted 10 lines · Joachim Henke

Ok, here is it - my first attempt for emulating the PC speaker using the audio API. This needs some testing, tough it seems to work well with full system emulation on my iMac G5. Would be nice if someone could test this in a different environment (on a PC, in user mode, ...).

With my patch, QEMU plays a sine wave, that is calculated on base of the count value in PIT channel 2. So it only supports tone generation (typical for system beeps), but no sample playing using two PITs (I mean that noisy sound from old DOS programs :) - It would probably be not that easy to emulate this in realtime.

The attached zip file also includes a DOS program, that came with an old computer book. It plays a long tone scale, each tone half a second. You can use this for testing, but maybe you have some more interesting applications.

The patch is probably a bit too hackish to be included yet. As I am a bit busy these days, I just sent it as a request for comments. I would prefer to use a sample rate of 32000 instead of 44100 Hz, but when playing with sample rates of 22050 or 32000 Hz, QEMU becomes so slow and unusable, that I need to kill it. I don't know if this is something Mac specific. malc, could you please have a look, if I use your audio API correctly?

Thanks
Jo.

Signature · 2 lines
Joachim Henke
http://he-jo.net/
#5 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation

In reply to Joachim Henke
Quoted 7 lines · Joachim Henke

Ok, here is it - my first attempt for emulating the PC speaker using the audio API. This needs some testing, tough it seems to work well with full system emulation on my iMac G5. Would be nice if someone could test this in a different environment (on a PC, in user mode, ...).

With my patch, QEMU plays a sine wave, that is calculated on base of the count value in PIT channel 2. So it only supports tone generation (typical for system beeps), but no sample playing using two PITs (I mean that noisy sound from old DOS programs :) - It would probably be not that easy to emulate this in realtime.

The attached zip file also includes a DOS program, that came with an old computer book. It plays a long tone scale, each tone half a second. You can use this for testing, but maybe you have some more interesting applications.

The patch is probably a bit too hackish to be included yet. As I am a bit busy these days, I just sent it as a request for comments. I would prefer to use a sample rate of 32000 instead of 44100 Hz, but when playing with sample rates of 22050 or 32000 Hz, QEMU becomes so slow and unusable, that I need to kill it. I don't know if this is something Mac specific. malc, could you please have a look, if I use your audio API correctly?

Nope.

a. Writing more than `free' bytes of data is pointless
b. Once AUD_write return zero one should stop writing untill some space
becomes available (for all intents and purposes after callback will be called next time)

Item b is probably why it hangs: you try to write N bytes, AUD_write returns zero, yet you are tring again immediately - ad nauseam. We have only one thread - hence audio can not push the data out of internal buffers into the host, and what you get is an infinite loop.

--
mailto:[email protected]

#6 lore raw

PATCH 1/1 PC speaker emulation (fixed)

Thanks a lot for your help! I wasn't aware that AUD_write could return zero. Seems that I was just lucky, it didn't do that with 44100 Hz sample rate (c:

Now it works fine with 32000 Hz, and I really encourage people (especially PC users) to test the attached patch. Call QEMU with the switch '-soundhw pcspk', and try to make some noise.

Regards,
Jo.

Quoted 8 lines · malc

b. Once AUD_write return zero one should stop writing untill some space
becomes available (for all intents and purposes after callback will be called next time)

Item b is probably why it hangs: you try to write N bytes, AUD_write returns zero, yet you are tring again immediately - ad nauseam. We have only one thread - hence audio can not push the data out of internal buffers into the host, and what you get is an infinite loop.

Signature · 2 lines
Joachim Henke
http://he-jo.net/
#7 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed)

Quoted 1 line · Joachim Henke

Thanks a lot for your help! I wasn't aware that AUD_write could return zero. Seems that I was just lucky, it didn't do that with 44100 Hz sample rate (c:

You are welcome.

Quoted 2 lines

Now it works fine with 32000 Hz, and I really encourage people (especially PC users) to test the attached patch. Call QEMU with the switch '-soundhw pcspk', and try to make some noise.

I'd like to not one thing, namely, you are using FPU to generate the samples. This is something Fabrice expressed dissatisfaction with. In the case of speaker it might be feasible to switch to fixed-point calculation.

Sincerely,
malc

Signature · 1 line
mailto:[email protected]
#8 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed)

Thanks for the hint! I assume, the reason, why floating point calculations should be avoided, is to be compatible with processors like ARM, that don't necessarily have an FPU.

Yes, I'll rewrite the waveform generation stuff to fit in fixed point. It should also be faster then.

Jo.

Quoted 4 lines · malc

I'd like to not one thing, namely, you are using FPU to generate the samples. This is something Fabrice expressed dissatisfaction with. In the case of speaker it might be feasible to switch to fixed-point calculation.

Signature · 5 lines
Joachim Henke
http://he-jo.net/

Lust, ein paar Euro nebenbei zu verdienen? Ohne Kosten, ohne Risiko!
Satte Provisionen für GMX Partner: http://www.gmx.net/de/go/partner
#9 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed)

Hi,

Quoted 6 lines · Joachim Henke

Thanks for the hint! I assume, the reason, why floating point calculations should be avoided, is to be compatible with processors like ARM, that don't necessarily have an FPU.

Yes, I'll rewrite the waveform generation stuff to fit in fixed point. It should also be faster then.

FWIW, I tried to "fix" some float usage once upon a time, and some documentation of this can be found here:

http://libvncserver.sourceforge.net/qemu/qemu-fixed-point.txt

Hth,
Dscho

#10 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed)

In reply to malc
Quoted 4 lines · malc

I'd like to not one thing, namely, you are using FPU to generate the samples. This is something Fabrice expressed dissatisfaction with. In the case of speaker it might be feasible to switch to fixed-point calculation.

One more note about that. PC-speaker generates just plain square wave not sine wave, so this would be more realistic. Then fixed point calculation (16 bit integer part and 16 bit fractional) is easy without all those sin calculation.

rgds
Sebastian Kaliszewski

#11 lore raw

PATCH 1/1 PC speaker emulation (fixed point)

I still prefer using a sine wave, it sounds more smooth and won't hurt our ears (and speakers) too much. I don't think that people want to play square waves, when they have their PC connected to their hifi system (c: The main purpose of the PC speaker is to generate tones and many programmers used it to play melodies. Why shouldn't we take this chance to make it sound as good as we can manage it?

I rewrote the wave form generation routine to fit into 32 bit integer, with the highest fixed point precision for the range of all possible parameters. To retrieve the sine values, we use a small lookup table with a resolution of 1024 entries (we need to store the first quarter only, since the rest is symmetric).

I extensively tested these routines (on iMac G5 / AMD K6, gcc 3 / gcc 4) to ensure, that the final sample values differ at most by +/-1 from their double float calculated counterparts. This difference doesn't seem to be much, but when comparing directly, you can hear a minor noise for frequencies below 200 Hz. Maybe it also depends a bit on the speakers used.

However, this approach should still be sufficient for our purpose. Most programs don't use too low frequencies, and if they do, people probably won't notice.

Just test the attached patch yourselves

Jo.

Quoted 1 line · Sebastian Kaliszewski

One more note about that. PC-speaker generates just plain square wave not sine wave, so this would be more realistic. Then fixed point calculation (16 bit integer part and 16 bit fractional) is easy without all those sin calculation.

Signature · 2 lines
Joachim Henke
http://he-jo.net/
#12 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed point)

Quoted 1 line · Joachim Henke

I still prefer using a sine wave, it sounds more smooth and won't hurt our ears (and speakers) too much.

Well, it sounds rather dull, and even worse, on non hi-fi computer speakers (which is 90% of PC users use) low frequencies (<100Hz) are simply unhearable (since equipment is very poor at transmitting those frequencies). While harmonics rich square wave will be hearable, due to harmonics being hearable.

Quoted 1 line

I don't think that people want to play square waves, when they have their PC connected to their hifi system

Many people connected their Apples IIs, Commodores 64s & Ataris 800s to their Hi-Fi equipment, and those didn't break their ears nor equipment.

Quoted 1 line

(c: The main purpose of the PC speaker is to generate tones and many programmers used it to play melodies. Why shouldn't we take this chance to make it sound as good as we can manage it?

Exactly. No real musical instrument generates sine wawes. Moreover as various psyhoacustics reaserch shows, using pure sine waes changes our perception of chords & harmony. Then 95% of home sound equipment degrages and attenuates low frequencies below 40-50Hz.

If Qemu is for emulation it should emulate things as close to reality as feasible. Why do more work & intentionally break the close emulation, while it's even easier to acheive?

[snip]

Quoted 1 line

However, this approach should still be sufficient for our purpose. Most programs don't use too low frequencies, and if they do, people probably won't notice.

Many PC games used low frequencies to emit varius buzzes and stuff (like engine noise). With sine wave those will be completely broken.

rgds

Signature · 1 line
Sebastian Kaliszewski
#13 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed point)

Ok, these are really strong arguments. Thanks a lot for your interesting statements! I'll do some testing on square waves and will post an updated patch, as I am also not totally satisfied with the current sound myself.

Sincerely
Jo.

Quoted 8 lines · Sebastian Kaliszewski

Well, it sounds rather dull, and even worse, on non hi-fi computer speakers (which is 90% of PC users use) low frequencies (<100Hz) are simply unhearable (since equipment is very poor at transmitting those frequencies). While harmonics rich square wave will be hearable, due to harmonics being hearable.

No real musical instrument generates sine wawes. Moreover as various psyhoacustics reaserch shows, using pure sine waes changes our perception of chords & harmony. Then 95% of home sound equipment degrages and attenuates low frequencies below 40-50Hz.

If Qemu is for emulation it should emulate things as close to reality as feasible. Why do more work & intentionally break the close emulation, while it's even easier to acheive?

Many PC games used low frequencies to emit varius buzzes and stuff (like engine noise). With sine wave those will be completely broken.

Signature · 2 lines
Joachim Henke
http://he-jo.net/
#14 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed point)

Quoted 1 line · Joachim Henke

Ok, these are really strong arguments. Thanks a lot for your interesting statements! I'll do some testing on square waves and will post an updated patch, as I am also not totally satisfied with the current sound myself.

One little suggestion...

Real PC-speaker is rather poor source of sound, and I also noticed, that sound cards which took PC-speaker sound for themselves (some SB-clones did route PC-speaker sound into thier own output) liked to low-pass filter the resulting audio. So to make things as real as feasible, use your wave table to store something like square wave with rounded corners -- maybe sth like pow(sin(x), 0.2) (i.e. sinus rooted to 5th degree) will sound pleasant enough.

rgds

Signature · 1 line
Sebastian Kaliszewski
#15 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed point)

Quoted 10 lines · Sebastian Kaliszewski
Quoted 1 line · Joachim Henke

Ok, these are really strong arguments. Thanks a lot for your interesting statements! I'll do some testing on square waves and will post an updated patch, as I am also not totally satisfied with the current sound myself.

One little suggestion...

Real PC-speaker is rather poor source of sound, and I also noticed, that sound cards which took PC-speaker sound for themselves (some SB-clones did route PC-speaker sound into thier own output) liked to low-pass filter the resulting audio. So to make things as real as feasible, use your wave table to store something like square wave with rounded corners -- maybe sth like pow(sin(x), 0.2) (i.e. sinus rooted to 5th degree)
will sound pleasant enough.

If you want to model the real PC speaker, the best to do is to generate a square signal and to pass it thru a low pass filter with a cut off frequency of a few kHz. Then you could even be able to play samples thru the simulated PC speaker using the tricks used in old MSDOS programs, provided QEMU implements a precise cycle counter (it will come someday !).

Fabrice.

#16 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed point)

Quoted 6 lines · Fabrice Bellard

If you want to model the real PC speaker, the best to do is to generate a square signal and to pass it thru a low pass filter with a cut off frequency of a few kHz. Then you could even be able to play samples thru the simulated PC speaker using the tricks used in old MSDOS programs, provided QEMU implements a precise cycle counter (it will come someday !).

Change that few to 15 [kHz] and that would be what some PC-speaker hooking SB-clones did. But there were PCs with piezoelectric speakers, and those could genereate sounds well above 20kHz. But yes, typically 12-15kHz cutoff with 1st or 2nd order filter would do the trick.

And if someone wants full reality one would also need some not-too-strong (1st order, 6dB/oct) hi-pass filter around 80-120Hz -- as those tiny PC-speakers are unable to emit bass.

rgds

Signature · 1 line
Sebastian Kaliszewski
#17 lore raw

PATCH 1/1 PC speaker emulation (square wave)

In reply to Fabrice Bellard

Ok, I simplified my patch to generate just plain square waves. Indeed, its sound is much closer to a real PC speaker now.

Does "cut off frequency" mean, that we have silence above that specific frequency? I changed my patch to go this way. Before, it always played the highest possible frequency for that case (f > 16 kHz).

This emulation should be sufficient for now. I'm not sure, if it's worth to add more overhead to emulate every possible shortcoming of an original PC speaker. We can go ahead, when we have realtime PIT emulation.

Regards
Jo.

Quoted 1 line · Fabrice Bellard

If you want to model the real PC speaker, the best to do is to generate a square signal and to pass it thru a low pass filter with a cut off frequency of a few kHz. Then you could even be able to play samples thru the simulated PC speaker using the tricks used in old MSDOS programs, provided QEMU implements a precise cycle counter (it will come someday !).

Signature · 2 lines
Joachim Henke
http://he-jo.net/
#18 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (square wave)

Quoted 6 lines · Joachim Henke

Ok, I simplified my patch to generate just plain square waves. Indeed, its sound is much closer to a real PC speaker now.

Does "cut off frequency" mean, that we have silence above that specific frequency? I changed my patch to go this way. Before, it always played the highest possible frequency for that case (f > 16 kHz).

No, it means the higher-frequency harmonic components of the square wave are attenuated, which affects how it sounds. Filtering a square wave in this way is particularly simple, but unlikely to be worth the effort.

-- Jamie

#19 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (square wave)

In reply to Joachim Henke
Quoted 3 lines · Joachim Henke

Ok, I simplified my patch to generate just plain square waves. Indeed, its sound is much closer to a real PC speaker now.

Does "cut off frequency" mean, that we have silence above that specific frequency?

No. Typical cutoff freq is where components of the signal are attenuated by 3dB (i.e. are half as loud). At 2 times taht frequency they'll be attenuated by 3 + A dB, where A is filter's rate in dB/octave. Typical 1st order filter is 6dB/octave, 2nd order is 12dB/octave, etc. Typical soundards used no more than 2nd order filter.

But, if you want to have your path behave really nicely, you should simply not generate at all waves at frequencies above half of current output sampling rate -- those can't be reproducted -- instead significant distortion will be generated (google for Nyquist frequency).

rgds

Signature · 1 line
Sebastian Kaliszewski
#20 lore raw

Re: [Qemu-devel] [PATCH] PC speaker emulation (square wave)

The fact, that we can only reproduce frequencies up to half of sampling frequency, has already been dealt with since the first patch I posted (2006-01-18).

I chose to use a sample rate of 32000 Hz, which should be sufficient for our purpose. So we can handle PIT count values down to 75. Previous patches also played the highest possible frequency for PIT count values 1-74. I changed this with the square wave patch: It plays "silence" for frequencies above 16 kHz now.

Currently, I want to keep things as simple as possible. I see no need for producing more overhead to emulate low-pass filtering for plain tone generation. We can implement this, when we have realtime PIT emulation, which would make full PC speaker emulation possible. But, as you told, there are different kinds of PC speakers anyway.

I'm currently improving the patch a bit and will post one, that could be included into CVS, soon.

Quoted 3 lines · Sebastian Kaliszewski

No. Typical cutoff freq is where components of the signal are attenuated by 3dB (i.e. are half as loud). At 2 times taht frequency they'll be attenuated by 3 + A dB, where A is filter's rate in dB/octave. Typical 1st order filter is 6dB/octave, 2nd order is 12dB/octave, etc. Typical soundards used no more than 2nd order filter.

But, if you want to have your path behave really nicely, you should simply not generate at all waves at frequencies above half of current output sampling rate -- those can't be reproducted -- instead significant distortion will be generated (google for Nyquist frequency).

Signature · 2 lines
Joachim Henke
http://he-jo.net/