添加回声效果.wav文件 [英] adding echo effect to a .wav file

查看:346
本文介绍了添加回声效果.wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在C以下code ++(样本code)来修改原来的.wav文件中的数据present和修改后的数据写入到一个新创建的文件。该计划为回波效应的声音(注: - 这是只有2声道wav文件书面)。

I have written the following code in c++ (sample code) to modify the data present in the original .wav file and write the modified data to a newly created file. This program gives 'echo-effect' to the sound (note:- this is written for a wav file with just 2 channels).

#include <iostream>
#include <conio.h>
#include <fstream>
#ifndef endl
#define endl "\n"
#endif

#define  wav_eco(filin, fileout, eco_rate)                      \
char temp[2];                                                  \
char ch[eco_rate][2], ch2[eco_rate][2];                        \
int i,j;                                                       \
    for(i=0; i<eco_rate; i++)                                           \
        for(j=0; j<2; j++)                                              \
            ch2[i][j] = 0;                                                      \
while(!filin.eof())                                                             \
{                                                                               \
    for(i=0; i<eco_rate && !filin.eof(); i++)                                   \
    filin.read((char*)&ch[i], sizeof(char[2]));                                 \
    for(i=0; i<eco_rate; i++)                                                   \
            {                                                                   \
                temp[0]  = ch[i][0];                            \
                temp[1]  = ch[i][1];                            \
                ch[i][0]+=ch2[i][0];                            \
                ch[i][1]+=ch2[i][1];                            \
                fileout.write((char*)&ch[i], sizeof(char[2]));                  \
                ch2[i][0] = temp[0];                                            \
                ch2[i][1] = temp[1];                                            \
            }                                                                   \
    }

 using namespace std;
 struct WAVEFILEHEADER
{
char ChunkId[4];
int ChunkSize;
char Format[4];
char SubChunkFmt[4];
int SubChunkFmtSize;
short int AudioFormat;
short int NumChannels;

int SampleRate;
int ByteRate;
short int BlockAlign;
short int BitsPerSample;

char ChunkData[4];
int SubChunkDataSize;
};

int main()
{
fstream filin("C:\\Users\\chess.Admin-PC.000\\Music\\ExampleRead.wav", ios::in|ios::binary);
fstream fileout("C:\\Users\\chess.Admin-PC.000\\Music\\ExampleWrite.wav",ios::out| ios::binary);
WAVEFILEHEADER wav;
filin.read((char*)&wav,sizeof(wav));
    //to display the contents of the header 
cout<<wav.ByteRate
    <<endl<<wav.ChunkSize
    <<endl<<wav.SampleRate// no of samples per second
    <<endl<<wav.SubChunkDataSize
    <<endl<<wav.SubChunkFmtSize
    <<endl<<wav.ChunkData
    <<endl<<wav.ChunkId
    <<endl<<wav.Format
    <<endl<<wav.SubChunkFmt
    <<endl<<wav.AudioFormat
    <<endl<<wav.AudioFormat
    <<endl<<wav.BitsPerSample//no of bits per second
    <<endl<<wav.BlockAlign
    <<endl<<wav.NumChannels
    <<endl<<endl<<sizeof(wav); /* size of the wav variable*/
getch();
fileout.write((char*)&wav, sizeof(wav));/// write the header to output file 
wav_eco(filin,fileout,10000)  ///modify the data and update the output file
filin.close();
fileout.close();
return 0;
}

/*algorithm implemented by the wav_eco function(this is not some stranded algorithm. I wrote it myself)  :-
1) obtain the 'echo_rate' of the samples
2) read the data present in the original wav file 
3) write this data, by adding each sample from the previously obtained data to the data that was read now
4) follow steps 2 and 3 until eof is reached  
  */

该程序运行正常,当我播放使用Windows Media Player的输出文件,我得到的回音效果,但除此之外,我收到了嘶嘶的声音。是什么原因导致这一点,并以何种方式将我不得不修改上面的code避免呢?是否可以简化 wav_eco()宏使得程序的计算时间减少?

The program works fine, when I play the output file using windows media player,I get the echo effect, but in addition I get a 'hissing' sound. what causes this and in what way will I have to modify the above code to avoid it? Is it possible to simplify thewav_eco() macro such that the program's computation time is reduced?

我的嘶嘶声猜测是因为加了 CH的[I] [0] + = CH2 [I] [0]; CH [I] [1] + = CH2 [I] [1]; 。是否修改错误的方式的声音? (这是我第一次编辑音)

my guess for the hissing sound is because of the addition ch[i][0] += ch2[i][0]; ch[i][1]+=ch2[i][1];. Does it modify the sound in the wrong way? (this is the first time I am editing sound)

推荐答案

您尝试添加16位在时间值8位。需要检测的低字节溢出并执行它交给高字节此外,或只使用16位算术从一开始。

You are attempting to add 16-bit values 8 bits at a time. You need to detect the overflow of the low byte and carry it over to the high byte addition, or just use 16-bit arithmetic from the start.

这篇关于添加回声效果.wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