DTMF

Share your advanced PureBasic knowledge/code with the community.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

DTMF

Post by dobro »

As you know our phones are can be a voice frequencies
Yes, but what does that mean?

shematiser for:
ben as our numbers when typing on the keyboard, it's music, each key has its own frequency!
in fact there are 2 cross frequency (broadcast in parallel)

good agreements, but what you do not know can not be,
is that if you hear a phone frequencies keys
Well it will dial the number (so that you do not touch your keyboard!: D)

ais I made ​​a small program that contains the keys of each Wav format DTMF

for example I use the number "3131" (in France) that lets you know who has sought to call us! (free number)
of course if you have a laptop try using one of your numbers
the only constraint being that we must enter the numbers separated by a comma (because of Stringfield ();))

use: once your number parametered

you put your phone line (to hear the beep out)
the approach of the loudspeakers of your computer (turn it up)
you run my prg, who will make his own music, and hop your correspondent has not tarry answer :)
it's magic :)

useful if you are programming an address book with your phone numbers
you can ask your computer to dial the number to your place :)


archive here:
http://michel.dobro.free.fr/Forum_pb/DTMF/DTMF.zip

have Fun :)

voici juste le code (qui est deja dans l'archive avec les sons )

in French
comme vous le savez peut etre nos telephones sont a frequences vocale
oui mais ça veut dire quoi ?

pour shematiser :
ben que lorsqu'on tape nos numeros sur le clavier , ça fait de la musique , chaque touche ayant sa propre frequence !
en fait il s'agit de 2 fréquences croisées (diffusé en parallèle )

bon d'accords , mais ce que vous ne savez peut etre pas,
c'est que si vous faites entendre a votre téléphone les fréquences des touches
ben il va composer le numéro ( alors que vous ne touchez pas votre clavier !! :D )

je vous ais fait un petit programme qui contient les Wav de chaque touches au format DTMF

pour l'exemple j'utilise le numero "3131" qui permet de savoir qui a cherché a nous appeler ! (numero gratuit)
bien sur si vous avez un portable essayer d'utiliser un de vos numeros
la seule contrainte etant qu'il faut entrer les numeros séparés par une virgule (a cause de Stringfield() ;) )

utilisation : une fois votre numero parametré

vous mettez votre telephone en ligne ( pour entendre le Biiip d'attente )
approchez le des hauts parleur de votre ordinateur (montez le son )
vous lancez mon prg , qui va faire sa petite musique , et hop votre correspondant ne tardera pas a répondre :)
c'est magique :)

pratique si vous vous programmez un carnet d'adresse avec vos numeros de telephone
vous pourrez demander a votre ordinateur de composer le numero a votre place :)

Code: Select all

;***********************************************
;Titre  :*DTMF
;Auteur  : Dobro
;Date  :28/08/2013
;Heure  :16:08:38
;Version Purebasic :  PureBasic 5.11 (Windows - x86)
;Version de l'editeur :EPB V2.40
; Libairies necessaire : Aucune 
;***********************************************

Declare  play_DTMF(numero_a_composer$)
Declare  DTMF(num.s)



n1$="3131"

InitSound()
CatchSound(15, ?etoile)
CatchSound(14, ?diese)
CatchSound(13, ?D)
CatchSound(12, ?C)
CatchSound(11, ?B)
CatchSound(10, ?A)
CatchSound(9, ?_9)
CatchSound(8, ?_8)
CatchSound(7, ?_7)
CatchSound(6, ?_6)
CatchSound(5, ?_5)
CatchSound(4, ?_4)
CatchSound(3, ?_3)
CatchSound(2, ?_2)
CatchSound(1, ?_1)
CatchSound(0, ?_0)
; *****************************************************
numero_a_composer$=n1$
play_DTMF(numero_a_composer$)

; *************** Zone Procedure *************************
procedure play_DTMF(numero_a_composer$)
	; By Dobro
	for i=1 to len(numero_a_composer$)
		num.s=mid(numero_a_composer$,i,1)
		DTMF(num.s)
	next i
Endprocedure

