Exploring the Mediastreamer2 VoIP engine. Part 4

The material of the article is taken from my zen channel.

Create a signal level meter

In the past article we have clarified the correct termination of programs using the media streamer.

In this article, we will assemble a signal level meter circuit and learn how to read the measurement result from the filter. Let us estimate the measurement accuracy.

The set of filters provided by the media streamer includes a filter, MS_VOLUME, which is able to measure the RMS level of the signal passing through it, attenuate the signal, and perform a lot of useful and unexpected functions. Later we will devote an entire article to this filter. But now we will use it as a meter.

As a signal source, we will use a tone generator, the signal from which we will send to the MS_VOLUME filter, to the output of which the sound card is connected.

In this example, we will use the generator filter in a slightly different mode - it will generate a single-tone signal for us, i.e. a signal containing only one sine wave.

In addition to the frequency and amplitude, we need to set the time during which the signal will be generated, it must be sufficient for enough samples to pass through the MS_VOLUME filter for measurement. The MSDtmfGenCustomTone structure is used to pass the settings generator:

struct _MSDtmfGenCustomTone{
    char tone_name[8];     /* ВСкстовоС Π½Π°Π·Π²Π°Π½ΠΈΠ΅ сигнала ΠΈΠ· 8 Π±ΡƒΠΊΠ².*/
    int duration;          /* Π”Π»ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΡŒ сигнала Π² миллисСкундах.*/
    int frequencies[2];    /* ΠŸΠ°Ρ€Π° частот ΠΈΠ· ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Π΄ΠΎΠ»ΠΆΠ΅Π½ ΡΠΎΡΡ‚ΠΎΡΡ‚ΡŒ Π²Ρ‹Ρ…ΠΎΠ΄Π½ΠΎΠΉ сигнал. */
    float amplitude;       /* Амплитуда Ρ‚ΠΎΠ½ΠΎΠ², 1.0 соотвСтствуСт ΡƒΡ€ΠΎΠ²Π½ΡŽ 0 Π΄Π‘ ΠΎΡ‚ ΠΌΠΈΠ»Π»ΠΈΠ²Π°Ρ‚Ρ‚Π° Π½Π° Π½Π°Π³Ρ€ΡƒΠ·ΠΊΠ΅ 600 Ом.*/
    int interval;          /* ΠŸΠ°ΡƒΠ·Π° Π² миллисСкундах ΠΏΠ΅Ρ€Π΅Π΄ Π½Π°Ρ‡Π°Π»ΠΎΠΌ ΠΏΠΎΠ²Ρ‚ΠΎΡ€Π½ΠΎΠ³ΠΎ проигрывания сигнала.*/
    int repeat_count;      /* ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ ΠΏΠΎΠ²Ρ‚ΠΎΡ€ΠΎΠ².*/
};
typedef struct _MSDtmfGenCustomTone MSDtmfGenCustomTone;

To start the generator, we will use its MS_DTMF_GEN_PLAY_CUSTOM method.

Block diagram of signal processing:

Exploring the Mediastreamer2 VoIP engine. Part 4

The program code that implements this scheme is shown below.

/* Π€Π°ΠΉΠ» mstest3.c */

#include <mediastreamer2/msfilter.h>
#include <mediastreamer2/msticker.h>
#include <mediastreamer2/dtmfgen.h>
#include <mediastreamer2/mssndcard.h>
#include <mediastreamer2/msvolume.h>

