Page 1 of 2

Short beeper routines in assembly

Posted: Wed Nov 15, 2017 8:43 pm
by R-Tape
I always end up using the same spot effects, usually nabbed from the DKTronics SoundFX tape. I know we've got BeepFX for the more elaborate ones, but I thought this would be a good place to share shortish beeper routines people can use.

Here's a simple zap.

Code: Select all

	;
zap:
	ld d,16		;speaker = bit 4
	ld e,0		;distance between speaker move counter
	ld b,255	;overall length counter
blp0:	ld a,d
	and 248		;keep border colour the same
	out (254),a	;move the speaker in or out depending on bit 4
	cpl		;toggle, so we alternative between speaker in and out to make sound
	ld d,a		;store it
	ld c,e		;now a pause
blp1:	dec c
	jr nz,blp1
	dec e		;change to inc e to reverse the sound, or remove to make it a note
	djnz blp0	;repeat B=255 times
	ret
	;
	

Re: Short beeper routines in assembly

Posted: Wed Nov 15, 2017 9:08 pm
by utz
Wrote a complete beeper music player/sequencer in 64 byte earlier this year: url=https://github.com/utz82/ZX-Spectrum-1- ... /nanobeep2
In it's most basic setup, it has just two square wave channels, and 8-bit frequency resolution (approx. 2.5 octaves note range). The code is modular, so you can activate extra features like increased note range and PWM sweeps, at the cost of increasing the player size.

The player is open source and free for use by anyone, but there's no editor for the music available yet. There's a short demo tune included in the download package, if anybody is curious about how it sounds.

Re: Short beeper routines in assembly

Posted: Wed Nov 15, 2017 9:15 pm
by Ralf
Utz, if I may ask I recall there was some forum about making music and effects on zx beeper - https://web.archive.org/web/20150423235 ... i-demo.pl/

Was it your forum? Do you know anything why it disappeared?

Re: Short beeper routines in assembly

Posted: Wed Nov 15, 2017 9:19 pm
by utz
Some drama happened and the former admin shut down the place. :roll:
We've got a new place at http://www.randomflux.info/1bit/ nowadays.

Re: Short beeper routines in assembly

Posted: Wed Nov 15, 2017 10:40 pm
by Morkin
utz wrote:
Wed Nov 15, 2017 9:19 pm
Some drama happened and the former admin shut down the place. :roll:
We've got a new place at http://www.randomflux.info/1bit/ nowadays.
Ah cool, I wondered what happened to that site. I was just starting to learn the basics from scratch (the beeper, frequencies etc.) and then the site seemed to disappear - will have to get back to it..!

Here's a sound effect almost identical to the 'pick up object' sound I used in Catacombs of Balachor. It uses the ROM beeper, which I used at the time because I had no idea how to use OUT properly to make sound effects..!

Code: Select all

sound_setup:
	ld de, 5
	ld b,3
	ld hl, 938 	; pitch
sound_loop:
	dec h
	push bc
	push de
	push hl
	call 949 	; call ROM beeper routine
	pop hl
	pop de
	pop bc
	djnz sound_loop
	ret
I liked the way you can just keep mucking about with the registers (DE, HL, BC), and the INCs and DECs to get very different effects.

Re: Short beeper routines in assembly

Posted: Thu Nov 16, 2017 11:05 pm
by utz
Morkin wrote:
Wed Nov 15, 2017 10:40 pm
I liked the way you can just keep mucking about with the registers (DE, HL, BC), and the INCs and DECs to get very different effects.
Yeah, that's actually one of the things that I really like about the beeper. You can just throw almost random code at it and it will often do something interesting.

Anyway, here's a simple laser effect in 13 bytes. By initializing D with different values, you can control the length. 0x28 is approximately one full zap.

Code: Select all

	di
	ld d,#28		;length/iterations
loop
	add hl,de
	dec de
	ld a,h
	out (#fe),a
	ld a,e
	or d
	jr nz,loop
	
	ei
	ret

Another nice thing to have is (pseudo-)white noise in all forms. My preferred method nowadays, provided that I have the cycles and registers available, is this poor man's XORshift PRNG:

Code: Select all

	di
;HL must be !0 aka set HL to 0 for silence
loop
	add hl,hl
	sbc a,a
	xor l
	ld l,a
	
	cp #10       ;cp n -  n = volume
	sbc a,a
	
	out (#fe),a
	jr loop
In this form, it will produce a pretty good rendition of "white noise" on the beeper. If you want to control the "pitch" of the noise, you can wrap the above in a prescaler, like so:

Code: Select all

	ld d,%01010101    ;prescaler mask
loop
	rlc d
	jr nc,skip
	add hl,hl
	...
skip	
	cp #10       ;cp n -  n = volume
	sbc a,a
	
	out (#fe),a
	jr loop

Re: Short beeper routines in assembly

Posted: Fri Dec 01, 2017 8:38 am
by R-Tape
Has anyone got, or is anyone willing to do a beep routine that sounds vaguely like a paper page turning? No samples, just few hundred byte routine max.

Also preferably a routine using OUT rather than CALL 949.

Re: Short beeper routines in assembly

Posted: Fri Dec 01, 2017 11:30 am
by Ast A. Moore
A page turning sound? Are you insane? Do you want us all get killed?
Image

Re: Short beeper routines in assembly

Posted: Fri Dec 01, 2017 12:10 pm
by HexTank
Well, it would inolve noise, so try a few RNGs with some adsr for amplitude.

Re: Short beeper routines in assembly

Posted: Fri Dec 01, 2017 3:44 pm
by R-Tape
Too teary?

Code: Select all

org 32768
	ld hl,0
	ld c,255
lp1:	inc hl
	ld a,(hl)
	and 248
	or 7
	out (254),a
	ld b,c
lp0:	djnz lp0
	dec c
	jr nz,lp1
	ret
	;
	
Yep too teary.