Sound intensity is the measure of amplitude at a particular instance of time from a 16 bit .wav file. This is a sample program to demonstrate the calculation of intensity.
Step 1: Read the input wav file
AVIStreamOpenFromFile
(PAVISTREAMobj,lpszWavFilePath,streamtypeAUDIO,0, OF_READ, NULL))
Step 2: Get the raw stream data
AVIStreamInfo
(PAVISTREAMobj,objAVISTREAMINFO,sizeof(objAVISTREAMINFO))
Step 3: Read stream format
AVIStreamReadFormat
(PAVISTREAMobj,AVIStreamStartTime(PAVISTREAMobj), objPCMWAVEFORMAT, sizeof(objPCMWAVEFORMAT))
Step 4: Calculate the size of steam
PCMWAVEFORMAT wf;
long lSize = ((StreamEndTime – StreamBeginTime)* wf.wf.nChannels * wf.wBitsPerSample / 8)
Step 5: Audio sampling
//pSampleData is an array of lSize
AVIStreamRead(PAVISTREAMobj, StreamBeginTime, StreamEndTime - StreamBeginTime, (void*)pSampleData, lSize, NULL, sizeof(wf));
//A loop from begin time to end time and store the max value as tMax
enum channel{left, right, mixed}
for (i = 0; i< (StreamEndTime - StreamBeginTime); i++)
{
sample = abs(GetSample(i, left));
tMax = sample > tMax ? sample : tMax;
}
//Sampling of 16 bit audio data which is filled into pSampleData
GetSample(long sampleNo, WaveForm::Channel channel)
{
short *sample16;
int sample;
long channels;
if (IsEmpty())
return 0;
sample16 = (short *)pSampleData;
channels = GetNumChannels() > 1 ? 2 : 1;
channels = channels * (GetBitsPerSample() == 8 ? 1 : 2);
if (sampleNo >= (long)lSampleDataSize/channels || sampleNo < 0)
return 0;
if (GetNumChannels() == 2)
{
if (channel == WaveForm::kLeftChannel || channel ==
WaveForm::kMixChannels)
sampleNo = sampleNo * 2;
else
sampleNo = sampleNo * 2 + 1;
}
sample = sample16[sampleNo];
if (channel == WaveForm::kMixChannels && GetNumChannels() == 2)
{
sample += sample16[sampleNo+1];
sample = sample /2;
}
return sample;
}
Step 6: Calculating intensity
float GetIntensityAtTime(int t)
{
if(t