blob: e3d5554c8f9e2745e47b11b9a1f28423e9b6b789 [file] [log] [blame]
Benny Prijono7f5c12c2006-09-02 23:45:18 +00001/* $Id$ */
2/*
Nanang Izzuddina62ffc92011-05-05 06:14:19 +00003 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono7f5c12c2006-09-02 23:45:18 +00005 *
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 */
Benny Prijono5ee1f2e2006-09-22 20:43:00 +000020
21/**
22 * \page page_pjmedia_samples_tonegen_c Samples: Sine Wave/Dual-Tone Generation
23 *
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
27 * generator.
28 *
29 * This file is pjsip-apps/src/samples/tonegen.c
30 *
31 * \includelineno tonegen.c
32 */
33
34
Benny Prijono7f5c12c2006-09-02 23:45:18 +000035#include <pjmedia.h>
36#include <pjlib.h>
37
38#define SAMPLES_PER_FRAME 64
39#define ON_DURATION 100
40#define OFF_DURATION 100
41
42
43/*
44 * main()
45 */
46int main()
47{
48 pj_caching_pool cp;
49 pjmedia_endpt *med_endpt;
50 pj_pool_t *pool;
51 pjmedia_port *port;
52 unsigned i;
53 pj_status_t status;
54
55
56 /* Must init PJLIB first: */
57 status = pj_init();
58 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
59
60 /* Must create a pool factory before we can allocate any memory. */
61 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
62
63 /*
64 * Initialize media endpoint.
65 * This will implicitly initialize PJMEDIA too.
66 */
67 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
68 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
69
70 /* Create memory pool for our file player */
71 pool = pj_pool_create( &cp.factory, /* pool factory */
72 "app", /* pool name. */
73 4000, /* init size */
74 4000, /* increment size */
75 NULL /* callback on error */
76 );
77
78 status = pjmedia_tonegen_create(pool, 8000, 1, SAMPLES_PER_FRAME, 16, 0, &port);
79 if (status != PJ_SUCCESS)
80 return 1;
81
82 {
83 pjmedia_tone_desc tones[3];
84
85 tones[0].freq1 = 200;
86 tones[0].freq2 = 0;
87 tones[0].on_msec = ON_DURATION;
88 tones[0].off_msec = OFF_DURATION;
89
90 tones[1].freq1 = 400;
91 tones[1].freq2 = 0;
92 tones[1].on_msec = ON_DURATION;
93 tones[1].off_msec = OFF_DURATION;
94
95 tones[2].freq1 = 800;
96 tones[2].freq2 = 0;
97 tones[2].on_msec = ON_DURATION;
98 tones[2].off_msec = OFF_DURATION;
99
100 status = pjmedia_tonegen_play(port, 3, tones, 0);
101 PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
102 }
103
104 {
105 pjmedia_tone_digit digits[2];
106
107 digits[0].digit = '0';
108 digits[0].on_msec = ON_DURATION;
109 digits[0].off_msec = OFF_DURATION;
110
111 digits[1].digit = '0';
112 digits[1].on_msec = ON_DURATION;
113 digits[1].off_msec = OFF_DURATION;
114
115 status = pjmedia_tonegen_play_digits(port, 2, digits, 0);
116 PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
117 }
118
119 {
120 pjmedia_frame frm;
121 FILE *f;
122 void *buf;
123
124 buf = pj_pool_alloc(pool, 2*8000);
125 frm.buf = buf;
126
127 f = fopen("tonegen.pcm", "wb");
128
129 for (i=0; i<8000/SAMPLES_PER_FRAME; ++i) {
Benny Prijono32d267b2009-01-01 22:08:21 +0000130 int count;
Benny Prijono7f5c12c2006-09-02 23:45:18 +0000131 pjmedia_port_get_frame(port, &frm);
Benny Prijono32d267b2009-01-01 22:08:21 +0000132 count = fwrite(buf, SAMPLES_PER_FRAME, 2, f);
133 if (count != 2)
134 break;
Benny Prijono7f5c12c2006-09-02 23:45:18 +0000135 }
136
137 pj_assert(pjmedia_tonegen_is_busy(port) == 0);
138 fclose(f);
139 }
140
141 /* Delete port */
142 pjmedia_port_destroy(port);
143
144 /* Release application pool */
145 pj_pool_release( pool );
146
147 /* Destroy media endpoint. */
148 pjmedia_endpt_destroy( med_endpt );
149
150 /* Destroy pool factory */
151 pj_caching_pool_destroy( &cp );
152
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000153 /* Shutdown PJLIB */
154 pj_shutdown();
155
Benny Prijono7f5c12c2006-09-02 23:45:18 +0000156
157 /* Done. */
158 return 0;
159}