tonegen.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #ifndef __PJMEDIA_TONEGEN_PORT_H__
  20. #define __PJMEDIA_TONEGEN_PORT_H__
  21. /**
  22. * @file tonegen.h
  23. * @brief Tone (sine, MF, DTMF) generator media port.
  24. */
  25. #include <pjmedia/port.h>
  26. /**
  27. * @defgroup PJMEDIA_MF_DTMF_TONE_GENERATOR Multi-frequency tone generator
  28. * @ingroup PJMEDIA_PORT
  29. * @brief Multi-frequency tone generator
  30. * @{
  31. *
  32. * This page describes tone generator media port. A tone generator can be
  33. * used to generate a single frequency sine wave or dual frequency tones
  34. * such as DTMF.
  35. *
  36. * The tone generator media port provides two functions to generate tones.
  37. * The function #pjmedia_tonegen_play() can be used to generate arbitrary
  38. * single or dual frequency tone, and #pjmedia_tonegen_play_digits() is
  39. * used to play digits such as DTMF. Each tone specified in the playback
  40. * function has individual on and off signal duration that must be
  41. * specified by application.
  42. *
  43. * In order to play digits such as DTMF, the tone generator is equipped
  44. * with digit map, which contain information about the frequencies of
  45. * the digits. The default digit map is DTMF (0-9,a-d,*,#), but application
  46. * may specifiy different digit map to the tone generator by calling
  47. * #pjmedia_tonegen_set_digit_map() function.
  48. */
  49. PJ_BEGIN_DECL
  50. /**
  51. * This structure describes individual MF digits to be played
  52. * with #pjmedia_tonegen_play().
  53. */
  54. typedef struct pjmedia_tone_desc
  55. {
  56. short freq1; /**< First frequency. */
  57. short freq2; /**< Optional second frequency. */
  58. short on_msec; /**< Playback ON duration, in miliseconds. */
  59. short off_msec; /**< Playback OFF duration, ini miliseconds. */
  60. short volume; /**< Volume (1-32767), or 0 for default, which
  61. PJMEDIA_TONEGEN_VOLUME will be used. */
  62. short flags; /**< Currently internal flags, must be 0 */
  63. } pjmedia_tone_desc;
  64. /**
  65. * This structure describes individual MF digits to be played
  66. * with #pjmedia_tonegen_play_digits().
  67. */
  68. typedef struct pjmedia_tone_digit
  69. {
  70. char digit; /**< The ASCI identification for the digit. */
  71. short on_msec; /**< Playback ON duration, in miliseconds. */
  72. short off_msec; /**< Playback OFF duration, ini miliseconds. */
  73. short volume; /**< Volume (1-32767), or 0 for default, which
  74. PJMEDIA_TONEGEN_VOLUME will be used. */
  75. } pjmedia_tone_digit;
  76. /**
  77. * This structure describes the digit map which is used by the tone generator
  78. * to produce tones from an ASCII digits.
  79. * Digit map used by a particular tone generator can be retrieved/set with
  80. * #pjmedia_tonegen_get_digit_map() and #pjmedia_tonegen_set_digit_map().
  81. */
  82. typedef struct pjmedia_tone_digit_map
  83. {
  84. unsigned count; /**< Number of digits in the map. */
  85. struct {
  86. char digit; /**< The ASCI identification for the digit. */
  87. short freq1; /**< First frequency. */
  88. short freq2; /**< Optional second frequency. */
  89. } digits[16]; /**< Array of digits in the digit map. */
  90. } pjmedia_tone_digit_map;
  91. /**
  92. * Tone generator options.
  93. */
  94. enum
  95. {
  96. /**
  97. * Play the tones in loop, restarting playing the first tone after
  98. * the last tone has been played.
  99. */
  100. PJMEDIA_TONEGEN_LOOP = 1,
  101. /**
  102. * Disable mutex protection to the tone generator.
  103. */
  104. PJMEDIA_TONEGEN_NO_LOCK = 2
  105. };
  106. /**
  107. * Create an instance of tone generator with the specified parameters.
  108. * When the tone generator is first created, it will be loaded with the
  109. * default digit map.
  110. *
  111. * @param pool Pool to allocate memory for the port structure.
  112. * @param clock_rate Sampling rate.
  113. * @param channel_count Number of channels. Currently only mono and stereo
  114. * are supported.
  115. * @param samples_per_frame Number of samples per frame.
  116. * @param bits_per_sample Number of bits per sample. This version of PJMEDIA
  117. * only supports 16bit per sample.
  118. * @param options Option flags. Application may specify
  119. * PJMEDIA_TONEGEN_LOOP to play the tone in a loop.
  120. * @param p_port Pointer to receive the port instance.
  121. *
  122. * @return PJ_SUCCESS on success, or the appropriate
  123. * error code.
  124. */
  125. PJ_DECL(pj_status_t) pjmedia_tonegen_create(pj_pool_t *pool,
  126. unsigned clock_rate,
  127. unsigned channel_count,
  128. unsigned samples_per_frame,
  129. unsigned bits_per_sample,
  130. unsigned options,
  131. pjmedia_port **p_port);
  132. /**
  133. * Create an instance of tone generator with the specified parameters.
  134. * When the tone generator is first created, it will be loaded with the
  135. * default digit map.
  136. *
  137. * @param pool Pool to allocate memory for the port structure.
  138. * @param name Optional name for the tone generator.
  139. * @param clock_rate Sampling rate.
  140. * @param channel_count Number of channels. Currently only mono and stereo
  141. * are supported.
  142. * @param samples_per_frame Number of samples per frame.
  143. * @param bits_per_sample Number of bits per sample. This version of PJMEDIA
  144. * only supports 16bit per sample.
  145. * @param options Option flags. Application may specify
  146. * PJMEDIA_TONEGEN_LOOP to play the tone in a loop.
  147. * @param p_port Pointer to receive the port instance.
  148. *
  149. * @return PJ_SUCCESS on success, or the appropriate
  150. * error code.
  151. */
  152. PJ_DECL(pj_status_t) pjmedia_tonegen_create2(pj_pool_t *pool,
  153. const pj_str_t *name,
  154. unsigned clock_rate,
  155. unsigned channel_count,
  156. unsigned samples_per_frame,
  157. unsigned bits_per_sample,
  158. unsigned options,
  159. pjmedia_port **p_port);
  160. /**
  161. * Check if the tone generator is still busy producing some tones.
  162. *
  163. * @param tonegen The tone generator instance.
  164. *
  165. * @return Non-zero if busy.
  166. */
  167. PJ_DECL(pj_bool_t) pjmedia_tonegen_is_busy(pjmedia_port *tonegen);
  168. /**
  169. * Instruct the tone generator to stop current processing.
  170. *
  171. * @param tonegen The tone generator instance.
  172. *
  173. * @return PJ_SUCCESS on success.
  174. */
  175. PJ_DECL(pj_status_t) pjmedia_tonegen_stop(pjmedia_port *tonegen);
  176. /**
  177. * Instruct the tone generator to stop looping of the current tone set.
  178. *
  179. * @param tonegen The tone generator instance.
  180. *
  181. * @return PJ_SUCCESS on success.
  182. */
  183. PJ_DECL(pj_status_t) pjmedia_tonegen_stop_loop(pjmedia_port *tonegen);
  184. /**
  185. * Rewind the playback. This will start the playback to the first
  186. * tone in the playback list.
  187. *
  188. * @param tonegen The tone generator instance.
  189. *
  190. * @return PJ_SUCCESS on success.
  191. */
  192. PJ_DECL(pj_status_t) pjmedia_tonegen_rewind(pjmedia_port *tonegen);
  193. /**
  194. * Instruct the tone generator to play single or dual frequency tones
  195. * with the specified duration. The new tones will be appended to currently
  196. * playing tones, unless #pjmedia_tonegen_stop() is called before calling
  197. * this function. The playback will begin as soon as the first get_frame()
  198. * is called to the generator.
  199. *
  200. * @param tonegen The tone generator instance.
  201. * @param count The number of tones in the array.
  202. * @param tones Array of tones to be played.
  203. * @param options Option flags. Application may specify
  204. * PJMEDIA_TONEGEN_LOOP to play the tone in a loop.
  205. *
  206. * @return PJ_SUCCESS on success, or PJ_ETOOMANY if
  207. * there are too many digits in the queue.
  208. */
  209. PJ_DECL(pj_status_t) pjmedia_tonegen_play(pjmedia_port *tonegen,
  210. unsigned count,
  211. const pjmedia_tone_desc tones[],
  212. unsigned options);
  213. /**
  214. * Instruct the tone generator to play multiple MF digits with each of
  215. * the digits having individual ON/OFF duration. Each of the digit in the
  216. * digit array must have the corresponding descriptor in the digit map.
  217. * The new tones will be appended to currently playing tones, unless
  218. * #pjmedia_tonegen_stop() is called before calling this function.
  219. * The playback will begin as soon as the first get_frame() is called
  220. * to the generator.
  221. *
  222. * @param tonegen The tone generator instance.
  223. * @param count Number of digits in the array.
  224. * @param digits Array of MF digits.
  225. * @param options Option flags. Application may specify
  226. * PJMEDIA_TONEGEN_LOOP to play the tone in a loop.
  227. *
  228. * @return PJ_SUCCESS on success, or PJ_ETOOMANY if
  229. * there are too many digits in the queue, or
  230. * PJMEDIA_RTP_EINDTMF if invalid digit is
  231. * specified.
  232. */
  233. PJ_DECL(pj_status_t) pjmedia_tonegen_play_digits(pjmedia_port *tonegen,
  234. unsigned count,
  235. const pjmedia_tone_digit digits[],
  236. unsigned options);
  237. /**
  238. * Get the digit-map currently used by this tone generator.
  239. *
  240. * @param tonegen The tone generator instance.
  241. * @param m On output, it will be filled with the pointer to
  242. * the digitmap currently used by the tone generator.
  243. *
  244. * @return PJ_SUCCESS on success.
  245. */
  246. PJ_DECL(pj_status_t) pjmedia_tonegen_get_digit_map(pjmedia_port *tonegen,
  247. const pjmedia_tone_digit_map **m);
  248. /**
  249. * Set digit map to be used by the tone generator.
  250. *
  251. * @param tonegen The tone generator instance.
  252. * @param m Digitmap to be used by the tone generator.
  253. *
  254. * @return PJ_SUCCESS on success.
  255. */
  256. PJ_DECL(pj_status_t) pjmedia_tonegen_set_digit_map(pjmedia_port *tonegen,
  257. pjmedia_tone_digit_map *m);
  258. PJ_END_DECL
  259. /**
  260. * @}
  261. */
  262. #endif /* __PJMEDIA_TONEGEN_PORT_H__ */