BLOG | DOCUMENTATION | TRAC

Home --> Documentations --> PJMEDIA Reference

Samples: Reading from WAV File

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

1 /* $Id: level.c 3664 2011-07-19 03:42:28Z nanang $ */
2 /*
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5  *
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 
21 
34 static 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 */
57 static int app_perror( const char *sender, const char *title,
58  pj_status_t status)
59 {
60  char errmsg[PJ_ERR_MSG_SIZE];
61 
62  PJ_UNUSED_ARG(sender);
63 
64  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  */
74 int main(int argc, char *argv[])
75 {
76  enum { NSAMPLES = 640, COUNT=100 };
77  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. */
98 
99  /*
100  * Initialize media endpoint.
101  * This will implicitly initialize PJMEDIA too.
102  */
103  status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
104  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 */
115  status = pjmedia_wav_player_port_create( pool, /* memory pool */
116  argv[1], /* file to play */
117  0, /* use default ptime*/
118  0, /* flags */
119  0, /* default buffer */
120  &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 
127  if (PJMEDIA_PIA_SPF(&file_port->info) > NSAMPLES) {
128  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 
135  for (i=0; i<COUNT; ++i) {
136  pj_int16_t framebuf[NSAMPLES];
137  pjmedia_frame frm;
138  pj_int32_t level32;
139  unsigned ms;
140  int level;
141 
142  frm.buf = framebuf;
143  frm.size = sizeof(framebuf);
144 
145  pjmedia_port_get_frame(file_port, &frm);
146 
147  level32 = pjmedia_calc_avg_signal(framebuf,
148  PJMEDIA_PIA_SPF(&file_port->info));
149  level = pjmedia_linear2ulaw(level32) ^ 0xFF;
150 
151  ms = i * 1000 * PJMEDIA_PIA_SPF(&file_port->info) /
152  PJMEDIA_PIA_SRATE(&file_port->info);
153  printf("%03d.%03d\t%7d\t%7d\n",
154  ms/1000, ms%1000, level, level32);
155  }
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 */
171 
172  /* Shutdown PJLIB */
173  pj_shutdown();
174 
175 
176  /* Done. */
177  return 0;
178 }
179 
PJMEDIA main header file.
#define PJ_ASSERT_RETURN(expr, retval)
pj_status_t pjmedia_port_get_frame(pjmedia_port *port, pjmedia_frame *frame)
pj_size_t size
Definition: frame.h:60
PJ_BEGIN_DECL typedef int pj_int32_t
pjmedia_port_info info
Definition: port.h:366
void pj_pool_release(pj_pool_t *pool)
pj_status_t pjmedia_port_destroy(pjmedia_port *port)
struct pjmedia_endpt pjmedia_endpt
Definition: types.h:187
int pj_status_t
short pj_int16_t
pj_pool_factory_policy pj_pool_factory_default_policy
pj_int32_t pjmedia_calc_avg_signal(const pj_int16_t samples[], pj_size_t count)
Definition: port.h:364
pj_pool_factory factory
pj_status_t pjmedia_endpt_create(pj_pool_factory *pf, pj_ioqueue_t *ioqueue, unsigned worker_cnt, pjmedia_endpt **p_endpt)
Definition: endpoint.h:105
void pj_caching_pool_init(pj_caching_pool *ch_pool, const pj_pool_factory_policy *policy, pj_size_t max_capacity)
void * buf
Definition: frame.h:59
pj_str_t pj_strerror(pj_status_t statcode, char *buf, pj_size_t bufsize)
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)
unsigned PJMEDIA_PIA_SPF(const pjmedia_port_info *pia)
Definition: port.h:298
#define PJ_ERR_MSG_SIZE
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)
unsigned PJMEDIA_PIA_SRATE(const pjmedia_port_info *pia)
Definition: port.h:244
void pj_shutdown(void)
Definition: frame.h:56
PJ_SUCCESS
pj_status_t pj_init(void)
pj_status_t pjmedia_endpt_destroy(pjmedia_endpt *endpt)
Definition: endpoint.h:146
#define PJ_UNUSED_ARG(arg)
void pj_caching_pool_destroy(pj_caching_pool *ch_pool)

 


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