playsine.c 9.0 KB

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