MasterGain
I'm a little confused about this. I have a JSlider which goes from 0 to 1.0 and then I apply the equation:
float dB = (float)(Math.log(gain)/Math.log(10.0)*20.0);
I then set the float control's value with:
gainControl.setValue(dB);
For some reason, the volume of the mp3 does not change whenever the user slides the bar. Here is some output:
Setting gain: -24.436975
Setting gain: -23.09804
Setting gain: -20.0
Setting gain: -18.416374
Setting gain: -17.077438
Setting gain: -16.478174
Setting gain: -15.917601
Setting gain: -15.391022
Setting gain: -14.89455
Setting gain: -14.424928
Setting gain: -13.555614
Setting gain: -13.1515465
Setting gain: -12.765443
Setting gain: -12.0412
Setting gain: -11.700533
Setting gain: -11.372725
Setting gain: -11.056839
Setting gain: -10.75204
Setting gain: -10.457575
Setting gain: -9.897
Setting gain: -9.118639
Setting gain: -8.404328
On the other hand, My MIDI volume control works. The code I use for that is:
MidiChannel[] channels = synth.getChannels();
for (int i=0; i<channels.length; i++){
channels[i].controlChange(7, (int)(gain * 127.0));
}
1. What range should I set the JSlider so that the gain values are not all negative when the equation is applied but that will also work for the MIDI volume.
2. Why is the mp3 volume not changing?
Thanks>
[1734 byte] By [
Nethera] at [2007-11-27 4:01:35]

log(10) = e, so you can replace Math.log(10.0)*20.0 with 20.0 * e.For the rest of that to be negative, the log(gain) value must be negative. Try printing out gain along with the result to see what it is.Mistook log() for log10()Message was edited by: hunter9000
> log(10) = e, so you can replace Math.log(10.0)*20.0
> with 20.0 * e.
>
> For the rest of that to be negative, the log(gain)
> value must be negative. Try printing out gain along
> with the result to see what it is.
>
The above is new math to me.
Log(e) = 1
Log(1) = 0
for any number a > 0 and a < 1,
Log(a) is negative
You've got to take logs of numbers greater than 1. Play w/ your equations a bit and you'll get there.
I changed the range of the sliderbar to 0-2.0Now I have dB values ranging from -30 to 6But I dont hear any change in the volume still.
> The above is new math to me.
> Log(e) = 1
> Log(1) = 0
>
> for any number a > 0 and a < 1,
> Log(a) is negative
I did make a mistake, log(10) is ~2.30, not e (~2.71). I should have double checked that.
But you misunderstood me about the rest, I meant that if the entire equation is negative, then the log(gain) term must be negative. For x times a positive number to be negative, x has to be negative. He needs to examine his gain input values to find out why his output is negative.
> I changed the range of the sliderbar to 0-2.0
> Now I have dB values ranging from -30 to 6
>
> But I dont hear any change in the volume still.
Your slider range isn't the problem, it's what you do with the value you get from the slider, and then how you use that to change the volume. Check the api for gainControl to make sure you're passing it a value in the range it accepts.
apparently the max is: 6.0206
and the min is: -80
I found this out from the errors it gave me:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Requested value -139.794 smaller than allowable minimum value -80.0.
But even when I set the dB values to these max and min, i hear no change in the volume!
> apparently the max is: 6.0206
> and the min is: -80
>
> I found this out from the errors it gave me:
> Exception in thread "AWT-EventQueue-0"
> java.lang.IllegalArgumentException: Requested value
> -139.794 smaller than allowable minimum value -80.0.
>
> But even when I set the dB values to these max and
> min, i hear no change in the volume!
If your numbers are now in the correct range, this suggests that the problem is the setting of the gain itself. Do you have more code to show? Forgetting the slider for now, can you throw in logical numbers into your setgain routine and change the volume this way?
> If your numbers are now in the correct range, this
> suggests that the problem is the setting of the gain
> itself. Do you have more code to show?
Yes I do:
//get controls
try{
AudioFormat audioFormat = new AudioFormat(44100f, 16, 1, false, false);
Line.Info info = new Line.Info(SourceDataLine.class);
SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info);
line.open(audioFormat);
if(line.isControlSupported(FloatControl.Type.MASTER_GAIN)){
gainControl = (FloatControl)line.getControl(FloatControl.Type.MASTER_GAIN);
setMasterGain(.5);
}
else{
System.err.println("No Master-Gain control");
}
if(line.isControlSupported(BooleanControl.Type.MUTE)){
muteControl = (BooleanControl)line.getControl(BooleanControl.Type.MUTE);
}
else{
System.err.println("No Mute Control");
}
}
catch(Exception e){
e.printStackTrace();
}
public static void setMasterGain(double value){
if(gainControl != null){
gain = value;
System.err.println("GAIN: "+gain);
float dB = (float)(Math.log(gain)/Math.log(10.0)*200.0);
System.err.println("DB: "+dB);
gainControl.setValue(dB);
}
}
> (float)(Math.log(gain)/Math.log(10.0)*200.0);
This is probably totally irrelevant, but you do know that the 200 above will be multiplied to the numerator, not the denominator, correct? To test this out, you can run this quick routine:
void testMultiplication()
{
double c;
c = 4.0/2*2;//= 4.0;
System.out.println("4/2*2= " + c);
c = 4.0/(2*2);// = 1.0
System.out.println("4/(2*2) = " + c);
}
yes, i know my order of operations.the problem is not the equation, its the mastergain control because even when i just set the value directly, it doesnt change the volume.