Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to control the volume of an audio card without disturbing the balance Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
Multimedia
Language
Delphi 2.x
Views
83
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			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   var
3     value: integer;
4     balance: real;
5     highChannel, lowChannel: byte;
6   begin
7     if percent > 1 then
8       percent := 1;
9     if percent < 0 then
10      percent := 0;
11    value := high(word) - round(percent * high(word));
12    if value > high(word) then
13      value := high(word);
14    if value < 0 then
15      value := 0;
16    aMixer.outputs[0].inputs[0].volume.beginUpdate;
17    if (aMixer.outputs[0].inputs[0].Volume.position[0] = 0) and
18      (aMixer.outputs[0].inputs[0].Volume.position[1] = 0) then
19    begin
20      {Both are muted, get old balance}
21      if oldBalance = 10 then
22        {oldBalance is set to 10 on program start (dummy value)}
23      begin
24        balance := 1;
25        highChannel := 0;
26        lowChannel := 1
27      end
28      else
29      begin
30        if oldBalance < 0 then
31        begin
32          highChannel := 0;
33          lowChannel := 1;
34          balance := oldBalance * -1
35        end
36        else
37        begin
38          highChannel := 1;
39          lowChannel := 0;
40          balance := oldBalance
41        end
42      end
43    end
44    else
45    begin
46      if aMixer.outputs[0].inputs[0].Volume.position[0] >
47        aMixer.outputs[0].inputs[0].Volume.position[1] then
48      begin
49        highChannel := 0;
50        lowChannel := 1
51      end
52      else
53      begin
54        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 then
63    begin
64      oldBalance := balance;
65      if highChannel = 0 then
66        oldBalance := oldBalance * -1
67    end;
68    aMixer.outputs[0].inputs[0].volume.endUpdate
69  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC