2017-08-04 67 views
1

所以在下面的程序中,它的速度的音頻文件,並使它聽起來好像人有「氦聲音」。這是通過改變索引值來完成的,而當你增加索引值時,文件的音高和速度會更快。我想知道是否有辦法做相反的事情,並且讓音頻播放更慢,聲音更深。放慢聲音速度

public void helium(String sourceFile, String targetFile) 
{ 
    Sound sourceObj = new Sound(sourceFile);        
    Sound target = new Sound(targetFile); 
    int sampleValue = 0;            
    int targetIndex = 0;             

    for(int index = 0; index < sourceObj.getLength(); index+=2) 
    { 
     sampleValue = sourceObj.getSampleValueAt(index);      
     target.setSampleValueAt(targetIndex,sampleValue);     
     targetIndex++;              
    } 
    target.play(); 

} 
+0

使用負值? – Tavo

+0

如果你把索引的負值,它只會導致錯誤 –

+0

如果你增加1並使用'索引/ 2'會怎麼樣? – shmosel

回答

1

而不是將樣品的大小減半,樣品的大小加倍,並使用每個值兩次。我相信有更先進的方法,但這會使波形慢兩倍,導致低音。

我有,因爲你使用了一些非標準的聲音API是我不熟悉的作出一些假設。爲了確保您的聲音是不是太渴望你的目標樣本,我現在限制在源索引到目標一半樣品的最大尺寸(index < target.getLength()/2

int sampleValue = 0;            
int targetIndex = 0;             

for(int index = 0; index < target.getLength()/2; index++) 
{ 
    sampleValue = sourceObj.getSampleValueAt(index);      
    target.setSampleValueAt(targetIndex, sampleValue); 
    target.setSampleValueAt(targetIndex + 1, sampleValue);     
    targetIndex += 2;              
} 
+0

對不起,我對Java很陌生,但是當我將代碼更改爲您所寫的內容時,它創建了一個錯誤,指出「該索引443904不存在,最後一個有效索引是443903」。我很抱歉,但我真的不知道這意味着什麼,如果你可以改變代碼或爲我解釋。 –

+0

@DannyM更新了答案。請參閱「爲了確保您的聲音對於目標樣本不太長,我現在將源中的索引限制爲目標樣本的最大大小的一半('index

+0

謝謝你的訣竅。如果你不介意我問,你知道爲什麼我上面說的錯誤發生了。 (對不起,我很新的節目) –

0

簡易方法將翻番每個樣品。

一種不太幼稚的方法是分裂的樣品之間(線性插值)的差異。

正確的做法是在每個採樣之間插入零,然後應用該拒絕採樣速率一半以上的任何信號的低通濾波器。看到https://en.wikipedia.org/wiki/Upsampling