CでWAVサウンドをコピーしようとしていますが、元のファイルは2秒のファイルですが、目的のファイルのデータを何度も複製したいので長い再生。たとえば、3回コピーすると、6秒間再生されます...そうですか?cでWAVファイルをループする(またはC++)
何らかの理由で、宛先ファイルが元のファイルよりも大きい場合でも、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;
}
。形式の説明については、[ここ](http://soundfile.sapp.org/doc/WaveFormat/)を参照してください。おそらく 'Subchunk2Size'フィールドに焦点を合わせるべきです。 –
私はヘッダーを変更していません。私はそれについて考えましたが、サンプルの数を変えてオーディオを一度コピーしても、それはまだ再生されます。もちろん、サンプルレート、チャンネル数などの変更されていないフィールドもあります。 – PhoenixBlue
詳細はわかりませんが、ヘッダーは何らかの方法で調整する必要があります。新しい長さ –