通过C(或C )中的WAV文件循环
loop through WAV file in c(or c++)
我正在尝试在C中复制Wav声音。原始文件是2秒的文件,但我想几次复制数据中的数据,以便它播放更长的时间。例如,如果我复制3次,它应该播放6秒...对吗?
,但由于某种原因,即使目标文件大于原始文件,它仍然播放2秒...有人可以帮忙吗?
这是我的代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct header_file
{
char chunk_id[4];
int chunk_size;
char format[4];
char subchunk1_id[4];
int subchunk1_size;
short int audio_format;
short int num_channels;
int sample_rate;
int byte_rate;
short int block_align;
short int bits_per_sample;
char subchunk2_id[4];
int subchunk2_size;
} header;
typedef struct header_file* header_p;
int main()
{
FILE * infile = fopen("../files/man1_nb.wav","rb"); // Open wave file in read mode
FILE * outfile = fopen("../files/Output.wav","wb"); // Create output ( wave format) file in write mode
int BUFSIZE = 2; // BUFSIZE can be changed according to the frame size required (eg: 512)
int count = 0; // For counting number of frames in wave file.
short int buff16[BUFSIZE]; // short int used for 16 bit as input data format is 16 bit PCM audio
header_p meta = (header_p)malloc(sizeof(header)); // header_p points to a header struct that contains the wave file metadata fields
int nb; // variable storing number of byes returned
if (infile)
{
fread(meta, 1, sizeof(header), infile); // Read only the header
fwrite(meta,1, sizeof(*meta), outfile); // copy header to destination file
int looper = 0; // number of times sound data is copied
for(looper=0; looper <2; looper++){
while (!feof(infile))
{
nb = fread(buff16,1,BUFSIZE,infile); // Reading data in chunks of BUFSIZE
count++; // Incrementing Number of frames
fwrite(buff16,1,nb,outfile); // Writing read data into output file
}
fseek(infile, 44, SEEK_SET); // Go back to end of header
}
}
fclose(infile); fclose(outfile);
return 0;
}
您的两个读取代码部分都是错误的。
WAV文件具有RIFF
格式,由TLV块组成。每个块由标题和数据组成。通常,WAV文件由3个块组成:带有FOURCC
代码的格式块,格式块,带有PCMWAVEFORMAT
结构,以及带有声音数据的数据块。同样,由于每个块的大小受到32位长度保存字段的限制,因此通过将WAV文件连接在一起构建大文件。
您需要解析文件块块,然后写入目标块,相应地更新标头。
当您更改数据大小时,您也需要更新输出标头。
long total_bytes_written_to_outfile = ftell(outfile);
// correct chunk_size and subchunk2_size just before closing outfile:
fseek(outfile, 0, SEEK_SET);
int size = total_bytes_written_to_outfile - sizeof(*meta);
meta->chunk_size = sizeof(header) - sizeof(meta->chunk_id) - sizeof(meta->chunk_size) + size;
meta->subchunk2_size = size;
fwrite(meta, 1, sizeof(*meta), outfile);
fclose(outfile);
另外,要确保您正在阅读正确的文件检查 meta->chunk_size
== man1_nb.wav的文件大小-8
相关文章:
- 尝试使用来自 Win32 WASAPI C++的"捕获流"创建 wav 文件
- 如何从wav文件中获取采样率?
- 写入 wav 文件时定期创建静音
- 如何使用curl c ++源代码发布wav文件数据,如何使用c ++使用--data-binary?
- 为什么 XAudio2 仅在系统暂停时播放.wav文件?
- webRTC : 如何通过从WAV文件获得的样本将webRTC的VAD应用于音频
- WAV文件停止功能
- 如何从特定点播放.WAV文件(例如30秒)
- 通过C(或C )中的WAV文件循环
- 开放式阅读WAV文件lib
- 使用C++ / Python的wav文件的FFT
- 在mbed中打开的wav文件数量是否有限制
- 音频编程 SDL - 如何使用 wav 文件在命令行中指定频率切割
- 无法使用两种不同的频率填充 *.wav 文件的缓冲区
- 禁用在使用QAudioRecorder时创建wav文件
- 无论如何,我可以从WAV文件中打印Sox即可打印幅度值
- WAV文件的数据在-1和1之间,C ,SNDFILE
- 如何将.wav文件保存在MFC中(使用文件对话框)
- 分析.WAV 文件
- 用A4色调扩展.WAV文件