Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 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 | |
Benny Prijono | 1ec70b3 | 2006-06-20 15:39:07 +0000 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * \page page_pjmedia_samples_level_c Samples: Reading from WAV File |
| 23 | * |
| 24 | * This is a very simple example to use the @ref PJMEDIA_FILE_PLAY, to |
| 25 | * directly read the samples from the file. |
| 26 | * |
| 27 | * This file is pjsip-apps/src/samples/level.c |
| 28 | * |
| 29 | * \includelineno level.c |
| 30 | */ |
| 31 | |
| 32 | |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 33 | static 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 */ |
| 56 | static int app_perror( const char *sender, const char *title, |
| 57 | pj_status_t status) |
| 58 | { |
| 59 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 60 | |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 61 | PJ_UNUSED_ARG(sender); |
| 62 | |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 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 | */ |
| 73 | int main(int argc, char *argv[]) |
| 74 | { |
| 75 | enum { NSAMPLES = 160, COUNT=100 }; |
| 76 | pj_caching_pool cp; |
| 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. */ |
| 96 | pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 97 | |
| 98 | /* |
| 99 | * Initialize media endpoint. |
| 100 | * This will implicitly initialize PJMEDIA too. |
| 101 | */ |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 102 | status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 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 */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 114 | status = pjmedia_wav_player_port_create( pool, /* memory pool */ |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 115 | argv[1], /* file to play */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 116 | 0, /* use default ptime*/ |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 117 | 0, /* flags */ |
| 118 | 0, /* default buffer */ |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 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 | for (i=0; i<COUNT; ++i) { |
| 127 | pj_int16_t framebuf[NSAMPLES]; |
| 128 | pjmedia_frame frm; |
| 129 | pj_int32_t level32; |
| 130 | int level; |
| 131 | |
| 132 | frm.buf = framebuf; |
| 133 | frm.size = sizeof(framebuf); |
| 134 | |
| 135 | pjmedia_port_get_frame(file_port, &frm); |
| 136 | |
| 137 | level32 = pjmedia_calc_avg_signal( framebuf, NSAMPLES ); |
Benny Prijono | bc96847 | 2006-11-30 01:35:00 +0000 | [diff] [blame] | 138 | level = pjmedia_linear2ulaw(level32) ^ 0xFF; |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 139 | |
| 140 | printf("%d ", level); |
| 141 | } |
| 142 | puts(""); |
| 143 | |
| 144 | |
| 145 | /* Destroy file port */ |
| 146 | status = pjmedia_port_destroy( file_port ); |
| 147 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 148 | |
| 149 | /* Release application pool */ |
| 150 | pj_pool_release( pool ); |
| 151 | |
| 152 | /* Destroy media endpoint. */ |
| 153 | pjmedia_endpt_destroy( med_endpt ); |
| 154 | |
| 155 | /* Destroy pool factory */ |
| 156 | pj_caching_pool_destroy( &cp ); |
| 157 | |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 158 | /* Shutdown PJLIB */ |
| 159 | pj_shutdown(); |
| 160 | |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 161 | |
| 162 | /* Done. */ |
| 163 | return 0; |
| 164 | } |
| 165 | |