blob: 6bba3fe1b8f67517cfed9d9ca6242c6202dbc4c1 [file] [log] [blame]
Benny Prijonoc9446cb2006-03-20 19:00:37 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonoc9446cb2006-03-20 19:00:37 +00005 *
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.
10 *
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.
15 *
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
19 */
20
Benny Prijono1ec70b32006-06-20 15:39:07 +000021
22/**
23 * \page page_pjmedia_samples_level_c Samples: Reading from WAV File
24 *
25 * This is a very simple example to use the @ref PJMEDIA_FILE_PLAY, to
26 * directly read the samples from the file.
27 *
28 * This file is pjsip-apps/src/samples/level.c
29 *
30 * \includelineno level.c
31 */
32
33
Benny Prijonoc9446cb2006-03-20 19:00:37 +000034static const char *desc =
35 " FILE: \n"
36 " level.c \n"
37 " \n"
38 " PURPOSE: \n"
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"
41 " \n"
42 " USAGE: \n"
43 " level file.wav \n"
44 " \n"
45 " The WAV file SHOULD have a 16bit mono samples. ";
46
47#include <pjmedia.h>
48#include <pjlib.h>
49
50#include <stdio.h>
51
52/* For logging purpose. */
53#define THIS_FILE "level.c"
54
55
56/* Util to display the error message for the specified error code */
57static int app_perror( const char *sender, const char *title,
58 pj_status_t status)
59{
60 char errmsg[PJ_ERR_MSG_SIZE];
61
Benny Prijono15953012006-04-27 22:37:08 +000062 PJ_UNUSED_ARG(sender);
63
Benny Prijonoc9446cb2006-03-20 19:00:37 +000064 pj_strerror(status, errmsg, sizeof(errmsg));
65
66 printf("%s: %s [code=%d]\n", title, errmsg, status);
67 return 1;
68}
69
70
71/*
72 * main()
73 */
74int main(int argc, char *argv[])
75{
Benny Prijono6c7cf562008-11-19 14:48:48 +000076 enum { NSAMPLES = 640, COUNT=100 };
Benny Prijonoc9446cb2006-03-20 19:00:37 +000077 pj_caching_pool cp;
78 pjmedia_endpt *med_endpt;
79 pj_pool_t *pool;
80 pjmedia_port *file_port;
81 int i;
82 pj_status_t status;
83
84
85 /* Verify cmd line arguments. */
86 if (argc != 2) {
87 puts("");
88 puts(desc);
89 return 1;
90 }
91
92 /* Must init PJLIB first: */
93 status = pj_init();
94 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
95
96 /* Must create a pool factory before we can allocate any memory. */
97 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
98
99 /*
100 * Initialize media endpoint.
101 * This will implicitly initialize PJMEDIA too.
102 */
Benny Prijono275fd682006-03-22 11:59:11 +0000103 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
Benny Prijonoc9446cb2006-03-20 19:00:37 +0000104 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
105
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 */
112 );
113
114 /* Create file media port from the WAV file */
Benny Prijono15953012006-04-27 22:37:08 +0000115 status = pjmedia_wav_player_port_create( pool, /* memory pool */
Benny Prijonoc9446cb2006-03-20 19:00:37 +0000116 argv[1], /* file to play */
Benny Prijono15953012006-04-27 22:37:08 +0000117 0, /* use default ptime*/
Benny Prijonoc9446cb2006-03-20 19:00:37 +0000118 0, /* flags */
119 0, /* default buffer */
Benny Prijonoc9446cb2006-03-20 19:00:37 +0000120 &file_port/* returned port */
121 );
122 if (status != PJ_SUCCESS) {
123 app_perror(THIS_FILE, "Unable to use WAV file", status);
124 return 1;
125 }
126
Benny Prijonoc45d9512010-12-10 11:04:30 +0000127 if (PJMEDIA_PIA_SPF(&file_port->info) > NSAMPLES) {
Benny Prijono6c7cf562008-11-19 14:48:48 +0000128 app_perror(THIS_FILE, "WAV clock rate is too big", PJ_EINVAL);
129 return 1;
130 }
131
132 puts("Time\tPCMU\tLinear");
133 puts("------------------------");
134
Benny Prijonoc9446cb2006-03-20 19:00:37 +0000135 for (i=0; i<COUNT; ++i) {
136 pj_int16_t framebuf[NSAMPLES];
137 pjmedia_frame frm;
138 pj_int32_t level32;
Benny Prijono6c7cf562008-11-19 14:48:48 +0000139 unsigned ms;
Benny Prijonoc9446cb2006-03-20 19:00:37 +0000140 int level;
141
142 frm.buf = framebuf;
143 frm.size = sizeof(framebuf);
144
145 pjmedia_port_get_frame(file_port, &frm);
146
Benny Prijono6c7cf562008-11-19 14:48:48 +0000147 level32 = pjmedia_calc_avg_signal(framebuf,
Benny Prijonoc45d9512010-12-10 11:04:30 +0000148 PJMEDIA_PIA_SPF(&file_port->info));
Benny Prijonobc968472006-11-30 01:35:00 +0000149 level = pjmedia_linear2ulaw(level32) ^ 0xFF;
Benny Prijonoc9446cb2006-03-20 19:00:37 +0000150
Benny Prijonoc45d9512010-12-10 11:04:30 +0000151 ms = i * 1000 * PJMEDIA_PIA_SPF(&file_port->info) /
152 PJMEDIA_PIA_SRATE(&file_port->info);
Benny Prijono6c7cf562008-11-19 14:48:48 +0000153 printf("%03d.%03d\t%7d\t%7d\n",
154 ms/1000, ms%1000, level, level32);
Benny Prijonoc9446cb2006-03-20 19:00:37 +0000155 }
156 puts("");
157
158
159 /* Destroy file port */
160 status = pjmedia_port_destroy( file_port );
161 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
162
163 /* Release application pool */
164 pj_pool_release( pool );
165
166 /* Destroy media endpoint. */
167 pjmedia_endpt_destroy( med_endpt );
168
169 /* Destroy pool factory */
170 pj_caching_pool_destroy( &cp );
171
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000172 /* Shutdown PJLIB */
173 pj_shutdown();
174
Benny Prijonoc9446cb2006-03-20 19:00:37 +0000175
176 /* Done. */
177 return 0;
178}
179