WARNING: The online documentation has moved to https://docs.pjsip.org.

Visit the new documentation at https://docs.pjsip.org:

BLOG | DOCUMENTATION | GITHUB

Home --> Documentations --> PJMEDIA Reference

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

1/*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 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
34#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{
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. */
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 pj_size_t count;
130 pjmedia_port_get_frame(port, &frm);
131 count = fwrite(buf, SAMPLES_PER_FRAME, 2, f);
132 if (count != 2)
133 break;
134 }
135
137 fclose(f);
138 }
139
140 /* Delete port */
142
143 /* Release application pool */
144 pj_pool_release( pool );
145
146 /* Destroy media endpoint. */
147 pjmedia_endpt_destroy( med_endpt );
148
149 /* Destroy pool factory */
151
152 /* Shutdown PJLIB */
153 pj_shutdown();
154
155
156 /* Done. */
157 return 0;
158}
pj_status_t pjmedia_tonegen_create(pj_pool_t *pool, unsigned clock_rate, unsigned channel_count, unsigned samples_per_frame, unsigned bits_per_sample, unsigned options, pjmedia_port **p_port)
pj_status_t pjmedia_tonegen_play(pjmedia_port *tonegen, unsigned count, const pjmedia_tone_desc tones[], unsigned options)
pj_bool_t pjmedia_tonegen_is_busy(pjmedia_port *tonegen)
pj_status_t pjmedia_tonegen_play_digits(pjmedia_port *tonegen, unsigned count, const pjmedia_tone_digit digits[], unsigned options)
pj_status_t pjmedia_port_destroy(pjmedia_port *port)
pj_status_t pjmedia_port_get_frame(pjmedia_port *port, pjmedia_frame *frame)
struct pjmedia_endpt pjmedia_endpt
Definition: pjmedia/types.h:186
pj_status_t pjmedia_endpt_create(pj_pool_factory *pf, pj_ioqueue_t *ioqueue, unsigned worker_cnt, pjmedia_endpt **p_endpt)
Definition: endpoint.h:127
pj_status_t pjmedia_endpt_destroy(pjmedia_endpt *endpt)
Definition: endpoint.h:168
pj_status_t pj_init(void)
size_t pj_size_t
int pj_status_t
void pj_shutdown(void)
PJ_SUCCESS
void pj_caching_pool_destroy(pj_caching_pool *ch_pool)
void pj_caching_pool_init(pj_caching_pool *ch_pool, const pj_pool_factory_policy *policy, pj_size_t max_capacity)
pj_pool_factory_policy pj_pool_factory_default_policy
void * pj_pool_alloc(pj_pool_t *pool, pj_size_t size)
pj_pool_t * pj_pool_create(pj_pool_factory *factory, const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback)
void pj_pool_release(pj_pool_t *pool)
#define pj_assert(expr)
#define PJ_ASSERT_RETURN(expr, retval)
PJMEDIA main header file.
pj_pool_factory factory
Definition: frame.h:56
void * buf
Definition: frame.h:58
Definition: port.h:378
Definition: tonegen.h:61
short on_msec
Definition: tonegen.h:64
short off_msec
Definition: tonegen.h:65
short freq1
Definition: tonegen.h:62
short freq2
Definition: tonegen.h:63
Definition: tonegen.h:78
short on_msec
Definition: tonegen.h:80
short off_msec
Definition: tonegen.h:81
char digit
Definition: tonegen.h:79

 


PJMEDIA small footprint Open Source media stack
Copyright (C) 2006-2008 Teluu Inc.