Thread: C programing doubt

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    1

    C programing doubt

    hi,

    I don't know c very much they are two separate c programs called
    1. app_talkdetect.c
    and
    2. app_record.c

    in the app_talkdetect.c file i have to find if any voice interrupt is occurs in that file if it occurs
    it will stop the process and do other things in original c programming but i have to change as instead of stopping the voice intercepted must be saved for that i have to call the app_record.c in the place of voice intercept and i have to record that voice.
    Code:
     code app_talkdetect.c
    
    *//*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 1999 - 2005, Digium, Inc.
     *
     * Mark Spencer <[email protected]>
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
     * This program is free software, distributed under the terms of
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    /*! \file
     *
     * \brief Playback a file with audio detect
     *
     * \ingroup applications
     */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
    
    #include "asterisk/lock.h"
    #include "asterisk/file.h"
    #include "asterisk/logger.h"
    #include "asterisk/channel.h"
    #include "asterisk/pbx.h"
    #include "asterisk/module.h"#include "asterisk/utils.h"
    #include "asterisk/dsp.h"
    #include "asterisk/translate.h"
    
    static char *tdesc = "Playback with Talk Detection";
    
    static char *app = "BackgroundDetect";
    
    static char *synopsis = "Background a file with talk detect";
    
    static char *descrip =
    "  BackgroundDetect(filename[|sil[|min|[max]]]):  Plays  back  a  given\n"
    "filename, waiting for interruption from a given digit (the digit must\n"
    "start the beginning of a valid extension, or it will be ignored).\n"
    "During the playback of the file, audio is monitored in the receive\n"
    "direction, and if a period of non-silence which is greater than 'min' ms\n"
    "yet less than 'max' ms is followed by silence for at least 'sil' ms then\n"
    "the audio playback is aborted and processing jumps to the 'talk' extension\n"
    "if available.  If unspecified, sil, min, and max default to 1000, 100, and\n"
    "infinity respectively.\n";
    
    STANDARD_LOCAL_USER;
    
    LOCAL_USER_DECL;
    
    #include "app_talkdetect.h"
    
    int wifi_record_exec(struct ast_channel *chan, void *data);
    
    static int background_detect_exec(struct ast_channel *chan, void *data)
    {
            int res = 0;
            struct localuser *u;
            char *tmp;
            char *options;
            char *stringp;
            struct ast_frame *fr;
            int notsilent=0;
            struct timeval start = { 0, 0};
    
            int sil = 1000;
            int min = 100;
            int max = -1;
            int x;
            int origrformat=0;
            struct ast_dsp *dsp;
    
            if (ast_strlen_zero(data)) {
                    ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
                    return -1;
            }
    
            LOCAL_USER_ADD(u);
    
            tmp = ast_strdupa(data);
            if (!tmp) {
                    ast_log(LOG_ERROR, "Out of memory\n");
                    LOCAL_USER_REMOVE(u);
                    return -1;
            }
    
            stringp=tmp;
            strsep(&stringp, "|");
            options = strsep(&stringp, "|");
            if (options) {
                    if ((sscanf(options, "%d", &x) == 1) && (x > 0))
                            sil = x;
                    options = strsep(&stringp, "|");
                    if (options) {
                            if ((sscanf(options, "%d", &x) == 1) && (x > 0))
                                    min = x;
                            options = strsep(&stringp, "|");
                            if (options) {
                                    if ((sscanf(options, "%d", &x) == 1) && (x > 0))
                                            max = x;
                            }
                    }
            }
            ast_log(LOG_DEBUG, "Preparing detect of '%s', sil=%d,min=%d,max=%d\n",
                                                    tmp, sil, min, max);
            if (chan->_state != AST_STATE_UP) {
                    /* Otherwise answer unless we're supposed to send this while on-hook */
                    res = ast_answer(chan);
            }
            if (!res) {
                    origrformat = chan->readformat;
                    if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)))
                            ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
            }
            if (!(dsp = ast_dsp_new())) {
                    ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
                    res = -1;
            }
            if (!res) {
                    //ast_stopstream(chan);
                    wifi_record_exec(chan,data);
                    res = ast_streamfile(chan, tmp, chan->language);
                    if (!res) {
                            while(chan->stream) {
                                    res = ast_sched_wait(chan->sched);
                                    if ((res < 0) && !chan->timingfunc) {
                                            res = 0;
                                            break;
                                    }
                                    if (res < 0)
                                            res = 1000;
                                    res = ast_waitfor(chan, res);
                                    if (res < 0) {
                                            ast_log(LOG_WARNING, "Waitfor failed on %s\n", chan->name);
                                            break;
                                    } else if (res > 0) {
                                            fr = ast_read(chan);
                                            if (!fr) {
                                                    res = -1;
                                                    break;
                                            } else if (fr->frametype == AST_FRAME_DTMF) {
                                                    char t[2];
                                                    t[0] = fr->subclass;
                                                    t[1] = '\0';
                                                    if (ast_canmatch_extension(chan, chan->context, t, 1, chan->cid.cid_num)) {
                                                            /* They entered a valid  extension, or might be anyhow */
                                                            res = fr->subclass;
                                                            ast_frfree(fr);
                                                            break;
                                                    }
                                            } else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass == AST_FORMAT_SLINEAR)) {
                                                    int totalsilence;
                                                    int ms;
                                                    res = ast_dsp_silence(dsp, fr, &totalsilence);
                                                    if (res && (totalsilence > sil)) {
                                                            /* We've been quiet a little while */
                                                            if (notsilent) {
                                                                    /* We had heard some talking */
                                                                    ms = ast_tvdiff_ms(ast_tvnow(), start);
                                                                    ms -= sil;
                                                                    if (ms < 0)
                                                                            ms = 0;
                                                                    if ((ms > min) && ((max < 0) || (ms < max))) {
                                                                            char ms_str[10];
                                                                            ast_log(LOG_DEBUG, "Found qualified token of %d ms\n", ms);
    
                                                                            /* Save detected talk time (in milliseconds) */
                                                                            sprintf(ms_str, "%d", ms );
                                                                            pbx_builtin_setvar_helper(chan, "TALK_DETECTED", ms_str);
    
                                                                            ast_goto_if_exists(chan, chan->context, "talk", 1);
                                                                            res = 0;
                                                                            ast_frfree(fr);
                                                                            break;
                                                                    } else
                                                                            ast_log(LOG_DEBUG, "Found unqualified token of %d ms\n", ms);
                                                                    notsilent = 0;
                                                            }
                                                    } else {
                                                            if (!notsilent) {
                                                                    /* Heard some audio, mark the begining of the token */
                                                                    start = ast_tvnow();
                                                                    ast_log(LOG_DEBUG, "Start of voice token!\n");
                                                                    notsilent = 1;
                                                            }
                                                    }
    
                                            }
                                            ast_frfree(fr);
                                    }
                                    ast_sched_runq(chan->sched);
                            }
    
                            wifi_record_exec(chan,data);
                            //ast_stopstream(chan);
                    } else {
                            ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
                            res = 0;
                    }
            }
            if (res > -1) {
                    if (origrformat && ast_set_read_format(chan, origrformat)) {
                            ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n",
                                    chan->name, ast_getformatname(origrformat));
                    }
            }
            if (dsp)
                    ast_dsp_free(dsp);
            LOCAL_USER_REMOVE(u);
            return res;
    }
    
    int unload_module(void)
    {
            int res;
    
            res = ast_unregister_application(app);
    
            STANDARD_HANGUP_LOCALUSERS;
    
            return res;
    }
    
    int load_module(void)
    {
            return ast_register_application(app, background_detect_exec, synopsis, descrip);
    }
    
    char *description(void)
    {
            return tdesc;
    }
    
    int usecount(void)
    {
            int res;
            STANDARD_USECOUNT(res);
            return res;
    }
    
    char *key()
    {
            return ASTERISK_GPL_KEY;
    }
    This is the first file.

    code app_recodr.c
    Code:
    /*/*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 1999 - 2005, Digium, Inc.
     *
     * Matthew Fredrickson <[email protected]>
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
     * This program is free software, distributed under the terms of
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    /*! \file
     *
     * \brief Trivial application to record a sound file
     *
     * \ingroup applications
     */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision: 55277 $")
    
    #include "asterisk/lock.h"
    #include "asterisk/file.h"
    #include "asterisk/logger.h"
    #include "asterisk/channel.h"
    #include "asterisk/pbx.h"
    #include "asterisk/module.h"
    #include "asterisk/translate.h"
    
    #include "asterisk/dsp.h"
    #include "asterisk/utils.h"
    #include "asterisk/options.h"
    #include "asterisk/app.h"
    
    static char *tdesc = "Trivial Record Application";
    
    static char *app = "Record";
    
    static char *synopsis = "Record to a file";
    
    static char *descrip =
    "  Record(filename.format|silence[|maxduration][|options])\n\n"
    "Records from the channel into a given filename. If the file exists it will\n"
    "be overwritten.\n"
    "- 'format' is the format of the file type to be recorded (wav, gsm, etc).\n"
    "- 'silence' is the number of seconds of silence to allow before returning.\n"
    "- 'maxduration' is the maximum recording duration in seconds. If missing\n"
    "or 0 there is no maximum.\n"
    "- 'options' may contain any of the following letters:\n"
    "     'a' : append to existing recording rather than replacing\n"
    "     'n' : do not answer, but record anyway if line not yet answered\n"
    "     'q' : quiet (do not play a beep tone)\n"
    "     's' : skip recording if the line is not yet answered\n"
    "     't' : use alternate '*' terminator key instead of default '#'\n"
    "\n"
    "If filename contains '%d', these characters will be replaced with a number\n"
    "incremented by one each time the file is recorded. A channel variable\n"
    "named RECORDED_FILE will also be set, which contains the final filemname.\n\n"
    "Use 'show file formats' to see the available formats on your system\n\n"
    "User can press '#' to terminate the recording and continue to the next priority.\n\n"
    "If the user should hangup during a recording, all data will be lost and the\n"
    "application will terminate. \n";
    
    STANDARD_LOCAL_USER;
    
    LOCAL_USER_DECL;
    
    static int record_exec(struct ast_channel *chan, void *data)
    {
            int res = 0;
            int count = 0;
            int percentflag = 0;
            char *filename, *ext = NULL, *silstr, *maxstr, *options;
            char *vdata, *p;
            int i = 0;
            char tmp[256];
    
            struct ast_filestream *s = '\0';
            struct localuser *u;
            struct ast_frame *f = NULL;
    
            struct ast_dsp *sildet = NULL;          /* silence detector dsp */
            int totalsilence = 0;
            int dspsilence = 0;
            int silence = 0;                /* amount of silence to allow */
            int gotsilence = 0;             /* did we timeout for silence? */
            int maxduration = 0;            /* max duration of recording in milliseconds */
            int gottimeout = 0;             /* did we timeout for maxduration exceeded? */
            int option_skip = 0;
            int option_noanswer = 0;
            int option_append = 0;
            int terminator = '#';
            int option_quiet = 0;
            int rfmt = 0;
            int flags;
            int waitres;
            struct ast_silence_generator *silgen = NULL;
    
            /* The next few lines of code parse out the filename and header from the input string */
            if (ast_strlen_zero(data)) { /* no data implies no filename or anything is present */
                    ast_log(LOG_WARNING, "Record requires an argument (filename)\n");
                    return -1;
            }
    
            LOCAL_USER_ADD(u);
            /* Yay for strsep being easy */
            vdata = ast_strdupa(data);
            if (!vdata) {
                    ast_log(LOG_ERROR, "Out of memory\n");
                    LOCAL_USER_REMOVE(u);
                    return -1;
            }
    
            p = vdata;
            filename = strsep(&p, "|");
            silstr = strsep(&p, "|");
            maxstr = strsep(&p, "|");
            options = strsep(&p, "|");
    
            if (filename) {
                    if (strstr(filename, "%d"))
                            percentflag = 1;
                    ext = strrchr(filename, '.'); /* to support filename with a . in the filename, not format */
                    if (!ext)
                            ext = strchr(filename, ':');
                    if (ext) {
                            *ext = '\0';
                            ext++;
                    }
            }
            if (!ext) {
                    ast_log(LOG_WARNING, "No extension specified to filename!\n");
                    LOCAL_USER_REMOVE(u);
                    return -1;
            }
            if (silstr) {
                    if ((sscanf(silstr, "%d", &i) == 1) && (i > -1)) {
                            silence = i * 1000;
                    } else if (!ast_strlen_zero(silstr)) {
                            ast_log(LOG_WARNING, "'%s' is not a valid silence duration\n", silstr);
                    }
            }
            if (maxstr) {
                    if ((sscanf(maxstr, "%d", &i) == 1) && (i > -1))
                            /* Convert duration to milliseconds */
                            maxduration = i * 1000;
                    else if (!ast_strlen_zero(maxstr))
                            ast_log(LOG_WARNING, "'%s' is not a valid maximum duration\n", maxstr);
            }
            if (options) {
                    /* Retain backwards compatibility with old style options */
                    if (!strcasecmp(options, "skip"))
                            option_skip = 1;
                    else if (!strcasecmp(options, "noanswer"))
                            option_noanswer = 1;
                    else {
                            if (strchr(options, 's'))
                                    option_skip = 1;
                            if (strchr(options, 'n'))
                                    option_noanswer = 1;
                            if (strchr(options, 'a'))
                                    option_append = 1;
                            if (strchr(options, 't'))
                                    terminator = '*';
                            if (strchr(options, 'q'))
                                    option_quiet = 1;
                    }
            }
    
            /* done parsing */
    
            /* these are to allow the use of the %d in the config file for a wild card of sort to
              create a new file with the inputed name scheme */
            if (percentflag) {
                    AST_DECLARE_APP_ARGS(fname,
                            AST_APP_ARG(piece)[100];
                    );
                    char *tmp2 = ast_strdupa(filename);
                    char countstring[15];
                    int i;
                    /* Separate each piece out by the format specifier */
                    /* AST_NONSTANDARD_APP_ARGS(fname, tmp2, '%'); */
                    fname.argc = ast_app_separate_args(tmp2, '%', fname.argv, (sizeof(fname) - sizeof(fname.argc)) / sizeof(fname.argv[0]));
                    do {
                            int tmplen;
                            /* First piece has no leading percent, so it's copied verbatim */
                            ast_copy_string(tmp, fname.piece[0], sizeof(tmp));
                            tmplen = strlen(tmp);
                            for (i = 1; i < fname.argc; i++) {
                                    if (fname.piece[i][0] == 'd') {
                                            /* Substitute the count */
                                            snprintf(countstring, sizeof(countstring), "%d", count);
                                            ast_copy_string(tmp + tmplen, countstring, sizeof(tmp) - tmplen);
                                            tmplen += strlen(countstring);
                                    } else if (tmplen + 2 < sizeof(tmp)) {
                                            /* Unknown format specifier - just copy it verbatim */
                                            tmp[tmplen++] = '%';
                                            tmp[tmplen++] = fname.piece[i][0];
                                    }
                                    /* Copy the remaining portion of the piece */
                                    ast_copy_string(tmp + tmplen, &(fname.piece[i][1]), sizeof(tmp) - tmplen);
                            }
                            count++;
                    } while ( ast_fileexists(tmp, ext, chan->language) != -1 );
                    pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
            } else
                    strncpy(tmp, filename, sizeof(tmp)-1);
            /* end of routine mentioned */
    
    
    
            if (chan->_state != AST_STATE_UP) {
                    if (option_skip) {
                            /* At the user's option, skip if the line is not up */
                            LOCAL_USER_REMOVE(u);
                            return 0;
                    } else if (!option_noanswer) {
                            /* Otherwise answer unless we're supposed to record while on-hook */
                            res = ast_answer(chan);
    
                    }
            }
    
            if (res) {
                    ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
                    goto out;
            }
    
            if (!option_quiet) {
                    /* Some code to play a nice little beep to signify the start of the record operation */
                    res = ast_streamfile(chan, "beep", chan->language);
                    if (!res) {
                            res = ast_waitstream(chan, "");
                    } else {
                            ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name);
                    }
                    ast_stopstream(chan);
            }
    
            /* The end of beep code.  Now the recording starts */
    
            if (silence > 0) {
                    rfmt = chan->readformat;
                    res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
                    if (res < 0) {
                            ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
                            LOCAL_USER_REMOVE(u);
                            return -1;
                    }
                    sildet = ast_dsp_new();
                    if (!sildet) {
                            ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
                            LOCAL_USER_REMOVE(u);
                            return -1;
                    }
                    ast_dsp_set_threshold(sildet, 256);
            }
    
    
            flags = option_append ? O_CREAT|O_APPEND|O_WRONLY : O_CREAT|O_TRUNC|O_WRONLY;
            s = ast_writefile( tmp, ext, NULL, flags , 0, 0644);
    
            if (!s) {
                    ast_log(LOG_WARNING, "Could not create file %s\n", filename);
                    goto out;
            }
    
            if (option_transmit_silence_during_record)
                    silgen = ast_channel_start_silence_generator(chan);
    
            /* Request a video update */
            ast_indicate(chan, AST_CONTROL_VIDUPDATE);
    
            if (maxduration <= 0)
                    maxduration = -1;
    
            while ((waitres = ast_waitfor(chan, maxduration)) > -1) {
                    if (maxduration > 0) {
                            if (waitres == 0) {
                                    gottimeout = 1;
                                    break;
                            }
                            maxduration = waitres;
                    }
    
                    f = ast_read(chan);
                    if (!f) {
                            res = -1;
                            break;
                    }
                    if (f->frametype == AST_FRAME_VOICE) {
                            res = ast_writestream(s, f);
    
                            if (res) {
                                    ast_log(LOG_WARNING, "Problem writing frame\n");
                                    ast_frfree(f);
                                    break;
                            }
    
                            if (silence > 0) {
                                    dspsilence = 0;
                                    ast_dsp_silence(sildet, f, &dspsilence);
                                    if (dspsilence) {
                                            totalsilence = dspsilence;
                                    } else {
                                            totalsilence = 0;
                                    }
                                    if (totalsilence > silence) {
                                            /* Ended happily with silence */
                                            ast_frfree(f);
                                            gotsilence = 1;
                                            break;
                                    }
                            }
                    } else if (f->frametype == AST_FRAME_VIDEO) {
                            res = ast_writestream(s, f);
    
                            if (res) {
                                    ast_log(LOG_WARNING, "Problem writing frame\n");
                                    ast_frfree(f);
                                    break;
                            }
                    } else if ((f->frametype == AST_FRAME_DTMF) &&
                        (f->subclass == terminator)) {
                            ast_frfree(f);
                            break;
                    }
                    ast_frfree(f);
            }
            if (!f) {
                    ast_log(LOG_DEBUG, "Got hangup\n");
                    res = -1;
            }
    
            if (gotsilence) {
                    ast_stream_rewind(s, silence-1000);
                    ast_truncstream(s);
            } else if (!gottimeout) {
                    /* Strip off the last 1/4 second of it */
                    ast_stream_rewind(s, 250);
                    ast_truncstream(s);
            }
            ast_closestream(s);
    
            if (silgen)
                    ast_channel_stop_silence_generator(chan, silgen);
    
     out:
            if ((silence > 0) && rfmt) {
                    res = ast_set_read_format(chan, rfmt);
                    if (res)
                            ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
                    if (sildet)
                            ast_dsp_free(sildet);
            }
    
            LOCAL_USER_REMOVE(u);
    
            return res;
    }
    
    int unload_module(void)
    {
            int res;
    
            res = ast_unregister_application(app);
    
            STANDARD_HANGUP_LOCALUSERS;
    
            return res;
    }
    
    int load_module(void)
    {
            return ast_register_application(app, record_exec, synopsis, descrip);
    }
    
    char *description(void)
    {
            return tdesc;
    }
    
    int usecount(void)
    {
            int res;
            STANDARD_USECOUNT(res);
            return res;
    }
    
    char *key()
    {
            return ASTERISK_GPL_KEY;
    }
    please it's very urgent. can any one help me so.....
    */

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And your actual question is? Just dumping several hundred lines of not exactly trivial code and hoping someone will "do the right thing" with it seems a bit hopeful to me.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Try this clue in the source code.

    * the project provides a web site, mailing lists and IRC
    * channels for your use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  2. Programing microcontrollers in c
    By gorabhat in forum C Programming
    Replies: 2
    Last Post: 09-09-2008, 10:40 AM
  3. Human brain and programing
    By avgprogamerjoe in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-27-2007, 04:48 PM
  4. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  5. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 04:41 AM