Author: Tomas Rutkauskas
I can easily adjust the left and right channels independently on an audio card
using the mixer API. My question is: How do I have one trackbar set the overall
volume without disturbing the current balance between channels? Or how do I use two
trackbars, but "lock" them together? Basically I need to directly manipulate one
channel, but have the other one follow while keeping the relative relationship
between the two channels the same.
Answer:
Here is the method I use. It uses a mixer component though, but I believe the code
should make sense.
1 procedure setVolume(percent: real);
2 var3 value: integer;
4 balance: real;
5 highChannel, lowChannel: byte;
6 begin7 if percent > 1 then8 percent := 1;
9 if percent < 0 then10 percent := 0;
11 value := high(word) - round(percent * high(word));
12 if value > high(word) then13 value := high(word);
14 if value < 0 then15 value := 0;
16 aMixer.outputs[0].inputs[0].volume.beginUpdate;
17 if (aMixer.outputs[0].inputs[0].Volume.position[0] = 0) and18 (aMixer.outputs[0].inputs[0].Volume.position[1] = 0) then19 begin20 {Both are muted, get old balance}21 if oldBalance = 10 then22 {oldBalance is set to 10 on program start (dummy value)}23 begin24 balance := 1;
25 highChannel := 0;
26 lowChannel := 1
27 end28 else29 begin30 if oldBalance < 0 then31 begin32 highChannel := 0;
33 lowChannel := 1;
34 balance := oldBalance * -1
35 end36 else37 begin38 highChannel := 1;
39 lowChannel := 0;
40 balance := oldBalance
41 end42 end43 end44 else45 begin46 if aMixer.outputs[0].inputs[0].Volume.position[0] >
47 aMixer.outputs[0].inputs[0].Volume.position[1] then48 begin49 highChannel := 0;
50 lowChannel := 1
51 end52 else53 begin54 highChannel := 1;
55 lowChannel := 0
56 end;
57 balance := aMixer.outputs[0].inputs[0].Volume.position[lowChannel] /
58 aMixer.outputs[0].inputs[0].Volume.position[highChannel]
59 end;
60 aMixer.outputs[0].inputs[0].Volume.position[highChannel] := value;
61 aMixer.outputs[0].inputs[0].Volume.position[lowChannel] := round(value * balance);
62 if value > 0 then63 begin64 oldBalance := balance;
65 if highChannel = 0 then66 oldBalance := oldBalance * -1
67 end;
68 aMixer.outputs[0].inputs[0].volume.endUpdate
69 end;