BLOG | DOCUMENTATION | TRAC

Home --> Documentations --> PJMEDIA Reference

Samples: Sine Wave/Dual-Tone Generation

This is a simple program to generate a tone and write the samples to a raw PCM file. The main purpose of this file is to analyze the quality of the tones/sine wave generated by PJMEDIA tone/sine wave generator.

This file is pjsip-apps/src/samples/tonegen.c

00001 /* $Id: tonegen.c 3553 2011-05-05 06:14:19Z nanang $ */
00002 /* 
00003  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
00004  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00019  */
00020 
00035 #include <pjmedia.h>
00036 #include <pjlib.h>
00037 
00038 #define SAMPLES_PER_FRAME   64
00039 #define ON_DURATION         100
00040 #define OFF_DURATION        100
00041 
00042 
00043 /*
00044  * main()
00045  */
00046 int main()
00047 {
00048     pj_caching_pool cp;
00049     pjmedia_endpt *med_endpt;
00050     pj_pool_t *pool;
00051     pjmedia_port *port;
00052     unsigned i;
00053     pj_status_t status;
00054 
00055 
00056     /* Must init PJLIB first: */
00057     status = pj_init();
00058     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00059 
00060     /* Must create a pool factory before we can allocate any memory. */
00061     pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
00062 
00063     /* 
00064      * Initialize media endpoint.
00065      * This will implicitly initialize PJMEDIA too.
00066      */
00067     status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
00068     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00069 
00070     /* Create memory pool for our file player */
00071     pool = pj_pool_create( &cp.factory,     /* pool factory         */
00072                            "app",           /* pool name.           */
00073                            4000,            /* init size            */
00074                            4000,            /* increment size       */
00075                            NULL             /* callback on error    */
00076                            );
00077 
00078     status = pjmedia_tonegen_create(pool, 8000, 1, SAMPLES_PER_FRAME, 16, 0, &port);
00079     if (status != PJ_SUCCESS)
00080         return 1;
00081 
00082     {
00083         pjmedia_tone_desc tones[3];
00084 
00085         tones[0].freq1 = 200;
00086         tones[0].freq2 = 0;
00087         tones[0].on_msec = ON_DURATION;
00088         tones[0].off_msec = OFF_DURATION;
00089 
00090         tones[1].freq1 = 400;
00091         tones[1].freq2 = 0;
00092         tones[1].on_msec = ON_DURATION;
00093         tones[1].off_msec = OFF_DURATION;
00094 
00095         tones[2].freq1 = 800;
00096         tones[2].freq2 = 0;
00097         tones[2].on_msec = ON_DURATION;
00098         tones[2].off_msec = OFF_DURATION;
00099 
00100         status = pjmedia_tonegen_play(port, 3, tones, 0);
00101         PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
00102     }
00103 
00104     {
00105         pjmedia_tone_digit digits[2];
00106 
00107         digits[0].digit = '0';
00108         digits[0].on_msec = ON_DURATION;
00109         digits[0].off_msec = OFF_DURATION;
00110 
00111         digits[1].digit = '0';
00112         digits[1].on_msec = ON_DURATION;
00113         digits[1].off_msec = OFF_DURATION;
00114 
00115         status = pjmedia_tonegen_play_digits(port, 2, digits, 0);
00116         PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
00117     }
00118 
00119     {
00120         pjmedia_frame frm;
00121         FILE *f;
00122         void *buf;
00123 
00124         buf = pj_pool_alloc(pool, 2*8000);
00125         frm.buf = buf;
00126 
00127         f = fopen("tonegen.pcm", "wb");
00128 
00129         for (i=0; i<8000/SAMPLES_PER_FRAME; ++i) {
00130             int count;
00131             pjmedia_port_get_frame(port, &frm);
00132             count = fwrite(buf, SAMPLES_PER_FRAME, 2, f);
00133             if (count != 2)
00134                 break;
00135         }
00136 
00137         pj_assert(pjmedia_tonegen_is_busy(port) == 0);
00138         fclose(f);
00139     }
00140 
00141     /* Delete port */
00142     pjmedia_port_destroy(port);
00143 
00144     /* Release application pool */
00145     pj_pool_release( pool );
00146 
00147     /* Destroy media endpoint. */
00148     pjmedia_endpt_destroy( med_endpt );
00149 
00150     /* Destroy pool factory */
00151     pj_caching_pool_destroy( &cp );
00152 
00153     /* Shutdown PJLIB */
00154     pj_shutdown();
00155 
00156 
00157     /* Done. */
00158     return 0;
00159 }

 


PJMEDIA small footprint Open Source media stack
Copyright (C) 2006-2008 Teluu Inc.