blob: e03b0d5d231bab5c71c7deb9320ef837a0354acb [file] [log] [blame]
Benny Prijonocbc1c472006-03-19 00:50:23 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonocbc1c472006-03-19 00:50:23 +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 */
20
Benny Prijono1ec70b32006-06-20 15:39:07 +000021
22/**
23 * \page page_pjmedia_samples_playsine_c Samples: Using Custom Ports (Sine Wave Generator)
24 *
25 * This example demonstrate how to create a custom media port (in this case, a
26 * sine wave generator) and connect it to the sound device.
27 *
28 * This file is pjsip-apps/src/samples/playsine.c
29 *
30 * \includelineno playsine.c
31 */
32
Benny Prijonocbc1c472006-03-19 00:50:23 +000033/*
34 * playsine.c
35 *
36 * PURPOSE:
37 * Demonstrate how to create and use custom media port which
38 * simply feed a sine wav to the sound player.
39 *
40 * USAGE:
41 * playsine [nchannel]
42 *
43 * where:
44 * nchannel is 1 for mono (this is the default) or 2 for stereo.
45 */
46
47#include <pjmedia.h>
48#include <pjlib.h>
49
50#include <stdlib.h> /* atoi() */
51#include <stdio.h>
Benny Prijonof6e77642008-02-07 11:55:05 +000052#include <math.h> /* sin() */
Benny Prijonocbc1c472006-03-19 00:50:23 +000053
54/* For logging purpose. */
55#define THIS_FILE "playsine.c"
56
57
58/* Util to display the error message for the specified error code */
59static int app_perror( const char *sender, const char *title,
60 pj_status_t status)
61{
62 char errmsg[PJ_ERR_MSG_SIZE];
63
Benny Prijono15953012006-04-27 22:37:08 +000064 PJ_UNUSED_ARG(sender);
65
Benny Prijonocbc1c472006-03-19 00:50:23 +000066 pj_strerror(status, errmsg, sizeof(errmsg));
67
68 printf("%s: %s [code=%d]\n", title, errmsg, status);
69 return 1;
70}
71
72
73/* Struct attached to sine generator */
74typedef struct
75{
76 pj_int16_t *samples; /* Sine samples. */
77} port_data;
78
79
80/* This callback is called to feed more samples */
81static pj_status_t sine_get_frame( pjmedia_port *port,
82 pjmedia_frame *frame)
83{
Benny Prijonoe85bc412006-07-29 20:29:24 +000084 port_data *sine = port->port_data.pdata;
Benny Prijonocbc1c472006-03-19 00:50:23 +000085 pj_int16_t *samples = frame->buf;
86 unsigned i, count, left, right;
87
88 /* Get number of samples */
89 count = frame->size / 2 / port->info.channel_count;
90
91 left = 0;
92 right = 0;
93
94 for (i=0; i<count; ++i) {
95 *samples++ = sine->samples[left];
96 ++left;
97
98 if (port->info.channel_count == 2) {
99 *samples++ = sine->samples[right];
100 right += 2; /* higher pitch so we can distinguish left and right. */
101 if (right >= count)
102 right = 0;
103 }
104 }
105
106 /* Must set frame->type correctly, otherwise the sound device
107 * will refuse to play.
108 */
109 frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
110
111 return PJ_SUCCESS;
112}
113
114#ifndef M_PI
115#define M_PI (3.14159265)
116#endif
117
118/*
119 * Create a media port to generate sine wave samples.
120 */
121static pj_status_t create_sine_port(pj_pool_t *pool,
122 unsigned sampling_rate,
123 unsigned channel_count,
124 pjmedia_port **p_port)
125{
126 pjmedia_port *port;
127 unsigned i;
128 unsigned count;
129 port_data *sine;
130
131 PJ_ASSERT_RETURN(pool && channel_count > 0 && channel_count <= 2,
132 PJ_EINVAL);
133
134 port = pj_pool_zalloc(pool, sizeof(pjmedia_port));
135 PJ_ASSERT_RETURN(port != NULL, PJ_ENOMEM);
136
137 /* Fill in port info. */
138 port->info.bits_per_sample = 16;
139 port->info.channel_count = channel_count;
140 port->info.encoding_name = pj_str("pcm");
141 port->info.has_info = 1;
142 port->info.name = pj_str("sine generator");
143 port->info.need_info = 0;
144 port->info.pt = 0xFF;
Benny Prijono15953012006-04-27 22:37:08 +0000145 port->info.clock_rate = sampling_rate;
Benny Prijonocbc1c472006-03-19 00:50:23 +0000146 port->info.samples_per_frame = sampling_rate * 20 / 1000 * channel_count;
147 port->info.bytes_per_frame = port->info.samples_per_frame * 2;
148 port->info.type = PJMEDIA_TYPE_AUDIO;
149
150 /* Set the function to feed frame */
151 port->get_frame = &sine_get_frame;
152
153 /* Create sine port data */
Benny Prijonoe85bc412006-07-29 20:29:24 +0000154 port->port_data.pdata = sine = pj_pool_zalloc(pool, sizeof(port_data));
Benny Prijonocbc1c472006-03-19 00:50:23 +0000155
156 /* Create samples */
157 count = port->info.samples_per_frame / channel_count;
158 sine->samples = pj_pool_alloc(pool, count * sizeof(pj_int16_t));
159 PJ_ASSERT_RETURN(sine->samples != NULL, PJ_ENOMEM);
160
161 /* initialise sinusoidal wavetable */
162 for( i=0; i<count; i++ )
163 {
164 sine->samples[i] = (pj_int16_t) (10000.0 *
165 sin(((double)i/(double)count) * M_PI * 8.) );
166 }
167
168 *p_port = port;
169
170 return PJ_SUCCESS;
171}
172
173
174/* Show usage */
175static void usage(void)
176{
177 puts("");
178 puts("Usage: playsine [nchannel]");
179 puts("");
180 puts("where");
181 puts(" nchannel is number of audio channels (1 for mono, or 2 for stereo).");
182 puts(" Default is 1 (mono).");
183 puts("");
184}
185
186
187/*
188 * main()
189 */
190int main(int argc, char *argv[])
191{
192 pj_caching_pool cp;
193 pjmedia_endpt *med_endpt;
194 pj_pool_t *pool;
195 pjmedia_port *sine_port;
196 pjmedia_snd_port *snd_port;
197 char tmp[10];
198 int channel_count = 1;
199 pj_status_t status;
200
201 if (argc == 2) {
202 channel_count = atoi(argv[1]);
203 if (channel_count < 1 || channel_count > 2) {
204 puts("Error: invalid arguments");
205 usage();
206 return 1;
207 }
208 }
209
210 /* Must init PJLIB first: */
211 status = pj_init();
212 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
213
214 /* Must create a pool factory before we can allocate any memory. */
215 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
216
217 /*
218 * Initialize media endpoint.
219 * This will implicitly initialize PJMEDIA too.
220 */
Benny Prijono275fd682006-03-22 11:59:11 +0000221 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
Benny Prijonocbc1c472006-03-19 00:50:23 +0000222 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
223
224 /* Create memory pool for our sine generator */
225 pool = pj_pool_create( &cp.factory, /* pool factory */
226 "wav", /* pool name. */
227 4000, /* init size */
228 4000, /* increment size */
229 NULL /* callback on error */
230 );
231
232 /* Create a media port to generate sine wave samples. */
233 status = create_sine_port( pool, /* memory pool */
234 11025, /* sampling rate */
235 channel_count,/* # of channels */
236 &sine_port /* returned port */
237 );
238 if (status != PJ_SUCCESS) {
239 app_perror(THIS_FILE, "Unable to create sine port", status);
240 return 1;
241 }
242
243 /* Create sound player port. */
244 status = pjmedia_snd_port_create_player(
245 pool, /* pool */
246 -1, /* use default dev. */
Benny Prijono15953012006-04-27 22:37:08 +0000247 sine_port->info.clock_rate, /* clock rate. */
Benny Prijonocbc1c472006-03-19 00:50:23 +0000248 sine_port->info.channel_count, /* # of channels. */
249 sine_port->info.samples_per_frame, /* samples per frame. */
250 sine_port->info.bits_per_sample, /* bits per sample. */
251 0, /* options */
252 &snd_port /* returned port */
253 );
254 if (status != PJ_SUCCESS) {
255 app_perror(THIS_FILE, "Unable to open sound device", status);
256 return 1;
257 }
258
259 /* Connect sine generator port to the sound player
260 * Stream playing will commence immediately.
261 */
262 status = pjmedia_snd_port_connect( snd_port, sine_port);
263 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
264
265
266
267 /*
268 * Audio should be playing in a loop now, using sound device's thread.
269 */
270
271
272 /* Sleep to allow log messages to flush */
273 pj_thread_sleep(100);
274
275
276 puts("Playing sine wave..");
277 puts("");
278 puts("Press <ENTER> to stop playing and quit");
279
Benny Prijono32d267b2009-01-01 22:08:21 +0000280 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
281 puts("EOF while reading stdin, will quit now..");
282 }
Benny Prijonocbc1c472006-03-19 00:50:23 +0000283
284
285 /* Start deinitialization: */
286
Benny Prijono4abeb9b2007-02-17 19:34:46 +0000287 /* Disconnect sound port from file port */
288 status = pjmedia_snd_port_disconnect(snd_port);
289 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
290
291 /* Without this sleep, Windows/DirectSound will repeteadly
292 * play the last frame during destroy.
293 */
294 pj_thread_sleep(100);
295
Benny Prijonocbc1c472006-03-19 00:50:23 +0000296 /* Destroy sound device */
297 status = pjmedia_snd_port_destroy( snd_port );
298 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
299
300
301 /* Destroy sine generator */
302 status = pjmedia_port_destroy( sine_port );
303 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
304
305
306 /* Release application pool */
307 pj_pool_release( pool );
308
309 /* Destroy media endpoint. */
310 pjmedia_endpt_destroy( med_endpt );
311
312 /* Destroy pool factory */
313 pj_caching_pool_destroy( &cp );
314
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000315 /* Shutdown PJLIB */
316 pj_shutdown();
317
Benny Prijonocbc1c472006-03-19 00:50:23 +0000318
319 /* Done. */
320 return 0;
321}