DelphiFAQ Home Search:
General :: Programming :: Delphi
General Delphi related information.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

Change left and right channel volume individually from Delphi

Question:

I want to change the volume of the left and right channel individually from my Delphi application. How do I do this?

Answer:

The current volume settings can be retrieved with WaveoutGetVolume. Windows represents this as a 64-bit unsigned integer. (DWord)
The upper two bytes are the right channel (unsigned 32 bit integer, Word) and the lower two bytes are the left channel.

After retrieving the current settings, you can update each channel individually and then use WaveoutSetVolume to save your setting.

Below are two procedures that demonstrate how this can be done. Each channel can be set to a value between 0 and 64k (0 .. 65535).

The necessary Windows functions WaveoutGetVolume, WaveoutSetVolume are interfaced in unit MMSystem.

program VolumeTest;
 
 uses
   MMSystem;
 
 procedure LeftVolume(Value: DWord);
 var
   Rvol,
   temp: DWord;
 begin { LeftVolume }
   // WaveoutGetVolume() retrieves the current volume
  WaveoutGetVolume(WAVE_MAPPER, @temp);
   Rvol := hiword(temp);
   asm
         shl  Rvol,16
   end;
   Rvol := Rvol and $ffff0000;
   WaveoutSetVolume(WAVE_MAPPER, Value or Rvol)
 end; { LeftVolume }
 
 
 procedure RightVolume(Value: DWord);
 var 
   Lvol, 
   temp: DWord; 
 begin { RightVolume } 
   // WaveoutGetVolume() retrieves the current volume 
  WaveoutGetVolume(WAVE_MAPPER, @temp); 
   Lvol := Loword(temp); 
   asm 
         shl  value,16 
   end; 
   Value := Value and $ffff0000; 
   WaveoutSetVolume(WAVE_MAPPER, Value or Lvol)
 end; { RightVolume } 
 
 
 begin 
   Left_volume(20000);
 end.

Generated 4:01:47 on Nov 24, 2017