blob: 27ea63182ae6f4444712f1c4bc15465ace05b5e6 [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 */
Benny Prijono5ee1f2e2006-09-22 20:43:00 +000019
20/**
21 * \page page_pjmedia_samples_tonegen_c Samples: Sine Wave/Dual-Tone Generation
22 *
23 * This is a simple program to generate a tone and write the samples to
24 * a raw PCM file. The main purpose of this file is to analyze the
25 * quality of the tones/sine wave generated by PJMEDIA tone/sine wave
26 * generator.
27 *
28 * This file is pjsip-apps/src/samples/tonegen.c
29 *
30 * \includelineno tonegen.c
31 */
32
33
Benny Prijono7f5c12c2006-09-02 23:45:18 +000034#include <pjmedia.h>
35#include <pjlib.h>
36
37#define SAMPLES_PER_FRAME 64
38#define ON_DURATION 100
39#define OFF_DURATION 100
40
41
42/*
43 * main()
44 */
45int main()
46{
47 pj_caching_pool cp;
48 pjmedia_endpt *med_endpt;
49 pj_pool_t *pool;
50 pjmedia_port *port;
51 unsigned i;
52 pj_status_t status;
53
54
55 /* Must init PJLIB first: */
56 status = pj_init();
57 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
58
59 /* Must create a pool factory before we can allocate any memory. */
60 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
61
62 /*
63 * Initialize media endpoint.
64 * This will implicitly initialize PJMEDIA too.
65 */
66 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
67 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
68
69 /* Create memory pool for our file player */
70 pool = pj_pool_create( &cp.factory, /* pool factory */
71 "app", /* pool name. */
72 4000, /* init size */
73 4000, /* increment size */
74 NULL /* callback on error */
75 );
76
77 status = pjmedia_tonegen_create(pool, 8000, 1, SAMPLES_PER_FRAME, 16, 0, &port);
78 if (status != PJ_SUCCESS)
79 return 1;
80
81 {
82 pjmedia_tone_desc tones[3];
83
84 tones[0].freq1 = 200;
85 tones[0].freq2 = 0;
86 tones[0].on_msec = ON_DURATION;
87 tones[0].off_msec = OFF_DURATION;
88
89 tones[1].freq1 = 400;
90 tones[1].freq2 = 0;
91 tones[1].on_msec = ON_DURATION;
92 tones[1].off_msec = OFF_DURATION;
93
94 tones[2].freq1 = 800;
95 tones[2].freq2 = 0;
96 tones[2].on_msec = ON_DURATION;
97 tones[2].off_msec = OFF_DURATION;
98
99 status = pjmedia_tonegen_play(port, 3, tones, 0);
100 PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
101 }
102
103 {
104 pjmedia_tone_digit digits[2];
105
106 digits[0].digit = '0';
107 digits[0].on_msec = ON_DURATION;
108 digits[0].off_msec = OFF_DURATION;
109
110 digits[1].digit = '0';
111 digits[1].on_msec = ON_DURATION;
112 digits[1].off_msec = OFF_DURATION;
113
114 status = pjmedia_tonegen_play_digits(port, 2, digits, 0);
115 PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
116 }
117
118 {
119 pjmedia_frame frm;
120 FILE *f;
121 void *buf;
122
123 buf = pj_pool_alloc(pool, 2*8000);
124 frm.buf = buf;
125
126 f = fopen("tonegen.pcm", "wb");
127
128 for (i=0; i<8000/SAMPLES_PER_FRAME; ++i) {
129 pjmedia_port_get_frame(port, &frm);
130 fwrite(buf, SAMPLES_PER_FRAME, 2, f);
131 }
132
133 pj_assert(pjmedia_tonegen_is_busy(port) == 0);
134 fclose(f);
135 }
136
137 /* Delete port */
138 pjmedia_port_destroy(port);
139
140 /* Release application pool */
141 pj_pool_release( pool );
142
143 /* Destroy media endpoint. */
144 pjmedia_endpt_destroy( med_endpt );
145
146 /* Destroy pool factory */
147 pj_caching_pool_destroy( &cp );
148
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000149 /* Shutdown PJLIB */
150 pj_shutdown();
151
Benny Prijono7f5c12c2006-09-02 23:45:18 +0000152
153 /* Done. */
154 return 0;
155}