blob: 8723d62f80c5340fb0c07ed055fbd5b7d95c0770 [file] [log] [blame]
Benny Prijonoc9446cb2006-03-20 19:00:37 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20static const char *desc =
21 " FILE: \n"
22 " level.c \n"
23 " \n"
24 " PURPOSE: \n"
25 " Read PCM WAV file and display the audio level the first 100 frames. \n"
26 " Each frame is assumed to have 160 samples. \n"
27 " \n"
28 " USAGE: \n"
29 " level file.wav \n"
30 " \n"
31 " The WAV file SHOULD have a 16bit mono samples. ";
32
33#include <pjmedia.h>
34#include <pjlib.h>
35
36#include <stdio.h>
37
38/* For logging purpose. */
39#define THIS_FILE "level.c"
40
41
42/* Util to display the error message for the specified error code */
43static int app_perror( const char *sender, const char *title,
44 pj_status_t status)
45{
46 char errmsg[PJ_ERR_MSG_SIZE];
47
48 pj_strerror(status, errmsg, sizeof(errmsg));
49
50 printf("%s: %s [code=%d]\n", title, errmsg, status);
51 return 1;
52}
53
54
55/*
56 * main()
57 */
58int main(int argc, char *argv[])
59{
60 enum { NSAMPLES = 160, COUNT=100 };
61 pj_caching_pool cp;
62 pjmedia_endpt *med_endpt;
63 pj_pool_t *pool;
64 pjmedia_port *file_port;
65 int i;
66 pj_status_t status;
67
68
69 /* Verify cmd line arguments. */
70 if (argc != 2) {
71 puts("");
72 puts(desc);
73 return 1;
74 }
75
76 /* Must init PJLIB first: */
77 status = pj_init();
78 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
79
80 /* Must create a pool factory before we can allocate any memory. */
81 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
82
83 /*
84 * Initialize media endpoint.
85 * This will implicitly initialize PJMEDIA too.
86 */
Benny Prijono275fd682006-03-22 11:59:11 +000087 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
Benny Prijonoc9446cb2006-03-20 19:00:37 +000088 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
89
90 /* Create memory pool for our file player */
91 pool = pj_pool_create( &cp.factory, /* pool factory */
92 "wav", /* pool name. */
93 4000, /* init size */
94 4000, /* increment size */
95 NULL /* callback on error */
96 );
97
98 /* Create file media port from the WAV file */
99 status = pjmedia_file_player_port_create( pool, /* memory pool */
100 argv[1], /* file to play */
101 0, /* flags */
102 0, /* default buffer */
103 NULL, /* user data */
104 &file_port/* returned port */
105 );
106 if (status != PJ_SUCCESS) {
107 app_perror(THIS_FILE, "Unable to use WAV file", status);
108 return 1;
109 }
110
111 for (i=0; i<COUNT; ++i) {
112 pj_int16_t framebuf[NSAMPLES];
113 pjmedia_frame frm;
114 pj_int32_t level32;
115 int level;
116
117 frm.buf = framebuf;
118 frm.size = sizeof(framebuf);
119
120 pjmedia_port_get_frame(file_port, &frm);
121
122 level32 = pjmedia_calc_avg_signal( framebuf, NSAMPLES );
123 level = linear2ulaw(level32) ^ 0xFF;
124
125 printf("%d ", level);
126 }
127 puts("");
128
129
130 /* Destroy file port */
131 status = pjmedia_port_destroy( file_port );
132 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
133
134 /* Release application pool */
135 pj_pool_release( pool );
136
137 /* Destroy media endpoint. */
138 pjmedia_endpt_destroy( med_endpt );
139
140 /* Destroy pool factory */
141 pj_caching_pool_destroy( &cp );
142
143
144 /* Done. */
145 return 0;
146}
147