DTMF Phone Demo: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "Demonstrates the Dual Tone Multi-Frequency sounds used by telephones. Includes dial tone, ringing and busy signals. {{CodeStart}} {{Cl|RANDOMIZE}} {{Cl|TIMER}} {{Cl|PRINT}} {{Cl|PRINT}} " Phone Dialer" {{Cl|PRINT}} {{Cl|PRINT}} " +---------+" {{Cl|PRINT}} " | 1 2 3 A |" {{Cl|PRINT}} " | 4 5 6 B |" {{Cl|PRINT}} "...")
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:


{{CodeStart}}
{{CodeStart}}
{{Cl|RANDOMIZE}} {{Cl|TIMER}}
{{Cl|RANDOMIZE}} {{Cl|TIMER (function)|TIMER}}
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|PRINT}} "                                Phone Dialer"
{{Cl|PRINT}} "                                Phone Dialer"
Line 23: Line 23:
   {{Cl|FOR...NEXT|FOR}} d = 1 {{Cl|TO}} {{Cl|LEN}}(dialed$)
   {{Cl|FOR...NEXT|FOR}} d = 1 {{Cl|TO}} {{Cl|LEN}}(dialed$)


     dial$ = {{Cl|MID$}}(dialed$, d, 1)
     dial$ = {{Cl|MID$ (function)|MID$}}(dialed$, d, 1)


     {{Cl|SELECT CASE|SELECT EVERYCASE}} dial$
     {{Cl|SELECT CASE|SELECT EVERYCASE}} dial$
Line 66: Line 66:
{{Cl|NEXT}}
{{Cl|NEXT}}
{{Cl|DO}}: {{Cl|LOOP}} {{Cl|WHILE}} {{Cl|_SNDRAWLEN}}
{{Cl|DO}}: {{Cl|LOOP}} {{Cl|WHILE}} {{Cl|_SNDRAWLEN}}
{{Cl|END SUB}} '' ''
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
{{Small|Code by Ted Weissgerber}}
;Note:Use comma to pause for one second between numbers dialed. Can use special * codes too!
;Note:Use comma to pause for one second between numbers dialed. Can use special * codes too!



Latest revision as of 00:33, 26 February 2023

Demonstrates the Dual Tone Multi-Frequency sounds used by telephones. Includes dial tone, ringing and busy signals.

RANDOMIZE TIMER
PRINT
PRINT "                                 Phone Dialer"
PRINT
PRINT "                                 +---------+"
PRINT "                                 | 1 2 3 A |"
PRINT "                                 | 4 5 6 B |"
PRINT "                                 | 7 8 9 C |"
PRINT "                                 | * 0 # D |"
PRINT "                                 +---------+"
PRINT "                         Use comma for 1 second delay"

DO
  LOCATE 15, 20: PRINT SPACE$(60);
  LOCATE 15, 20: LINE INPUT "Enter a number to dial: ", dialed$
  IF LEN(dialed$) = 0 THEN EXIT DO

  PlayTone 350, 440, 4

  FOR d = 1 TO LEN(dialed$)

    dial$ = MID$(dialed$, d, 1)

    SELECT EVERYCASE dial$
      CASE "1", "4", "7", "*": left = 1209
      CASE "2", "5", "8", "0": left = 1336
      CASE "3", "6", "9", "#": left = 1477
      CASE "A", "B", "C", "D": left = 1633
      CASE ",": left = 0: rite = 0: _DELAY 1
      CASE "1", "2", "3", "A": rite = 697
      CASE "4", "5", "6", "B": rite = 770
      CASE "7", "8", "9", "C": rite = 852
      CASE "*", "0", "#", "D": rite = 941
      CASE ELSE: left = 0: rite = 0
    END SELECT

    IF left THEN PlayTone left, rite, .25

  NEXT

  rand = INT(RND * 2)                             'random sounds after dial
  FOR i = 1 TO 4
    IF rand = 1 THEN
      _DELAY 3: PlayTone 440, 480, 1              'ringing sound
    ELSE: _DELAY .6: PlayTone 480, 620, .3        'busy signal
    END IF
  NEXT

LOOP
END

SUB PlayTone (FREQ1, FREQ2, Seconds)
Pi2 = 8 * ATN(1) '2 * pi
Amplitude = .5 'amplitude of the signal from -1.0 to 1.0
SampleRate = _SNDRATE 'sets the sample rate
FRate1 = FREQ1 / SampleRate
FRate2 = FREQ2 / SampleRate
FOR Duration = 0 TO Seconds * SampleRate 'play 5 seconds
  left = Amplitude * SIN(Pi2 * Duration * FRate1) 'sine wave
  rite = Amplitude * SIN(Pi2 * Duration * FRate2)
  'LOCATE 1, 1: PRINT left, rite
  _SNDRAW left, rite
NEXT
DO: LOOP WHILE _SNDRAWLEN
END SUB
Code by Ted Weissgerber
Note
Use comma to pause for one second between numbers dialed. Can use special * codes too!


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link