Translate

Monday 29 January 2018

Textual description of firstImageUrl

Getting RMS Value from FFMPEG using C Language and setting up the Audio Level to use Train announcement System



Author Name : Onkar Dubey

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{

    int  count = 0, flag = 1;
    FILE *pipein;
    pipein  = popen("ffmpeg -f alsa -i hw:0,0 -af astats=metadata=1:reset=1,ametadata=print:key=lavfi.astats.Overall.RMS_level -f null - 2>&1", "r");

    char line[1024];
    char *s;
    double om_rms;

    while(1)
    {

        fscanf(pipein, "%[^\n]\n", line);

        s = strstr(line, "RMS_level");
        if (s)
        {
            s += 10;

            om_rms = atof(s);

            // Print the RMS value
            //fprintf(stderr, "  RMS_VALUE = %lf\n", om_rms);

                if(om_rms > -15.00000)
                {
                    count ++;
                    if((count == 10) && (flag == 1))
                        {
                        printf("Alert ON\n");
                        fprintf(stderr, "Train announcement is going on....Stop othe process\nRMS_VALUE = %lf dB\n\n", om_rms);
                        flag = 0;
                        }
                }
                else if (om_rms < -18.00000)
                {
                    count --;
                    if((count == -200) && (flag == 0))
                        {
                        printf("Alert OFF\n");
                        fprintf(stderr, "Train announcement have been stoped..... Start othe process\nRMS_VALUE = %lf dB\n\n", om_rms);
                        flag = 1;
                        }
                    else if (count < -200) count = -200;
                }
                else count = 0;
         }

}}




 To connect with socket

 server.c

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>


#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void main(){

    char alert[10] = "Alert ON";
    char alert1[10] = "Alert OFF";
    int count = 0, flag = 1;
    FILE *pipein;
    pipein  = popen("ffmpeg -f alsa -i hw:0,0 -af astats=metadata=1:reset=1,ametadata=print:key=lavfi.astats.Overall.RMS_level -f null - 2>&1", "r");

    char line[1024]; // longer than required - only stores one line of text at a time!
    char *s;
    double om_rms;

        int net_socket;
        net_socket=socket(AF_INET,SOCK_STREAM,0);

        struct sockaddr_in server_address;
        server_address.sin_family=AF_INET;
        server_address.sin_port=htons(2020);
        server_address.sin_addr.s_addr=INADDR_ANY;

        int k = bind(net_socket, (struct sockaddr *) &server_address, sizeof(server_address));
        if(k<0){
                printf("Error in socket binding");
        }
        listen(net_socket,6);

        int client_socket=accept(net_socket,NULL, NULL);





    while(1){
        // Read a line of text from the input pipe
        fscanf(pipein, "%[^\n]\n", line);

        // Find the substring "RMS_level" if it is present in this line
        s = strstr(line, "RMS_level");
        if (s){
            // Substring "RMS_level" was found, so jump to beginning of value
            s += 10;

            // Convert the value from a string (the rest of the line) to a double
            om_rms = atof(s);

            // Print the RMS value
            //fprintf(stderr, "  RMS_VALUE = %lf\n", om_rms);
            if(om_rms > -26.000000)
                {
                    count ++;
                    if((count == 10) && (flag == 1))
                        {
                        send(client_socket,alert,sizeof(alert),0);
                        printf("Alert ON\n");
                        fprintf(stderr, "User is Speaking, Stop othe process  RMS_VALUE = %lf\n\n", om_rms);
                        flag = 0;
                        }
                }
                else if (om_rms < -28.00000)
                {
                    count --;
                    if((count == -200) && (flag == 0))
                        {
                        int client_socket=accept(net_socket,NULL, NULL);
                        send(client_socket,alert1,sizeof(alert1),0);
                        printf("Alert OFF\n");
                        fprintf(stderr, "User is not speaking, Start othe process  RMS_VALUE = %lf\n\n", om_rms);
                        flag = 1;
                        }
                else if (count < -200) count = -200;
                }
                else count = 0;
         }
    }
}


Client.c


#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


int main(){
        int network_socket;
    char recieve_msg[20];

        network_socket=socket(AF_INET,SOCK_STREAM,0);

        struct sockaddr_in server_address;
        server_address.sin_family=AF_INET;
        server_address.sin_port=htons(2020);
        server_address.sin_addr.s_addr=INADDR_ANY;

int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
        if(connection_status<0){
                printf("Error in connection\n");
        close(network_socket);
        }
    else {
    int ret = recv(network_socket,&recieve_msg,sizeof(recieve_msg),0);
while(1)
{
if(ret){
    printf("Recieved massage is:  %s\n: ",recieve_msg);
    }
}
close(network_socket);


return 0;
}
}

to execute the program run as
server side
$ gcc -o server server.c
$ ./server 2020
client side
$ gcc -o Client Client.c
$ ./Client 2020
     

I will make it more reliable to get that inbox me.
To know more detail inbox or comment on the comment box


                                      Give Something to the world and it will never let you down. 
                                                                                           Onkar Dubey 
 
 

No comments:

Post a Comment