我正试图在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;
}这是我的结果。有锯齿,但有奇怪的行为。我不知道为什么会有这个。我检查了我的样本,一切看起来都很好。

有人能帮我吗?我被困住了我不知道该怎么办。我检查了两个不同的程序:听觉和波音频,在这两种情况下,这种行为存在。所以我的密码肯定有问题。请帮帮我。

发布于 2018-11-27 21:09:54
当我查看文件转储(MSVC编译)时,我注意到了一些0D 0A数据对。然后我改变了这个
fout = fopen( fileName, "w" )来指定二进制文件。
fout = fopen( fileName, "wb" )现在故障已经消失了。
请注意,
如果
t或b没有在mode中提供,则由全局变量_fmode定义默认的转换模式。
因此,默认的翻译模式必须是“文本”。
发布于 2018-11-27 19:56:57
下面是一些我得到的输出的屏幕截图。对我来说很好。
使用内置规格。COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-cygwin/5.4.0/lto-wrapper.exe目标:i 686-pc-cygwin配置: /cygdrive/i/szsz/tmpp/gcc/gcc-5.4.0-1.i686/src/gcc-5.4.0/configure --srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-5.4.0-1.i686/src/ gcc-5.4.0 -前缀=/usr-exec-前缀=/usr --localstatedir=/var -sysconfdir=/etc-docdir=/usr/share/doc/gcc-htmldir=/usr/share/doc/gcc/h tml -C -build=i 686-pc-cygwin-host=i 686-pc-cygwin-目标=i686-pc-cygwin-目标=i686-pc-cygwin-不带-libiconv前缀-不带-libintl前缀-前缀-libexecdir= /usr/lib -启用-共享-启用共享-libgcc-启用-静态-允许-特定运行时-libintl-可引导-引导-启用-允许-共享-启用-libgcc-使能__cxa_ate xit --具有-侏儒-具有-arch=i 686-具有--xit=泛型-禁用-sjlj-异常-启用--语言=ada,c,c++,fortran,java,lto,objc,obj-c++ -启用-libssp-enable-libgomp enable-libcilkrts-启用-libgomp-启用-libgomp-启用-lib44-enab-libssp-支持-启用-libssp-启用-libada-启用-libjava-启用-libgcj-sublibs禁用-java awt-禁用-symvers-与-ecj-jar=/usr/share/java/ecj.-with-gnu-as-with-gnu-as-with-cloog g-包括/usr/usr/usr/cloog-isl-没有--libintl-前缀-前缀
无名-a Cygwin _NT-WOW DE-E 03895 2.8.0(0.309/5/3) 2017-04-01 20:42 i686 Cygwin


这是你的输出中损坏的样本的屏幕截图;

https://stackoverflow.com/questions/53506849
复制相似问题