blob: de8925b1968b74331483e5ceb5db55bbea3da0ec [file] [log] [blame]
Benny Prijono7f5c12c2006-09-02 23:45:18 +00001/* $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#include <pjmedia.h>
20#include <pjlib.h>
21
22#define SAMPLES_PER_FRAME 64
23#define ON_DURATION 100
24#define OFF_DURATION 100
25
26
27/*
28 * main()
29 */
30int main()
31{
32 pj_caching_pool cp;
33 pjmedia_endpt *med_endpt;
34 pj_pool_t *pool;
35 pjmedia_port *port;
36 unsigned i;
37 pj_status_t status;
38
39
40 /* Must init PJLIB first: */
41 status = pj_init();
42 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
43
44 /* Must create a pool factory before we can allocate any memory. */
45 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
46
47 /*
48 * Initialize media endpoint.
49 * This will implicitly initialize PJMEDIA too.
50 */
51 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
52 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
53
54 /* Create memory pool for our file player */
55 pool = pj_pool_create( &cp.factory, /* pool factory */
56 "app", /* pool name. */
57 4000, /* init size */
58 4000, /* increment size */
59 NULL /* callback on error */
60 );
61
62 status = pjmedia_tonegen_create(pool, 8000, 1, SAMPLES_PER_FRAME, 16, 0, &port);
63 if (status != PJ_SUCCESS)
64 return 1;
65
66 {
67 pjmedia_tone_desc tones[3];
68
69 tones[0].freq1 = 200;
70 tones[0].freq2 = 0;
71 tones[0].on_msec = ON_DURATION;
72 tones[0].off_msec = OFF_DURATION;
73
74 tones[1].freq1 = 400;
75 tones[1].freq2 = 0;
76 tones[1].on_msec = ON_DURATION;
77 tones[1].off_msec = OFF_DURATION;
78
79 tones[2].freq1 = 800;
80 tones[2].freq2 = 0;
81 tones[2].on_msec = ON_DURATION;
82 tones[2].off_msec = OFF_DURATION;
83
84 status = pjmedia_tonegen_play(port, 3, tones, 0);
85 PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
86 }
87
88 {
89 pjmedia_tone_digit digits[2];
90
91 digits[0].digit = '0';
92 digits[0].on_msec = ON_DURATION;
93 digits[0].off_msec = OFF_DURATION;
94
95 digits[1].digit = '0';
96 digits[1].on_msec = ON_DURATION;
97 digits[1].off_msec = OFF_DURATION;
98
99 status = pjmedia_tonegen_play_digits(port, 2, digits, 0);
100 PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
101 }
102
103 {
104 pjmedia_frame frm;
105 FILE *f;
106 void *buf;
107
108 buf = pj_pool_alloc(pool, 2*8000);
109 frm.buf = buf;
110
111 f = fopen("tonegen.pcm", "wb");
112
113 for (i=0; i<8000/SAMPLES_PER_FRAME; ++i) {
114 pjmedia_port_get_frame(port, &frm);
115 fwrite(buf, SAMPLES_PER_FRAME, 2, f);
116 }
117
118 pj_assert(pjmedia_tonegen_is_busy(port) == 0);
119 fclose(f);
120 }
121
122 /* Delete port */
123 pjmedia_port_destroy(port);
124
125 /* Release application pool */
126 pj_pool_release( pool );
127
128 /* Destroy media endpoint. */
129 pjmedia_endpt_destroy( med_endpt );
130
131 /* Destroy pool factory */
132 pj_caching_pool_destroy( &cp );
133
134
135 /* Done. */
136 return 0;
137}