Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
6.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // voice_tweak.cpp : Defines the class behaviors for the application.
  9. //
  10. #include "stdafx.h"
  11. #include <assert.h>
  12. #include "voice_tweak.h"
  13. #include "waveout.h"
  14. #include "voice_tweakDlg.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. //extern IVoiceRecord* CreateVoiceRecord_WaveIn(int sampleRate);
  21. extern IVoiceRecord* CreateVoiceRecord_DSound(int sampleRate);
  22. typedef enum
  23. {
  24. LANGUAGE_ENGLISH=0,
  25. LANGUAGE_SPANISH=1,
  26. LANGUAGE_FRENCH=2,
  27. LANGUAGE_ITALIAN=3,
  28. LANGUAGE_GERMAN=4,
  29. LANGUAGE_COUNT=5
  30. } VoiceTweakLanguageID;
  31. VoiceTweakLanguageID g_CurrentLanguage = LANGUAGE_ENGLISH;
  32. #define LANGENTRY(name) {IDS_##name##, IDS_SPANISH_##name##, IDS_FRENCH_##name##, IDS_ITALIAN_##name##, IDS_GERMAN_##name##}
  33. int g_StringIDs[][LANGUAGE_COUNT] =
  34. {
  35. LANGENTRY(HELPTEXT),
  36. LANGENTRY(ERROR),
  37. LANGENTRY(CANTFINDMICBOOST),
  38. LANGENTRY(CANTFINDMICVOLUME),
  39. LANGENTRY(CANTFINDMICMUTE),
  40. LANGENTRY(CANTCREATEWAVEIN),
  41. LANGENTRY(CANTLOADVOICEMODULE),
  42. LANGENTRY(CANTCREATEWAVEOUT),
  43. LANGENTRY(NODPLAYVOICE),
  44. LANGENTRY(WINDOWTITLE),
  45. LANGENTRY(OKAY),
  46. LANGENTRY(CANCEL),
  47. LANGENTRY(SYSTEMSETUP),
  48. LANGENTRY(HELP),
  49. LANGENTRY(VOICEINPUT),
  50. LANGENTRY(VOLUME),
  51. LANGENTRY(ENABLEGAIN),
  52. };
  53. #define NUM_STRINGIDS (sizeof(g_StringIDs)/sizeof(g_StringIDs[0]))
  54. // Pass in the english string ID, and this returns the string ID in the current language.
  55. int MapLanguageStringID( int idEnglish )
  56. {
  57. for( int i=0; i < NUM_STRINGIDS; i++ )
  58. {
  59. if( idEnglish == g_StringIDs[i][LANGUAGE_ENGLISH] )
  60. return g_StringIDs[i][g_CurrentLanguage];
  61. }
  62. assert( !"MapLanguageStringID: unknown string ID" );
  63. return 0;
  64. }
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CVoiceTweakApp
  67. BEGIN_MESSAGE_MAP(CVoiceTweakApp, CWinApp)
  68. //{{AFX_MSG_MAP(CVoiceTweakApp)
  69. // NOTE - the ClassWizard will add and remove mapping macros here.
  70. // DO NOT EDIT what you see in these blocks of generated code!
  71. //}}AFX_MSG
  72. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  73. END_MESSAGE_MAP()
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CVoiceTweakApp construction
  76. CVoiceTweakApp::CVoiceTweakApp()
  77. {
  78. m_pVoiceRecord = 0;
  79. m_pWaveOut = 0;
  80. m_pMixerControls = 0;
  81. }
  82. CVoiceTweakApp::~CVoiceTweakApp()
  83. {
  84. StopDevices();
  85. }
  86. bool CVoiceTweakApp::StartDevices()
  87. {
  88. StopDevices();
  89. CString str, errStr;
  90. // Setup wave in.
  91. if( !(m_pVoiceRecord = CreateVoiceRecord_DSound(VOICE_TWEAK_SAMPLE_RATE)) )
  92. {
  93. //if( !(m_pVoiceRecord = CreateVoiceRecord_WaveIn(VOICE_TWEAK_SAMPLE_RATE)) )
  94. {
  95. str.LoadString( MapLanguageStringID(IDS_CANTCREATEWAVEIN) );
  96. ::MessageBox(NULL, str, errStr, MB_OK);
  97. return false;
  98. }
  99. }
  100. m_pVoiceRecord->RecordStart();
  101. if( !(m_pWaveOut = CreateWaveOut(VOICE_TWEAK_SAMPLE_RATE)) )
  102. {
  103. str.LoadString( MapLanguageStringID(IDS_CANTCREATEWAVEOUT) );
  104. ::MessageBox(NULL, str, errStr, MB_OK);
  105. return false;
  106. }
  107. return true;
  108. }
  109. void CVoiceTweakApp::StopDevices()
  110. {
  111. if(m_pVoiceRecord)
  112. {
  113. m_pVoiceRecord->Release();
  114. m_pVoiceRecord = NULL;
  115. }
  116. if(m_pWaveOut)
  117. {
  118. m_pWaveOut->Release();
  119. m_pWaveOut = NULL;
  120. }
  121. }
  122. /////////////////////////////////////////////////////////////////////////////
  123. // The one and only CVoiceTweakApp object
  124. CVoiceTweakApp theApp;
  125. char const* FindArg(char const *pName)
  126. {
  127. for(int i=0; i < __argc; i++)
  128. if(stricmp(__argv[i], pName) == 0)
  129. return ((i+1) < __argc) ? __argv[i+1] : "";
  130. return NULL;
  131. }
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CVoiceTweakApp initialization
  134. BOOL CVoiceTweakApp::InitInstance()
  135. {
  136. // Set the thread locale so it grabs the string resources for the right language. If
  137. // we don't have resources for the system default language, it just uses English.
  138. if( FindArg("-french") )
  139. g_CurrentLanguage = LANGUAGE_FRENCH;
  140. else if( FindArg("-spanish") )
  141. g_CurrentLanguage = LANGUAGE_SPANISH;
  142. else if(FindArg("-italian"))
  143. g_CurrentLanguage = LANGUAGE_ITALIAN;
  144. else if(FindArg("-german"))
  145. g_CurrentLanguage = LANGUAGE_GERMAN;
  146. else
  147. g_CurrentLanguage = LANGUAGE_ENGLISH;
  148. CString errStr, str;
  149. errStr.LoadString( MapLanguageStringID(IDS_ERROR) );
  150. m_pMixerControls = GetMixerControls();
  151. // Initialize the mixer controls.
  152. bool bFoundVolume, bFoundMute;
  153. float volume, mute;
  154. bFoundVolume = m_pMixerControls->GetValue_Float(IMixerControls::Control::MicVolume, volume);
  155. bFoundMute = m_pMixerControls->GetValue_Float(IMixerControls::Control::MicMute, mute);
  156. if(!bFoundVolume)
  157. {
  158. str.LoadString( MapLanguageStringID(IDS_CANTFINDMICVOLUME) );
  159. ::MessageBox(NULL, str, errStr, MB_OK);
  160. return FALSE;
  161. }
  162. if(!bFoundMute)
  163. {
  164. str.LoadString( MapLanguageStringID(IDS_CANTFINDMICMUTE) );
  165. ::MessageBox(NULL, str, errStr, MB_OK);
  166. return FALSE;
  167. }
  168. // Set mute and boost for them automatically.
  169. m_pMixerControls->SetValue_Float(IMixerControls::Control::MicMute, 1);
  170. // We cycle the mic boost because for some reason Windows misses the first call to set it to 1, but
  171. // if the user clicks the checkbox on and off again, it works.
  172. m_pMixerControls->SetValue_Float(IMixerControls::Control::MicBoost, 1);
  173. m_pMixerControls->SetValue_Float(IMixerControls::Control::MicBoost, 0);
  174. m_pMixerControls->SetValue_Float(IMixerControls::Control::MicBoost, 1);
  175. // Enable the mic for wave input.
  176. m_pMixerControls->SelectMicrophoneForWaveInput();
  177. if(!StartDevices())
  178. return false;
  179. // Standard initialization
  180. // If you are not using these features and wish to reduce the size
  181. // of your final executable, you should remove from the following
  182. // the specific initialization routines you do not need.
  183. #ifdef _AFXDLL
  184. Enable3dControls(); // Call this when using MFC in a shared DLL
  185. #else
  186. Enable3dControlsStatic(); // Call this when linking to MFC statically
  187. #endif
  188. CVoiceTweakDlg dlg;
  189. m_pMainWnd = &dlg;
  190. int nResponse = dlg.DoModal();
  191. if (nResponse == IDOK)
  192. {
  193. // TODO: Place code here to handle when the dialog is
  194. // dismissed with OK
  195. }
  196. else if (nResponse == IDCANCEL)
  197. {
  198. // TODO: Place code here to handle when the dialog is
  199. // dismissed with Cancel
  200. }
  201. // Since the dialog has been closed, return FALSE so that we exit the
  202. // application, rather than start the application's message pump.
  203. return FALSE;
  204. }