gpt4 book ai didi

c - 应用程序崩溃,但我不明白原因

转载 作者:太空宇宙 更新时间:2023-11-04 02:09:19 25 4
gpt4 key购买 nike

<分区>

输入文件是in.wav。我必须读取 block (成功)并读取样本以规范化音频文件...

问题是它在尝试获取 .wav 文件样本的 maxmin 值时崩溃。它只会找到数组中的最小值和最大值,但它崩溃了......请告诉我哪里出了问题。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#define hdr_SIZE 64

typedef struct FMT
{
char SubChunk1ID[4];
int SubChunk1Size;
short int AudioFormat;
short int NumChannels;
int SampleRate;
int ByteRate;
short int BlockAlign;
short int BitsPerSample;

} fmt;

typedef struct DATA
{
char Subchunk2ID[4];
int Subchunk2Size;
int Data[441000];
} data;

typedef struct HEADER
{
char ChunkID[4];
int ChunkSize;
char Format[4];
fmt S1;
data S2;
} header;



int main()
{
FILE *input = fopen( "in.wav", "rb"); /// nameIn

if(input == NULL)
{
printf("Unable to open wave file (input)\n");
exit(EXIT_FAILURE);
}

FILE *output = fopen( "out.wav", "wb"); /// nameOut
header hdr;


fread(&hdr, sizeof(char), hdr_SIZE, input);
/* NOTE: Chunks has been copied successfully. */


/*###############################*/
/*##### UPDATE (char *ptr;) #####*/
/*###############################*/
char *ptr; // 'int' was written here instead of 'char'. That's was a stupid mistake...
long n = hdr.S2.Subchunk2Size;


/// COPYING SAMPLES...
ptr = malloc(sizeof(hdr.S2.Subchunk2Size));
while ( n-- != 0 )
{
fread(&ptr, 1, 1, input); // Continues reading after the least 'stop' place.
} // I was being told here (on "stack") that it is so...


n = hdr.S2.Subchunk2Size; // Resetting 'n'.
int min = ptr[0], max = ptr[0], i;

/* THE PROBLEM IS HERE: */
for ( i = 0; i < n; i++ )
{
if ( ptr[i] < min ) // If the next elements is less than previous, swap them.
min = ptr[i];
if ( ptr[i] > max ) // If the next elements is bigger than previous, swap them.
max = ptr[i];
}

printf("> > >%d__%d\n", min, max); // Displaying of 'min' and 'max'.

fclose(input);
fclose(output);

return 0;
}

更新:

Eureka !这都是因为每个样本有 8 位!我必须像处理一种 char 一样处理它们(使用样本)。 (请参阅我的“### UPDATE ###”-代码中的注释)

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