Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id: playsine.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 | |
| 21 | |
| 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 | |
| 33 | /* |
| 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> |
| 52 | #include <math.h> /* sin() */ |
| 53 | |
| 54 | /* For logging purpose. */ |
| 55 | #define THIS_FILE "playsine.c" |
| 56 | |
| 57 | |
| 58 | /* Util to display the error message for the specified error code */ |
| 59 | static int app_perror( const char *sender, const char *title, |
| 60 | pj_status_t status) |
| 61 | { |
| 62 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 63 | |
| 64 | PJ_UNUSED_ARG(sender); |
| 65 | |
| 66 | 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 */ |
| 74 | typedef struct |
| 75 | { |
| 76 | pj_int16_t *samples; /* Sine samples. */ |
| 77 | } port_data; |
| 78 | |
| 79 | |
| 80 | /* This callback is called to feed more samples */ |
| 81 | static pj_status_t sine_get_frame( pjmedia_port *port, |
| 82 | pjmedia_frame *frame) |
| 83 | { |
| 84 | port_data *sine = port->port_data.pdata; |
| 85 | pj_int16_t *samples = frame->buf; |
| 86 | unsigned i, left, right; |
| 87 | pj_size_t count; |
| 88 | |
| 89 | /* Get number of samples */ |
| 90 | count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info); |
| 91 | |
| 92 | left = 0; |
| 93 | right = 0; |
| 94 | |
| 95 | for (i=0; i<count; ++i) { |
| 96 | *samples++ = sine->samples[left]; |
| 97 | ++left; |
| 98 | |
| 99 | if (PJMEDIA_PIA_CCNT(&port->info) == 2) { |
| 100 | *samples++ = sine->samples[right]; |
| 101 | right += 2; /* higher pitch so we can distinguish left and right. */ |
| 102 | if (right >= count) |
| 103 | right = 0; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* Must set frame->type correctly, otherwise the sound device |
| 108 | * will refuse to play. |
| 109 | */ |
| 110 | frame->type = PJMEDIA_FRAME_TYPE_AUDIO; |
| 111 | |
| 112 | return PJ_SUCCESS; |
| 113 | } |
| 114 | |
| 115 | #ifndef M_PI |
| 116 | #define M_PI (3.14159265) |
| 117 | #endif |
| 118 | |
| 119 | /* |
| 120 | * Create a media port to generate sine wave samples. |
| 121 | */ |
| 122 | static pj_status_t create_sine_port(pj_pool_t *pool, |
| 123 | unsigned sampling_rate, |
| 124 | unsigned channel_count, |
| 125 | pjmedia_port **p_port) |
| 126 | { |
| 127 | pjmedia_port *port; |
| 128 | unsigned i; |
| 129 | unsigned count; |
| 130 | pj_str_t name; |
| 131 | port_data *sine; |
| 132 | |
| 133 | PJ_ASSERT_RETURN(pool && channel_count > 0 && channel_count <= 2, |
| 134 | PJ_EINVAL); |
| 135 | |
| 136 | port = pj_pool_zalloc(pool, sizeof(pjmedia_port)); |
| 137 | PJ_ASSERT_RETURN(port != NULL, PJ_ENOMEM); |
| 138 | |
| 139 | /* Fill in port info. */ |
| 140 | name = pj_str("sine generator"); |
| 141 | pjmedia_port_info_init(&port->info, &name, |
| 142 | PJMEDIA_SIG_CLASS_PORT_AUD('s', 'i'), |
| 143 | sampling_rate, |
| 144 | channel_count, |
| 145 | 16, sampling_rate * 20 / 1000 * channel_count); |
| 146 | |
| 147 | /* Set the function to feed frame */ |
| 148 | port->get_frame = &sine_get_frame; |
| 149 | |
| 150 | /* Create sine port data */ |
| 151 | port->port_data.pdata = sine = pj_pool_zalloc(pool, sizeof(port_data)); |
| 152 | |
| 153 | /* Create samples */ |
| 154 | count = PJMEDIA_PIA_SPF(&port->info) / channel_count; |
| 155 | sine->samples = pj_pool_alloc(pool, count * sizeof(pj_int16_t)); |
| 156 | PJ_ASSERT_RETURN(sine->samples != NULL, PJ_ENOMEM); |
| 157 | |
| 158 | /* initialise sinusoidal wavetable */ |
| 159 | for( i=0; i<count; i++ ) |
| 160 | { |
| 161 | sine->samples[i] = (pj_int16_t) (10000.0 * |
| 162 | sin(((double)i/(double)count) * M_PI * 8.) ); |
| 163 | } |
| 164 | |
| 165 | *p_port = port; |
| 166 | |
| 167 | return PJ_SUCCESS; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /* Show usage */ |
| 172 | static void usage(void) |
| 173 | { |
| 174 | puts(""); |
| 175 | puts("Usage: playsine [nchannel]"); |
| 176 | puts(""); |
| 177 | puts("where"); |
| 178 | puts(" nchannel is number of audio channels (1 for mono, or 2 for stereo)."); |
| 179 | puts(" Default is 1 (mono)."); |
| 180 | puts(""); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /* |
| 185 | * main() |
| 186 | */ |
| 187 | int main(int argc, char *argv[]) |
| 188 | { |
| 189 | pj_caching_pool cp; |
| 190 | pjmedia_endpt *med_endpt; |
| 191 | pj_pool_t *pool; |
| 192 | pjmedia_port *sine_port; |
| 193 | pjmedia_snd_port *snd_port; |
| 194 | char tmp[10]; |
| 195 | int channel_count = 1; |
| 196 | pj_status_t status; |
| 197 | |
| 198 | if (argc == 2) { |
| 199 | channel_count = atoi(argv[1]); |
| 200 | if (channel_count < 1 || channel_count > 2) { |
| 201 | puts("Error: invalid arguments"); |
| 202 | usage(); |
| 203 | return 1; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* Must init PJLIB first: */ |
| 208 | status = pj_init(); |
| 209 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 210 | |
| 211 | /* Must create a pool factory before we can allocate any memory. */ |
| 212 | pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 213 | |
| 214 | /* |
| 215 | * Initialize media endpoint. |
| 216 | * This will implicitly initialize PJMEDIA too. |
| 217 | */ |
| 218 | status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
| 219 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 220 | |
| 221 | /* Create memory pool for our sine generator */ |
| 222 | pool = pj_pool_create( &cp.factory, /* pool factory */ |
| 223 | "wav", /* pool name. */ |
| 224 | 4000, /* init size */ |
| 225 | 4000, /* increment size */ |
| 226 | NULL /* callback on error */ |
| 227 | ); |
| 228 | |
| 229 | /* Create a media port to generate sine wave samples. */ |
| 230 | status = create_sine_port( pool, /* memory pool */ |
| 231 | 11025, /* sampling rate */ |
| 232 | channel_count,/* # of channels */ |
| 233 | &sine_port /* returned port */ |
| 234 | ); |
| 235 | if (status != PJ_SUCCESS) { |
| 236 | app_perror(THIS_FILE, "Unable to create sine port", status); |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | /* Create sound player port. */ |
| 241 | status = pjmedia_snd_port_create_player( |
| 242 | pool, /* pool */ |
| 243 | -1, /* use default dev. */ |
| 244 | PJMEDIA_PIA_SRATE(&sine_port->info),/* clock rate. */ |
| 245 | PJMEDIA_PIA_CCNT(&sine_port->info),/* # of channels. */ |
| 246 | PJMEDIA_PIA_SPF(&sine_port->info), /* samples per frame. */ |
| 247 | PJMEDIA_PIA_BITS(&sine_port->info),/* bits per sample. */ |
| 248 | 0, /* options */ |
| 249 | &snd_port /* returned port */ |
| 250 | ); |
| 251 | if (status != PJ_SUCCESS) { |
| 252 | app_perror(THIS_FILE, "Unable to open sound device", status); |
| 253 | return 1; |
| 254 | } |
| 255 | |
| 256 | /* Connect sine generator port to the sound player |
| 257 | * Stream playing will commence immediately. |
| 258 | */ |
| 259 | status = pjmedia_snd_port_connect( snd_port, sine_port); |
| 260 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 261 | |
| 262 | |
| 263 | |
| 264 | /* |
| 265 | * Audio should be playing in a loop now, using sound device's thread. |
| 266 | */ |
| 267 | |
| 268 | |
| 269 | /* Sleep to allow log messages to flush */ |
| 270 | pj_thread_sleep(100); |
| 271 | |
| 272 | |
| 273 | puts("Playing sine wave.."); |
| 274 | puts(""); |
| 275 | puts("Press <ENTER> to stop playing and quit"); |
| 276 | |
| 277 | if (fgets(tmp, sizeof(tmp), stdin) == NULL) { |
| 278 | puts("EOF while reading stdin, will quit now.."); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /* Start deinitialization: */ |
| 283 | |
| 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 | |
| 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 | |
| 312 | /* Shutdown PJLIB */ |
| 313 | pj_shutdown(); |
| 314 | |
| 315 | |
| 316 | /* Done. */ |
| 317 | return 0; |
| 318 | } |