<commit_before>// 
// 
// Copyright (C) 2005-2006 SIPez LLC.
// Licensed to SIPfoundry under a Contributor Agreement.
// 
// Copyright (C) 2004-2006 SIPfoundry Inc.
// Licensed by SIPfoundry under the LGPL license.
// 
// Copyright (C) 2004-2006 Pingtel Corp.
// Licensed to SIPfoundry under a Contributor Agreement.
// 
// $$
//////////////////////////////////////////////////////////////////////////////

#include "mp/MpCodec.h"

#define DEF_HSVOLMAX 54
#define DEF_HSVOLSTEP 2
static int hsVolMax = DEF_HSVOLMAX;
static int hsVolStep = DEF_HSVOLSTEP;

// TODO
// The rest of this file is really two independent platform-specific
// implementations of the MpCodec module.  These should be broken out into
// separate files.

#if defined(_WIN32) /* [ */

#include <stdio.h>
#include <string.h>
#include <assert.h>


#include "os/OsUtil.h"
#include "mp/dmaTask.h"
#include "mp/MpCodec.h"

extern "C" void setHandsetMuteState(UtlBoolean state);
static int s_iGainLevel = 5 ;


OsStatus MpCodec_setGain(int level)
{
   s_iGainLevel = level ;
   OsStatus ret = OS_UNSPECIFIED;
   MMRESULT mmresult;

   // Open the mixer device
   HMIXER hmx;
   mmresult = mixerOpen(&hmx, 0, 0, 0, MIXER_OBJECTF_MIXER );

   if (MMSYSERR_NOERROR == mmresult)
   {
	   // Get the line info for the wave in destination line
	   MIXERLINE mxl;
	   memset(&mxl, 0, sizeof(MIXERLINE));
	   mxl.cbStruct = sizeof(mxl);
	   mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
	   mmresult = mixerGetLineInfo((HMIXEROBJ)hmx, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);

	   if (MMSYSERR_NOERROR == mmresult)
	   {
		   // Now find the microphone source line connected to this wave in
		   // destination
		   DWORD cConnections = mxl.cConnections;

		   for(DWORD j=0; j<cConnections; j++)
		   {
			  mxl.dwSource = j;
			  mixerGetLineInfo((HMIXEROBJ)hmx, &mxl, MIXER_GETLINEINFOF_SOURCE);
			  if (MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE == mxl.dwComponentType)
				 break;
		   }
		   
		   // Find a volume control, if any, of the microphone line
		   LPMIXERCONTROL pmxctrl = (LPMIXERCONTROL)calloc(1, sizeof MIXERCONTROL);
		   MIXERLINECONTROLS mxlctrl = {sizeof mxlctrl, mxl.dwLineID, MIXERCONTROL_CONTROLTYPE_VOLUME, 1, sizeof MIXERCONTROL, pmxctrl};
		   
		   if(!mixerGetLineControls((HMIXEROBJ) hmx, &mxlctrl, MIXER_GETLINECONTROLSF_ONEBYTYPE))
		   {
			  // Found!
			  DWORD cChannels = mxl.cChannels;
			  if (MIXERCONTROL_CONTROLF_UNIFORM & pmxctrl->fdwControl)
				 cChannels = 1;

			  LPMIXERCONTROLDETAILS_UNSIGNED pUnsigned =
			  (LPMIXERCONTROLDETAILS_UNSIGNED)  malloc(cChannels * sizeof MIXERCONTROLDETAILS_UNSIGNED);
		      
			  MIXERCONTROLDETAILS mxcd = {sizeof(mxcd), pmxctrl->dwControlID, cChannels, (HWND)0, 
				 sizeof MIXERCONTROLDETAILS_UNSIGNED, (LPVOID) pUnsigned};
			  mixerGetControlDetails((HMIXEROBJ)hmx, &mxcd, MIXER_SETCONTROLDETAILSF_VALUE);

			  // Set the volume to the the level  (for both channels as needed)
			  pUnsigned[0].dwValue = pUnsigned[cChannels - 1].dwValue = (DWORD)((float)pmxctrl->Bounds.dwMinimum+(float)pmxctrl->Bounds.dwMaximum) * ( (float)((float)level-1.0f) / 9.0f )  ;
			  mixerSetControlDetails((HMIXEROBJ)hmx, &mxcd, MIXER_SETCONTROLDETAILSF_VALUE);

			  free(pmxctrl);
			  free(pUnsigned);
		   }
		   else
		   {
			  free(pmxctrl);
		   }
		   mixerClose(hmx);

		   DmaTask::setMuteEnabled(s_iGainLevel <= 1); // if Mic level is 1 or less, mute
		   ret = OS_SUCCESS;
	   }
   }

   return ret;
}


