qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [patch] minimal PC speaker output
@ 2006-01-09 19:01 Joachim Henke
  2006-01-09 20:40 ` Fabrice Bellard
  0 siblings, 1 reply; 21+ messages in thread
From: Joachim Henke @ 2006-01-09 19:01 UTC (permalink / raw)
  To: qemu-devel

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
+++ pc.c
@@ -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");
 }
 

-- 
Joachim Henke
http://he-jo.net/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [patch] minimal PC speaker output
  2006-01-09 19:01 [Qemu-devel] [patch] minimal PC speaker output Joachim Henke
@ 2006-01-09 20:40 ` Fabrice Bellard
  2006-01-10  9:11   ` he.jo
  2006-01-18 23:05   ` [Qemu-devel] [PATCH] PC speaker emulation Joachim Henke
  0 siblings, 2 replies; 21+ messages in thread
From: Fabrice Bellard @ 2006-01-09 20:40 UTC (permalink / raw)
  To: qemu-devel

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.

Joachim Henke wrote:
> 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
> +++ pc.c
> @@ -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");
>  }
>  
> 

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [patch] minimal PC speaker output
  2006-01-09 20:40 ` Fabrice Bellard
@ 2006-01-10  9:11   ` he.jo
  2006-01-18 23:05   ` [Qemu-devel] [PATCH] PC speaker emulation Joachim Henke
  1 sibling, 0 replies; 21+ messages in thread
From: he.jo @ 2006-01-10  9:11 UTC (permalink / raw)
  To: qemu-devel

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="us-ascii", Size: 474 bytes --]

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.

> 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.

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [Qemu-devel] [PATCH] PC speaker emulation
  2006-01-09 20:40 ` Fabrice Bellard
  2006-01-10  9:11   ` he.jo
@ 2006-01-18 23:05   ` Joachim Henke
  2006-01-18 23:32     ` [Qemu-devel] " Joachim Henke
  2006-01-19  0:32     ` [Qemu-devel] " malc
  1 sibling, 2 replies; 21+ messages in thread
From: Joachim Henke @ 2006-01-18 23:05 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 1563 bytes --]

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.


Fabrice Bellard wrote:
> 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.

-- 
Joachim Henke
http://he-jo.net/

[-- Attachment #2: speaker.zip --]
[-- Type: application/zip, Size: 8045 bytes --]

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [Qemu-devel] Re: [PATCH] PC speaker emulation
  2006-01-18 23:05   ` [Qemu-devel] [PATCH] PC speaker emulation Joachim Henke
@ 2006-01-18 23:32     ` Joachim Henke
  2006-01-19  0:32     ` [Qemu-devel] " malc
  1 sibling, 0 replies; 21+ messages in thread
From: Joachim Henke @ 2006-01-18 23:32 UTC (permalink / raw)
  To: qemu-devel

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

Sorry
Jo.

Joachim Henke wrote:
> 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.

-- 
Joachim Henke
http://he-jo.net/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation
  2006-01-18 23:05   ` [Qemu-devel] [PATCH] PC speaker emulation Joachim Henke
  2006-01-18 23:32     ` [Qemu-devel] " Joachim Henke
@ 2006-01-19  0:32     ` malc
  2006-01-19 20:44       ` [Qemu-devel] [PATCH] PC speaker emulation (fixed) Joachim Henke
  1 sibling, 1 reply; 21+ messages in thread
From: malc @ 2006-01-19  0:32 UTC (permalink / raw)
  To: qemu-devel

On Thu, 19 Jan 2006, Joachim Henke wrote:

> 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:malc@pulsesoft.com

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [Qemu-devel] [PATCH] PC speaker emulation (fixed)
  2006-01-19  0:32     ` [Qemu-devel] " malc
@ 2006-01-19 20:44       ` Joachim Henke
  2006-01-19 23:29         ` malc
  0 siblings, 1 reply; 21+ messages in thread
From: Joachim Henke @ 2006-01-19 20:44 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 869 bytes --]

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.


malc wrote:
> 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.

-- 
Joachim Henke
http://he-jo.net/

[-- Attachment #2: pcspeaker.diff.gz --]
[-- Type: application/x-gzip, Size: 3162 bytes --]

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed)
  2006-01-19 20:44       ` [Qemu-devel] [PATCH] PC speaker emulation (fixed) Joachim Henke
@ 2006-01-19 23:29         ` malc
  2006-01-20  9:42           ` Joachim Henke
  2006-01-20 12:03           ` Sebastian Kaliszewski
  0 siblings, 2 replies; 21+ messages in thread
