1
/* $Id: level.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
* \page page_pjmedia_samples_level_c Samples: Reading from WAV File
25
* This is a very simple example to use the @ref PJMEDIA_FILE_PLAY, to
26
* directly read the samples from the file.
28
* This file is pjsip-apps/src/samples/level.c
30
* \includelineno level.c
34
static const char *desc =
39
" Read PCM WAV file and display the audio level the first 100 frames. \n"
40
" Each frame is assumed to have 160 samples. \n"
45
" The WAV file SHOULD have a 16bit mono samples. ";
52
/* For logging purpose. */
53
#define THIS_FILE "level.c"
56
/* Util to display the error message for the specified error code */
57
static int app_perror( const char *sender, const char *title,
60
char errmsg[PJ_ERR_MSG_SIZE];
62
PJ_UNUSED_ARG(sender);
64
pj_strerror(status, errmsg, sizeof(errmsg));
66
printf("%s: %s [code=%d]\n", title, errmsg, status);
74
int main(int argc, char *argv[])
76
enum { NSAMPLES = 640, COUNT=100 };
78
pjmedia_endpt *med_endpt;
80
pjmedia_port *file_port;
85
/* Verify cmd line arguments. */
92
/* Must init PJLIB first: */
94
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
96
/* Must create a pool factory before we can allocate any memory. */
97
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
100
* Initialize media endpoint.
101
* This will implicitly initialize PJMEDIA too.
103
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
104
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
106
/* Create memory pool for our file player */
107
pool = pj_pool_create( &cp.factory, /* pool factory */
108
"wav", /* pool name. */
109
4000, /* init size */
110
4000, /* increment size */
111
NULL /* callback on error */
114
/* Create file media port from the WAV file */
115
status = pjmedia_wav_player_port_create( pool, /* memory pool */
116
argv[1], /* file to play */
117
0, /* use default ptime*/
119
0, /* default buffer */
120
&file_port/* returned port */
122
if (status != PJ_SUCCESS) {
123
app_perror(THIS_FILE, "Unable to use WAV file", status);
127
if (file_port->info.samples_per_frame > NSAMPLES) {
128
app_perror(THIS_FILE, "WAV clock rate is too big", PJ_EINVAL);
132
puts("Time\tPCMU\tLinear");
133
puts("------------------------");
135
for (i=0; i<COUNT; ++i) {
136
pj_int16_t framebuf[NSAMPLES];
143
frm.size = sizeof(framebuf);
145
pjmedia_port_get_frame(file_port, &frm);
147
level32 = pjmedia_calc_avg_signal(framebuf,
148
file_port->info.samples_per_frame);
149
level = pjmedia_linear2ulaw(level32) ^ 0xFF;
151
ms = i * 1000 * file_port->info.samples_per_frame /
152
file_port->info.clock_rate;
153
printf("%03d.%03d\t%7d\t%7d\n",
154
ms/1000, ms%1000, level, level32);
159
/* Destroy file port */
160
status = pjmedia_port_destroy( file_port );
161
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
163
/* Release application pool */
164
pj_pool_release( pool );
166
/* Destroy media endpoint. */
167
pjmedia_endpt_destroy( med_endpt );
169
/* Destroy pool factory */
170
pj_caching_pool_destroy( &cp );