Lot of Noise is added after smbfft in pitch shifting.

Discussion about the DSP Dimension's articles, tutorials and code.

Moderator: neuronaut

Lot of Noise is added after smbfft in pitch shifting.

Postby vinisoundEngg » 13.05.2011 10:26

Hi All,

I am very new in audio , I am tring to change the pitch of audio. But facing problem while pith shifting using source given on http://www.dspdimension.com/admin/pitch ... ng-the-ft/.

I am reading a wav file in a float buffer and convering its all data to the range of [-1.0 to 1.0](After removing nan values) by diving the float value to FLTMAX then passing that float buffer to smbPitchShift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sampleRate, float *indata, float *outdata) function.
internally it is calling function smbffft() for applying fft and then inverse fft. after these processing I am getting a float buffer that has the values outside [-1.0 to 1.0] also . next I am multiplying these values to FLTMAX and writing to another file. but when I am playing, it is noise only.
I tried to figure out the exact problem but not getting , if any one can help out it will be greatfull.

Thanks & Regards,
Vini
vinisoundEngg
 
Posts: 7
Joined: 13.05.2011 10:05

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby neuronaut » 13.05.2011 11:06

This looks wrong. You need to divide by whatever max value your data representation uses (check out the example project at http://downloads.dspdimension.com/smbPi ... roject.zip) not FLT_MAX (which is the maximum number representable by the IEE754 single precision floating point format, ie around 1E+37).

Also, converting the signal from your audio file should never produce any NaNs - you're probably misinterpreting the data it contains (stuffing integer data into a floating point variable can create NaNs which is what I guess you're doing).

HTH
+Stephan

[EDIT] The example project above might not be very helpful as miniAIFF does the conversion for you. In a nutshell your conversion code for a common (16bit signed short) WAV file should look something like this:

Code: Select all
for (long v = 0; v < bufferSize; v++) mFloatBuffer[v] = (float)mSignedShortIntBuffer[v] / 32768.f;


and the conversion back:

Code: Select all
for (long v = 0; v < bufferSize; v++) {
    if (mFloatBuffer[v] > 0.999f) mFloatBuffer[v] = 0.999f;
    else if (mFloatBuffer[v] < -1.f) mFloatBuffer[v] = -1.f;
    mSignedShortIntBuffer[v] = (short)(mFloatBuffer[v] * 32768.f);
}
Free DSP tutorials by Stephan M. Bernsee at http://www.dspdimension.com
"There are 10 types of people in this world: those who understand binary, those who don't"
--Unknown
neuronaut
 
Posts: 1332
Joined: 17.11.2005 09:15
Location: Mainz, Germany

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby vinisoundEngg » 13.05.2011 13:15

Dear Stephen,
Many many thanks for your immediate reply. as per your suggetion I did the changes still I am facing the same issue(lot of Noise).
I am very new to audio so not able to figure it out. I am putting code snippet please have look at it, it would be a greate help.

Code: Select all

long iNumberOfSamples = m_iSubchunk2Size/(m_BitsPerSample/8);

m_pInData=new int[iNumberOfSamples]; // Create an element for every sample
fread(m_pInData, (m_BitsPerSample/8), iNumberOfSamples, fhandle); // Reading raw audio data
fclose(fhandle);
fhandle = NULL;

//Finding the max value out of input data.
   short iVal, iMaxVal = -1;
   for(long i=0; i< iNumberOfSamples; i++)
   {
      iVal = abs(m_pInData[i]);
      if(iVal > iMaxVal)
         iMaxVal = iVal;
   }

//scalling data to [-1.0 to 1.0].
   //First craete a buffer of same size of type flote to store scaled values.
   float* pScaledDataBuff = new float[iNumberOfSamples];

   for(long i=0; i< iNumberOfSamples; i++)
   {
      float fValue = m_pInData[i]/iMaxVal;
      pScaledDataBuff[i] = fValue;
   }

   _pPitchShifter->smbPitchShift(2.0 , (long)iNumberOfSamples, 2048, (long)16, (float)m_iSampleRate, pScaledDataBuff, pScaledDataBuff);

   //again Scalling data values to original range.
   for(long i=0; i< iNumberOfSamples; i++)
   {
      m_pInData[i] = pScaledDataBuff[i]*iMaxVal;
   }

       fhandle = fopen(m_pOutPutPath,"wb");
   fwrite(m_pInData,(m_BitsPerSample/8),iNumberOfSamples, fhandle);
   fclose(fhandle);
   delete m_pInData;
   delete pScaledDataBuff;


Thanks,
vini
vinisoundEngg
 
Posts: 7
Joined: 13.05.2011 10:05

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby neuronaut » 13.05.2011 13:28

Ok but I don't see where you have integrated the changes I proposed.

Also, your m_pInData is of data type "int". Unless your audio file contains 32bit signed integer sample frames (which is a rather unlikely case) you will want to use the "short" data type instead. Make sure that you're aware how many channels you have in that file and what the data format is (including word length and big/little endian issues)! Unless you have full control over the audio files that you're feeding your application you need to make sure that it can deal with all of this accordingly.

