~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjsip-apps/src/samples/level.c

  • Committer: Jackson Doak
  • Date: 2013-07-10 21:04:46 UTC
  • mfrom: (20.1.3 sid)
  • Revision ID: noskcaj@ubuntu.com-20130710210446-y8f587vza807icr9
Properly merged from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: level.c 3553 2011-05-05 06:14:19Z 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
 
 
22
 
/**
23
 
 * \page page_pjmedia_samples_level_c Samples: Reading from WAV File
24
 
 *
25
 
 * This is a very simple example to use the @ref PJMEDIA_FILE_PLAY, to
26
 
 * directly read the samples from the file.
27
 
 *
28
 
 * This file is pjsip-apps/src/samples/level.c
29
 
 *
30
 
 * \includelineno level.c
31
 
 */
32
 
 
33
 
 
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. */
97
 
    pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
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 (file_port->info.samples_per_frame > 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
 
                                          file_port->info.samples_per_frame);
149
 
        level = pjmedia_linear2ulaw(level32) ^ 0xFF;
150
 
 
151
 
        ms = i * 1000 * file_port->info.samples_per_frame /
152
 
                        file_port->info.clock_rate;
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 */
170
 
    pj_caching_pool_destroy( &cp );
171
 
 
172
 
    /* Shutdown PJLIB */
173
 
    pj_shutdown();
174
 
 
175
 
 
176
 
    /* Done. */
177
 
    return 0;
178
 
}
179