我想处理一个 .wav 文件,例如降低幅度;当我使用以下代码时,输出会失真,这并不令人愉快。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char* wav_mem;
ifstream wav_file;
wav_file.open("1.wav", ios::binary | ios::ate);
int file_size = wav_file.tellg();
wav_mem = new char[file_size];
wav_file.seekg(0, ios::beg);
wav_file.read(wav_mem, file_size);
int16_t sample = 0;
wav_file.close();
for(int i = 44; i <= file_size; i += 2)
{
sample = ((wav_mem[i + 1] << 8) | (wav_mem[i]));
sample = (int16_t)(sample * 0.5);
wav_mem[i] = sample;
wav_mem[i+1] = (sample >> 8);
}
ofstream out_file;
out_file.open("out.wav", ios::binary);
out_file.write(wav_mem, file_size);
}
如何修复失真?