2014-02-18 25 views
0

我正在尝试对样本进行移调。我正在使用线性插值方法。线性插值 - 音频移位

如果音量是一个整数值,音调就会干净地移动。如果音高偏移量合理,则声音严重失真。实施似乎有效。

这是我的代码,我试着评论。

public void generateOutTrack() 
{ 
    Note currNote; 
    float[] output=new float[pattern.getPlayTimeInSmps()];//returns total play time of pattern in #samples. 


    float[] currSample=sample.getData();//the pcm data of the sample to be used 
    int currPeriod=0;//length of next note in number of samples 
    int outputPtr=0;//points to next sample in output buffer array 
    float pitch;//amount to pitch sample by 
    float linInt=0;//linear interpolater 
    float phasePtr=0;//floating point index in sample 
    int ptr=0;//integer index into sample 

    JavAud.checkRange(currSample); 

    while((currNote=pattern.nextNote())!=null)//each iteration plays one note 
    { 

     currPeriod=currNote.getPeriodInSmps();//length of current note in samples 
     pitch=currNote.getPitch();//pitch of current note 

     for(int i=0;i<currPeriod;i++)//run for length of note 
     { 
      ptr=(int)phasePtr;//floor of floating point index 
      linInt=phasePtr-ptr; 

      //if we are not at end of sample copy data to output 
      if(ptr<currSample.length*(1/pitch)-1) 
      { 

       //linear interpolation pitch shifting 
       output[outputPtr]=(currSample[ptr+1]*linInt)+(currSample[ptr]*(1-linInt)); 

       //alternate pitch shifting by simple sample dropping(has less distortion) 
       //output[outputPtr]=currSample[ptr]; 

      } 
      else//else silent 
      { 
       output[outputPtr]=0; 
      } 

      outputPtr++; 
      phasePtr=phasePtr+pitch; 
     } 

     phasePtr=0; 

    } 
    JavAud.checkRange(output); 
    WavFileWriter writer = new WavFileWriter(); 
    writer.writeWave(new WavFile(1, JavAud.GLB_SMP_RATE, output), "outputTone.wav"); 

} 
+1

嗨。要求人们发现代码中的错误并不是特别有效。您应该使用调试器(或者添加打印语句)来分析问题,追踪程序的进度,并将其与预期发生的情况进行比较。只要两者发生分歧,那么你就发现了你的问题。 (然后如果有必要,你应该构造一个[最小测试用例](http:// sscce/org)。) –

回答

1

它看起来像你试图通过重新采样做一个音高移位。与线性插值相比,执行更好质量重采样的一种常见方法是使用窗口化的Sinc低通滤波器作为插值内核。一个(缓慢)加窗Sinc重采样方法的伪代码在这里:http://www.nicholson.com/rhn/dsp.html#3

+0

由于算法的选择,我的质量并不差。由于代码中的错误,我的质量变差。我得到了严重的失真,并且价值观失控......不仅仅是从糟糕的算法中走出来。 – ScottF