' {$STAMP BS2}
' ==============================================================================
'
' File...... Ex20 - Sound FX.BS2
' Purpose... Stamp-generated sounds
' Author.... Parallax
' E-mail.... stamptech@parallaxinc.com
' Started...
' Updated... 01 MAY 2002
'
'
' ==============================================================================
' ------------------------------------------------------------------------------
' Program Description
' ------------------------------------------------------------------------------
' This program demonstrates several realistic and interesting sound effects
' that can be generated by the BASIC Stamp using FREQOUT and DTMFOUT. This
' ==============================================================================
'
' File...... Ex20 - Sound FX.BS2
' Purpose... Stamp-generated sounds
' Author.... Parallax
' E-mail.... stamptech@parallaxinc.com
' Started...
' Updated... 01 MAY 2002
'
'
' ==============================================================================
' ------------------------------------------------------------------------------
' Program Description
' ------------------------------------------------------------------------------
' This program demonstrates several realistic and interesting sound effects
' that can be generated by the BASIC Stamp using FREQOUT and DTMFOUT. This
' program works best when played through an amplifier.
' ------------------------------------------------------------------------------
' I/O Definitions
' ------------------------------------------------------------------------------
Speaker CON 0 ' speaker on pin 0
' ------------------------------------------------------------------------------
' Constants
' ------------------------------------------------------------------------------
R CON 0 ' rest
C CON 33 ' ideal is 32.703
Cs CON 35 ' ideal is 34.648
D CON 39 ' ideal is 38.891
E CON 41 ' ideal is 41.203
F CON 44 ' ideal is 43.654
Fs CON 46 ' ideal is 46.249
G CON 49 ' ideal is 48.999
Gs CON 52 ' ideal is 51.913
A CON 55 ' ideal is 55.000
As CON 58 ' ideal is 58.270
B CON 62 ' ideal is 61.735
N1 CON 500 ' whole note duration
N2 CON N1/2 ' half note
N3 CON N1/3 ' third note
N4 CON N1/4 ' quarter note
N8 CON N1/8 ' eighth note
ScaleT CON $0100 ' time scale - BS2/BS2e
ScaleF CON $0100 ' frequency scale - BS2/BS2e
' ScaleT CON $0280 ' time scale - BS2sx
' ScaleF CON $0066 ' frequency scale - BS2sx
' ScaleT CON $03C6 ' time scale - BS2p
' ScaleF CON $0043 ' frequency scale - BS2p
' ------------------------------------------------------------------------------
' Variables
' ------------------------------------------------------------------------------
x VAR Word ' loop counter
note1 VAR Word ' first tone for FREQOUT
note2 VAR Word ' second tone for FREQOUT
onTime VAR Word ' duration for FREQOUT
offTime VAR Word
oct1 VAR Nib ' octave for freq1 (1 - 8)
oct2 VAR Nib ' octave for freq2 (1 - 8)
eePtr VAR Byte ' EEPROM pointer
digit VAR Byte ' DTMF digit
clickDly VAR Word ' delay betweens "clicks"
' ------------------------------------------------------------------------------
' EEPROM Data
' ------------------------------------------------------------------------------
'
Phone1 DATA "972-555-1212", 0 ' a stored telephone number
Phone2 DATA "916-624-8333", 0 ' another number
' ------------------------------------------------------------------------------
' Program Code
' ------------------------------------------------------------------------------
Main:
PAUSE 250
DEBUG CLS, "BASIC Stamp Sound FX Demo", CR, CR
Dial_Tone:
DEBUG "Dial tone", CR
onTime = 35 */ ScaleT
note1 = 35 */ ScaleF
FREQOUT Speaker, onTime, note1 ' "click"
PAUSE 100
onTime = 2000 */ ScaleT
note1 = 350 */ ScaleF
note2 = 440 */ ScaleF
FREQOUT Speaker, onTime, note1, note2 ' combine 350 Hz & 440 Hz
Dial_Phone1: ' dial phone from EE
DEBUG "Dialing number: "
eePtr = Phone1 ' initialize eePtr pointer
GOSUB Dial_Phone
Phone_Busy:
PAUSE 1000
DEBUG CR, " - busy...", CR
onTime = 400 */ ScaleT
note1 = 480 */ ScaleF
note2 = 620 */ ScaleF
FOR x = 1 TO 8
FREQOUT Speaker, onTime, note1, note2 ' combine 480 Hz and 620 Hz
PAUSE 620
NEXT
onTime = 35 */ ScaleT
note1 = 35 */ ScaleF
FREQOUT Speaker, onTime, note1 ' "click"
Dial_Phone2:
DEBUG "Calling Parallax: "
eePtr = Phone2
GOSUB Dial_Phone
Phone_Rings:
PAUSE 1000
DEBUG CR, " - ringing"
onTime = 2000 */ ScaleT
note1 = 440 */ ScaleF
note2 = 480 */ ScaleF
FREQOUT Speaker, onTime, note1, note2 ' combine 440 Hz and 480 Hz
PAUSE 4000
FREQOUT Speaker, onTime, note1, note2 ' combine 440 Hz and 480 Hz
PAUSE 2000
Camptown_Song:
DEBUG CR, "Play a Camptown song", CR
FOR x = 0 TO 13
LOOKUP x, [ G, G, E, G, A, G, E, R, E, D, R, E, D, R], note1
LOOKUP x, [ 4, 4, 4, 4, 4, 4, 4, 1, 4, 4, 1, 4, 4, 1], oct1
LOOKUP x, [N2, N2, N2, N2, N2, N2, N2, N2, N2, N1, N2, N2, N1, N8], onTime
GOSUB Play_1_Note
NEXT
Howler:
DEBUG "Howler -- watch out!!!", CR
FOR x = 1 TO 4
onTime = 1000 */ ScaleT
note1 = 1400 */ ScaleF
note2 = 2060 */ ScaleF
FREQOUT Speaker, onTime, note1, note2 ' combine 1400 Hz and 2060 Hz
onTime = 1000 */ ScaleT
note1 = 2450 */ ScaleF
note2 = 2600 */ ScaleF
FREQOUT Speaker, onTime, note1, note2 ' combine 2450 Hz and 2600 Hz
onTime = 400 */ ScaleT
note1 = 480 */ ScaleF
note2 = 620 */ ScaleF
FOR x = 1 TO 8
FREQOUT Speaker, onTime, note1, note2 ' combine 480 Hz and 620 Hz
PAUSE 620
NEXT
DEBUG CR, "Sound demo complete."
INPUT Speaker
END
' ------------------------------------------------------------------------------
' Subroutines
' ------------------------------------------------------------------------------
Dial_Phone:
READ eePtr, digit ' read a digit
IF (digit = 0) THEN Dial_Exit ' when 0, number is done
DEBUG digit ' display digit
IF (digit < "0") THEN Next_Digit ' don't dial non-digits
onTime = 150 */ ScaleT
offTime = 75 */ ScaleT
DTMFOUT Speaker, onTime, offTime, [digit - 48]
Next_Digit:
eePtr = eePtr + 1 ' update eePtr pointer
GOTO Dial_Phone ' get another digit
Dial_Exit:
RETURN
Play_1_Note:
note1 = note1 << (oct1 - 1) ' get frequency for note + octave
onTime = onTime */ ScaleT
note1 = note1 */ ScaleF
FREQOUT Speaker, onTime, note1 ' play it
RETURN
Play_2_Notes:
note1 = note1 << (oct1 - 1) ' get frequency for note + octave
note2 = note2 << (oct2 - 1) ' get frequency for note + octave
onTime = onTime */ ScaleT
note1 = note1 */ ScaleF
note2 = note2 */ ScaleF
FREQOUT Speaker, onTime, note1, note2 ' play both
RETURN