Help - Search - Members - Calendar
Full Version: C++ programming question
Hydrogenaudio Forums > Hydrogenaudio Forum > General Audio
Parkin_m
Hi, Im new thought i would ask a question on here (not sure if anyone will be able to help)

Im writing a program in c++ that opens modifys and saves a wav file.

i have a function that changes the gain:

CODE
    void processGain(void)
    {
 //variables declared
 int i;
 double gain;
 double dB;
 
 cout<<endl;
 cout<<"GAIN"<<endl;
 cout<<"Enter desired gain: ";
 cin>>gain;
 //into dB
 dB = 20*log10(gain);
 
 cout<<"dB gain of "<<dB<<endl;
 
 for (i=0; i<length; i++)
 {
     sample[i]=(sample[i])*gain;
 }
    }


the problem with this function is: it changes the gain using a number, then displays the DB change.

i want to be able to be able to have the user enter a dB, then for the program to enter this and reduce/gain by that amount.
johny5
If you mean the inverse function, it is:
gain = pow(10,dB/20);

to use pow you should import the math library
Parkin_m
do you have MSN? Is there any possiblitly we could have a quick chat on there, or on a chat room?

I would be really grateful!

Parkin_m@hotmail.com

cheers mike
ErikS
[redundant post removed]
Parkin_m
QUOTE (ErikS @ Apr 17 2005, 12:22 PM)
[redundant post removed]
*


what does that mean?
Parkin_m
If anyone can help, please add me! biggrin.gif
HotshotGG
Most compilers should come with a standard math library. In order to use the power function you need to make sure you are using standard math library that comes with your compiler. I have one question for you? you speak of adjusting volume if you input a particular set of PCM samples into the program so that it can adjust the gain, but were are your file I/O routines? don't you need to be using something like fstream.h to do binary processing? unsure.gif I am sure somebody with more programming experience can help you.
Parkin_m
QUOTE (HotshotGG @ Apr 17 2005, 12:34 PM)
Most compilers should come with a standard math library. In order to use the power function you need to make sure you are using standard math library that comes with your compiler. I have one question for you? you speak of adjusting volume if you input a particular set of PCM samples into the program so that it can adjust the gain, but were are your file I/O routines? don't you need to be using something like fstream.h to do binary processing?  unsure.gif I am sure somebody with more programming experience can help you.
*

i only included the part of the program i was having trouble with.
I have a whole class that opens and closes a wav file. this is just a function which edits it. so yes them files are already called from the library.
Otto42
QUOTE (Parkin_m @ Apr 17 2005, 12:11 PM)
Hi, Im new thought i would ask a question on here (not sure if anyone will be able to help)

Im writing a program in c++ that opens modifys and saves a wav file.
...
the problem with this function is: it changes the gain using a number, then displays the DB change.

i want to be able to be able to have the user enter a dB, then for the program to enter this and reduce/gain by that amount.
*


Try something similar to this (this is just psuedocode):
CODE
#include <math.h>

float dbvalue;
cout << "Enter dB adjustment: ";
cin >> dbvalue;

float multiplier = pow(10,dbvalue * 0.05);

for (i=0; i<length; i++)
{
 sample[i]=(sample[i])*multiplier;
}


Basically, you take the dB adjustment, and plug it into this equation:
multiplier = 10 ^ (dB / 20);

This turns the dB adjustment into a number which you then want to multiply every sample by. Simple.

Obviously, it's more complex than that. For WAV files, you have an upper and lower end than you have to check for, because you'll be clipping the sound sometimes. So it'll be slightly more complex than the above.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.