2005. március 27., vasárnap

How to control the MIDI speaker output volume


Problem/Question/Abstract:

How can I control the MIDI speaker output volume? If that's not directly possible: how can I programmatically open the volume control?

Answer:

First you need to ID the device


tmpreg := TRegistry.Create;
tmpreg.RootKey := HKEY_CURRENT_USER;
tmpreg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Multimedia\MIDIMap', false);


{ ... }
if tmpreg.ValueExists('CurrentInstrument') then
begin
  MidiOutPutDev := tmpreg.ReadString('CurrentInstrument');
end;
tmpreg.destroy;
{ ... }

Then get a handle of the device


amt := MidiOutGetNumDevs;
MidiOutputDevid := -1;
for t := 1 to amt do
begin
  MidiOutGetDevCaps(t - 1, @Midicap, Sizeof(Midicap));
  if Strpas(@MidiCap.szPName) = MidiOutPutDev then
  begin
    MidiOutputDevid := t - 1;
  end;
end;


Then set the volume either master or seperate


procedure SetVolumeMidi(RVolume, LVolume: Cardinal);
begin
  midiOutSetVolume(MidiOutputDevid, (RVolume * 256 * 256) + LVolume);
end;

procedure SetMVolumeWave(Volume: Cardinal);
var
  pl, pr: Cardinal;
begin
  pr := (WRPan * Volume) div 100;
  pl := (WLPan * Volume) div 100;
  waveOutSetVolume(WaveOutputDevid, (pr * 256 * 256) + pl);
end;


Include mmsystem in your Uses clause

Nincsenek megjegyzések:

Megjegyzés küldése