 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dave Atkin Guest
|
Posted: Thu Aug 10, 2006 8:11 am Post subject: Normalize WAV file? |
|
|
Hi all,
Is there an easy way to normalize a wav file using Delphi.
In theory I know that I need to scan the data chunk of the file and look for
the loudest sample, calculate how much louder that sample could go without
clipping, and then bring up the volume of all the samples by that amount.
In practise I have no idea how to do it! I'm a newbie when it comes to
working with wav files, so any pointers will be gratefully received!
Best regards,
Dave Atkin |
|
| Back to top |
|
 |
Boian Mitov Guest
|
Posted: Thu Aug 10, 2006 8:11 am Post subject: Re: Normalize WAV file? |
|
|
Hi Dave,
You can use the Win32 API :
var
Rtn : Integer;
AudioInfo : TAVIStreamInfo;
....
....
Rtn := AVIFileOpen(@FAVIFile, @(FFileName[1]), 0, nil);
if ( Rtn <> 0) then
Exit;
// Get Audio Info.
AVIFileGetStream(FAVIFile, @FAudioStream, streamtypeAUDIO, 0);
if( FAudioStream = NIL ) then
begin
AVIFileRelease(FAVIFile);
Exit;
end;
AVIStreamInfo(FAudioStream, @AudioInfo, sizeof(AudioInfo));
....
....
// In a loop:
if( AVIStreamRead( FAudioStream, FReadPosition, Size, PData,
ABufferLength, @ABufferLength, @ALengthRead ) <>
0 ) then
begin
Result := -1;
Exit;
end;
....
....
if( FAudioStream <> NIL )then
AVIStreamRelease(FAudioStream);
if( FAVIFile <> NIL ) then
AVIFileRelease(FAVIFile);
FAudioStream := nil;
You can also use AudioLab - www.mitov.com which is free for personal
usage. The code snippets are from parts of the library .
With best regards,
Boian Mitov
Dave Atkin wrote:
| Quote: | Hi all,
Is there an easy way to normalize a wav file using Delphi.
In theory I know that I need to scan the data chunk of the file and look for
the loudest sample, calculate how much louder that sample could go without
clipping, and then bring up the volume of all the samples by that amount.
In practise I have no idea how to do it! I'm a newbie when it comes to
working with wav files, so any pointers will be gratefully received! |
|
|
| Back to top |
|
 |
Dave Atkin Guest
|
Posted: Fri Aug 11, 2006 4:30 am Post subject: Re: Normalize WAV file? |
|
|
Hi Boian,
Thanks for the info.
| Quote: | Rtn := AVIFileOpen(@FAVIFile, @(FFileName[1]), 0, nil);
if ( Rtn <> 0) then
Exit;
// Get Audio Info.
AVIFileGetStream(FAVIFile, @FAudioStream, streamtypeAUDIO, 0);
if( FAudioStream = NIL ) then
begin
AVIFileRelease(FAVIFile);
Exit;
end;
AVIStreamInfo(FAudioStream, @AudioInfo, sizeof(AudioInfo));
etc...
|
I notice the code refers to AVI Files - I'm working with WAV Files, does
this make a difference?
Best regards,
Dave Atkin |
|
| Back to top |
|
 |
Boian Mitov Guest
|
Posted: Fri Aug 11, 2006 10:00 pm Post subject: Re: Normalize WAV file? |
|
|
Hi Dave,
It will work .
With best regards,
Boian Mitov
Dave Atkin wrote:
| Quote: | I notice the code refers to AVI Files - I'm working with WAV Files, does
this make a difference? |
|
|
| Back to top |
|
 |
Dave Atkin Guest
|
Posted: Sat Aug 12, 2006 6:16 am Post subject: Re: Normalize WAV file? |
|
|
Hi Boian,
Thanks again, but could you explain which part of the code does the volume
checking, and which part does the actual normalization - I'm finding it hard
to follow and I need to understand how the process works.
My knowledge is very limited in this area :)
Best regards,
Dave Atkin. |
|
| Back to top |
|
 |
