| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#include <pjmedia.h> |
| 47 |
#include <pjlib.h> |
| 48 |
|
| 49 |
#include <stdlib.h> |
| 50 |
#include <stdio.h> |
| 51 |
#include <math.h> |
| 52 |
|
| 53 |
|
| 54 |
#define THIS_FILE "playsine.c" |
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 63 |
PJ_UNUSED_ARG(sender); |
| 64 |
|
| 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 |
|
| 73 |
typedef struct |
| 74 |
{ |
| 75 |
pj_int16_t *samples; |
| 76 |
} port_data; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static pj_status_t sine_get_frame( pjmedia_port *port, |
| 81 |
pjmedia_frame *frame) |
| 82 |
{ |
| 83 |
port_data *sine = port->port_data.pdata; |
| 84 |
pj_int16_t *samples = frame->buf; |
| 85 |
unsigned i, left, right; |
| 86 |
pj_size_t count; |
| 87 |
|
| 88 |
|
| 89 |
count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info); |
| 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 (PJMEDIA_PIA_CCNT(&port->info) == 2) { |
| 99 |
*samples++ = sine->samples[right]; |
| 100 |
right += 2; |
| 101 |
if (right >= count) |
| 102 |
right = 0; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 120 |
|
| 121 |
static 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 |
pj_str_t name; |
| 130 |
port_data *sine; |
| 131 |
|
| 132 |
PJ_ASSERT_RETURN(pool && channel_count > 0 && channel_count <= 2, |
| 133 |
PJ_EINVAL); |
| 134 |
|
| 135 |
port = pj_pool_zalloc(pool, sizeof(pjmedia_port)); |
| 136 |
PJ_ASSERT_RETURN(port != NULL, PJ_ENOMEM); |
| 137 |
|
| 138 |
|
| 139 |
name = pj_str("sine generator"); |
| 140 |
pjmedia_port_info_init(&port->info, &name, |
| 141 |
PJMEDIA_SIG_CLASS_PORT_AUD('s', 'i'), |
| 142 |
sampling_rate, |
| 143 |
channel_count, |
| 144 |
16, sampling_rate * 20 / 1000 * channel_count); |
| 145 |
|
| 146 |
|
| 147 |
port->get_frame = &sine_get_frame; |
| 148 |
|
| 149 |
|
| 150 |
port->port_data.pdata = sine = pj_pool_zalloc(pool, sizeof(port_data)); |
| 151 |
|
| 152 |
|
| 153 |
count = PJMEDIA_PIA_SPF(&port->info) / channel_count; |
| 154 |
sine->samples = pj_pool_alloc(pool, count * sizeof(pj_int16_t)); |
| 155 |
PJ_ASSERT_RETURN(sine->samples != NULL, PJ_ENOMEM); |
| 156 |
|
| 157 |
|
| 158 |
for( i=0; i<count; i++ ) |
| 159 |
{ |
| 160 |
sine->samples[i] = (pj_int16_t) (10000.0 * |
| 161 |
sin(((double)i/(double)count) * M_PI * 8.) ); |
| 162 |
} |
| 163 |
|
| 164 |
*p_port = port; |
| 165 |
|
| 166 |
return PJ_SUCCESS; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
static void usage(void) |
| 172 |
{ |
| 173 |
puts(""); |
| 174 |
puts("Usage: playsine [nchannel]"); |
| 175 |
puts(""); |
| 176 |
puts("where"); |
| 177 |
puts(" nchannel is number of audio channels (1 for mono, or 2 for stereo)."); |
| 178 |
puts(" Default is 1 (mono)."); |
| 179 |
puts(""); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
int main(int argc, char *argv[]) |
| 187 |
{ |
| 188 |
pj_caching_pool cp; |
| 189 |
pjmedia_endpt *med_endpt; |
| 190 |
pj_pool_t *pool; |
| 191 |
pjmedia_port *sine_port; |
| 192 |
pjmedia_snd_port *snd_port; |
| 193 |
char tmp[10]; |
| 194 |
int channel_count = 1; |
| 195 |
pj_status_t status; |
| 196 |
|
| 197 |
if (argc == 2) { |
| 198 |
channel_count = atoi(argv[1]); |
| 199 |
if (channel_count < 1 || channel_count > 2) { |
| 200 |
puts("Error: invalid arguments"); |
| 201 |
usage(); |
| 202 |
return 1; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
status = pj_init(); |
| 208 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 209 |
|
| 210 |
|
| 211 |
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt); |
| 218 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 219 |
|
| 220 |
|
| 221 |
pool = pj_pool_create( &cp.factory, |
| 222 |
"wav", |
| 223 |
4000, |
| 224 |
4000, |
| 225 |
NULL |
| 226 |
); |
| 227 |
|
| 228 |
|
| 229 |
status = create_sine_port( pool, |
| 230 |
11025, |
| 231 |
channel_count, |
| 232 |
&sine_port |
| 233 |
); |
| 234 |
if (status != PJ_SUCCESS) { |
| 235 |
app_perror(THIS_FILE, "Unable to create sine port", status); |
| 236 |
return 1; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
status = pjmedia_snd_port_create_player( |
| 241 |
pool, |
| 242 |
-1, |
| 243 |
PJMEDIA_PIA_SRATE(&sine_port->info), |
| 244 |
PJMEDIA_PIA_CCNT(&sine_port->info), |
| 245 |
PJMEDIA_PIA_SPF(&sine_port->info), |
| 246 |
PJMEDIA_PIA_BITS(&sine_port->info), |
| 247 |
0, |
| 248 |
&snd_port |
| 249 |
); |
| 250 |
if (status != PJ_SUCCESS) { |
| 251 |
app_perror(THIS_FILE, "Unable to open sound device", status); |
| 252 |
return 1; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
status = pjmedia_snd_port_connect( snd_port, sine_port); |
| 259 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
pj_thread_sleep(100); |
| 270 |
|
| 271 |
|
| 272 |
puts("Playing sine wave.."); |
| 273 |
puts(""); |
| 274 |
puts("Press <ENTER> to stop playing and quit"); |
| 275 |
|
| 276 |
if (fgets(tmp, sizeof(tmp), stdin) == NULL) { |
| 277 |
puts("EOF while reading stdin, will quit now.."); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
status = pjmedia_snd_port_disconnect(snd_port); |
| 285 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
pj_thread_sleep(100); |
| 291 |
|
| 292 |
|
| 293 |
status = pjmedia_snd_port_destroy( snd_port ); |
| 294 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
status = pjmedia_port_destroy( sine_port ); |
| 299 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
pj_pool_release( pool ); |
| 304 |
|
| 305 |
|
| 306 |
pjmedia_endpt_destroy( med_endpt ); |
| 307 |
|
| 308 |
|
| 309 |
pj_caching_pool_destroy( &cp ); |
| 310 |
|
| 311 |
|
| 312 |
pj_shutdown(); |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
return 0; |
| 317 |
} |