VoiceChanger.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Serialization;
  4. using Agora.Rtc;
  5. using Agora.Util;
  6. using Logger = Agora.Util.Logger;
  7. namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.VoiceChanger
  8. {
  9. public class VoiceChanger : MonoBehaviour
  10. {
  11. [FormerlySerializedAs("appIdInput")]
  12. [SerializeField]
  13. private AppIdInput _appIdInput;
  14. [Header("_____________Basic Configuration_____________")]
  15. [FormerlySerializedAs("APP_ID")]
  16. [SerializeField]
  17. private string _appID = "";
  18. [FormerlySerializedAs("TOKEN")]
  19. [SerializeField]
  20. private string _token = "";
  21. [FormerlySerializedAs("CHANNEL_NAME")]
  22. [SerializeField]
  23. private string _channelName = "";
  24. public Text LogText;
  25. internal Logger Log;
  26. internal IRtcEngine RtcEngine = null;
  27. private void Start()
  28. {
  29. LoadAssetData();
  30. if (CheckAppId())
  31. {
  32. InitEngine();
  33. SetupUI();
  34. JoinChannel();
  35. }
  36. }
  37. [ContextMenu("ShowAgoraBasicProfileData")]
  38. public void LoadAssetData()
  39. {
  40. if (_appIdInput == null) return;
  41. _appID = _appIdInput.appID;
  42. _token = _appIdInput.token;
  43. _channelName = _appIdInput.channelName;
  44. }
  45. private bool CheckAppId()
  46. {
  47. Log = new Logger(LogText);
  48. return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset");
  49. }
  50. private void InitEngine()
  51. {
  52. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  53. UserEventHandler handler = new UserEventHandler(this);
  54. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  55. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING,
  56. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
  57. RtcEngine.Initialize(context);
  58. RtcEngine.InitEventHandler(handler);
  59. }
  60. private void JoinChannel()
  61. {
  62. RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  63. RtcEngine.EnableAudio();
  64. RtcEngine.JoinChannel(_token, _channelName, "");
  65. }
  66. private void Update()
  67. {
  68. PermissionHelper.RequestMicrophontPermission();
  69. }
  70. private void SetupUI()
  71. {
  72. Transform content = GameObject.Find("Canvas/Scroll View/Viewport/Content").transform;
  73. Button but = content.Find("ChatBeautifierButton").GetComponent<Button>();
  74. but.onClick.AddListener(OnChatBeautifierButtonPress);
  75. but = content.Find("SingingBeautifierButton").GetComponent<Button>();
  76. but.onClick.AddListener(OnSingingBeautifierButtonPress);
  77. but = content.Find("TimbreTransformationButton").GetComponent<Button>();
  78. but.onClick.AddListener(OnTimbreTransformationButtonPress);
  79. but = content.Find("OffVoiceBeautifierButton").GetComponent<Button>();
  80. but.onClick.AddListener(OnOffVoiceBeautifierButtonPress);
  81. but = content.Find("StyleTransformationButton").GetComponent<Button>();
  82. but.onClick.AddListener(OnStyleTransformationButtonPress);
  83. but = content.Find("RoomAcoustuicsButton").GetComponent<Button>();
  84. but.onClick.AddListener(OnRoomAcoustuicsButtonPress);
  85. but = content.Find("PitchButton").GetComponent<Button>();
  86. but.onClick.AddListener(OnPitchButtonPress);
  87. but = content.Find("OffAudioEffectButton").GetComponent<Button>();
  88. but.onClick.AddListener(OnOffAudioEffectButtonPress);
  89. but = content.Find("VoiceChangerButton").GetComponent<Button>();
  90. but.onClick.AddListener(OnVoiceChangerButtonPress);
  91. but = content.Find("OffVoiceChangerButton").GetComponent<Button>();
  92. but.onClick.AddListener(OnOffVoiceChangerButtonPress);
  93. but = content.Find("CustomVocalEffectsButton").GetComponent<Button>();
  94. but.onClick.AddListener(OnCustomVocalEffectsButtonPress);
  95. }
  96. private void OnDestroy()
  97. {
  98. Debug.Log("OnDestroy");
  99. if (RtcEngine == null) return;
  100. RtcEngine.InitEventHandler(null);
  101. RtcEngine.LeaveChannel();
  102. RtcEngine.Dispose();
  103. }
  104. #region VoiceBeautifier
  105. private void OnChatBeautifierButtonPress()
  106. {
  107. RtcEngine.SetAudioProfile(AUDIO_PROFILE_TYPE.AUDIO_PROFILE_MUSIC_HIGH_QUALITY, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  108. int nRet = RtcEngine.SetVoiceBeautifierPreset(VOICE_BEAUTIFIER_PRESET.CHAT_BEAUTIFIER_MAGNETIC);
  109. this.Log.UpdateLog(string.Format("SetVoiceBeautifierPreset nRet:{0}", nRet));
  110. }
  111. private void OnSingingBeautifierButtonPress()
  112. {
  113. RtcEngine.SetAudioProfile(AUDIO_PROFILE_TYPE.AUDIO_PROFILE_MUSIC_HIGH_QUALITY, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  114. int nRet = RtcEngine.SetVoiceBeautifierPreset(VOICE_BEAUTIFIER_PRESET.SINGING_BEAUTIFIER);
  115. this.Log.UpdateLog(string.Format("SetVoiceBeautifierPreset nRet:{0}", nRet));
  116. }
  117. private void OnTimbreTransformationButtonPress()
  118. {
  119. RtcEngine.SetAudioProfile(AUDIO_PROFILE_TYPE.AUDIO_PROFILE_MUSIC_HIGH_QUALITY, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  120. int nRet = RtcEngine.SetVoiceBeautifierPreset(VOICE_BEAUTIFIER_PRESET.TIMBRE_TRANSFORMATION_VIGOROUS);
  121. this.Log.UpdateLog(string.Format("SetVoiceBeautifierPreset nRet:{0}", nRet));
  122. }
  123. private void OnOffVoiceBeautifierButtonPress()
  124. {
  125. RtcEngine.SetAudioProfile(AUDIO_PROFILE_TYPE.AUDIO_PROFILE_MUSIC_HIGH_QUALITY, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  126. int nRet = RtcEngine.SetVoiceBeautifierPreset(VOICE_BEAUTIFIER_PRESET.VOICE_BEAUTIFIER_OFF);
  127. this.Log.UpdateLog(string.Format("SetVoiceBeautifierPreset nRet:{0}", nRet));
  128. }
  129. #endregion
  130. #region AudioEffect
  131. private void OnStyleTransformationButtonPress()
  132. {
  133. RtcEngine.SetAudioProfile(AUDIO_PROFILE_TYPE.AUDIO_PROFILE_MUSIC_HIGH_QUALITY, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  134. int nRet = RtcEngine.SetAudioEffectPreset(AUDIO_EFFECT_PRESET.STYLE_TRANSFORMATION_POPULAR);
  135. this.Log.UpdateLog(string.Format("SetAudioEffectPreset nRet:{0}", nRet));
  136. }
  137. private void OnRoomAcoustuicsButtonPress()
  138. {
  139. RtcEngine.SetAudioProfile(AUDIO_PROFILE_TYPE.AUDIO_PROFILE_MUSIC_HIGH_QUALITY, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  140. int nRet = RtcEngine.SetAudioEffectPreset(AUDIO_EFFECT_PRESET.ROOM_ACOUSTICS_KTV);
  141. this.Log.UpdateLog(string.Format("SetAudioEffectPreset nRet:{0}", nRet));
  142. }
  143. private void OnPitchButtonPress()
  144. {
  145. RtcEngine.SetAudioProfile(AUDIO_PROFILE_TYPE.AUDIO_PROFILE_MUSIC_HIGH_QUALITY, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  146. int nRet = RtcEngine.SetAudioEffectPreset(AUDIO_EFFECT_PRESET.PITCH_CORRECTION);
  147. this.Log.UpdateLog(string.Format("SetAudioEffectPreset nRet:{0}", nRet));
  148. }
  149. private void OnOffAudioEffectButtonPress()
  150. {
  151. RtcEngine.SetAudioProfile(AUDIO_PROFILE_TYPE.AUDIO_PROFILE_MUSIC_HIGH_QUALITY, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  152. int nRet = RtcEngine.SetAudioEffectPreset(AUDIO_EFFECT_PRESET.AUDIO_EFFECT_OFF);
  153. this.Log.UpdateLog(string.Format("SetAudioEffectPreset nRet:{0}", nRet));
  154. }
  155. #endregion
  156. #region VoiceConversion
  157. private void OnVoiceChangerButtonPress()
  158. {
  159. RtcEngine.SetAudioProfile(AUDIO_PROFILE_TYPE.AUDIO_PROFILE_MUSIC_HIGH_QUALITY, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  160. int nRet = RtcEngine.SetVoiceConversionPreset(VOICE_CONVERSION_PRESET.VOICE_CHANGER_NEUTRAL);
  161. this.Log.UpdateLog(string.Format("SetVoiceConversionPreset nRet:{0}", nRet));
  162. }
  163. private void OnOffVoiceChangerButtonPress()
  164. {
  165. RtcEngine.SetAudioProfile(AUDIO_PROFILE_TYPE.AUDIO_PROFILE_MUSIC_HIGH_QUALITY, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING);
  166. int nRet = RtcEngine.SetVoiceConversionPreset(VOICE_CONVERSION_PRESET.VOICE_CONVERSION_OFF);
  167. this.Log.UpdateLog(string.Format("SetVoiceConversionPreset nRet:{0}", nRet));
  168. }
  169. #endregion
  170. #region Custom vocal effects
  171. private void OnCustomVocalEffectsButtonPress()
  172. {
  173. //Set the tone. It can be set in the range of [0.5, 2.0]. The smaller the value, the lower the tone. The default value is 1.0, which means there is no need to modify the tone.
  174. int nRet = RtcEngine.SetLocalVoicePitch(0.5);
  175. this.Log.UpdateLog(string.Format("SetLocalVoicePitch nRet:{0}", nRet));
  176. /**
  177. * Set the center frequency of the local vocal equalization band
  178. * The first parameter is the spectrum subband index, with a value range of [0,9], representing 10 frequency bands respectively, and the corresponding center frequency is [31,62,125,250,500,1000,2000,4000,8000,16000] Hz
  179. * The second parameter is the gain value of each frequency interval, the value range is [- 15,15], the unit is dB, and the default value is 0
  180. */
  181. nRet = RtcEngine.SetLocalVoiceEqualization(AUDIO_EQUALIZATION_BAND_FREQUENCY.AUDIO_EQUALIZATION_BAND_31, -15);
  182. //nRet = mRtcEngine.SetLocalVoiceEqualization(AUDIO_EQUALIZATION_BAND_FREQUENCY.AUDIO_EQUALIZATION_BAND_62, 3);
  183. //nRet = mRtcEngine.SetLocalVoiceEqualization(AUDIO_EQUALIZATION_BAND_FREQUENCY.AUDIO_EQUALIZATION_BAND_125, -9);
  184. //nRet = mRtcEngine.SetLocalVoiceEqualization(AUDIO_EQUALIZATION_BAND_FREQUENCY.AUDIO_EQUALIZATION_BAND_250, -8);
  185. //nRet = mRtcEngine.SetLocalVoiceEqualization(AUDIO_EQUALIZATION_BAND_FREQUENCY.AUDIO_EQUALIZATION_BAND_500, -6);
  186. //nRet = mRtcEngine.SetLocalVoiceEqualization(AUDIO_EQUALIZATION_BAND_FREQUENCY.AUDIO_EQUALIZATION_BAND_1K, -4);
  187. //nRet = mRtcEngine.SetLocalVoiceEqualization(AUDIO_EQUALIZATION_BAND_FREQUENCY.AUDIO_EQUALIZATION_BAND_2K, -3);
  188. //nRet = mRtcEngine.SetLocalVoiceEqualization(AUDIO_EQUALIZATION_BAND_FREQUENCY.AUDIO_EQUALIZATION_BAND_4K, -2);
  189. //nRet = mRtcEngine.SetLocalVoiceEqualization(AUDIO_EQUALIZATION_BAND_FREQUENCY.AUDIO_EQUALIZATION_BAND_8K, -1);
  190. //nRet = mRtcEngine.SetLocalVoiceEqualization(AUDIO_EQUALIZATION_BAND_FREQUENCY.AUDIO_EQUALIZATION_BAND_16K, 1);
  191. this.Log.UpdateLog(string.Format("SetLocalVoiceEqualization nRet:{0}", nRet));
  192. // The original vocal intensity, the so-called dry signal, has a value range of [- 20,10], and the unit is dB
  193. nRet = RtcEngine.SetLocalVoiceReverb(AUDIO_REVERB_TYPE.AUDIO_REVERB_DRY_LEVEL, 10);
  194. this.Log.UpdateLog(string.Format("SetLocalVoiceReverb nRet:{0}", nRet));
  195. // The early reflected signal intensity, the so-called wet signal, has a value range of [- 20,10], and the unit is dB
  196. nRet = RtcEngine.SetLocalVoiceReverb(AUDIO_REVERB_TYPE.AUDIO_REVERB_WET_LEVEL, 7);
  197. this.Log.UpdateLog(string.Format("SetLocalVoiceReverb nRet:{0}", nRet));
  198. // The room size required for reverberation effect. Generally, the larger the room is, the stronger the reverberation effect is. Value range [0100]
  199. nRet = RtcEngine.SetLocalVoiceReverb(AUDIO_REVERB_TYPE.AUDIO_REVERB_ROOM_SIZE, 6);
  200. this.Log.UpdateLog(string.Format("SetLocalVoiceReverb nRet:{0}", nRet));
  201. // Initial delay length of wet signal, value range [0200], unit: ms
  202. nRet = RtcEngine.SetLocalVoiceReverb(AUDIO_REVERB_TYPE.AUDIO_REVERB_WET_DELAY, 124);
  203. this.Log.UpdateLog(string.Format("SetLocalVoiceReverb nRet:{0}", nRet));
  204. // The continuous intensity of reverberation effect. The value range is [0100]. The greater the value, the stronger the reverberation effect
  205. nRet = RtcEngine.SetLocalVoiceReverb(AUDIO_REVERB_TYPE.AUDIO_REVERB_STRENGTH, 78);
  206. this.Log.UpdateLog(string.Format("SetLocalVoiceReverb nRet:{0}", nRet));
  207. }
  208. #endregion
  209. }
  210. #region -- Agora Event ---
  211. internal class UserEventHandler : IRtcEngineEventHandler
  212. {
  213. private readonly VoiceChanger _voiceChanger;
  214. internal UserEventHandler(VoiceChanger voiceChanger)
  215. {
  216. _voiceChanger = voiceChanger;
  217. }
  218. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  219. {
  220. int build = 0;
  221. _voiceChanger.Log.UpdateLog(string.Format("sdk version: ${0}",
  222. _voiceChanger.RtcEngine.GetVersion(ref build)));
  223. _voiceChanger.Log.UpdateLog(string.Format(
  224. "onJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}", connection.channelId,
  225. connection.localUid, elapsed));
  226. }
  227. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  228. {
  229. _voiceChanger.Log.UpdateLog("OnLeaveChannelSuccess");
  230. }
  231. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  232. {
  233. _voiceChanger.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid,
  234. elapsed));
  235. }
  236. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  237. {
  238. _voiceChanger.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  239. (int)reason));
  240. }
  241. public override void OnError(int error, string msg)
  242. {
  243. _voiceChanger.Log.UpdateLog(
  244. string.Format("OnSDKError error: {0}, msg: {1}", error, msg));
  245. }
  246. public override void OnConnectionLost(RtcConnection connection)
  247. {
  248. _voiceChanger.Log.UpdateLog(string.Format("OnConnectionLost "));
  249. }
  250. }
  251. #endregion
  252. }