The material of the article is taken from my
Create a signal level meter
In the past
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:
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