C/C++ - 了解音频插值代码

标签 c audio interpolation packets

下面的代码旨在实现两种策略来插入丢失的数据包 - 这两种策略是 1) 用静音填充间隙,或 2) 重复前一个数据包,如果超过一个数据包,则用静音填充其余的数据包。该代码有广泛的评论,但我似乎无法理解不同变量的含义。有人介意帮我理解这段代码吗?感谢任何指导!

#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;
};

最佳答案

变量约定:

  • 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。

关于C/C++ - 了解音频插值代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10243435/

相关文章:

python - 如何连续改变正弦声音的频率?

c# - 在 WinCE 中发出哔哔声,可能吗?

python - 如何使用 python 对常规经纬度数据进行面积加权重新网格化

python - 如何在 python 中执行三次样条插值?

python - Numpy 网格数据插值到一定半径

c++ - 字符串[包含整数]的乘法,输出也存储在字符串中,如何?

c - 为什么我不能使用 "fgets"将字符串读取到我的结构元素中?

c - 简单的 C 程序用户输入 5 个 friend 的名字

C潜在的内存泄漏malloc

Android - 设置短信声音不会改变声音?