Boian Mitov Guest
|
Posted: Sat Aug 12, 2006 8:11 am Post subject: Re: Normalize WAV file? |
|
|
Hi Dave,
Actually, I just sent you the code that will read the WAVE data into
your own buffer. The normalization is up to you from that point .
With best regards,
Boian Mitov
Dave Atkin wrote:
| Quote: | Thanks again, but could you explain which part of the code does the volume
checking, and which part does the actual normalization - I'm finding it hard
to follow and I need to understand how the process works. |
|
|
| Back to top |
|
 |
Dave Atkin Guest
|
Posted: Mon Aug 14, 2006 2:30 am Post subject: Re: Normalize WAV file? |
|
|
Hi Boian & all,
My original post was a question about "normalizing a WAV file", not about
reading one into a buffer, which I already know how to do! Boian Mitov has
kindly showed me another way to read the wave data into a buffer, but I
still need some pointers as to how to calculate the loudest sample, and how
to subsequently normalize the data - which is what I asked in my original
post. If anyone is able to help here I would be extremely grateful :)
Best regards,
Dave Atkin |
|
| Back to top |
|
 |
Boian Mitov Guest
|
Posted: Mon Aug 14, 2006 2:57 am Post subject: Re: Normalize WAV file? |
|
|
Hi Dave,
I am sorry, I misunderstood the question.
Knowing the Wave format, you can move from sample to sample. The
nBlockAlign field of the format will give you the step. If the data is 8
bit (wBitsPerSample = , each sample fits in a character, if
wBitsPerSample = 16 - ShortInt, nChannels gives you the number of
channels. The procedure is simple. You go from sample to sample and you
compare the absolute value with the saved MaxValue to thi point. If the
sample value is bigger than MaxValue, you set MaxValue := SampleValue.
When you are done, you have the MaxValue, and then you do a second pass
to normalize.
With best regards,
Boian Mitov
Dave Atkin wrote:
| Quote: | Hi Boian & all,
My original post was a question about "normalizing a WAV file", not about
reading one into a buffer, which I already know how to do! Boian Mitov has
kindly showed me another way to read the wave data into a buffer, but I
still need some pointers as to how to calculate the loudest sample, and how
to subsequently normalize the data - which is what I asked in my original
post. If anyone is able to help here I would be extremely grateful
|
|
|
| Back to top |
|
 |
Boian Mitov Guest
|
Posted: Mon Aug 14, 2006 3:54 am Post subject: Re: Normalize WAV file? |
|
|
BTW: The normalization coefficient is:
K := Range / MaxValue;
where:
Range := ( 1 shl wBitsPerSample ) / 2;
Boian Mitov wrote:
> I am sorry, I misunderstood the question. |
|
| Back to top |
|
 |
Dave Atkin Guest
|
Posted: Mon Aug 14, 2006 6:31 am Post subject: Re: Normalize WAV file? |
|
|
Hi Boian,
| Quote: | Knowing the Wave format, you can move from sample to sample. The
nBlockAlign field of the format will give you the step. If the data is 8
bit (wBitsPerSample = , each sample fits in a character, if
wBitsPerSample = 16 - ShortInt, nChannels gives you the number of
channels. The procedure is simple. You go from sample to sample and you
compare the absolute value with the saved MaxValue to thi point. If the
sample value is bigger than MaxValue, you set MaxValue := SampleValue.
When you are done, you have the MaxValue, and then you do a second pass
to normalize.
BTW: The normalization coefficient is:
K := Range / MaxValue;
where:
Range := ( 1 shl wBitsPerSample ) / 2;
|
Thanks for your explanation, I understand much better now :)
I am only working with 16 bit samples, so obviously the value can be
represented by a ShortInt, but just out of interest - how do you represent
the value of a 24 bit sample? Do you need to declare a record containing a
Byte & a ShortInt as separate fields, or would you use an array[0..2] of
Byte? Just interested for future reference...
Best regards,
Dave Atkin |
|
| Back to top |
|
 |
Boian Mitov Guest
|
Posted: Mon Aug 14, 2006 6:38 am Post subject: Re: Normalize WAV file? |
|
|
Hi Dave,
Well... This is a different ball game . We are just adding the 24
bit support in our libraries, and it is a bit of challenge. First not
all the API supports 24 bits. Second, the description structure is
expanded, so all the code has to be changed to accommodate the new
bigger structure, finally the data is packed, so each sample is not word
aligned. The result is a fair number of shifts and OR operation.
Hopefully, we are using IPP functions to optimize the speed .
With best regards,
Boian Mitov
Dave Atkin wrote:
| Quote: | Hi Boian,
Thanks for your explanation, I understand much better now :)
I am only working with 16 bit samples, so obviously the value can be
represented by a ShortInt, but just out of interest - how do you represent
the value of a 24 bit sample? Do you need to declare a record containing a
Byte & a ShortInt as separate fields, or would you use an array[0..2] of
Byte? Just interested for future reference... |
|
|
| Back to top |
|
 |
