C 产生正弦波超过1秒时出现问题

C 产生正弦波超过1秒时出现问题,c,wave,sine-wave,C,Wave,Sine Wave,我搜索了几乎所有的论坛,但没有找到。我没有从头开始编写代码。我得到了代码,我正试图根据我的要求进行修改 所以首先要归功于原始编码器。我很高兴: 在这里,他创建的正弦波只有1秒。但我无法超越这一点。我需要至少播放20秒 这是我的密码。希望得到一些提示/帮助。提前谢谢。再次感谢原始编码器 WAVFILE\u每秒采样数=44100 示例.c文件 #include <stdio.h> #include <math.h> #include <stdlib.h> #in

我搜索了几乎所有的论坛,但没有找到。我没有从头开始编写代码。我得到了代码,我正试图根据我的要求进行修改

所以首先要归功于原始编码器。我很高兴:

在这里,他创建的正弦波只有1秒。但我无法超越这一点。我需要至少播放20秒

这是我的密码。希望得到一些提示/帮助。提前谢谢。再次感谢原始编码器

WAVFILE\u每秒采样数=44100

示例.c文件

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <errno.h>

#include "wavfile.h"

const int NUM_SAMPLES = (WAVFILE_SAMPLES_PER_SECOND*2);

int main()
{
    unsigned long waveform[NUM_SAMPLES*4]; // here I am changing it to 4
    double frequency = 440;
    int volume = 255;
    unsigned long int length = NUM_SAMPLES*4; // here I am changing it to 4

    // For one second the length = 88200
    unsigned long int i;
    for(i=0;i<length;i++) {
        long double t = (long double) i / WAVFILE_SAMPLES_PER_SECOND;
        waveform[i] = volume*sin(frequency*t*2*M_PI);
    }

    printf("length=%d\n",length);
    FILE * f = wavfile_open("sound.wav");
    if(!f) {
        printf("couldn't open sound.wav for writing: %s",strerror(errno));
        return 1;
    }

    wavfile_write(f,waveform,length);
    wavfile_close(f);

    return 0;
}
#ifndef WAVFILE_H
#define WAVFILE_H

#include <stdio.h>
#include <inttypes.h>

FILE * wavfile_open( const char *filename );
void wavfile_write( FILE *file, short data[], int length );
void wavfile_close( FILE * file );

#define WAVFILE_SAMPLES_PER_SECOND 44100

#endif
#包括
#包括
#包括
#包括
#包括
#包括
#包括“wavfile.h”
const int NUM_SAMPLES=(WAVFILE_SAMPLES_/秒*2);
int main()
{
无符号长波形[NUM_SAMPLES*4];//这里我将它改为4
双频=440;
int volume=255;
unsigned long int length=NUM_SAMPLES*4;//这里我将其更改为4
//一秒钟的长度=88200
无符号长整数i;

对于(i=0;i在原始版本中,创建了1秒的wav文件。据我所知,该文件工作正常。以下行将1秒的音频写入.wav文件:

wavfile_write(f,waveform,length);

应该可以在一个循环中调用该行20次,以获得20秒的音频。由于正弦的频率为440 Hz,440个完整的正弦适合一秒。因此,正弦在第二秒开始时的位置与第一秒相同。因此,我认为正弦应该是正确的。

对于原始代码,要获得20秒,您只需需要换电话吗

const int NUM_SAMPLES = (WAVFILE_SAMPLES_PER_SECOND*2);


因此,您唯一的更改是在两个标记位置添加
*4
?结果如何?是否出现堆栈溢出?没有添加4不会在编译中给出任何错误。但是添加8将显示进程返回-1073741571(0xC00000FD)。顺便说一句,我在windows 7中使用了代码块。在你说检查wavfile_open和wavfile_write之后,我找到了它。非常感谢你的帮助。在早上1:30,我倾向于失去焦点。是的,我回过几分钟。感谢你指出它。至少它可能会帮助其他人。假设
sizeof(int)>2
const int NUM_SAMPLES = (WAVFILE_SAMPLES_PER_SECOND*20);