int main()
{
    ms_init();
    /* Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ экзСмпляры Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ΠΎΠ². */
    MSFilter  *voidsource=ms_filter_new(MS_VOID_SOURCE_ID);
    MSFilter  *dtmfgen=ms_filter_new(MS_DTMF_GEN_ID);
    MSFilter  *volume=ms_filter_new(MS_VOLUME_ID);
    MSSndCard *card_playback=ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());
    MSFilter  *snd_card_write=ms_snd_card_create_writer(card_playback);

    /* Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ Ρ‚ΠΈΠΊΠ΅Ρ€. */
    MSTicker *ticker=ms_ticker_new();

    /* БоСдиняСм Ρ„ΠΈΠ»ΡŒΡ‚Ρ€Ρ‹ Π² Ρ†Π΅ΠΏΠΎΡ‡ΠΊΡƒ. */
    ms_filter_link(voidsource, 0, dtmfgen, 0);
    ms_filter_link(dtmfgen, 0, volume, 0);
    ms_filter_link(volume, 0, snd_card_write, 0);

    /* ΠŸΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ источник Ρ‚Π°ΠΊΡ‚ΠΎΠ². */
    ms_ticker_attach(ticker,voidsource);

    MSDtmfGenCustomTone dtmf_cfg;

   /* УстанавливаСм имя нашСго сигнала, помня ΠΎ Ρ‚ΠΎΠΌ, Ρ‡Ρ‚ΠΎ Π² массивС ΠΌΡ‹ Π΄ΠΎΠ»ΠΆΠ½Ρ‹
    ΠΎΡΡ‚Π°Π²ΠΈΡ‚ΡŒ мСсто для нуля, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚ ΠΊΠΎΠ½Π΅Ρ† строки. */
    strncpy(dtmf_cfg.tone_name, "busy", sizeof(dtmf_cfg.tone_name));
    dtmf_cfg.duration=1000;
    dtmf_cfg.frequencies[0]=440; /* Π‘ΡƒΠ΄Π΅ΠΌ Π³Π΅Π½Π΅Ρ€ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΎΠ΄ΠΈΠ½ Ρ‚ΠΎΠ½, частоту Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ Ρ‚ΠΎΠ½Π° установим Π² 0.*/
    dtmf_cfg.frequencies[1]=0;
    dtmf_cfg.amplitude=1.0; /* Π’Π°ΠΊΠΎΠΉ Π°ΠΌΠΏΠ»ΠΈΡ‚ΡƒΠ΄Π΅ синуса Π΄ΠΎΠ»ΠΆΠ΅Π½ ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΠΎΠ²Π°Ρ‚ΡŒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ измСрСния 0.707.*/
    dtmf_cfg.interval=0.;
    dtmf_cfg.repeat_count=0.;

   /* Π’ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ Π·Π²ΡƒΠΊΠΎΠ²ΠΎΠΉ Π³Π΅Π½Π΅Ρ€Π°Ρ‚ΠΎΡ€. */
   ms_filter_call_method(dtmfgen, MS_DTMF_GEN_PLAY_CUSTOM, (void*)&dtmf_cfg);

   /* Π”Π°Π΅ΠΌ, врСмя ΠΏΠΎΠ»ΠΎΠ²ΠΈΠ½Ρƒ сСкунды, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΈΠ·ΠΌΠ΅Ρ€ΠΈΡ‚Π΅Π»ΡŒ Π½Π°ΠΊΠΎΠΏΠΈΠ» Π΄Π°Π½Π½Ρ‹Π΅. */
   ms_usleep(500000);

   /* Π§ΠΈΡ‚Π°Π΅ΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ измСрСния. */
  float level=0;
   ms_filter_call_method(volume, MS_VOLUME_GET_LINEAR,&level);
   printf("АмплитудС синуса %f Π²ΠΎΠ»ΡŒΡ‚  соотвСтствуСт срСднСквадратичСскоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ %f Π²ΠΎΠ»ΡŒΡ‚.n", dtmf_cfg.amplitude, level);
}

We compile our example, just like we did before, only using the file name mstest3. Run for execution and get the result:

АмплитудС синуса 1.000000 Π²ΠΎΠ»ΡŒΡ‚  соотвСтствуСт срСднСквадратичСскоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 0.707733 Π²ΠΎΠ»ΡŒΡ‚.

As you can see, the measurement result coincided up to the third decimal place with the theoretical value equal to the square root of two divided in half: sqr(2)/2=0,7071067811865475

The relative deviation of the result from the true value was 0.1%. We made an estimate of the measurement error at the maximum signal level. Accordingly, as the level decreases, the error should increase. I suggest you evaluate it yourself for low signal levels.

In the next article, we will assemble a circuit that detects the presence of a given frequency tone at the input using a tone detector. We will also learn how to handle events generated by filters.

Source: habr.com

Add a comment