WARNING: The online documentation has moved to https://docs.pjsip.org.

Visit the new documentation at https://docs.pjsip.org:

BLOG | DOCUMENTATION | GITHUB

Home --> Documentations --> PJMEDIA Reference

Samples: Reading from WAV File

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

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

1/*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 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
20
33static const char *desc =
34 " FILE: \n"
35 " level.c \n"
36 " \n"
37 " PURPOSE: \n"
38 " Read PCM WAV file and display the audio level the first 100 frames. \n"
39 " Each frame is assumed to have 160 samples. \n"
40 " \n"
41 " USAGE: \n"
42 " level file.wav \n"
43 " \n"
44 " The WAV file SHOULD have a 16bit mono samples. ";
45
46#include <pjmedia.h>
47#include <pjlib.h>
48
49#include <stdio.h>
50
51/* For logging purpose. */
52#define THIS_FILE "level.c"
53
54
55/* Util to display the error message for the specified error code */
56static int app_perror( const char *sender, const char *title,
57 pj_status_t status)
58{
59 char errmsg[PJ_ERR_MSG_SIZE];
60
61 PJ_UNUSED_ARG(sender);
62
63 pj_strerror(status, errmsg, sizeof(errmsg));
64
65 printf("%s: %s [code=%d]\n", title, errmsg, status);
66 return 1;
67}
68
69
70/*
71 * main()
72 */
73int main(int argc, char *argv[])
74{
75 enum { NSAMPLES = 640, COUNT=100 };
77 pjmedia_endpt *med_endpt;
78 pj_pool_t *pool;
79 pjmedia_port *file_port;
80 int i;
81 pj_status_t status;
82
83
84 /* Verify cmd line arguments. */
85 if (argc != 2) {
86 puts("");
87 puts(desc);
88 return 1;
89 }
90
91 /* Must init PJLIB first: */
92 status = pj_init();
93 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
94
95 /* Must create a pool factory before we can allocate any memory. */
97
98 /*
99 * Initialize media endpoint.
100 * This will implicitly initialize PJMEDIA too.
101 */
102 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
103 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
104
105 /* Create memory pool for our file player */
106 pool = pj_pool_create( &cp.factory, /* pool factory */
107 "wav", /* pool name. */
108 4000, /* init size */
109 4000, /* increment size */
110 NULL /* callback on error */
111 );
112
113 /* Create file media port from the WAV file */
114 status = pjmedia_wav_player_port_create( pool, /* memory pool */
115 argv[1], /* file to play */
116 0, /* use default ptime*/
117 0, /* flags */
118 0, /* default buffer */
119 &file_port/* returned port */
120 );
121 if (status != PJ_SUCCESS) {
122 app_perror(THIS_FILE, "Unable to use WAV file", status);
123 return 1;
124 }
125
126 if (PJMEDIA_PIA_SPF(&file_port->info) > NSAMPLES) {
127 app_perror(THIS_FILE, "WAV clock rate is too big", PJ_EINVAL);
128 return 1;
129 }
130
131 puts("Time\tPCMU\tLinear");
132 puts("------------------------");
133
134 for (i=0; i<COUNT; ++i) {
135 pj_int16_t framebuf[NSAMPLES];
136 pjmedia_frame frm;
137 pj_int32_t level32;
138 unsigned ms;
139 int level;
140
141 frm.buf = framebuf;
142 frm.size = sizeof(framebuf);
143
144 pjmedia_port_get_frame(file_port, &frm);
145
146 level32 = pjmedia_calc_avg_signal(framebuf,
147 PJMEDIA_PIA_SPF(&file_port->info));
148 level = pjmedia_linear2ulaw(level32) ^ 0xFF;
149
150 ms = i * 1000 * PJMEDIA_PIA_SPF(&file_port->info) /
151 PJMEDIA_PIA_SRATE(&file_port->info);
152 printf("%03d.%03d\t%7d\t%7d\n",
153 ms/1000, ms%1000, level, level32);
154 }
155 puts("");
156
157
158 /* Destroy file port */
159 status = pjmedia_port_destroy( file_port );
160 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
161
162 /* Release application pool */
163 pj_pool_release( pool );
164
165 /* Destroy media endpoint. */
166 pjmedia_endpt_destroy( med_endpt );
167
168 /* Destroy pool factory */
170
171 /* Shutdown PJLIB */
172 pj_shutdown();
173
174
175 /* Done. */
176 return 0;
177}
178
pj_status_t pjmedia_wav_player_port_create(pj_pool_t *pool, const char *filename, unsigned ptime, unsigned flags, pj_ssize_t buff_size, pjmedia_port **p_port)
pj_status_t pjmedia_port_destroy(pjmedia_port *port)
unsigned PJMEDIA_PIA_SPF(const pjmedia_port_info *pia)
Definition: port.h:311
pj_status_t pjmedia_port_get_frame(pjmedia_port *port, pjmedia_frame *frame)
unsigned PJMEDIA_PIA_SRATE(const pjmedia_port_info *pia)
Definition: port.h:257
pj_int32_t pjmedia_calc_avg_signal(const pj_int16_t samples[], pj_size_t count)
struct pjmedia_endpt pjmedia_endpt
Definition: pjmedia/types.h:186
pj_status_t pjmedia_endpt_create(pj_pool_factory *pf, pj_ioqueue_t *ioqueue, unsigned worker_cnt, pjmedia_endpt **p_endpt)
Definition: endpoint.h:127
pj_status_t pjmedia_endpt_destroy(pjmedia_endpt *endpt)
Definition: endpoint.h:168
pj_status_t pj_init(void)
short pj_int16_t
int pj_status_t
int pj_int32_t
void pj_shutdown(void)
PJ_SUCCESS
void pj_caching_pool_destroy(pj_caching_pool *ch_pool)
void pj_caching_pool_init(pj_caching_pool *ch_pool, const pj_pool_factory_policy *policy, pj_size_t max_capacity)
pj_pool_factory_policy pj_pool_factory_default_policy
pj_pool_t * pj_pool_create(pj_pool_factory *factory, const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback)
void pj_pool_release(pj_pool_t *pool)
#define PJ_ASSERT_RETURN(expr, retval)
#define PJ_UNUSED_ARG(arg)
#define PJ_ERR_MSG_SIZE
pj_str_t pj_strerror(pj_status_t statcode, char *buf, pj_size_t bufsize)
PJMEDIA main header file.
pj_pool_factory factory
Definition: frame.h:56
void * buf
Definition: frame.h:58
pj_size_t size
Definition: frame.h:59
Definition: port.h:378
pjmedia_port_info info
Definition: port.h:379

 


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