Location>code7788 >text

python3 parse wav file to get dtmf value

Popularity:561 ℃/2024-08-03 11:42:41

Operating system: Windows 10_x64

Python version: 3.9.2

Working on FreeSwitch-related tasks, the odds are that you will encounter DTMF, which can be delivered in three ways:
  • In-band
  • RFC2833
  • SIP-INFO

With DTMF using RFC2833 or SIP-INFO delivery mode, FreeSwitch can print out in the log, but when encountering DTMF in inband mode, FreeSwitch's log will not print out.

In case of a conference scenario, DTMF is sent using In-band mode (e.g., landline), and the sound of the DTMF key is mixed with the audio stream and sent to the opposite end:

1) After the transfer process is converted to 2833 format dtmf, 2833 format DTMF key information is added and the DTMF keys mixed inside the audio stream are not removed;

2) It will appear that a DTMF key tone can be heard coming from the landline in the conference system.

There is no better solution to this problem, and it is documented here. But the problem has provoked me to think:

Can DTMF in In-band mode be recognized? If it can be recognized, how?

Today I am organizing my notes on In-band mode DTMF identification. I will expand on the following areas:

  • Introduction to DTMF

  • How to generate a wav file containing DTMF key tones

  • How to parse a wav file for DTMF keystrokes using python scripts

  • Screenshots and Resources

I. Introduction to DTMF

DTMF signal is Dual Tone Multi-Frequency signal, there are more information introduced on the internet, the main concern here is the frequency information, you can refer to the introduction about DTMF on pypi:
/project/dtmf/

II. Generate a wav file containing DTMF key tones

1、Use Audacity to generate wav files

Audacity version: 3.1.3 Audacity is used here for noise file generation, if you don't know Audacity software, please refer to this article:

/MikeZhang/p/

1) Select "Generate ->DTMF Tone..."

2) Generate 0123456789 audio

3) Export Audio

Here the audio is exported using 8000hz.

If you want to use the exported audio file () directly, you can get it from the following sources:

Get it by replying 20240803 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

2、Use softphone to simulate inband mode to generate wav file

1) Configure the softphone to use inband mode

2) DTMF keys to simulate the call process with a conference room

conference test bgdial user/1001
conference test bgdial user/1002

1001 keystroke, keystroke tones can be heard at 1002 in the conference room (keystroke value: 123456789*0#).

3) Executive boardroom recording

The commands are as follows:

conference test recording start /tmp/

Describing the general process here, theIf you want to use the exported audio file () directly, you can get it from the following sources:

Get it by replying 20240803 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

Third, python parse wav file to get DTMF key value

Found a library for parsing dtmf on GitHub:

/ribt/dtmf-decoder

If you can't open it, you can get the downloaded source zip from the source provided at the end of the article.

The core is a file that runs as follows:

Or you can just use my modified code ():

The full code is available from the following sources:

Get it by replying 20240803 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

The running effect is as follows:

IV. Downloading of resources

This article covers the source code and related documentation, which can be obtained from the following sources:

Get it by replying 20240803 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

Okay, that's all, don't forget to like it haha!