Why can I hear this?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
dfzx
Manic Miner
Posts: 683
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Why can I hear this?

Post by dfzx »

Apropos of nothing, I found myself wondering what the highest pitched noise is that a 48K can produce from its loudspeaker. So I wrote this:

Code: Select all

org 32768
	
	di
	ld a,0
	ld b,0x10
loop:		
	xor b            ; 4T
	out (0xfe),a     ; 11T

	jp loop          ; 10T (jr is 12T)
That's the fastest way to toggle the speaker bit, isn't it?

The loop is 25Ts, so 3,500,000/25 is 140,000. It takes 2 loops to toggle the bit and make a click, so this sends out a whine at 70kHz. Only, that's way above the limit of human hearing, and I can still hear it, which means there's a flaw in my logic somewhere. Where's my error?

I don't have real hardware and I'm using Fuse, which may or may not have something to do with it.
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
User avatar
1024MAK
Bugaboo
Posts: 3123
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Why can I hear this?

Post by 1024MAK »

Lower harmonic frequencies or the emulator is not able to handle that frequency correctly.

I very much doubt that the loud speaker on a real ZX Spectrum would be able to make any significant sound at 70kHz anyway, they were not exactly hi-fi technology! :lol:

And never mind 70kHz, young ears top out at around 20kHz in normal situations. Fifty year old ears will be at 14kHz or lower... (If you can hear the 15kHz from a CRT you’re doing well). [Wikipedia gives the details]

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
Sparky
Manic Miner
Posts: 611
Joined: Tue Dec 15, 2020 9:42 pm

Re: Why can I hear this?

Post by Sparky »

Image
Alex
User avatar
1024MAK
Bugaboo
Posts: 3123
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Why can I hear this?

Post by 1024MAK »

Sparky wrote: Sun Mar 07, 2021 5:39 pm Image
I live next to dead people, behind my house, within a handful of yards there is a large graveyard...

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
User avatar
utz
Microbot
Posts: 116
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: Why can I hear this?

Post by utz »

The main reason for the parasite tone here will probably be the fact that this loop is not timing-stable, because of IO contention.

Aligning the output to a multiple of 8 t-states should improve the result somewhat, but there will still be whine.

Code: Select all

  di
  xor a
  ld bc,0x10fe
  ld hl,loop

loop:
  xor b      ;4
  out (c),a  ;12
  nop        ;4
  jp (hl)    ;4
To get a near-perfect sound you'd need to apply the Floating Bus Trick.
User avatar
1024MAK
Bugaboo
Posts: 3123
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Why can I hear this?

Post by 1024MAK »

dfzx wrote: Sun Mar 07, 2021 4:07 pm Apropos of nothing, I found myself wondering what the highest pitched noise is that a 48K can produce from its loudspeaker. So I wrote this:

Code: Select all

org 32768
	
	di
	ld a,0
	ld b,0x10
loop:		
	xor b            ; 4T
	out (0xfe),a     ; 11T

	jp loop          ; 10T (jr is 12T)
That's the fastest way to toggle the speaker bit, isn't it?

The loop is 25Ts, so 3,500,000/25 is 140,000. It takes 2 loops to toggle the bit and make a click, so this sends out a whine at 70kHz. Only, that's way above the limit of human hearing, and I can still hear it, which means there's a flaw in my logic somewhere. Where's my error?

I don't have real hardware and I'm using Fuse, which may or may not have something to do with it.
I’ve just tried this on an issue 6a ZX Spectrum+, but with the code from 0xEA61. It sounds like the frequency of the whine is around 4kHz.

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
User avatar
Guesser
Manic Miner
Posts: 641
Joined: Wed Nov 15, 2017 2:35 pm
Contact:

Re: Why can I hear this?

Post by Guesser »

Remember that on an emulator whatever waveform you try to create has to be resampled to play on the soundcard. Probably 48kHz. There's probably all sorts of aliasing happening.
Sparky
Manic Miner
Posts: 611
Joined: Tue Dec 15, 2020 9:42 pm

Re: Why can I hear this?

Post by Sparky »

I can definitely hear it at 4Khz
Alex
dfzx
Manic Miner
Posts: 683
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: Why can I hear this?

Post by dfzx »

1024MAK wrote: Sun Mar 07, 2021 7:49 pm I’ve just tried this on an issue 6a ZX Spectrum+, but with the code from 0xEA61. It sounds like the frequency of the whine is around 4kHz.
How on earth do you know the frequency? Your ears just know this kind of thing? Tone deaf, me. :(

So that 4kHz must be a lower harmonic frequency?
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
User avatar
1024MAK
Bugaboo
Posts: 3123
Joined: Wed Nov 15, 2017 2:52 pm
Location: Sunny Somerset in the U.K. in Europe

Re: Why can I hear this?

Post by 1024MAK »

dfzx wrote: Mon Mar 08, 2021 10:08 am
1024MAK wrote: Sun Mar 07, 2021 7:49 pm I’ve just tried this on an issue 6a ZX Spectrum+, but with the code from 0xEA61. It sounds like the frequency of the whine is around 4kHz.
How on earth do you know the frequency? Your ears just know this kind of thing? Tone deaf, me. :(

So that 4kHz must be a lower harmonic frequency?
My ears are also tone deaf :lol:
I stuffed a iPad mini near the Spectrum and then used a Spectrum analyser web tool to give an indication of the frequency.

Mark
:!: Standby alert :!:
“There are four lights!”
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :dance
Looking forward to summer later in the year.
Post Reply