audio - libsndfile 用法加入和混合 .wav 文件

标签 audio mixing wav libsndfile

我需要帮助开始使用 libsndfile。

我有四个 .wav 文件(都具有相同的采样率)。

我想把前两个连在一起,然后再把下两个连在一起,

然后将生成的 2 个 .wav 文件混合为一个。

最佳答案

用于混合两个 wav 文件。

#include <cstdio>
#include <sndfile.h>
#include <windows.h>
#include <cstdlib>
#include <cmath>

#define BUFFER_LEN 1024
#define MAX_CHANNELS 6

static void data_processing (double *data, int count, int channels) ;

int main (void) {

  static double data [BUFFER_LEN] ;
  static double data2 [BUFFER_LEN] ;
  static double outdata [BUFFER_LEN] ;

  SNDFILE *infile, *outfile, *infile2 ;
  SF_INFO sfinfo ;
  int readcount ;
  SF_INFO sfinfo2 ;
  int readcount2 ;

  const char *infilename = "inputOne.wav" ;
  const char *infilename2 = "inputTwo.wav" ;
  const char *outfilename = "output.wav" ;

  if (! (infile = sf_open (infilename, SFM_READ, &sfinfo))) {
    /* Open failed so print an error message. */
    printf ("Not able to open input file %s.\n", infilename) ;

    /* Print the error message from libsndfile. */
    puts (sf_strerror (NULL)) ;
    return 1 ;
  } ;

  if (sfinfo.channels > MAX_CHANNELS) {
    printf ("Not able to process more than %d channels\n", MAX_CHANNELS) ;
    return 1 ;
  } ;

  if (! (infile2 = sf_open (infilename2, SFM_READ, &sfinfo2))) {
    /* Open failed so print an error message. */
    printf ("Not able to open input file %s.\n", infilename2) ;

    /* Print the error message from libsndfile. */
    puts (sf_strerror (NULL)) ;
    return 1 ;
  } ;

  if (sfinfo2.channels > MAX_CHANNELS) {
    printf ("Not able to process more than %d channels\n", MAX_CHANNELS) ;
    return 1 ;
  } ;

  /* Open the output file. */
  if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo))) {
    printf ("Not able to open output file %s.\n", outfilename) ;
    puts (sf_strerror (NULL)) ;
    return 1 ;
  } ;

  while ((readcount = sf_read_double (infile, data, BUFFER_LEN)) &&
         (readcount2 = sf_read_double (infile2, data2, BUFFER_LEN))) { 
    data_processing (data, readcount, sfinfo.channels) ;
data_processing(data2, readcount2, sfinfo2.channels) ;

    for(int i=0; i < 1024;++i) {
  outdata[i] = (data[i] + data2[i]) -(data[i])*(data2[i])/65535;
    }

    sf_write_double (outfile, outdata , readcount) ;
  } ;

  /* Close input and output files. */
  sf_close (infile) ;
  sf_close (infile2) ;
  sf_close (outfile) ;
  system("PAUSE");
  return 0 ;
} /* main */

static void data_processing(double *data, int count, int channels) { 
  double channel_gain [MAX_CHANNELS] = { 0.5, 0.8, 0.1, 0.4, 0.4, 0.9 } ;
  int k, chan ;

  for (chan = 0 ; chan < channels ; chan ++)
    for (k = chan ; k < count ; k+= channels)
      data [k] *= channel_gain [chan] ;

  return ;
} /* data_processing */

这就是我混合两个 16 位信号的简单 wav 文件的方式。

首先,音频混合并不像人们想象的那么容易。连接两个信号后可能会有很多歧义。在我的例子中,它工作得很好,就像我需要的那样,但如果你需要精确的结果,你可能需要更多地谷歌搜索来精确叠加信号。

要连接两个 wav 文件,您只需读取第一个 wav 文件,复制结果 wav 文件中的数据,最后将第二个 wav 文件的数据附加到结果 wav 文件。

此链接可能对您也有用 http://www.vttoth.com/digimix.htm

关于audio - libsndfile 用法加入和混合 .wav 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5671055/

相关文章:

java - JPanel 中的 JPanel

android - 重新采样音频会改变速度

c++ - 如何对 WAV 文件数据执行 FFT?

ios - 跨设备建立同步音乐流

objective-c - 适用于 Mac OS X 的 AirPlay API

angularjs - Angular $ sce阻止了我的音频src,尝试了$ sce.trustAsResourceUrl,但仍然出现错误

swift - 如何在播放期间更改音频音高? ( swift 4)

audio - 将WAV文件与libsndfile混合时的工件

python - 如何播放.wav(或任何其他类型的文件)文件?像实际的音频输出

java - 停止.wav