From: malc @ 2006-01-19 23:29 UTC (permalink / raw)
  To: qemu-devel

On Thu, 19 Jan 2006, Joachim Henke wrote:

> 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.

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

-- 
mailto:malc@pulsesoft.com

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed)
  2006-01-19 23:29         ` malc
@ 2006-01-20  9:42           ` Joachim Henke
  2006-01-20 13:03             ` Johannes Schindelin
  2006-01-20 12:03           ` Sebastian Kaliszewski
  1 sibling, 1 reply; 21+ messages in thread
From: Joachim Henke @ 2006-01-20  9:42 UTC (permalink / raw)
  To: qemu-devel

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="us-ascii", Size: 700 bytes --]

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.


malc wrote:
> 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.

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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed)
  2006-01-19 23:29         ` malc
  2006-01-20  9:42           ` Joachim Henke
@ 2006-01-20 12:03           ` Sebastian Kaliszewski
  2006-01-23 21:10             ` [Qemu-devel] [PATCH] PC speaker emulation (fixed point) Joachim Henke
  1 sibling, 1 reply; 21+ messages in thread
From: Sebastian Kaliszewski @ 2006-01-20 12:03 UTC (permalink / raw)
  To: qemu-devel

malc wrote:
> 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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed)
  2006-01-20  9:42           ` Joachim Henke
@ 2006-01-20 13:03             ` Johannes Schindelin
  0 siblings, 0 replies; 21+ messages in thread
From: Johannes Schindelin @ 2006-01-20 13:03 UTC (permalink / raw)
  To: qemu-devel

Hi,

On Fri, 20 Jan 2006, Joachim Henke wrote:

> 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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [Qemu-devel] [PATCH] PC speaker emulation (fixed point)
  2006-01-20 12:03           ` Sebastian Kaliszewski
@ 2006-01-23 21:10             ` Joachim Henke
  2006-01-24 12:35               ` Sebastian Kaliszewski
  0 siblings, 1 reply; 21+ messages in thread
From: Joachim Henke @ 2006-01-23 21:10 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 1633 bytes --]

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.


Sebastian Kaliszewski wrote:
> 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.

-- 
Joachim Henke
http://he-jo.net/

[-- Attachment #2: pc_speaker.diff.gz --]
[-- Type: application/x-gzip, Size: 3572 bytes --]

[-- Attachment #3: Type: text/plain, Size: 1 bytes --]



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed point)
  2006-01-23 21:10             ` [Qemu-devel] [PATCH] PC speaker emulation (fixed point) Joachim Henke
@ 2006-01-24 12:35               ` Sebastian Kaliszewski
  2006-01-24 15:50                 ` Joachim Henke
  0 siblings, 1 reply; 21+ messages in thread
From: Sebastian Kaliszewski @ 2006-01-24 12:35 UTC (permalink / raw)
  To: qemu-devel

Joachim Henke wrote:
> 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.

> 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.


> (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]
> 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
-- 
Sebastian Kaliszewski

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed point)
  2006-01-24 12:35               ` Sebastian Kaliszewski
@ 2006-01-24 15:50                 ` Joachim Henke
  2006-01-24 18:29                   ` Sebastian Kaliszewski
  0 siblings, 1 reply; 21+ messages in thread
From: Joachim Henke @ 2006-01-24 15:50 UTC (permalink / raw)
  To: qemu-devel

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.


Sebastian Kaliszewski wrote:
> 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.

-- 
Joachim Henke
http://he-jo.net/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed point)
  2006-01-24 15:50                 ` Joachim Henke
@ 2006-01-24 18:29                   ` Sebastian Kaliszewski
  2006-01-24 21:45                     ` Fabrice Bellard
  0 siblings, 1 reply; 21+ messages in thread
From: Sebastian Kaliszewski @ 2006-01-24 18:29 UTC (permalink / raw)
  To: qemu-devel

Joachim Henke wrote:
> 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
-- 
Sebastian Kaliszewski

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed point)
  2006-01-24 18:29                   ` Sebastian Kaliszewski
@ 2006-01-24 21:45                     ` Fabrice Bellard
  2006-01-25  0:38                       ` Sebastian Kaliszewski
  2006-01-25 21:54                       ` [Qemu-devel] [PATCH] PC speaker emulation (square wave) Joachim Henke
  0 siblings, 2 replies; 21+ messages in thread
From: Fabrice Bellard @ 2006-01-24 21:45 UTC (permalink / raw)
  To: qemu-devel

