tmedia_denoise.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. /**@file tmedia_denoise.c
  23. * @brief Denoiser (Noise suppression, AGC, AEC, VAD) Plugin
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinymedia/tmedia_denoise.h"
  29. #include "tinymedia/tmedia_defaults.h"
  30. #include "tsk_debug.h"
  31. static const tmedia_denoise_plugin_def_t* __tmedia_denoise_plugin = tsk_null;
  32. int tmedia_denoise_init(tmedia_denoise_t* self)
  33. {
  34. if(!self) {
  35. TSK_DEBUG_ERROR("Invalid parameter");
  36. return -1;
  37. }
  38. self->echo_tail = tmedia_defaults_get_echo_tail();
  39. self->echo_skew = tmedia_defaults_get_echo_skew();
  40. self->echo_supp_enabled = tmedia_defaults_get_echo_supp_enabled();
  41. self->agc_enabled = tmedia_defaults_get_agc_enabled();
  42. self->agc_level = tmedia_defaults_get_agc_level();
  43. self->vad_enabled = tmedia_defaults_get_vad_enabled();
  44. self->noise_supp_enabled = tmedia_defaults_get_noise_supp_enabled();
  45. self->noise_supp_level = tmedia_defaults_get_noise_supp_level();
  46. return 0;
  47. }
  48. int tmedia_denoise_set(tmedia_denoise_t* self, const tmedia_param_t* param)
  49. {
  50. if(!self || !self->plugin) {
  51. TSK_DEBUG_ERROR("Invalid parameter");
  52. return -1;
  53. }
  54. if(self->plugin->set) {
  55. return self->plugin->set(self, param);
  56. }
  57. return 0;
  58. }
  59. int tmedia_denoise_open(tmedia_denoise_t* self, uint32_t record_frame_size_samples, uint32_t record_sampling_rate, uint32_t record_channels, uint32_t playback_frame_size_samples, uint32_t playback_sampling_rate, uint32_t playback_channels)
  60. {
  61. if (!self || !self->plugin) {
  62. TSK_DEBUG_ERROR("Invalid parameter");
  63. return -1;
  64. }
  65. if (self->opened) {
  66. TSK_DEBUG_WARN("Denoiser already opened");
  67. return 0;
  68. }
  69. if (self->plugin->open) {
  70. int ret;
  71. TSK_OBJECT_SAFE_FREE(self->record_frame);
  72. TSK_OBJECT_SAFE_FREE(self->playback_frame);
  73. if (!(self->record_frame = tsk_buffer_create(tsk_null, (record_frame_size_samples * sizeof(int16_t))))) {
  74. TSK_DEBUG_ERROR("Failed to create record the buffer");
  75. return -2;
  76. }
  77. if (!(self->playback_frame = tsk_buffer_create(tsk_null, (playback_frame_size_samples * sizeof(int16_t))))) {
  78. TSK_DEBUG_ERROR("Failed to create playback the buffer");
  79. return -2;
  80. }
  81. if ((ret = self->plugin->open(self, record_frame_size_samples, record_sampling_rate, record_channels, playback_frame_size_samples, playback_sampling_rate, playback_channels))) {
  82. TSK_DEBUG_ERROR("Failed to open [%s] denoiser", self->plugin->desc);
  83. return ret;
  84. }
  85. else {
  86. self->opened = tsk_true;
  87. return 0;
  88. }
  89. }
  90. else {
  91. self->opened = tsk_true;
  92. return 0;
  93. }
  94. }
  95. int tmedia_denoise_echo_playback(tmedia_denoise_t* self, const void* echo_frame, uint32_t echo_frame_size_bytes)
  96. {
  97. if(!self || !self->plugin) {
  98. TSK_DEBUG_ERROR("Invalid parameter");
  99. return -1;
  100. }
  101. if(!self->opened) {
  102. TSK_DEBUG_ERROR("Denoiser not opened");
  103. return -2;
  104. }
  105. if(self->plugin->echo_playback) {
  106. return self->plugin->echo_playback(self, echo_frame, echo_frame_size_bytes);
  107. }
  108. else {
  109. return 0;
  110. }
  111. }
  112. int tmedia_denoise_process_record(tmedia_denoise_t* self, void* audio_frame, uint32_t audio_frame_size_bytes, tsk_bool_t* silence_or_noise)
  113. {
  114. if(!self || !self->plugin || !silence_or_noise) {
  115. TSK_DEBUG_ERROR("Invalid parameter");
  116. return -1;
  117. }
  118. if(!self->opened) {
  119. TSK_DEBUG_ERROR("Denoiser not opened");
  120. return -2;
  121. }
  122. if(self->plugin->process_record) {
  123. return self->plugin->process_record(self, audio_frame, audio_frame_size_bytes, silence_or_noise);
  124. }
  125. else {
  126. *silence_or_noise = tsk_false;
  127. return 0;
  128. }
  129. }
  130. int tmedia_denoise_process_playback(tmedia_denoise_t* self, void* audio_frame, uint32_t audio_frame_size_bytes)
  131. {
  132. if(!self || !self->plugin) {
  133. TSK_DEBUG_ERROR("Invalid parameter");
  134. return -1;
  135. }
  136. if(!self->opened) {
  137. TSK_DEBUG_ERROR("Denoiser not opened");
  138. return -2;
  139. }
  140. if(self->plugin->process_playback) {
  141. return self->plugin->process_playback(self, audio_frame, audio_frame_size_bytes);
  142. }
  143. return 0;
  144. }
  145. int tmedia_denoise_close(tmedia_denoise_t* self)
  146. {
  147. if(!self || !self->plugin) {
  148. TSK_DEBUG_ERROR("Invalid parameter");
  149. return -1;
  150. }
  151. if(!self->opened) {
  152. return 0;
  153. }
  154. if(self->plugin->close) {
  155. int ret;
  156. if((ret = self->plugin->close(self))) {
  157. TSK_DEBUG_ERROR("Failed to close [%s] denoiser", self->plugin->desc);
  158. return ret;
  159. }
  160. else {
  161. self->opened = tsk_false;
  162. return 0;
  163. }
  164. }
  165. else {
  166. self->opened = tsk_false;
  167. return 0;
  168. }
  169. }
  170. int tmedia_denoise_deinit(tmedia_denoise_t* self)
  171. {
  172. if (!self) {
  173. TSK_DEBUG_ERROR("Invalid parameter");
  174. return -1;
  175. }
  176. if (self->opened) {
  177. tmedia_denoise_close(self);
  178. }
  179. TSK_OBJECT_SAFE_FREE(self->record_frame);
  180. TSK_OBJECT_SAFE_FREE(self->playback_frame);
  181. return 0;
  182. }
  183. int tmedia_denoise_plugin_register(const tmedia_denoise_plugin_def_t* plugin)
  184. {
  185. if(!plugin) {
  186. TSK_DEBUG_ERROR("Invalid parameter");
  187. return -1;
  188. }
  189. if(!__tmedia_denoise_plugin) {
  190. TSK_DEBUG_INFO("Register denoiser: %s", plugin->desc);
  191. __tmedia_denoise_plugin = plugin;
  192. }
  193. return 0;
  194. }
  195. int tmedia_denoise_plugin_unregister(const tmedia_denoise_plugin_def_t* plugin)
  196. {
  197. (void)(plugin);
  198. __tmedia_denoise_plugin = tsk_null;
  199. return 0;
  200. }
  201. tmedia_denoise_t* tmedia_denoise_create()
  202. {
  203. tmedia_denoise_t* denoise = tsk_null;
  204. if(__tmedia_denoise_plugin) {
  205. if((denoise = tsk_object_new(__tmedia_denoise_plugin->objdef))) {
  206. denoise->plugin = __tmedia_denoise_plugin;
  207. }
  208. }
  209. return denoise;
  210. }