C++ 中的二进制文件,更改音频文件中原始数据的内容
Binary Files in C++, changing the content of raw data on an audio file
我以前从未使用过二进制文件。我使用 ios::binary
模式打开一个 .mp3
文件,从中读取数据,将 0
分配给读取的每个字节,然后将它们重写到另一个以 ios::binary
模式打开的文件。我在媒体播放器上打开输出文件,它听起来已损坏,但我仍然可以听到这首歌。我想知道身体上发生了什么。
如何使用 C++ access/modify 音频(视频、图像等)的原始数据(字节)(稍后练习文件 encryption/decryption)?
这是我的代码:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(){
char buffer[256];
ifstream inFile;
inFile.open("Backstreet Boys - Incomplete.mp3",ios::binary);
ofstream outFile;
outFile.open("Output.mp3",ios::binary);
while(!inFile.eof()){
inFile.read(buffer,256);
for(int i = 0; i<strlen(buffer); i++){
buffer[i] = 0;
}
outFile.write(buffer,256);
}
inFile.close();
outFile.close();
}
您所做的与二进制文件或音频无关。您只需复制文件,同时将一些字节归零。 (你没有将所有字节归零的原因是因为你使用 i<strlen(buffer)
,它只是计算到第一个零字节而不是报告缓冲区的大小。你还修改了缓冲区,这意味着 strlen(buffer)
将第一个字节置零后将长度报告为零。)
因此,您获得的音频的确切变化完全取决于 mp3 文件格式及其使用的音频压缩。 MP3 不是一种可以直接以有用方式操作的音频格式。
如果你想处理数字音频,你需要了解如何处理原始音频 is represented by computers。
其实也不难。例如,这里有一个程序可以写出仅包含 400Hz 音调的原始音频文件。
#include <fstream>
#include <limits>
int main() {
const double pi = 3.1415926535;
double tone_frequency = 400.0;
int samples_per_second = 44100;
double output_duration_seconds = 5.0;
int output_sample_count =
static_cast<int>(output_duration_seconds * samples_per_second);
std::ofstream out("signed-16-bit_mono-channel_44.1kHz-sample-rate.raw",
std::ios::binary);
for (int sample_i = 0; sample_i < output_sample_count; ++sample_i) {
double t = sample_i / static_cast<double>(samples_per_second);
double sound_amplitude = std::sin(t * 2 * pi * tone_frequency);
// encode amplitude as a 16-bit, signed integral value
short sample_value =
static_cast<short>(sound_amplitude * std::numeric_limits<short>::max());
out.write(reinterpret_cast<char const *>(&sample_value),
sizeof sample_value);
}
}
要播放声音,您需要一个可以处理原始音频的程序,例如 Audacity。 运行程序生成音频文件后,可以File > Import > Raw data...
导入数据播放
How can I access/modify the raw data ( bytes ) of an audio ( video, images, ... ) using C++ ( to practice file encryption/decryption later )?
如前所述,您现有的代码未将数据完全归零的原因是您使用的缓冲区大小不正确:strlen(buffer)
。正确的大小是放入缓冲区的字节数 read()
,您可以使用函数 gcount()
:
获得
inFile.read(buffer,256);
int buffer_size = inFile.gcount();
for(int i = 0; i < buffer_size; i++){
buffer[i] = 0;
}
outFile.write(buffer, buffer_size);
注意:如果您要使用调试器单步执行程序,当您注意到内部循环的执行比您预期的要少时,您可能很快就会自己发现问题。调试器是学习如何使用的非常方便的工具。
我注意到您在这里使用了 open()
和 close()
方法。这在这个程序中有点毫无意义。只需在构造函数中打开文件,并允许文件在 inFile
和 outFile
超出范围时自动关闭:
{
ifstream inFile("Backstreet Boys - Incomplete.mp3",ios::binary);
ofstream outFile("Output.mp3",ios::binary);
// don't bother calling .close(), it happens automatically.
}
我以前从未使用过二进制文件。我使用 ios::binary
模式打开一个 .mp3
文件,从中读取数据,将 0
分配给读取的每个字节,然后将它们重写到另一个以 ios::binary
模式打开的文件。我在媒体播放器上打开输出文件,它听起来已损坏,但我仍然可以听到这首歌。我想知道身体上发生了什么。
如何使用 C++ access/modify 音频(视频、图像等)的原始数据(字节)(稍后练习文件 encryption/decryption)?
这是我的代码:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(){
char buffer[256];
ifstream inFile;
inFile.open("Backstreet Boys - Incomplete.mp3",ios::binary);
ofstream outFile;
outFile.open("Output.mp3",ios::binary);
while(!inFile.eof()){
inFile.read(buffer,256);
for(int i = 0; i<strlen(buffer); i++){
buffer[i] = 0;
}
outFile.write(buffer,256);
}
inFile.close();
outFile.close();
}
您所做的与二进制文件或音频无关。您只需复制文件,同时将一些字节归零。 (你没有将所有字节归零的原因是因为你使用 i<strlen(buffer)
,它只是计算到第一个零字节而不是报告缓冲区的大小。你还修改了缓冲区,这意味着 strlen(buffer)
将第一个字节置零后将长度报告为零。)
因此,您获得的音频的确切变化完全取决于 mp3 文件格式及其使用的音频压缩。 MP3 不是一种可以直接以有用方式操作的音频格式。
如果你想处理数字音频,你需要了解如何处理原始音频 is represented by computers。
其实也不难。例如,这里有一个程序可以写出仅包含 400Hz 音调的原始音频文件。
#include <fstream>
#include <limits>
int main() {
const double pi = 3.1415926535;
double tone_frequency = 400.0;
int samples_per_second = 44100;
double output_duration_seconds = 5.0;
int output_sample_count =
static_cast<int>(output_duration_seconds * samples_per_second);
std::ofstream out("signed-16-bit_mono-channel_44.1kHz-sample-rate.raw",
std::ios::binary);
for (int sample_i = 0; sample_i < output_sample_count; ++sample_i) {
double t = sample_i / static_cast<double>(samples_per_second);
double sound_amplitude = std::sin(t * 2 * pi * tone_frequency);
// encode amplitude as a 16-bit, signed integral value
short sample_value =
static_cast<short>(sound_amplitude * std::numeric_limits<short>::max());
out.write(reinterpret_cast<char const *>(&sample_value),
sizeof sample_value);
}
}
要播放声音,您需要一个可以处理原始音频的程序,例如 Audacity。 运行程序生成音频文件后,可以File > Import > Raw data...
导入数据播放
How can I access/modify the raw data ( bytes ) of an audio ( video, images, ... ) using C++ ( to practice file encryption/decryption later )?
如前所述,您现有的代码未将数据完全归零的原因是您使用的缓冲区大小不正确:strlen(buffer)
。正确的大小是放入缓冲区的字节数 read()
,您可以使用函数 gcount()
:
inFile.read(buffer,256);
int buffer_size = inFile.gcount();
for(int i = 0; i < buffer_size; i++){
buffer[i] = 0;
}
outFile.write(buffer, buffer_size);
注意:如果您要使用调试器单步执行程序,当您注意到内部循环的执行比您预期的要少时,您可能很快就会自己发现问题。调试器是学习如何使用的非常方便的工具。
我注意到您在这里使用了 open()
和 close()
方法。这在这个程序中有点毫无意义。只需在构造函数中打开文件,并允许文件在 inFile
和 outFile
超出范围时自动关闭:
{
ifstream inFile("Backstreet Boys - Incomplete.mp3",ios::binary);
ofstream outFile("Output.mp3",ios::binary);
// don't bother calling .close(), it happens automatically.
}