Also, you should be aware that fread is totally unaware of the structural information of the file format, so fwrite will write over the file header if you're not respecting the file format. Your output file is likely to be rendered unusable in the process (that's what miniAIFF is for, btw. You might want to consider using it instead: http://www.dspdimension.com/admin/miniaiff/).

HTH
+Stephan
Free DSP tutorials by Stephan M. Bernsee at http://www.dspdimension.com
"There are 10 types of people in this world: those who understand binary, those who don't"
--Unknown
neuronaut
 
Posts: 1332
Joined: 17.11.2005 09:15
Location: Mainz, Germany

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby vinisoundEngg » 13.05.2011 14:45

Dear Stephen,

I am realy soory for giving you much trouble. There may be a silly issue in my code but I am not able to figure it out...

some discription for the file i am using...

1) wav file - little endian
2) nChannel - 1;
3) sammple rate = 11025.

earlear I was using short buffer for reading the file but after FFT it was giving zero value for all data. even i loged it out in a text file, after reading I loged data. it was some thing non zero. and after pitchshift and fft it loged full file as zero. and the text file size was half of the initial text file size.
I am giving full code that I have implemented. Kindly have a look once...it will solve my issue where i am strugling from more than two days.



Code: Select all

 //Wav Attributes:
   char    m_ChunkID[4],m_Subchunk1ID[4];
   char    m_Format[4],m_Subchunk2ID[4];
   int    m_iChunkSize,m_iSubchunk1Size;
   int    m_iSubchunk2Size,m_iSampleRate;
   short    m_AudioFormat, m_NumChannels;
   short    m_BlockAlign, m_BitsPerSample;
   int    m_iByteRate;
   short   *m_pInData;                                                               //Praposed change.

   FILE *fhandle = fopen(PATH,"rb");
   fread(m_ChunkID,1,4,fhandle);
   fread(&m_iChunkSize,4,1,fhandle);
   fread(m_Format,1,4,fhandle);
   fread(m_Subchunk1ID,1,4,fhandle);
   fread(&m_iSubchunk1Size,4,1,fhandle);
   fread(&m_AudioFormat,2,1,fhandle);
   fread(&m_NumChannels,2,1,fhandle);
   fread(&m_iSampleRate,4,1,fhandle);
   fread(&m_iByteRate,4,1,fhandle);
   fread(&m_BlockAlign,2,1,fhandle);
   fread(&m_BitsPerSample,2,1,fhandle);
   fread(&m_Subchunk2ID,1,4,fhandle);
   fread(&m_iSubchunk2Size,4,1,fhandle);

   long iNumberOfSamples = m_iSubchunk2Size/(m_BitsPerSample/8);

   m_pInData=new short[iNumberOfSamples]; // Create an element for every sample
   fread(m_pInData, (m_BitsPerSample/8), iNumberOfSamples, fhandle); // Reading raw audio data
   fclose(fhandle);
   fhandle = NULL;

   // Write the same file
   fhandle = fopen(m_pOutPutPath,"wb");

   fwrite(m_ChunkID,1,4,fhandle);
   fwrite(&m_iChunkSize,4,1,fhandle);
   fwrite(m_Format,1,4,fhandle);
   fwrite(m_Subchunk1ID,1,4,fhandle);
   fwrite(&m_iSubchunk1Size,4,1,fhandle);
   fwrite(&m_AudioFormat,2,1,fhandle);
   fwrite(&m_NumChannels,2,1,fhandle);
   fwrite(&m_iSampleRate,4,1,fhandle);
   fwrite(&m_iByteRate,4,1,fhandle);
   fwrite(&m_BlockAlign,2,1,fhandle);
   fwrite(&m_BitsPerSample,2,1,fhandle);
   fwrite(&m_Subchunk2ID,1,4,fhandle);
   fwrite(&m_iSubchunk2Size,4,1,fhandle);

   FILE* fp1 = fopen("/Media/wav.txt", "ab");
   for(long i=0; i<iNumberOfSamples; i++)
      fprintf(fp1, "%d \t", m_pInData[i]);
   fclose(fp1);


   //Finding the max value out of input data.                           //proposed change
   short iVal, iMaxVal = -1;
   for(long i=0; i< iNumberOfSamples; i++)
   {
      iVal = abs(m_pInData[i]);
      if(iVal > iMaxVal)
         iMaxVal = iVal;
   }
   AppLog("iMaxVal : %d", iMaxVal);

   //scaling data to [-1.0 to 1.0].                                                                       //proposed change
   //First create a buffer of same size of type float to store scaled values.
   float* pScaledDataBuff = new float[iNumberOfSamples];

   for(long i=0; i< iNumberOfSamples; i++)
   {
      float fValue = m_pInData[i]/iMaxVal;
      pScaledDataBuff[i] = fValue;
   }

   _pPitchShifter->smbPitchShift(2.0 , (long)iNumberOfSamples, 2048, (long)4, (float)m_iSampleRate, pScaledDataBuff, pScaledDataBuff);

   //again Scalling data values to original range.
   for(long i=0; i< iNumberOfSamples; i++)
   {
      m_pInData[i] = pScaledDataBuff[i]*iMaxVal;
   }

   FILE* fp = fopen("/Media/wav1.txt", "ab");
   for(long i=0; i<iNumberOfSamples; i++)
      fprintf(fp1, "%d \t", m_pInData[i]);
   fclose(fp);

   fwrite(m_pInData,(m_BitsPerSample/8),iNumberOfSamples, fhandle);
   fclose(fhandle);
   delete m_pInData;
   delete pScaledDataBuff;