Atmapuri Guest
|
Posted: Wed Aug 16, 2006 1:10 pm Post subject: Re: Normalize WAV file? |
|
|
Hi!
| Quote: | Is there an easy way to normalize a wav file using Delphi.
In theory I know that I need to scan the data chunk of the file and look
for
the loudest sample, calculate how much louder that sample could go without
clipping, and then bring up the volume of all the samples by that amount.
In practise I have no idea how to do it! I'm a newbie when it comes to
working with wav files, so any pointers will be gratefully received!
|
Normalization means that you ensure that the mean value of
the wav is zero. This can be achieved with a highpass filter
or simply by computing mean of entire wav file and then
subtracting it.
Normalization is also reffered to when storing floating point wav
files. You search for the abs max and divide by it so that
the maximum comes between -1 and +1. Once you read
the file you determine the bit resolution of A/D and scale
it up.
You can read 24 bit wav files with our library and also
apply a highpass filter. The library is free for educational
purposes.
www.dewresearch.com
Regards!
Atmapuri |
|
| Back to top |
|
 |
Dave Atkin Guest
|
Posted: Thu Aug 17, 2006 7:03 am Post subject: Re: Normalize WAV file? |
|
|
Hi Atmapuri
| Quote: | Normalization means that you ensure that the mean value of
the wav is zero
|
This sounds more like DC offset correction...
| Quote: | This can be achieved with a highpass filter
|
How does that work?
A high pass filter will attenuate the frequencies below the cutoff point,
and pass only the frequencies above that point unattenuated.
Im talking about modifying the wav data so that it plays back at it's
maximum loudness, as in your second paragraph:
| Quote: | Normalization is also reffered to when storing floating point wav
files. You search for the abs max
|
It's confusing when a word has more than one meaning... :)
Best regards,
Dave Atkin |
|
| Back to top |
|
 |
Staiger Guest
|
Posted: Thu Aug 17, 2006 8:11 am Post subject: Re: Normalize WAV file? |
|
|
| Quote: | Normalization means that you ensure that the mean value of
the wav is zero
This sounds more like DC offset correction...
|
I agree - normalisation "normally" means to adjust the amplitude of the
sound so it plays at maximum volume. In other words, to amplify it so that
the highest value reaches +/- 32K.
(Maximum "loudness", by the way, is usually taken to mean the subjective
experience, and frequently involves non-linear companding, such that the
quiet passages are amplified more than the loud passages).
Atmapuri was talking about DC offset correction.
| Quote: | This can be achieved with a highpass filter
How does that work?
A high pass filter will attenuate the frequencies below the cutoff
point.....
|
You've just said it. A DC offset has an infinitely low frequency, so it
will be attenuated to zero by applying a high pass filter.
Staiger |
|
| Back to top |
|
 |
Atmapuri Guest
|
Posted: Thu Aug 17, 2006 8:11 am Post subject: Re: Normalize WAV file? |
|
|
"Dave Atkin" <musictronics (AT) optusnet (DOT) com.au> wrote in message
news:44e3ce19 (AT) newsgroups (DOT) borland.com...
| Quote: | Hi Atmapuri
Normalization means that you ensure that the mean value of
the wav is zero
This sounds more like DC offset correction...
|
The formula for normalization is:
(X[i] - d)*c
d in this case can be mean and c is the scale factor to amplify.
I believe there was CoolEdit or a similar program
that was doing normalization like that.
Of course, you can have d set to zero or c
set to 1.
| Quote: | This can be achieved with a highpass filter
How does that work?
A high pass filter will attenuate the frequencies below the cutoff point,
and pass only the frequencies above that point unattenuated.
|
Yes. If your cutoff point is 0Hz, it will remove the DC
(the mean). You should not amplify the signal unless its
mean is zero, because it will also increase its DC.
Regards!
Atmapuri |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|