This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center。
7年前关闭。
输入文件是
问题在于,尝试获取.wav文件样本的
它只会在数组中找到最小值和最大值,但是会崩溃...
请告诉我有什么问题。
这是代码:
更新:
EUREKA!这是因为每个样本有8位!我必须像使用char类型一样对它们进行操作(使用示例)。 (请参见代码中的“### UPDATE ###”注释)
覆盖
您可能想要的是:
注意,不需要
7年前关闭。
输入文件是
in.wav
。我必须读取块(成功)并读取样本以规范音频文件...问题在于,尝试获取.wav文件样本的
max
和min
值时,它崩溃了。它只会在数组中找到最小值和最大值,但是会崩溃...
请告诉我有什么问题。
这是代码:
#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 ###”注释)
最佳答案
这段代码:
/// 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...
覆盖
ptr
变量n
次的第一个字节。这会破坏ptr
的值。即使您将其固定为读入分配的缓冲区(通过删除&
),您也只会重写分配的内存的第一个字节。您可能想要的是:
fread(ptr, 1, n, input);
注意,不需要
while
循环。但是,那将是我对您的真实意图的猜测。关于c - 我无法理解原因,但应用崩溃了,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16468793/
10-11 22:33