Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 1 | /* $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 | |
| 20 | static 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 */ |
| 43 | static int app_perror( const char *sender, const char *title, |
| 44 | pj_status_t status) |
| 45 | { |
| 46 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 47 | |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame^] | 48 | PJ_UNUSED_ARG(sender); |
| 49 | |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 50 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 51 | |
| 52 | printf("%s: %s [code=%d]\n", title, errmsg, status); |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /* |
| 58 | * main() |
| 59 | */ |
| 60 | int main(int argc, char *argv[]) |
| 61 | { |
| 62 | enum { NSAMPLES = 160, COUNT=100 }; |
| 63 | pj_caching_pool cp; |
| 64 | pjmedia_endpt *med_endpt; |
| 65 | pj_pool_t *pool; |
| 66 | pjmedia_port *file_port; |
| 67 | int i; |
| 68 | pj_status_t status; |
| 69 | |
| 70 | |
| 71 | /* Verify cmd line arguments. */ |
| 72 | if (argc != 2) { |
| 73 | puts(""); |
| 74 | puts(desc); |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | /* Must init PJLIB first: */ |
| 79 | status = pj_init(); |
| 80 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 81 | |
| 82 | /* Must create a pool factory before we can allocate any memory. */ |
| 83 | pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 84 | |
| 85 | /* |
| 86 | * Initialize media endpoint. |
| 87 | * This will implicitly initialize PJMEDIA too. |
| 88 | */ |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 89 | status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 90 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 91 | |
| 92 | /* Create memory pool for our file player */ |
| 93 | pool = pj_pool_create( &cp.factory, /* pool factory */ |
| 94 | "wav", /* pool name. */ |
| 95 | 4000, /* init size */ |
| 96 | 4000, /* increment size */ |
| 97 | NULL /* callback on error */ |
| 98 | ); |
| 99 | |
| 100 | /* Create file media port from the WAV file */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame^] | 101 | status = pjmedia_wav_player_port_create( pool, /* memory pool */ |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 102 | argv[1], /* file to play */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame^] | 103 | 0, /* use default ptime*/ |
Benny Prijono | c9446cb | 2006-03-20 19:00:37 +0000 | [diff] [blame] | 104 | 0, /* flags */ |
| 105 | 0, /* default buffer */ |
| 106 | NULL, /* user data */ |
| 107 | &file_port/* returned port */ |
| 108 | ); |
| 109 | if (status != PJ_SUCCESS) { |
| 110 | app_perror(THIS_FILE, "Unable to use WAV file", status); |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | for (i=0; i<COUNT; ++i) { |
| 115 | pj_int16_t framebuf[NSAMPLES]; |
| 116 | pjmedia_frame frm; |
| 117 | pj_int32_t level32; |
| 118 | int level; |
| 119 | |
| 120 | frm.buf = framebuf; |
| 121 | frm.size = sizeof(framebuf); |
| 122 | |
| 123 | pjmedia_port_get_frame(file_port, &frm); |
| 124 | |
| 125 | level32 = pjmedia_calc_avg_signal( framebuf, NSAMPLES ); |
| 126 | level = linear2ulaw(level32) ^ 0xFF; |
| 127 | |
| 128 | printf("%d ", level); |
| 129 | } |
| 130 | puts(""); |
| 131 | |
| 132 | |
| 133 | /* Destroy file port */ |
| 134 | status = pjmedia_port_destroy( file_port ); |
| 135 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 136 | |
| 137 | /* Release application pool */ |
| 138 | pj_pool_release( pool ); |
| 139 | |
| 140 | /* Destroy media endpoint. */ |
| 141 | pjmedia_endpt_destroy( med_endpt ); |
| 142 | |
| 143 | /* Destroy pool factory */ |
| 144 | pj_caching_pool_destroy( &cp ); |
| 145 | |
| 146 | |
| 147 | /* Done. */ |
| 148 | return 0; |
| 149 | } |
| 150 | |