DTMF Phone Demo: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 4: Line 4:
{{Cl|RANDOMIZE}} {{Cl|TIMER (function)|TIMER}}
{{Cl|RANDOMIZE}} {{Cl|TIMER (function)|TIMER}}
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|PRINT}} "                                Phone Dialer"
{{Cl|PRINT}} {{Text|<nowiki>"                                Phone Dialer"</nowiki>|#FFB100}}
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|PRINT}} "                                +---------+"
{{Cl|PRINT}} {{Text|<nowiki>"                                +---------+"</nowiki>|#FFB100}}
{{Cl|PRINT}} "                                | 1 2 3 A |"
{{Cl|PRINT}} {{Text|<nowiki>"                                | 1 2 3 A |"</nowiki>|#FFB100}}
{{Cl|PRINT}} "                                | 4 5 6 B |"
{{Cl|PRINT}} {{Text|<nowiki>"                                | 4 5 6 B |"</nowiki>|#FFB100}}
{{Cl|PRINT}} "                                | 7 8 9 C |"
{{Cl|PRINT}} {{Text|<nowiki>"                                | 7 8 9 C |"</nowiki>|#FFB100}}
{{Cl|PRINT}} "                                | * 0 # D |"
{{Cl|PRINT}} {{Text|<nowiki>"                                | * 0 # D |"</nowiki>|#FFB100}}
{{Cl|PRINT}} "                                +---------+"
{{Cl|PRINT}} {{Text|<nowiki>"                                +---------+"</nowiki>|#FFB100}}
{{Cl|PRINT}} "                        Use comma for 1 second delay"
{{Cl|PRINT}} {{Text|<nowiki>"                        Use comma for 1 second delay"</nowiki>|#FFB100}}


{{Cl|DO...LOOP|DO}}
{{Cl|DO}}
  {{Cl|LOCATE}} 15, 20: {{Cl|PRINT}} {{Cl|SPACE$}}(60);
    {{Cl|LOCATE}} {{Text|15|#F580B1}}, {{Text|20|#F580B1}}: {{Cl|PRINT}} {{Cl|SPACE$}}({{Text|60|#F580B1}});
  {{Cl|LOCATE}} 15, 20: {{Cl|LINE INPUT}} "Enter a number to dial: ", dialed$
    {{Cl|LOCATE}} {{Text|15|#F580B1}}, {{Text|20|#F580B1}}: {{Cl|LINE INPUT}} {{Text|<nowiki>"Enter a number to dial: "</nowiki>|#FFB100}}, dialed$
  {{Cl|IF...THEN|IF}} {{Cl|LEN}}(dialed$) = 0 {{Cl|THEN}} {{Cl|EXIT DO}}
    {{Cl|IF}} {{Cl|LEN}}(dialed$) = {{Text|0|#F580B1}} {{Cl|THEN}} {{Cl|EXIT DO}}


  PlayTone 350, 440, 4
    {{Text|PlayTone|#55FF55}} {{Text|350|#F580B1}}, {{Text|440|#F580B1}}, {{Text|4|#F580B1}}


  {{Cl|FOR...NEXT|FOR}} d = 1 {{Cl|TO}} {{Cl|LEN}}(dialed$)
    {{Cl|FOR}} d = {{Text|1|#F580B1}} {{Cl|TO}} {{Cl|LEN}}(dialed$)


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


    {{Cl|SELECT CASE|SELECT EVERYCASE}} dial$
        {{Cl|SELECT CASE|SELECT EVERYCASE}} dial$
      {{Cl|CASE}} "1", "4", "7", "*": left = 1209
            {{Cl|CASE}} {{Text|<nowiki>"1"</nowiki>|#FFB100}}, {{Text|<nowiki>"4"</nowiki>|#FFB100}}, {{Text|<nowiki>"7"</nowiki>|#FFB100}}, {{Text|<nowiki>"*"</nowiki>|#FFB100}}: left = {{Text|1209|#F580B1}}
      {{Cl|CASE}} "2", "5", "8", "0": left = 1336
            {{Cl|CASE}} {{Text|<nowiki>"2"</nowiki>|#FFB100}}, {{Text|<nowiki>"5"</nowiki>|#FFB100}}, {{Text|<nowiki>"8"</nowiki>|#FFB100}}, {{Text|<nowiki>"0"</nowiki>|#FFB100}}: left = {{Text|1336|#F580B1}}
      {{Cl|CASE}} "3", "6", "9", "#": left = 1477
            {{Cl|CASE}} {{Text|<nowiki>"3"</nowiki>|#FFB100}}, {{Text|<nowiki>"6"</nowiki>|#FFB100}}, {{Text|<nowiki>"9"</nowiki>|#FFB100}}, {{Text|<nowiki>"#"</nowiki>|#FFB100}}: left = {{Text|1477|#F580B1}}
      {{Cl|CASE}} "A", "B", "C", "D": left = 1633
            {{Cl|CASE}} {{Text|<nowiki>"A"</nowiki>|#FFB100}}, {{Text|<nowiki>"B"</nowiki>|#FFB100}}, {{Text|<nowiki>"C"</nowiki>|#FFB100}}, {{Text|<nowiki>"D"</nowiki>|#FFB100}}: left = {{Text|1633|#F580B1}}
      {{Cl|CASE}} ",": left = 0: rite = 0: {{Cl|_DELAY}} 1
            {{Cl|CASE}} {{Text|<nowiki>","</nowiki>|#FFB100}}: left = {{Text|0|#F580B1}}: rite = {{Text|0|#F580B1}}: {{Cl|_DELAY}} {{Text|1|#F580B1}}
      {{Cl|CASE}} "1", "2", "3", "A": rite = 697
            {{Cl|CASE}} {{Text|<nowiki>"1"</nowiki>|#FFB100}}, {{Text|<nowiki>"2"</nowiki>|#FFB100}}, {{Text|<nowiki>"3"</nowiki>|#FFB100}}, {{Text|<nowiki>"A"</nowiki>|#FFB100}}: rite = {{Text|697|#F580B1}}
      {{Cl|CASE}} "4", "5", "6", "B": rite = 770
            {{Cl|CASE}} {{Text|<nowiki>"4"</nowiki>|#FFB100}}, {{Text|<nowiki>"5"</nowiki>|#FFB100}}, {{Text|<nowiki>"6"</nowiki>|#FFB100}}, {{Text|<nowiki>"B"</nowiki>|#FFB100}}: rite = {{Text|770|#F580B1}}
      {{Cl|CASE}} "7", "8", "9", "C": rite = 852
            {{Cl|CASE}} {{Text|<nowiki>"7"</nowiki>|#FFB100}}, {{Text|<nowiki>"8"</nowiki>|#FFB100}}, {{Text|<nowiki>"9"</nowiki>|#FFB100}}, {{Text|<nowiki>"C"</nowiki>|#FFB100}}: rite = {{Text|852|#F580B1}}
      {{Cl|CASE}} "*", "0", "#", "D": rite = 941
            {{Cl|CASE}} {{Text|<nowiki>"*"</nowiki>|#FFB100}}, {{Text|<nowiki>"0"</nowiki>|#FFB100}}, {{Text|<nowiki>"#"</nowiki>|#FFB100}}, {{Text|<nowiki>"D"</nowiki>|#FFB100}}: rite = {{Text|941|#F580B1}}
      {{Cl|CASE ELSE}}: left = 0: rite = 0
            {{Cl|CASE ELSE}}: left = {{Text|0|#F580B1}}: rite = {{Text|0|#F580B1}}
    {{Cl|END SELECT}}
        {{Cl|END SELECT}}


    {{Cl|IF...THEN|IF}} left {{Cl|THEN}} PlayTone left, rite, .25
        {{Cl|IF}} left {{Cl|THEN}} {{Text|PlayTone|#55FF55}} left, rite, {{Text|.25|#F580B1}}


  {{Cl|NEXT}}
    {{Cl|NEXT}}


  rand = {{Cl|INT}}({{Cl|RND}} * 2)                             'random sounds after dial
    rand = {{Cl|INT}}({{Cl|RND}} * {{Text|2|#F580B1}}) {{Text|<nowiki>'random sounds after dial</nowiki>|#919191}}
  {{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} 4
    {{Cl|FOR}} i = {{Text|1|#F580B1}} {{Cl|TO}} {{Text|4|#F580B1}}
    {{Cl|IF...THEN|IF}} rand = 1 {{Cl|THEN}}
        {{Cl|IF}} rand = {{Text|1|#F580B1}} {{Cl|THEN}}
      {{Cl|_DELAY}} 3: PlayTone 440, 480, 1             'ringing sound
            {{Cl|_DELAY}} {{Text|3|#F580B1}}: {{Text|PlayTone|#55FF55}} {{Text|440|#F580B1}}, {{Text|480|#F580B1}}, {{Text|1|#F580B1}} {{Text|<nowiki>'ringing sound</nowiki>|#919191}}
    {{Cl|ELSE}}: {{Cl|_DELAY}} .6: PlayTone 480, 620, .3       'busy signal
        {{Cl|ELSE}}: {{Cl|_DELAY}} {{Text|.6|#F580B1}}: {{Text|PlayTone|#55FF55}} {{Text|480|#F580B1}}, {{Text|620|#F580B1}}, {{Text|.3|#F580B1}} {{Text|<nowiki>'busy signal</nowiki>|#919191}}
    {{Cl|END IF}}
        {{Cl|END IF}}
  {{Cl|NEXT}}
    {{Cl|NEXT}}


{{Cl|LOOP}}
{{Cl|LOOP}}
{{Cl|END}}
{{Cl|END}}


{{Cl|SUB}} PlayTone (FREQ1, FREQ2, Seconds)
{{Cl|SUB}} {{Text|PlayTone|#55FF55}} (FREQ1, FREQ2, Seconds)
Pi2 = 8 * {{Cl|ATN}}(1) '2 * pi
    Pi2 = {{Text|8|#F580B1}} * {{Cl|ATN}}({{Text|1|#F580B1}}) {{Text|<nowiki>'2 * pi</nowiki>|#919191}}
Amplitude = .5 'amplitude of the signal from -1.0 to 1.0
    Amplitude = {{Text|.5|#F580B1}} {{Text|<nowiki>'amplitude of the signal from -1.0 to 1.0</nowiki>|#919191}}
SampleRate = {{Cl|_SNDRATE}} 'sets the sample rate
    SampleRate = {{Cl|_SNDRATE}} {{Text|<nowiki>'sets the sample rate</nowiki>|#919191}}
FRate1 = FREQ1 / SampleRate
    FRate1 = FREQ1 / SampleRate
FRate2 = FREQ2 / SampleRate
    FRate2 = FREQ2 / SampleRate
{{Cl|FOR...NEXT|FOR}} Duration = 0 {{Cl|TO}} Seconds * SampleRate 'play 5 seconds
    {{Cl|FOR}} Duration = {{Text|0|#F580B1}} {{Cl|TO}} Seconds * SampleRate {{Text|<nowiki>'play 5 seconds</nowiki>|#919191}}
  left = Amplitude * {{Cl|SIN}}(Pi2 * Duration * FRate1) 'sine wave
        left = Amplitude * {{Cl|SIN}}(Pi2 * Duration * FRate1) {{Text|<nowiki>'sine wave</nowiki>|#919191}}
  rite = Amplitude * {{Cl|SIN}}(Pi2 * Duration * FRate2)
        rite = Amplitude * {{Cl|SIN}}(Pi2 * Duration * FRate2)
  '{{Cl|LOCATE}} 1, 1: {{Cl|PRINT}} left, rite
        {{Text|<nowiki>'LOCATE 1, 1: PRINT left, rite</nowiki>|#919191}}
  {{Cl|_SNDRAW}} left, rite
        {{Cl|_SNDRAW}} left, rite
{{Cl|NEXT}}
    {{Cl|NEXT}}
{{Cl|DO}}: {{Cl|LOOP}} {{Cl|WHILE}} {{Cl|_SNDRAWLEN}}
    {{Cl|DO}}: {{Cl|DO...LOOP|LOOP WHILE}} {{Cl|_SNDRAWLEN}}
{{Cl|END SUB}}
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}

Latest revision as of 20:35, 8 December 2024

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