BLOG | DOCUMENTATION | TRAC

Home --> Documentations --> PJMEDIA Reference

Samples: Reading from WAV File

This is a very simple example to use the AVI File Player, to directly read the samples from the file.

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

00001 /* $Id: level.c 3664 2011-07-19 03:42:28Z 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 
00021 
00034 static const char *desc = 
00035  " FILE:                                                                    \n"
00036  "  level.c                                                                 \n"
00037  "                                                                          \n"
00038  " PURPOSE:                                                                 \n"
00039  "  Read PCM WAV file and display the audio level the first 100 frames.     \n"
00040  "  Each frame is assumed to have 160 samples.                              \n"
00041  "                                                                          \n"
00042  " USAGE:                                                                   \n"
00043  "  level file.wav                                                          \n"
00044  "                                                                          \n"
00045  "  The WAV file SHOULD have a 16bit mono samples.                          ";
00046 
00047 #include <pjmedia.h>
00048 #include <pjlib.h>
00049 
00050 #include <stdio.h>
00051 
00052 /* For logging purpose. */
00053 #define THIS_FILE   "level.c"
00054 
00055 
00056 /* Util to display the error message for the specified error code  */
00057 static int app_perror( const char *sender, const char *title, 
00058                        pj_status_t status)
00059 {
00060     char errmsg[PJ_ERR_MSG_SIZE];
00061 
00062     PJ_UNUSED_ARG(sender);
00063 
00064     pj_strerror(status, errmsg, sizeof(errmsg));
00065 
00066     printf("%s: %s [code=%d]\n", title, errmsg, status);
00067     return 1;
00068 }
00069 
00070 
00071 /*
00072  * main()
00073  */
00074 int main(int argc, char *argv[])
00075 {
00076     enum { NSAMPLES = 640, COUNT=100 };
00077     pj_caching_pool cp;
00078     pjmedia_endpt *med_endpt;
00079     pj_pool_t *pool;
00080     pjmedia_port *file_port;
00081     int i;
00082     pj_status_t status;
00083 
00084 
00085     /* Verify cmd line arguments. */
00086     if (argc != 2) {
00087         puts("");
00088         puts(desc);
00089         return 1;
00090     }
00091 
00092     /* Must init PJLIB first: */
00093     status = pj_init();
00094     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00095 
00096     /* Must create a pool factory before we can allocate any memory. */
00097     pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
00098 
00099     /* 
00100      * Initialize media endpoint.
00101      * This will implicitly initialize PJMEDIA too.
00102      */
00103     status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
00104     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00105 
00106     /* Create memory pool for our file player */
00107     pool = pj_pool_create( &cp.factory,     /* pool factory         */
00108                            "wav",           /* pool name.           */
00109                            4000,            /* init size            */
00110                            4000,            /* increment size       */
00111                            NULL             /* callback on error    */
00112                            );
00113 
00114     /* Create file media port from the WAV file */
00115     status = pjmedia_wav_player_port_create(  pool,     /* memory pool      */
00116                                               argv[1],  /* file to play     */
00117                                               0,        /* use default ptime*/
00118                                               0,        /* flags            */
00119                                               0,        /* default buffer   */
00120                                               &file_port/* returned port    */
00121                                               );
00122     if (status != PJ_SUCCESS) {
00123         app_perror(THIS_FILE, "Unable to use WAV file", status);
00124         return 1;
00125     }
00126 
00127     if (PJMEDIA_PIA_SPF(&file_port->info) > NSAMPLES) {
00128         app_perror(THIS_FILE, "WAV clock rate is too big", PJ_EINVAL);
00129         return 1;
00130     }
00131 
00132     puts("Time\tPCMU\tLinear");
00133     puts("------------------------");
00134 
00135     for (i=0; i<COUNT; ++i) {
00136         pj_int16_t framebuf[NSAMPLES];
00137         pjmedia_frame frm;
00138         pj_int32_t level32;
00139         unsigned ms;
00140         int level;
00141 
00142         frm.buf = framebuf;
00143         frm.size = sizeof(framebuf);
00144         
00145         pjmedia_port_get_frame(file_port, &frm);
00146 
00147         level32 = pjmedia_calc_avg_signal(framebuf, 
00148                                           PJMEDIA_PIA_SPF(&file_port->info));
00149         level = pjmedia_linear2ulaw(level32) ^ 0xFF;
00150 
00151         ms = i * 1000 * PJMEDIA_PIA_SPF(&file_port->info) /
00152                 PJMEDIA_PIA_SRATE(&file_port->info);
00153         printf("%03d.%03d\t%7d\t%7d\n", 
00154                 ms/1000, ms%1000, level, level32);
00155     }
00156     puts("");
00157     
00158 
00159     /* Destroy file port */
00160     status = pjmedia_port_destroy( file_port );
00161     PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00162 
00163     /* Release application pool */
00164     pj_pool_release( pool );
00165 
00166     /* Destroy media endpoint. */
00167     pjmedia_endpt_destroy( med_endpt );
00168 
00169     /* Destroy pool factory */
00170     pj_caching_pool_destroy( &cp );
00171 
00172     /* Shutdown PJLIB */
00173     pj_shutdown();
00174 
00175 
00176     /* Done. */
00177     return 0;
00178 }
00179 

 


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