gpt4 book ai didi

c - c中的声波锯齿波

转载 作者:太空狗 更新时间:2023-10-29 16:53:54 25 4
gpt4 key购买 nike

我正在尝试在 c 中生成锯齿波。我的工作快结束了,但我遇到了必须解决的问题。我在下面附上了 clode。

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <tgmath.h>

/******************************
* Magic file format strings. *
******************************/
const char fChunkID[] = {'R', 'I', 'F', 'F'};
const char fFormat[] = {'W', 'A', 'V', 'E'};
const char fSubchunk1ID[] = {'f', 'm', 't', ' '};
const char fSubchunk2ID[] = {'d', 'a', 't', 'a'};

/********************************
* WriteWavePCM() configuration: *
* - 2 channels, *
* - frequency 44100 Hz. *
********************************/
const unsigned short N_CHANNELS = 2;
const unsigned int SAMPLE_RATE = 48000;
const unsigned short BITS_PER_BYTE = 8;

bool WriteWavePCM(short* sound, size_t pairAmount, char* fileName){
const static unsigned int fSubchunk1Size = 16;
const static unsigned short fAudioFormat = 1;
const static unsigned short fBitsPerSample = 16;

unsigned int fByteRate = SAMPLE_RATE * N_CHANNELS * fBitsPerSample / BITS_PER_BYTE;

unsigned short fBlockAlign = N_CHANNELS * fBitsPerSample / BITS_PER_BYTE;
unsigned int fSubchunk2Size;
unsigned int fChunkSize;

FILE* fout;
size_t ws;

if (!sound || !fileName || !(fout = fopen( fileName, "w" ))) return false;

fSubchunk2Size = pairAmount * N_CHANNELS * fBitsPerSample / BITS_PER_BYTE;
fChunkSize = 36 + fSubchunk2Size;

// Writing the RIFF header:
fwrite(&fChunkID, 1, sizeof(fChunkID), fout);
fwrite(&fChunkSize, sizeof(fChunkSize), 1, fout);
fwrite(&fFormat, 1, sizeof(fFormat), fout);

// "fmt" chunk:
fwrite(&fSubchunk1ID, 1, sizeof(fSubchunk1ID), fout);
fwrite(&fSubchunk1Size, sizeof(fSubchunk1Size), 1, fout);
fwrite(&fAudioFormat, sizeof(fAudioFormat), 1, fout);
fwrite(&N_CHANNELS, sizeof(N_CHANNELS), 1, fout);
fwrite(&SAMPLE_RATE, sizeof(SAMPLE_RATE), 1, fout);
fwrite(&fByteRate, sizeof(fByteRate), 1, fout);
fwrite(&fBlockAlign, sizeof(fBlockAlign), 1, fout);
fwrite(&fBitsPerSample, sizeof(fBitsPerSample), 1, fout);

/* "data" chunk: */
fwrite(&fSubchunk2ID, 1, sizeof(fSubchunk2ID), fout);
fwrite(&fSubchunk2Size, sizeof(fSubchunk2Size), 1, fout);

/* sound data: */
ws = fwrite(sound, sizeof(short), pairAmount * N_CHANNELS, fout);
fclose(fout);
return true;
}

////////////////////////////////////////////////

const unsigned int N_SAMPLE_PAIRS = 50000;

int main(int argc, char* argv[]){
short* sound;
int i;
int j;
bool status;
char* file_name;
int l; // dodane

sound = (int*) malloc(sizeof(int) * N_SAMPLE_PAIRS * N_CHANNELS );

if (!sound)
{
puts("Could not allocate space for the sound data.");
return (EXIT_FAILURE);
}

int amplitude = 10000;
int frequency = 80;
short record = 0;
short waveNumber = 1;
int samplesPerWavelength = SAMPLE_RATE / (frequency/N_CHANNELS);
int soundLen = 10 * samplesPerWavelength;
int ampStep = (int)((amplitude*2)/(int)samplesPerWavelength);
short step = 5*samplesPerWavelength;
short muteRate = amplitude/(soundLen/samplesPerWavelength);
int totalSamplesWritten = 0;
int tempSample =0;

for (i=0, j=0; i<N_SAMPLE_PAIRS*N_CHANNELS; i+=2, j++) {
ampStep = (int)((amplitude*2)/(int)samplesPerWavelength);
tempSample = (int)((totalSamplesWritten%samplesPerWavelength)*ampStep);
sound[i] = tempSample;
sound[i+1] = tempSample;
totalSamplesWritten++;
}


file_name = argc > 1 ? argv[1] : "Default2.wav";
status = WriteWavePCM(sound, N_SAMPLE_PAIRS, file_name);


free(sound);

if (status)
{
printf("Discotheque is ready in \"%s\"\n", file_name);
}
else
{
puts( "Something seems to have gone wrong." );
return (EXIT_FAILURE);
}

return 0;
}

这是我的结果。有锯齿但有奇怪的行为。我不知道为什么会这样。我检查了我的 sample 阵列,一切看起来都很好。

enter image description here

有人可以帮助我吗?我被卡住了,我不知道该怎么办。我在 2 个不同的程序中检查了它:audiocity 和 wavepadaudio,然后在这两个程序中,都存在这种行为。所以我的代码肯定有问题。请帮助我。

enter image description here

最佳答案

当我查看文件转储(MSVC 编译)时,我注意到一些 0D 0A 数据对。然后我改变了这个

fout = fopen( fileName, "w" )

为此指定一个二进制文件

fout = fopen( fileName, "wb" )

现在故障消失了。

注意

If tor b is not given in mode, the default translation mode is defined by the global variable _fmode.

所以默认的翻译模式一定是“文本”。

关于c - c中的声波锯齿波,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53506849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com