"Thumping" sound in sound routine

On the creation of AY or Beeper music, including the packages used to do so.
Post Reply
MarcoL
Drutt
Posts: 2
Joined: Fri Oct 14, 2022 4:51 pm

"Thumping" sound in sound routine

Post by MarcoL »

Hey, everyone.

I've been playing with routines to add sound to my games but I'm hitting a weird problem and I just can't figure out if that's just the way it is or if I'm missing something. I've taken Shiru's BeepFX routines and adapted them to play in the "background". If you're familiar with BeepFX, it plays sounds in frames. I've created my sounds with relatively small frames so I can play each one as part of each raster cycle. I store all the registers after playing a frame and resume playing the next frame on the next cycle. I've extracted the code from my game to work standalone and pasted it below.

The PlaySound routine is pretty simple. It looks if there's a new sound effect to play or if there's already one that's playing. If neither are true, it loads the siren sound, plays the first frame and repeats the cycle. In the next cycle, there's already a sound that's playing and continues to the next frame.

The thing is I get this weird "thumping" sound when the siren is looping in the background and I have no idea what's causing it. All emulators I've tried recreate it pretty consistently so I'm assuming a real Spectrum would do it as well (don't have any real hardware to test).

Do any of you have more experience than I do to help clarify this mystery? Or, alternatively, suggest a different routine I could try?

Thanks!

Code: Select all

.org 60000
	
Start:
	ld b, 16

PlayLoop:
	halt
	push bc
	call PlaySound
	pop bc
	djnz PlayLoop

	jp Start

ReadData:
	ld c, (ix+1)			;read duration 1
	ld b, (ix+2)
	ld e, (ix+3)			;read duration 2
	ld d, (ix+4)

	push de
	pop iy

	ld e, (ix+5)			;freq
	ld d, (ix+6)
	ld a, (ix+9)			;duty
	ld (sfx_routine_tone_duty+1), a
	ret

PlaySound:
	ld ix, (play_sound)
	ld a, ixh
	or ixl
	jp z, PlaySound_NoNewSound
	ld hl, 0
	ld (play_sound), hl
	call ReadData
	jp PlaySound_Frame

	PlaySound_NoNewSound:
	ld bc, (sound_frame)
	ld a, b
	or c
	jp z, PlaySound_NoNewFrame
	ld de, (sound_pitch)
	ld ix, (sound_tone)
	ld iy, (sound_frame_length)
	jp PlaySound_Frame

	PlaySound_NoNewFrame:
	ld ix, sound_siren
	call ReadData

	PlaySound_Frame:
	push bc
	push iy
	pop bc

	PlaySound_Loop:
	add hl, de			;11
	ld a, h				;4
	sfx_routine_tone_duty: cp 0				;7
	sbc a, a			;4
	and %00010000		;7
	or 0				;7 dummy
	out (254), a		;11
	ld a, (0)			;13	dummy
	dec bc				;6
	ld a, b				;4
	or c				;4
	jr nz, PlaySound_Loop		;10=88t

	ld a, (sfx_routine_tone_duty+1)	 ;duty change
	add a, (ix+10)
	ld (sfx_routine_tone_duty+1), a

	ld c, (ix+7)		;slide
	ld b, (ix+8)
	ex de, hl
	add hl, bc
	ex de, hl

	pop bc
	dec bc
	ld a, b
	or c
	jp nz, PlaySound_MoreFrames

	ld a, (ix+11)
	or a
	jp z, PlaySound_MoreFrames

	ld c, 11
	add ix, bc
	call ReadData

	PlaySound_MoreFrames:
	ld (sound_frame), bc
	ld (sound_pitch), de
	ld (sound_tone), ix
	ld (sound_frame_length), iy

	ret

sound_siren:
	.db 1
	.dw 8, 160, 1000, 100, 128
	.db 1
	.dw 8, 160, 1700, -100, 128
	.db 0

play_sound:
	.dw 0

sound_frame:
	.dw 0

sound_pitch:
	.dw 0

sound_tone:
	.dw 0

sound_frame_length:
	.dw 0
User avatar
R-Tape
Site Admin
Posts: 6440
Joined: Thu Nov 09, 2017 11:46 am

Re: "Thumping" sound in sound routine

Post by R-Tape »

MarcoL wrote: Sat Oct 15, 2022 5:32 pm The thing is I get this weird "thumping" sound when the siren is looping in the background and I have no idea what's causing it. All emulators I've tried recreate it pretty consistently so I'm assuming a real Spectrum would do it as well (don't have any real hardware to test).
Can you clarify what you mean by 'thumping'? I don't hear it in SPIN or Spectaculator. It sounds like a passable siren sound to me, albeit gravelly. Is that what you mean - it's coarse rather than a pure siren?

If you're not averse to ROM calls, this snippet of code from Jonathan Cauldwell's 'How to write Spectrum Games' has a clean sounding pitchbend routine, and that's half a siren call.

Code: Select all

org 60000
ld hl,500 ; starting pitch.
 ld b,250 ; length of pitch bend.
loop push bc
 push hl ; store pitch.
 ld de,1 ; very short duration.
 call 949 ; ROM beeper routine.
 pop hl ; restore pitch.
 inc hl ; pitch going up.
 pop bc
 djnz loop ; repeat.
 ret
MarcoL
Drutt
Posts: 2
Joined: Fri Oct 14, 2022 4:51 pm

Re: "Thumping" sound in sound routine

Post by MarcoL »

Beeper sound is fascinating... and frustrating! :) Found out what was causing the "thumping" but the fix introduces "humming".

I actually recorded the sound coming out of the emulator through Audacity is the waveform it captured:

Image

When I finished a loop for a frame, I saved the counter that was being used to determine whether the OUT would output a 1 or a 0 instead of always starting again from 0. This caused an audible "thump" (at least to me). By always outputting a 0 at the end of the routine and starting a new frame always from 0 I get a "cleaner" waveform:

Image

The thump is gone but now I get a hum which makes perfect sense. My bursts of sound are so short and I play them in every raster cycle. I do get the frequency I'm outputting but also mixed in with a nice 50Hz hum.

I'm going to try the ROM BEEPER routine to see how it works.
Post Reply