DelphiFAQ Home Search:

Change left and right channel volume individually from Delphi

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

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.

Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: