1
/* $Id: tonegen.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
22
* \page page_pjmedia_samples_tonegen_c Samples: Sine Wave/Dual-Tone Generation
24
* This is a simple program to generate a tone and write the samples to
25
* a raw PCM file. The main purpose of this file is to analyze the
26
* quality of the tones/sine wave generated by PJMEDIA tone/sine wave
29
* This file is pjsip-apps/src/samples/tonegen.c
31
* \includelineno tonegen.c
38
#define SAMPLES_PER_FRAME 64
39
#define ON_DURATION 100
40
#define OFF_DURATION 100
49
pjmedia_endpt *med_endpt;
56
/* Must init PJLIB first: */
58
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
60
/* Must create a pool factory before we can allocate any memory. */
61
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
64
* Initialize media endpoint.
65
* This will implicitly initialize PJMEDIA too.
67
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
68
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
70
/* Create memory pool for our file player */
71
pool = pj_pool_create( &cp.factory, /* pool factory */
72
"app", /* pool name. */
74
4000, /* increment size */
75
NULL /* callback on error */
78
status = pjmedia_tonegen_create(pool, 8000, 1, SAMPLES_PER_FRAME, 16, 0, &port);
79
if (status != PJ_SUCCESS)
83
pjmedia_tone_desc tones[3];
87
tones[0].on_msec = ON_DURATION;
88
tones[0].off_msec = OFF_DURATION;
92
tones[1].on_msec = ON_DURATION;
93
tones[1].off_msec = OFF_DURATION;
97
tones[2].on_msec = ON_DURATION;
98
tones[2].off_msec = OFF_DURATION;
100
status = pjmedia_tonegen_play(port, 3, tones, 0);
101
PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
105
pjmedia_tone_digit digits[2];
107
digits[0].digit = '0';
108
digits[0].on_msec = ON_DURATION;
109
digits[0].off_msec = OFF_DURATION;
111
digits[1].digit = '0';
112
digits[1].on_msec = ON_DURATION;
113
digits[1].off_msec = OFF_DURATION;
115
status = pjmedia_tonegen_play_digits(port, 2, digits, 0);
116
PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
124
buf = pj_pool_alloc(pool, 2*8000);
127
f = fopen("tonegen.pcm", "wb");
129
for (i=0; i<8000/SAMPLES_PER_FRAME; ++i) {
131
pjmedia_port_get_frame(port, &frm);
132
count = fwrite(buf, SAMPLES_PER_FRAME, 2, f);
137
pj_assert(pjmedia_tonegen_is_busy(port) == 0);
142
pjmedia_port_destroy(port);
144
/* Release application pool */
145
pj_pool_release( pool );
147
/* Destroy media endpoint. */
148
pjmedia_endpt_destroy( med_endpt );
150
/* Destroy pool factory */
151
pj_caching_pool_destroy( &cp );