2017-05-04 17 views
2

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; 
} 
+1

。形式の説明については、[ここ](http://soundfile.sapp.org/doc/WaveFormat/)を参照してください。おそらく 'Subchunk2Size'フィールドに焦点を合わせるべきです。 –

+0

私はヘッダーを変更していません。私はそれについて考えましたが、サンプルの数を変えてオーディオを一度コピーしても、それはまだ再生されます。もちろん、サンプルレート、チャンネル数などの変更されていないフィールドもあります。 – PhoenixBlue

+0

詳細はわかりませんが、ヘッダーは何らかの方法で調整する必要があります。新しい長さ –

答えて

0

データのサイズを変更する場合は、出力ヘッダーも更新する必要があります。また

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); 

、man1_nb.wavのmeta->chunk_size ==ファイルサイズという正しいファイルのチェックを読んでいることを確認するために - あなたは、出力ファイルのヘッダに任意のchangementsをしない8

+0

パベル、あなたのコードで、なぜ "36"ですか?第6ラインで – PhoenixBlue

+0

@PhoenixBlueこれは、chunk_sizeフィールドを超えたバイト数です。 36は 'sizeof(header) - sizeof(meta-> chunk_id) - sizeof(meta-> chunk_size) 'です。 – Pavel

+0

ありがとうございます!出来た! – PhoenixBlue

2

は、あなたの読み取りと書き込みのコード部分の両方が間違っています。

wavファイルにはRIFF formatがあり、tlvチャンクで構成されています。各チャンクは、ヘッダーとデータで構成されています。通常、wavファイルは、フォーマットチャンクFOURCCコード、フォーマットチャンクPCMWAVEFORMAT構造体とデータチャンクの3つのチャンクで構成されています。また、各チャンクのサイズは32ビットの長さの保持フィールドによって制限されるため、wavファイルを連結して大きなファイルを構築します。

チャンクごとのファイルを解析し、チャンクごとにチャンクごとに書き込んで、それに応じてヘッダを更新する必要があります。

+0

ありがとう、それを見てください – PhoenixBlue

関連する問題