BLOG | DOCUMENTATION | TRAC

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 /* $Id: tonegen.c 4537 2013-06-19 06:47:43Z riza $ */
2 /*
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5  *
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  */
20 
35 #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  */
46 int 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. */
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) {
130  pj_size_t count;
131  pjmedia_port_get_frame(port, &frm);
132  count = fwrite(buf, SAMPLES_PER_FRAME, 2, f);
133  if (count != 2)
134  break;
135  }
136 
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 */
152 
153  /* Shutdown PJLIB */
154  pj_shutdown();
155 
156 
157  /* Done. */
158  return 0;
159 }
PJMEDIA main header file.
#define PJ_ASSERT_RETURN(expr, retval)
pj_status_t pjmedia_port_get_frame(pjmedia_port *port, pjmedia_frame *frame)
void pj_pool_release(pj_pool_t *pool)
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)
struct pjmedia_endpt pjmedia_endpt
Definition: types.h:187
Definition: tonegen.h:78
short freq1
Definition: tonegen.h:63
short on_msec
Definition: tonegen.h:65
pj_bool_t pjmedia_tonegen_is_busy(pjmedia_port *tonegen)
pj_status_t pjmedia_tonegen_play(pjmedia_port *tonegen, unsigned count, const pjmedia_tone_desc tones[], unsigned options)
int pj_status_t
pj_pool_factory_policy pj_pool_factory_default_policy
void * pj_pool_alloc(pj_pool_t *pool, pj_size_t size)
Definition: port.h:364
short on_msec
Definition: tonegen.h:81
pj_pool_factory factory
short off_msec
Definition: tonegen.h:66
#define pj_assert(expr)
pj_status_t pjmedia_endpt_create(pj_pool_factory *pf, pj_ioqueue_t *ioqueue, unsigned worker_cnt, pjmedia_endpt **p_endpt)
Definition: endpoint.h:105
void pj_caching_pool_init(pj_caching_pool *ch_pool, const pj_pool_factory_policy *policy, pj_size_t max_capacity)
void * buf
Definition: frame.h:59
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)
char digit
Definition: tonegen.h:80
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_shutdown(void)
Definition: frame.h:56
short off_msec
Definition: tonegen.h:82
PJ_SUCCESS
Definition: tonegen.h:61
pj_status_t pj_init(void)
pj_status_t pjmedia_endpt_destroy(pjmedia_endpt *endpt)
Definition: endpoint.h:146
size_t pj_size_t
short freq2
Definition: tonegen.h:64
void pj_caching_pool_destroy(pj_caching_pool *ch_pool)

 


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