int MpCodec_getGain()
{      
   return s_iGainLevel;
}


extern HWAVEOUT audioOutH;
extern HWAVEOUT audioOutCallH;

//setVolume for Win32
OsStatus MpCodec_setVolume(int level)
{
   OsStatus ret = OS_INVALID;
   
   //scale to our max value
   unsigned short volSeg = 0xFFFF/100;
   unsigned short newLeftVal = level * volSeg;
   unsigned short newRightVal = newLeftVal;
   
   //now set this to the left and right speakers
   DWORD bothVolume = (newRightVal << 16) + newLeftVal ;

   HWAVEOUT hOut = NULL ;
   if (DmaTask::isRingerEnabled())
   {
      hOut = audioOutH ;		
   }
   else
   {
	  hOut = audioOutCallH ;
   }

   
   if (hOut != NULL)
   {
      waveOutSetVolume(hOut, bothVolume) ;
      ret = OS_SUCCESS;
   }

   return ret;
}

int MpCodec_getVolume() 
{
   DWORD bothVolume;
   int volume;
   unsigned short volSeg = 0xFFFF/100;

   HWAVEOUT hOut = NULL ;
   if (DmaTask::isRingerEnabled())
   {
      hOut = audioOutH ;	
   }
   else
   {
	  hOut = audioOutCallH ;
   }

   
   if (hOut != NULL)
   {
      waveOutGetVolume(audioOutH, &bothVolume) ;

      //mask out one
      unsigned short rightChannel = ((unsigned short) bothVolume & 0xFFFF) ;
      volume = rightChannel /volSeg;         
   }

   return volume;
}


/* dummy routine */
int MpCodec_isSpeakerOn() { return 0;}

/* dummy routine */
OsStatus MpCodec_doProcessFrame() { return OS_SUCCESS;}

#elif defined(__pingtel_on_posix__) /* WIN32 ] [ */

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "os/OsUtil.h"
#include "mp/dmaTask.h"<commit_msg>XMR-62:BUGFIX: In MpCodec_getVolume() wrong handle was passed to waveOutGetVolume() function. This lead to wrong volume returned.<commit_after>// 
// 
// Copyright (C) 2005-2006 SIPez LLC.
// Licensed to SIPfoundry under a Contributor Agreement.
// 
// Copyright (C) 2004-2006 SIPfoundry Inc.
// Licensed by SIPfoundry under the LGPL license.
// 
// Copyright (C) 2004-2006 Pingtel Corp.
// Licensed to SIPfoundry under a Contributor Agreement.
// 
// $$
//////////////////////////////////////////////////////////////////////////////

#include "mp/MpCodec.h"

#define DEF_HSVOLMAX 54
#define DEF_HSVOLSTEP 2
static int hsVolMax = DEF_HSVOLMAX;
static int hsVolStep = DEF_HSVOLSTEP;

// TODO
// The rest of this file is really two independent platform-specific
// implementations of the MpCodec module.  These should be broken out into
// separate files.

#if defined(_WIN32) /* [ */

#include <stdio.h>
#include <string.h>
#include <assert.h>


#include "os/OsUtil.h"
#include "mp/dmaTask.h"
#include "mp/MpCodec.h"

extern "C" void setHandsetMuteState(UtlBoolean state);
static int s_iGainLevel = 5 ;


