2012-04-20 65 views
1

下面的代碼旨在實現兩種策略來插入缺少的數據包 - 兩種策略是1)用無聲填充空白,或2)重複以前的數據包,如果超過一個小包用沉默填滿了其餘的部分。該代碼有廣泛的評論,但我似乎無法弄清楚不同的變量應該是什麼意思。有人會介意幫助我理解這些代碼嗎?感謝任何指導!C/C++ - 瞭解音頻插值代碼

#include "interpolate.h" 
#include "assert.h" 

/* write a packet's worth of silence */ 
void write_silence(FILE *ofile, int num_samples) { 
    short missing_pkt[num_samples]; 
    int i; 
    for (i=0; i < num_samples; i++) { 
    missing_pkt[i]=0; 
    } 
    fwrite(missing_pkt, 1, num_samples*2, ofile); 
} 

/* simulate the reception of a packet, and apply a loss repair strategy */ 
void recv_packet(int seqno, int len, char *data, FILE *ofile, int strategy) { 
    static int prev_seqno = -1; 
    static short* prev_samples = 0; 

    /* interpret the data as signed 16 bit integers */ 
    short *samples = (short*)data; 
    int num_samples = len/2; 

    printf("recv_packet: seqno=%d\n", seqno); 

    if (prev_seqno != -1 && (prev_seqno+1 != seqno)) { 
    int i, missing_seqno; 
    /* there was missing data */ 

    printf("s=%d\n", strategy); 
    switch(strategy) { 
    case SILENCE: 
     { 
    /* create a packet containing silence */ 
    missing_seqno = prev_seqno + 1; 
    while (missing_seqno < seqno) { 
     write_silence(ofile, num_samples); 
     missing_seqno++; 
    } 
    break; 
     } 
    case REPEAT_PREV: 
     { 
    /* repeat the previous packet */ 
    fwrite(prev_samples, 2, num_samples, ofile); 
    missing_seqno = prev_seqno + 2; 
    while (missing_seqno < seqno) { 
     /* repeating the same packet more than once sounds bad */ 
     write_silence(ofile, num_samples); 
     missing_seqno++; 
    } 
    break; 
     } 

    default: 
     abort(); 
    } 
    } 

    fwrite(samples, 1, num_samples*2, ofile); 

    /* hold onto the last received packet - we may need it */ 
    if (prev_samples != 0) 
    free(prev_samples); 
    prev_samples = samples; 
    prev_seqno = seqno; 
}; 
+0

一旦你想出來了,確保你在好的數據包和插入的數據包之間添加某種淡入淡出;否則,不連續性將導致可怕的點擊。 – 2012-04-20 09:47:29

+0

需要一些時間來理解您將需要的C語法和基本功能。似乎你還不熟悉。 (ps不是C++) – UmNyobe 2012-04-20 10:03:44

回答

1

變量約定:

  • seqno:序列號
  • samples:音頻數據作爲樣本
  • prev_:以前
  • ofile:輸出文件

你怎麼知道代碼應該做什麼而不理解它? 這個想法是數據包有遞增的序號,從0到無窮大。 -1是初始狀態,甚至沒有收到數據包。 當你當你進入這個條件接收數據包和

if (prev_seqno != -1 //it is not the first packet 
     && 
    (prev_seqno+1 != seqno) //the packet is not the one we expected 
    ){ 
    //there are (seqno - prev_seqno - 1) packet missing. 
    //do something about it. 

那麼你有一定的差距。根據策略,您可以將輸出文件ofile填入靜默或收到的最後一個樣本的副本。在音頻silence = 0

例如,如果最後一個數據包爲0並且您收到數據包4,則表示數據包1,2和3丟失。在這種情況下,根據策略,您會將1,2,3作爲靜音數據包或重複使用0。

+0

感謝您的幫助UmNyobe(就像這個名字!)這是考試準備練習的一部分 - 練習包括實施一些更多的策略,但在我能做到這些之前,我需要理解代碼。這個問題說明了這些策略應該做什麼。 我可以問,爲什麼序列號從-1開始?此外,我似乎無法理解爲什麼條件: prev_seqno!= -1 &&(prev_seqno + 1!= seqno) 構成了一個缺口。你介意闡述嗎?謝謝 – user1058210 2012-04-20 09:54:05

+0

-1只是一個選擇,它可能是任何東西。讓我編輯我的答案。 – UmNyobe 2012-04-20 09:56:20

+0

感謝您的解釋 - 我現在理解第二個條款,但是這不是說只有前一個序列號不是第一個數據包才能存在差距嗎?當然這是不正確的,因爲你可以接收第一個數據包,錯過第二個數據包,並接收第三個數據包,即序列號爲-1,1。 – user1058210 2012-04-20 10:06:53