;
procedure DTMF(num.s)
	; By Dobro
	select num
		Case "0"
		PlaySound(0,#PB_Sound_Loop):delay(200):StopSound(0)
		Case "1"
		PlaySound(1,#PB_Sound_Loop):delay(200):StopSound(1)
		Case "2"
		PlaySound(2,#PB_Sound_Loop):delay(200):StopSound(2)
		Case "3"
		PlaySound(3,#PB_Sound_Loop):delay(200):StopSound(3)
		Case "4"
		PlaySound(4,#PB_Sound_Loop):delay(200):StopSound(4)
		Case "5"
		PlaySound(5,#PB_Sound_Loop):delay(200):StopSound(5)
		Case "6"
		PlaySound(6,#PB_Sound_Loop):delay(200):StopSound(6)
		Case "7"
		PlaySound(7,#PB_Sound_Loop):delay(200):StopSound(7)
		Case "8"
		PlaySound(8,#PB_Sound_Loop):delay(200):StopSound(8)
		Case "9"
		PlaySound(9,#PB_Sound_Loop):delay(200):StopSound(9)
		Case "A"
		PlaySound(10,#PB_Sound_Loop):delay(200):StopSound(10) ;A
		Case "B"
		PlaySound(11,#PB_Sound_Loop):delay(200):StopSound(11) ;B
		Case "C"
		PlaySound(12,#PB_Sound_Loop):delay(200):StopSound(12) ;C
		Case "D"
		PlaySound(13,#PB_Sound_Loop):delay(200):StopSound(13) ;D
		Case "#"
		PlaySound(14,#PB_Sound_Loop):delay(200):StopSound(14) ;#
		Case "*"
		PlaySound(15,#PB_Sound_Loop):delay(200):StopSound(15) ;*
	Endselect
Endprocedure

DataSection
	; By Dobro
	etoile: IncludeBinary "etoile.wav"
	diese: IncludeBinary "diese.wav"
	D: IncludeBinary "D.wav"
	C: IncludeBinary "C.wav"
	B: IncludeBinary "B.wav"
	A: IncludeBinary "A.wav"
	_9: IncludeBinary "9.wav"
	_8: IncludeBinary "8.wav"
	_7: IncludeBinary "7.wav"
	_6: IncludeBinary "6.wav"
	_5: IncludeBinary "5.wav"
	_4: IncludeBinary "4.wav"
	_3: IncludeBinary "3.wav"
	_2: IncludeBinary "2.wav"
	_1: IncludeBinary "1.wav"
	_0: IncludeBinary "0.wav"
EndDataSection

; EPB
Last edited by dobro on Wed Aug 28, 2013 3:10 pm, edited 3 times in total.
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: DTMF

Post by wilbert »

On OSX it unfortunately doesn't work (it doesn't support 8Khz sample rate).

Here's a solution without audio files tested on Windows 7 and OS X

Code: Select all

InitSound()

Procedure PlayDTMF(Number.s)
  Static Sound, Freq1.q = $066105C5053804B9, Freq2.q = $03AD0354030202B9
  Protected *SampleData.Ascii, Samples, *WavData, c1.d, c2.d, i, s, l = Len(Number)
  If l
    Samples = l * 8820
    *WavData = AllocateMemory(Samples + 44)
    If *WavData
      PokeL(*WavData, $46464952)
      PokeL(*WavData + 4, Samples + 36)
      PokeQ(*WavData + 8, $20746D6645564157)
      PokeQ(*WavData + 16, $0001000100000010)
      PokeQ(*WavData + 24, $0000AC440000AC44)
      PokeQ(*WavData + 32, $6174616400080001)
      PokeL(*WavData + 40, Samples)
      FillMemory(*WavData + 44, Samples, 128)
      While i < l
        s = FindString("123A456B789C*0#D", Mid(Number, i + 1, 1)) - 1
        If s >= 0
          *SampleData = *WavData + i * 8820 + 44
          c1 = PeekU(@Freq1 + (s & $3) << 1) * 2 * #PI / 44100
          c2 = PeekU(@Freq2 + (s & $C) >> 1) * 2 * #PI / 44100
          For s = 0 To 4409
            *SampleData\a = 128 + 63 * Sin(s * c1) + 63 * Sin(s * c2)
            *SampleData + 1
          Next
        EndIf
        i + 1
      Wend
      If Sound : FreeSound(Sound) : EndIf
      Sound = CatchSound(#PB_Any, *WavData)
      FreeMemory(*WavData)
      If Sound : PlaySound(Sound) : EndIf
    EndIf
  EndIf
  ProcedureReturn Sound
EndProcedure

Define DTMF = PlayDTMF("*#21")
If DTMF
  While SoundStatus(DTMF) = #PB_Sound_Playing : Delay(1) : Wend
EndIf
Last edited by wilbert on Tue Apr 16, 2013 9:27 am, edited 1 time in total.
Windows (x64)
Raspberry Pi OS (Arm64)
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: DTMF

Post by kvitaliy »

wilbert wrote: Here's a solution without audio files tested on Windows 7 and OS X
Linux Ubuntu 10 - Ok!
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: DTMF

Post by infratec »

Hi wilbert,

small enhancement:

Code: Select all

If Sound
        PlaySound(Sound)
        While SoundStatus(Sound) = #PB_Sound_Playing : Delay(1) : Wend
      EndIf
Than you don't need the MessageRequester()

:idea:
I made it now optional with an optional parameter Wait.i=#False
If the parameter is #True I execute the while loop.

Bernd
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: DTMF

Post by wilbert »

infratec wrote:Hi wilbert,

small enhancement:
Thanks for the suggestion Bernd.
I din't realize I could get that sound status.
I updated my code to not use a MessageRequester.
I did it a little different as you suggested because I like the function itself to immediately return.
Windows (x64)
Raspberry Pi OS (Arm64)
infratec
Always Here
Always Here
Posts: 7622
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: DTMF

Post by infratec »

Hi wilbert,

that was my second idea resulting in an optional parameter.

Bernd
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: DTMF

Post by wilbert »

infratec wrote:Hi wilbert,

that was my second idea resulting in an optional parameter.

Bernd
That might be better.
It's also possible to split off the generation part like this

Code: Select all

InitSound()

Procedure GenerateDTMF(Number.s)
  Static Freq1.q = $066105C5053804B9, Freq2.q = $03AD0354030202B9
  Protected *SampleData.Ascii, Samples, *WavData, c1.d, c2.d, i, s, l = Len(Number)
  If l
    Samples = l * 8820
    *WavData = AllocateMemory(Samples + 44)
    If *WavData
      PokeL(*WavData, $46464952)
      PokeL(*WavData + 4, Samples + 36)
      PokeQ(*WavData + 8, $20746D6645564157)
      PokeQ(*WavData + 16, $0001000100000010)
      PokeQ(*WavData + 24, $0000AC440000AC44)
      PokeQ(*WavData + 32, $6174616400080001)
      PokeL(*WavData + 40, Samples)
      FillMemory(*WavData + 44, Samples, 128)
      While i < l
        s = FindString("123A456B789C*0#D", Mid(Number, i + 1, 1)) - 1
        If s >= 0
          *SampleData = *WavData + i * 8820 + 44
          c1 = PeekU(@Freq1 + (s & $3) << 1) * 2 * #PI / 44100
          c2 = PeekU(@Freq2 + (s & $C) >> 1) * 2 * #PI / 44100
          For s = 0 To 4409
            *SampleData\a = 128 + 63 * Sin(s * c1) + 63 * Sin(s * c2)
            *SampleData + 1
          Next
        EndIf
        i + 1
      Wend
    EndIf
  EndIf
  ProcedureReturn *WavData
EndProcedure
  
Procedure PlayDTMF(Number.s, WaitUntilFinished = #False)
  Static Sound
  Protected *WavData = GenerateDTMF(Number)
  If *WavData
    If Sound : FreeSound(Sound) : EndIf
    Sound = CatchSound(#PB_Any, *WavData)
    FreeMemory(*WavData)
    If Sound
      PlaySound(Sound)
      If WaitUntilFinished
        While SoundStatus(Sound) = #PB_Sound_Playing : Delay(1) : Wend
      EndIf
    EndIf
  EndIf
EndProcedure



PlayDTMF("*#21", #True)
That would also allow to create a wav file easily like this

Code: Select all

Define MyDTMF = GenerateDTMF("*#21-40709")
If MyDTMF
  If CreateFile(0, "Test.wav")
    WriteData(0, MyDTMF, MemorySize(MyDTMF))
    CloseFile(0)
  EndIf
  FreeMemory(MyDTMF)
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: DTMF

Post by dobro »

Thank you for this adaptation :D
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: DTMF

Post by dobro »

wilbert wrote:On OSX it unfortunately doesn't work (it doesn't support 8Khz sample rate).
I redid my archive with its 44 100 16 bits ;)

http://michel.dobro.free.fr/Forum_pb/DTMF/DTMF.zip
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: DTMF

Post by wilbert »

dobro wrote:I redid my archive with its 44 100 16 bits ;)
The sounds are fine now on OSX.
It looks like you forgot to include "etoile" in your updated archive.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: DTMF

Post by dobro »

Rhooo! what a fool :oops:

Thank you this is corrected :lol:
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Post Reply