OsStatus MpCodec_setGain(int level)
{
   s_iGainLevel = level ;
   OsStatus ret = OS_UNSPECIFIED;
   MMRESULT mmresult;

   // Open the mixer device
   HMIXER hmx;
   mmresult = mixerOpen(&hmx, 0, 0, 0, MIXER_OBJECTF_MIXER );

   if (MMSYSERR_NOERROR == mmresult)
   {
	   // Get the line info for the wave in destination line
	   MIXERLINE mxl;
	   memset(&mxl, 0, sizeof(MIXERLINE));
	   mxl.cbStruct = sizeof(mxl);
	   mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE;
	   mmresult = mixerGetLineInfo((HMIXEROBJ)hmx, &mxl, MIXER_GETLINEINFOF_COMPONENTTYPE);

	   if (MMSYSERR_NOERROR == mmresult)
	   {
		   // Now find the microphone source line connected to this wave in
		   // destination
		   DWORD cConnections = mxl.cConnections;

		   for(DWORD j=0; j<cConnections; j++)
		   {
			  mxl.dwSource = j;
			  mixerGetLineInfo((HMIXEROBJ)hmx, &mxl, MIXER_GETLINEINFOF_SOURCE);
			  if (MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE == mxl.dwComponentType)
				 break;
		   }
		   
		   // Find a volume control, if any, of the microphone line
		   LPMIXERCONTROL pmxctrl = (LPMIXERCONTROL)calloc(1, sizeof MIXERCONTROL);
		   MIXERLINECONTROLS mxlctrl = {sizeof mxlctrl, mxl.dwLineID, MIXERCONTROL_CONTROLTYPE_VOLUME, 1, sizeof MIXERCONTROL, pmxctrl};
		   
		   if(!mixerGetLineControls((HMIXEROBJ) hmx, &mxlctrl, MIXER_GETLINECONTROLSF_ONEBYTYPE))
		   {
			  // Found!
			  DWORD cChannels = mxl.cChannels;
			  if (MIXERCONTROL_CONTROLF_UNIFORM & pmxctrl->fdwControl)
				 cChannels = 1;

			  LPMIXERCONTROLDETAILS_UNSIGNED pUnsigned =
			  (LPMIXERCONTROLDETAILS_UNSIGNED)  malloc(cChannels * sizeof MIXERCONTROLDETAILS_UNSIGNED);
		      
			  MIXERCONTROLDETAILS mxcd = {sizeof(mxcd), pmxctrl->dwControlID, cChannels, (HWND)0, 
				 sizeof MIXERCONTROLDETAILS_UNSIGNED, (LPVOID) pUnsigned};
			  mixerGetControlDetails((HMIXEROBJ)hmx, &mxcd, MIXER_SETCONTROLDETAILSF_VALUE);

			  // Set the volume to the the level  (for both channels as needed)
			  pUnsigned[0].dwValue = pUnsigned[cChannels - 1].dwValue = (DWORD)((float)pmxctrl->Bounds.dwMinimum+(float)pmxctrl->Bounds.dwMaximum) * ( (float)((float)level-1.0f) / 9.0f )  ;
			  mixerSetControlDetails((HMIXEROBJ)hmx, &mxcd, MIXER_SETCONTROLDETAILSF_VALUE);

			  free(pmxctrl);
			  free(pUnsigned);
		   }
		   else
		   {
			  free(pmxctrl);
		   }
		   mixerClose(hmx);

		   DmaTask::setMuteEnabled(s_iGainLevel <= 1); // if Mic level is 1 or less, mute
		   ret = OS_SUCCESS;
	   }
   }

   return ret;
}


int MpCodec_getGain()
{      
   return s_iGainLevel;
}


extern HWAVEOUT audioOutH;
extern HWAVEOUT audioOutCallH;

//setVolume for Win32
OsStatus MpCodec_setVolume(int level)
{
   OsStatus ret = OS_INVALID;
   
   //scale to our max value
   unsigned short volSeg = 0xFFFF/100;
   unsigned short newLeftVal = level * volSeg;
   unsigned short newRightVal = newLeftVal;
   
   //now set this to the left and right speakers
   DWORD bothVolume = (newRightVal << 16) + newLeftVal ;

   HWAVEOUT hOut = NULL ;
   if (DmaTask::isRingerEnabled())
   {
      hOut = audioOutH ;		
   }
   else
   {
	  hOut = audioOutCallH ;
   }

   
   if (hOut != NULL)
   {
      waveOutSetVolume(hOut, bothVolume) ;
      ret = OS_SUCCESS;
   }

   return ret;
}

int MpCodec_getVolume() 
{
   DWORD bothVolume;
   int volume;
   unsigned short volSeg = 0xFFFF/100;

   HWAVEOUT hOut = NULL ;
   if (DmaTask::isRingerEnabled())
   {
      hOut = audioOutH ;	
   }
   else
   {
	  hOut = audioOutCallH ;
   }

   
   if (hOut != NULL)
   {
      waveOutGetVolume(hOut, &bothVolume) ;

      //mask out one
      unsigned short rightChannel = ((unsigned short) bothVolume & 0xFFFF) ;
      volume = rightChannel /volSeg;         
   }

   return volume;
}


/* dummy routine */
int MpCodec_isSpeakerOn() { return 0;}

/* dummy routine */
OsStatus MpCodec_doProcessFrame() { return OS_SUCCESS;}

#elif defined(__pingtel_on_posix__) /* WIN32 ] [ */

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "os/OsUtil.h"
#include "mp/dmaTask.h"
{'fraction_non_alphanumeric': 0.1186741639538325, 'fraction_numerical': 0.012627009963500049, 'mean_word_length': 3.037435284747113, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4017280', 'n_tokens_mistral': 4070, 'n_tokens_neox': 3638, 'n_words': 836}