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
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00053 #define THIS_FILE "level.c"
00054
00055
00056
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
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
00086 if (argc != 2) {
00087 puts("");
00088 puts(desc);
00089 return 1;
00090 }
00091
00092
00093 status = pj_init();
00094 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00095
00096
00097 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
00098
00099
00100
00101
00102
00103 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
00104 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00105
00106
00107 pool = pj_pool_create( &cp.factory,
00108 "wav",
00109 4000,
00110 4000,
00111 NULL
00112 );
00113
00114
00115 status = pjmedia_wav_player_port_create( pool,
00116 argv[1],
00117 0,
00118 0,
00119 0,
00120 &file_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
00160 status = pjmedia_port_destroy( file_port );
00161 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
00162
00163
00164 pj_pool_release( pool );
00165
00166
00167 pjmedia_endpt_destroy( med_endpt );
00168
00169
00170 pj_caching_pool_destroy( &cp );
00171
00172
00173 pj_shutdown();
00174
00175
00176
00177 return 0;
00178 }
00179