为.wav文件添加回声效果
adding echo effect to a .wav file
我用c++编写了以下代码(示例代码)来修改原始.wav文件中的数据,并将修改后的数据写入新创建的文件。这个程序给出了"回声效果"的声音(注意:-这是为一个只有2个通道的wav文件写的)。
#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媒体播放器播放输出文件时,我得到了回声效果,但另外我得到了"嘶嘶"的声音。是什么原因导致了这种情况,我应该如何修改上面的代码来避免这种情况?是否有可能简化wav_eco()
宏,从而减少程序的计算时间?
我猜测嘶嘶声是因为添加了ch[i][0] += ch2[i][0]; ch[i][1]+=ch2[i][1];
。它是否以错误的方式修饰了声音?(这是我第一次编辑声音)
这个。wav文件有几种格式,格式列表如下。上面的问题只有在格式已知的情况下才能回答(无论是8位还是16位)。为了调试问题中给出的代码,我们不需要bit_rate,所以我们不需要知道格式是否为PCM等。因此,如果文件是16位的,那么wav_echo()
可以使用以下代码:
#define wav_echo2_16(filin, fileout, eco_rate)
short int temp;
short int ch[eco_rate], ch2[eco_rate];
int i,j;
for(i=0; i<eco_rate; i++)
ch2[i] = 0;
while(!filin.eof())
{
for(i=0; i<eco_rate && !filin.eof(); i++)
filin.read((char*)&ch[i], sizeof(short int));
for(i=0; i<eco_rate; i++)
{
temp = ch[i];
ch[i]+=ch2[i];
fileout.write((char*)&ch[i], sizeof(short int));
ch2[i] = temp;
}
}
上面的宏wav_echo2_16()
为16位文件提供了回声效果(注意,echo_rate
常数应该是偶数,因为有2个通道)。下面显示了8位格式的代码:
#define wav_echo2_8(filin, fileout, eco_rate)
char temp;
char ch[eco_rate], ch2[eco_rate];
int i,j;
for(i=0; i<eco_rate; i++)
ch2[i] = 0;
while(!filin.eof())
{
for(i=0; i<eco_rate && !filin.eof(); i++)
filin.read((char*)&ch[i], sizeof(char));
for(i=0; i<eco_rate; i++)
{
temp = ch[i];
ch[i]+=ch2[i];
fileout.write((char*)&ch[i], sizeof(char));
ch2[i] = temp;
}
}
和使用8位宏的16位格式导致嘶嘶声!
您正在尝试添加16位值,每次8位。您需要检测低字节溢出并将其带入高字节加法,或者从一开始就使用16位算术。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- Qt和C++:将QLineEdit添加到QTabWidget中
- 将QIcon添加到QTableView单元格
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 将图像添加到资源文件夹UWP C++
- 为.wav文件添加回声效果