2014-12-26 291 views
3

我試圖創建一個C代碼,產生10秒的C音符。但它似乎輸出.wav文件沒有產生任何聲音。C輸出Wav文件沒有產生任何聲音

我還是新的C編程,這將是有益的,如果你能指出我的錯誤。

#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 

//music note 
#define C 261.6256 

#define TIME 10 
#define POINT 20 
#define AMP 10000 
#define c 5 

//wav file header 
typedef struct 
{ 
    char ChuckID[4]; 
    unsigned long ChuckSize; 
    char format[4]; 
    char subChunk1ID[4]; 
    unsigned long SubChunk1Size; 
    unsigned short AudioFormat; 
    unsigned short NumChannels; 
    unsigned long SampleRate; 
    unsigned long ByteRate; 
    unsigned short block_allign; 
    unsigned short bits_per_sample; 
    char data[4]; 
    unsigned long data_size; 

    /*char riff_tag[4]; 
    int  riff_length; 
    char wave_tag[4]; 
    char fmt_tag[4]; 
    int  fmt_length; 
    short audio_format; 
    short num_channels; 
    int  sample_rate; 
    int  byte_rate; 
    short block_align; 
    short bits_per_sample; 
    char data_tag[4]; 
    int  data_length;*/ 
} wavheader; 

int main(int argc, char **argv) 
{ 
    wavheader wave = {"RIFF",1764036,"WAVE","fmt",16,1,1,44100,176400,4,32,"data",1764000}; 

    float data; 
    float f = C; 
    int fs = 44100; 
    int k; 
    float *buff; 

    FILE *out_file = fopen("ongaku.wav","w"); 
    buff = (float*)malloc(sizeof(float)*fs*TIME); 

    for (k = 0; k<(int)(TIME*fs); k++) 
    { 
     data=AMP*sin(2*M_PI*f*k/fs); 
     //printf("%f\n",data); 
    } 

    fwrite(buff,sizeof(float),fs*TIME,out_file); 

    return 0; 
} 
+2

您還沒有向文件或任何數據寫入「buff」。 –

回答

1

我有這個工作的8位數據,但不成功,12位/ 16位更不用說float數據。有一兩件事是重要的,是不是在頭硬編碼的緩衝區大小。其他需要注意的字節序(我碰巧不是需要調整),以及結構包裝(同上)。 12位數據時,我的BPS/8使用也將來到脫膠。

#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 

#define FREQ 261.6256 // C 
//#define FREQ 440.0  // A 
#define M_PI 3.14159265358979323846 
#define TIME 10 
#define AMP  64.0  // don't use max volume 
#define MID  128.0  // 8-bit is range 0..255 
//#define MID 0.0   // 16-bit is range -32767.. 32767 
#define BPS  8 
#define CHANNS 1 
#define RATE 44100 

//wav file header 
typedef struct { 
    char ChuckID[4]; 
    unsigned long ChuckSize; 
    char format[4]; 
    char subChunk1ID[4]; 
    unsigned long SubChunk1Size; 
    unsigned short AudioFormat; 
    unsigned short NumChannels; 
    unsigned long SampleRate; 
    unsigned long ByteRate; 
    unsigned short block_allign; 
    unsigned short bits_per_sample; 
    char data[4]; 
    unsigned long data_size; 
} wavheader; 

int main(int argc, char **argv) 
{ 
    int k, samples = RATE * TIME; 
    double data; 
    FILE *out_file; 
    unsigned char *buff; 
    wavheader wave = { 
     "RIFF", 
     36 + samples * CHANNS * BPS/8, 
     "WAVE", 
     "fmt ",   // "fmt" was error in OP 
     16, 
     1, 
     CHANNS, 
     RATE, 
     RATE * CHANNS * BPS/8, 
     CHANNS * BPS/8, 
     BPS, 
     "data", 
     samples * CHANNS * BPS/8 
     }; 

    buff = malloc(BPS/8 * samples); 
    out_file = fopen("ongaku.wav","w"); 
    fwrite(&wave, sizeof(wave), 1, out_file); 
    for (k=0; k<samples; k++) { 
     data = MID + AMP * sin(2 * M_PI * FREQ * TIME * k/(double)samples); 
     buff[k] = (unsigned char)floor(data+0.5); 
    } 
    fwrite(buff, BPS/8, samples, out_file); 
    fclose (out_file); 
    free (buff); 
    return 0; 
} 
+0

謝謝...我的工作:d –

+0

@Kamilin感謝。我發現使用最大幅度創造了令人不快的聲音。對不起,我不知道爲什麼我不能讓16位工作。 –

1

把一些數據放入buff,我猜你的數據變量是保存該值。而 後,如果一切正常,使用

fflush(out_file); 

或使用

fclose(out_file);