summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/samples/tonegen.c
blob: 51901865d563266d9cc88460c3ec373e3ad0f986 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* $Id: tonegen.c 3553 2011-05-05 06:14:19Z nanang $ */
/* 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

/**
 * \page page_pjmedia_samples_tonegen_c Samples: Sine Wave/Dual-Tone Generation
 *
 * This is a simple program to generate a tone and write the samples to
 * a raw PCM file. The main purpose of this file is to analyze the
 * quality of the tones/sine wave generated by PJMEDIA tone/sine wave
 * generator.
 *
 * This file is pjsip-apps/src/samples/tonegen.c
 *
 * \includelineno tonegen.c
 */


#include <pjmedia.h>
#include <pjlib.h>

#define SAMPLES_PER_FRAME   64
#define ON_DURATION	    100
#define OFF_DURATION	    100


/*
 * main()
 */
int main()
{
    pj_caching_pool cp;
    pjmedia_endpt *med_endpt;
    pj_pool_t *pool;
    pjmedia_port *port;
    unsigned i;
    pj_status_t status;


    /* Must init PJLIB first: */
    status = pj_init();
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);

    /* Must create a pool factory before we can allocate any memory. */
    pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);

    /* 
     * Initialize media endpoint.
     * This will implicitly initialize PJMEDIA too.
     */
    status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
    PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);

    /* Create memory pool for our file player */
    pool = pj_pool_create( &cp.factory,	    /* pool factory	    */
			   "app",	    /* pool name.	    */
			   4000,	    /* init size	    */
			   4000,	    /* increment size	    */
			   NULL		    /* callback on error    */
			   );

    status = pjmedia_tonegen_create(pool, 8000, 1, SAMPLES_PER_FRAME, 16, 0, &port);
    if (status != PJ_SUCCESS)
	return 1;

    {
	pjmedia_tone_desc tones[3];

	tones[0].freq1 = 200;
	tones[0].freq2 = 0;
	tones[0].on_msec = ON_DURATION;
	tones[0].off_msec = OFF_DURATION;

	tones[1].freq1 = 400;
	tones[1].freq2 = 0;
	tones[1].on_msec = ON_DURATION;
	tones[1].off_msec = OFF_DURATION;

	tones[2].freq1 = 800;
	tones[2].freq2 = 0;
	tones[2].on_msec = ON_DURATION;
	tones[2].off_msec = OFF_DURATION;

	status = pjmedia_tonegen_play(port, 3, tones, 0);
	PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
    }

    {
	pjmedia_tone_digit digits[2];

	digits[0].digit = '0';
	digits[0].on_msec = ON_DURATION;
	digits[0].off_msec = OFF_DURATION;

	digits[1].digit = '0';
	digits[1].on_msec = ON_DURATION;
	digits[1].off_msec = OFF_DURATION;

	status = pjmedia_tonegen_play_digits(port, 2, digits, 0);
	PJ_ASSERT_RETURN(status==PJ_SUCCESS, 1);
    }

    {
	pjmedia_frame frm;
	FILE *f;
	void *buf;

	buf = pj_pool_alloc(pool, 2*8000);
	frm.buf = buf;

	f = fopen("tonegen.pcm", "wb");

	for (i=0; i<8000/SAMPLES_PER_FRAME; ++i) {
	    int count;
	    pjmedia_port_get_frame(port, &frm);
	    count = fwrite(buf, SAMPLES_PER_FRAME, 2, f);
	    if (count != 2)
		break;
	}

	pj_assert(pjmedia_tonegen_is_busy(port) == 0);
	fclose(f);
    }

    /* Delete port */
    pjmedia_port_destroy(port);

    /* Release application pool */
    pj_pool_release( pool );

    /* Destroy media endpoint. */
    pjmedia_endpt_destroy( med_endpt );

    /* Destroy pool factory */
    pj_caching_pool_destroy( &cp );

    /* Shutdown PJLIB */
    pj_shutdown();


    /* Done. */
    return 0;
}