Sebastian Kaliszewski wrote:
> Joachim Henke wrote:
> 
>> 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.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (fixed point)
  2006-01-24 21:45                     ` Fabrice Bellard
@ 2006-01-25  0:38                       ` Sebastian Kaliszewski
  2006-01-25 21:54                       ` [Qemu-devel] [PATCH] PC speaker emulation (square wave) Joachim Henke
  1 sibling, 0 replies; 21+ messages in thread
From: Sebastian Kaliszewski @ 2006-01-25  0:38 UTC (permalink / raw)
  To: qemu-devel

On Tuesday 24 January 2006 22:45, Fabrice Bellard wrote:
> 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
-- 
Sebastian Kaliszewski

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [Qemu-devel] [PATCH] PC speaker emulation (square wave)
  2006-01-24 21:45                     ` Fabrice Bellard
  2006-01-25  0:38                       ` Sebastian Kaliszewski
@ 2006-01-25 21:54                       ` Joachim Henke
  2006-01-26  4:50                         ` Jamie Lokier
  2006-01-26 16:55                         ` Sebastian Kaliszewski
  1 sibling, 2 replies; 21+ messages in thread
From: Joachim Henke @ 2006-01-25 21:54 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 1006 bytes --]

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.

Fabrice Bellard wrote:
> 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 !).

-- 
Joachim Henke
http://he-jo.net/

[-- Attachment #2: pcspk.diff.gz --]
[-- Type: application/x-gzip, Size: 3172 bytes --]

[-- Attachment #3: Type: text/plain, Size: 1 bytes --]



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (square wave)
  2006-01-25 21:54                       ` [Qemu-devel] [PATCH] PC speaker emulation (square wave) Joachim Henke
@ 2006-01-26  4:50                         ` Jamie Lokier
  2006-01-26 16:55                         ` Sebastian Kaliszewski
  1 sibling, 0 replies; 21+ messages in thread
From: Jamie Lokier @ 2006-01-26  4:50 UTC (permalink / raw)
  To: qemu-devel

Joachim Henke wrote:
> 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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (square wave)
  2006-01-25 21:54                       ` [Qemu-devel] [PATCH] PC speaker emulation (square wave) Joachim Henke
  2006-01-26  4:50                         ` Jamie Lokier
@ 2006-01-26 16:55                         ` Sebastian Kaliszewski
  2006-01-29 10:44                           ` Joachim Henke
  1 sibling, 1 reply; 21+ messages in thread
From: Sebastian Kaliszewski @ 2006-01-26 16:55 UTC (permalink / raw)
  To: qemu-devel

Joachim Henke wrote:
> 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
-- 
Sebastian Kaliszewski

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [Qemu-devel] [PATCH] PC speaker emulation (square wave)
  2006-01-26 16:55                         ` Sebastian Kaliszewski
@ 2006-01-29 10:44                           ` Joachim Henke
  0 siblings, 0 replies; 21+ messages in thread
From: Joachim Henke @ 2006-01-29 10:44 UTC (permalink / raw)
  To: qemu-devel

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.


Sebastian Kaliszewski wrote:
> 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).

-- 
Joachim Henke
http://he-jo.net/

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2006-01-29 10:47 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-01-09 19:01 [Qemu-devel] [patch] minimal PC speaker output Joachim Henke
2006-01-09 20:40 ` Fabrice Bellard
2006-01-10  9:11   ` he.jo
2006-01-18 23:05   ` [Qemu-devel] [PATCH] PC speaker emulation Joachim Henke
2006-01-18 23:32     ` [Qemu-devel] " Joachim Henke
2006-01-19  0:32     ` [Qemu-devel] " malc
2006-01-19 20:44       ` [Qemu-devel] [PATCH] PC speaker emulation (fixed) Joachim Henke
2006-01-19 23:29         ` malc
2006-01-20  9:42           ` Joachim Henke
2006-01-20 13:03             ` Johannes Schindelin
2006-01-20 12:03           ` Sebastian Kaliszewski
2006-01-23 21:10             ` [Qemu-devel] [PATCH] PC speaker emulation (fixed point) Joachim Henke
2006-01-24 12:35               ` Sebastian Kaliszewski
2006-01-24 15:50                 ` Joachim Henke
2006-01-24 18:29                   ` Sebastian Kaliszewski
2006-01-24 21:45                     ` Fabrice Bellard
2006-01-25  0:38                       ` Sebastian Kaliszewski
2006-01-25 21:54                       ` [Qemu-devel] [PATCH] PC speaker emulation (square wave) Joachim Henke
2006-01-26  4:50                         ` Jamie Lokier
2006-01-26 16:55                         ` Sebastian Kaliszewski
2006-01-29 10:44                           ` Joachim Henke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).