Aviator on the BBC Micro

# Sound: MakeEngineSound

```       Name: MakeEngineSound                                         [Show more]
Type: Subroutine
Category: Sound
Summary: Make the engine sound, with the choppiness and pitch affected by
thrust and airspeed
Context: See this subroutine in context in the source code
References: This subroutine is called as follows:
* ToggleEngineSound calls MakeEngineSound

.MakeEngineSound

LDA engineStatus       \ If engineStatus is zero, then the engine is not
BEQ engs4              \ running, so jump to engs4 to return from the
\ subroutine

LDA thrustHi           \ Set (K A) = (thrusthi thrustLo)
STA K                  \           = Thrust
LDA thrustLo

LDY #3                 \ Set Y = 3 to act as a shift counter in the following
\ loop, where we right shift (K A) four times

.engs1

LSR K                  \ Set (K A) = (R A) / 2
ROR A                  \           = Thrust / 2

DEY                    \ Decrement the shift counter

BPL engs1              \ Loop back until we have shifted right by 4 places, so
\ we now have:
\
\   (K A) = (K A) / 8
\         = Thrust / 8
\
\ We now ignore the high byte in K, so presumably it is
\ zero

CLC                    \ Set K = A + zVelocityPHi
ADC zVelocityPHi       \       = (Thrust / 8) + zVelocityPHi
STA K

LDA #50                \ Set A = 50 - K
SEC                    \       = 50 - ((Thrust / 8) + zVelocityPHi)
SBC K

BEQ engs2              \ If A = 0, jump to engs2 to set the engine choppiness
\ to 1 (a very smooth engine sound)

BPL engs3              \ If A is positive, jump to engs3 to set the engine
\ choppiness to A

.engs2

LDA #1                 \ If we get here then A is either 0 or negative, so we
\ set A = 1 to use as the choppiness value in the engine
\ sound, giving a very smooth engine sound

.engs3

\ By the time we get here, A is in the range 1 to 50 and
\ represents the engine choppiness, with a lower value
\ indicating a smoother engine, and a higher value
\ making the sound jump up and down in pitch more
\
\ In other words, the lower the forward airspeed and
\ thrust, the choppier the engine sound
\
\ We can apply this to the sound envelope for the engine
\ by altering the amount that the pitch changes in each
\ stage of the sound envelope. These values are set in
\ the third, fourth and fifth values in the envelope
\ definition, with larger values giving us a larger
\ amount of pitch change, and hence more choppiness in
\ the engine sound

STA envelopeData+2     \ Set the third and fifth values of sound envelope 1 to
STA envelopeData+4     \ the value in A, and set the fourth value to the
EOR #&FF               \ inverse of A, which is -(A + 1) as this is a signed
STA envelopeData+3     \ value

\ We now want to set the pitch of the engine sound so
\ that higher thrust and airspeed give us a higher
\ pitch, making our Spitfire scream through the air
\
\ The engine pitch is controlled by byte #5 of sound #7,
\ at soundData+60, which contains the low byte of the
\ engine pitch, so we now need to calculate a suitable
\ pitch value

LDA K                  \ Set A = K + 80
CLC                    \       = (Thrust / 8) + zVelocityPHi + 50
\ so A is a higher value when we have higher thrust and
\ airspeed, which is what we want

CMP soundData+60       \ If the pitch in byte #5 of sound #7 is already at this
BEQ engs4              \ value, jump to engs4 to return from the subroutine, as
\ we don't need to change the pitch

STA soundData+60       \ Otherwise set byte #5 of sound #7 to our new pitch in
\ A, so the pitch of the engine sound goes up with our
\ thrust and airspeed

LDA #0                 \ Call DefineEnvelope with A = 0 to redefine the first
JSR DefineEnvelope     \ sound envelope with the new choppiness values

LDA #7                 \ Make sound #7, the engine pitch, to change the engine
JSR MakeSound          \ sound's pitch and choppiness

LDA #1                 \ Make sound #1, the engine amplitude, to ensure that
JSR MakeSound          \ both engine sounds are being made, as we need to make
\ both #1 and #7 for the sound to work

.engs4

RTS                    \ Return from the subroutine
```