2013-10-11 66 views
3

我試圖將16位單聲道聲音轉換爲立體聲。聲音存儲爲一個字節數組,所以據我的理解,這意味着我一次複製兩個字節。將單聲道轉換爲立體聲

enter image description here

我這樣做對嗎?我製作的代碼改變了頻率。

編輯:

我成功地生成一個單色調並將其存儲在byte [] generatedSnd

播放單聲道聲音(工作):

AudioTrack audioTrack = null;         // Get audio track 
    try { 
     audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 
       sampleRate, AudioFormat.CHANNEL_OUT_MONO, 
       AudioFormat.ENCODING_PCM_16BIT, (int)numSamples*2, 
       AudioTrack.MODE_STATIC); 
     audioTrack.setStereoVolume(0f, 1f); 
     audioTrack.write(generatedSnd, 0, generatedSnd.length);  // Load the track 
     audioTrack.play();           // Play the track 
    } 
    catch (Exception e){ } 

轉換爲立體聲:

int monoByteArrayLength = generatedSnd.length; 
    byte [] stereoGeneratedSnd = new byte[monoByteArrayLength * 2]; 

    stereoGeneratedSnd[0] = generatedSnd[0]; 
    stereoGeneratedSnd[2] = generatedSnd[0]; 

    for (int x=1; x<monoByteArrayLength; x+=2) { 

     stereoGeneratedSnd[x*2-1] = generatedSnd[x]; 
     stereoGeneratedSnd[x*2+1] = generatedSnd[x]; 

     if (x+1 < monoByteArrayLength) { 
      stereoGeneratedSnd[x*2] = generatedSnd[x+1]; 
      stereoGeneratedSnd[x*2+2] = generatedSnd[x+1]; 
     } 
    } 

    AudioTrack audioTrack = null;         // Get audio track 
    try { 
     audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 
       sampleRate, AudioFormat.CHANNEL_OUT_STEREO, 
       AudioFormat.ENCODING_PCM_16BIT, (int)numSamples*2, 
       AudioTrack.MODE_STATIC); 
     audioTrack.setStereoVolume(0f, 1f); 
     audioTrack.write(stereoGeneratedSnd, 0, stereoGeneratedSnd.length);  // Load the track 
     audioTrack.play();           // Play the track 
    } 
    catch (Exception e){ } 

我想要做的是發揮出來的聲音y單通道

+1

請添加更多上下文。這是什麼格式?你用什麼庫來播放文件?什麼語言?還有一個關於常識的問題:**播放流的邏輯應該如何知道這是單聲道文件,每兩個採樣相同,還是假立體聲?它不能只是「猜測」...應該有一些元數據,描述流,不是嗎?** – ppeterka

回答

3

加倍算法的輸出爲0, 1, 2, 1, 2, 3, 4, 3, 4, 5, 0, 5

做翻番將是一個更簡單的(正確)的方式:

for (int i = 0; i < monoByteArrayLength; i += 2) { 
    stereoGeneratedSnd[i*2+0] = generatedSnd[i]; 
    stereoGeneratedSnd[i*2+1] = generatedSnd[i+1]; 
    stereoGeneratedSnd[i*2+2] = generatedSnd[i]; 
    stereoGeneratedSnd[i*2+3] = generatedSnd[i+1]; 
} 

輸出:0, 1, 0, 1, 2, 3, 2, 3, 4, 5, 4, 5

+0

它仍然沒有發揮正確的語氣 – meeeee

+1

在哪裏是不對的?另外,請記住,當您在靜態模式下使用'AudioTrack'時,緩衝區大小參數指定_「將爲此實例播放的聲音的最大大小[以**字節**爲單位]。 – Michael

+0

修復了緩衝區大小。我認爲這隻會影響持續時間而不是頻率,但我們現在是。謝謝。 – meeeee

4

是故意的,你在同一時間重複的兩個字節? 在16位PCM Wave格式的立體聲模式需要:

DATA[ ] : [1st byte from Chanel 1], [1st byte from Chanel 2], [2nd byte from Chanel 1], [2nd byte from Chanel 2]... 

所以,如果你想轉換單聲道到立體聲,你的陣列應該是:

Mono : 0, 1, 2, 3 ... 
Stereo : 0, 0, 1, 1, 2, 2, 3, 3 ... 

,如果你想只有一個通道

Stereo : 0, 0, 1, 0, 2, 0, 3, 0 ... 
相關問題