Thanks,
vini
vinisoundEngg
 
Posts: 7
Joined: 13.05.2011 10:05

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby neuronaut » 13.05.2011 15:01

Again, I don't see that you implemented the suggestion from my first reply above. Please try this first and get back to us if it doesn't work.

Regards
+Stephan

[EDIT: okay, I'm going to give you one more hint: replace multiplication and division by iMaxVal with 32768.f and see what happens then]
Free DSP tutorials by Stephan M. Bernsee at http://www.dspdimension.com
"There are 10 types of people in this world: those who understand binary, those who don't"
--Unknown
neuronaut
 
Posts: 1332
Joined: 17.11.2005 09:15
Location: Mainz, Germany

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby vinisoundEngg » 16.05.2011 10:07

Dear Stephen,

First of all many many thanks for your great support. Finally, I have successfuly converted audio file to a 7 semitone audio file.
But I am getting slow noise in the background of audio . So I changed semitones for pitch shifting but noise is alwayas present.
I am using afile that is having...

mono channel.
16 bits/sample.
samplerate - 11025
MAX_FRAME_LENGTH 8192
fftFrameSize 2048.
osamp = 16,
pitchshift = 1.49.....f(7 semitones)

I am not able to check with ur given sample proj its giving build error for log function on vs2008.
any idea would push me ahead ....thanks again for great help.

Regards,
vini
vinisoundEngg
 
Posts: 7
Joined: 13.05.2011 10:05

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby neuronaut » 16.05.2011 10:14

Works for me. Can you post your updated code and/or an example sound file somewhere.

You need to include math.h so you can use the log function. I'm pretty sure it's available in VS 2008 as well (you might want to check the documentation).

+Stephan
Free DSP tutorials by Stephan M. Bernsee at http://www.dspdimension.com
"There are 10 types of people in this world: those who understand binary, those who don't"
--Unknown
neuronaut
 
Posts: 1332
Joined: 17.11.2005 09:15
Location: Mainz, Germany

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby vinisoundEngg » 16.05.2011 10:59

Dear Stephen,

I got your sample project build and run for your given "voice" audio file.
But I found the same kind of noise (a kind of backgroung vibration) in out file for 7 semitone that is the same thing comming in my output file also.
it is not very clear as voice audio file. Please can you check with your sample project or i shall upload some where these output files.



Regards.
vini
vinisoundEngg
 
Posts: 7
Joined: 13.05.2011 10:05

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby neuronaut » 16.05.2011 11:15

If you are referring to an amplitude modulation this is due to your overlap being insufficient. Try a value of 64 to see if that changes anything.

+Stephan
Free DSP tutorials by Stephan M. Bernsee at http://www.dspdimension.com
"There are 10 types of people in this world: those who understand binary, those who don't"
--Unknown
neuronaut
 
Posts: 1332
Joined: 17.11.2005 09:15
Location: Mainz, Germany

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby vinisoundEngg » 16.05.2011 12:04

Dear Stephen,

I tried with a value of 64 /*for osamp*/ but it didnt work, output is same. I am not very confident but amplitude modulation deals with db of song.
But here there is noise kind of thing if u check with the output of "smbPitchShiftProject".

Regards,
Vini
vinisoundEngg
 
Posts: 7
Joined: 13.05.2011 10:05

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby neuronaut » 16.05.2011 12:11

I did, it works ok. I need more info if you want me to help you with this, like code or an example file (input and output).

regards
+Stephan
Free DSP tutorials by Stephan M. Bernsee at http://www.dspdimension.com
"There are 10 types of people in this world: those who understand binary, those who don't"
--Unknown
neuronaut
 
Posts: 1332
Joined: 17.11.2005 09:15
Location: Mainz, Germany

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby vinisoundEngg » 16.05.2011 12:33

Thanks...Stephen for your support, it worked there was problem with audio file, not with code.


Regards,
vini
vinisoundEngg
 
Posts: 7
Joined: 13.05.2011 10:05

Re: Lot of Noise is added after smbfft in pitch shifting.

Postby neuronaut » 16.05.2011 12:47

Ok glad you got it working.

Best wishes
Stephan
Free DSP tutorials by Stephan M. Bernsee at http://www.dspdimension.com
"There are 10 types of people in this world: those who understand binary, those who don't"
--Unknown
neuronaut
 
Posts: 1332
Joined: 17.11.2005 09:15
Location: Mainz, Germany


Return to The DSP Dimension

Who is online

Users browsing this forum: No registered users and 0 guests

cron