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