include cstdio include cstdlib include cstring include cmath pragma pa

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#pragma pack(push, 1)
struct WavChunkHeader {
char id[4];
unsigned int size;
};
struct WavRiffHeader {
char chunk_id[4];
unsigned int chunk_size;
char format[4];
};
struct WavFmtHeader {
char chunk_id[4];
unsigned int chunk_size;
unsigned short audio_format;
unsigned short num_channels;
unsigned int sample_rate;
unsigned int byte_rate;
unsigned short block_align;
unsigned short bits_per_sample;
};
struct WavDataHeader {
char chunk_id[4];
unsigned int chunk_size;
unsigned char *data;
};
#pragma pack(pop)
int main() {
struct WavRiffHeader header;
struct WavFmtHeader fmt_header;
//FILE *fp = fopen("/home/azq2/.local/share/Steam/SteamApps/common/Half-Life/valve/sound/scientist/stopasking.wav", "rb");
FILE *fp = fopen("/tmp/test.wav", "rb");
if (fread(&header, sizeof(struct WavRiffHeader), 1, fp) != 1
|| fread(&fmt_header, sizeof(struct WavFmtHeader), 1, fp) != 1) {
fprintf(stderr, "Corrupted\n");
return 1;
}
fseek(fp, sizeof(struct WavRiffHeader) + fmt_header.chunk_size + sizeof(struct WavChunkHeader), SEEK_SET);
printf(
"channels: %d\n"
"audio_format: %d\n"
"sample_rate: %d\n"
"bits_per_sample: %d\n",
fmt_header.num_channels,
fmt_header.audio_format,
fmt_header.sample_rate,
fmt_header.bits_per_sample
);
// double freq = (double) fmt_header.sample_rate;
struct WavDataHeader data_chunk;
struct WavChunkHeader chunk;
while (!feof(fp)) {
if (fread(&chunk, sizeof(struct WavChunkHeader), 1, fp) != 1) {
fprintf(stderr, "Corrupted!!!\n");
break;
}
memcpy(data_chunk.chunk_id, chunk.id, sizeof(chunk.id));
data_chunk.chunk_size = chunk.size;
printf("id = %c.%c.%c.%c\n", data_chunk.chunk_id[0], data_chunk.chunk_id[1], data_chunk.chunk_id[2], data_chunk.chunk_id[3]);
printf("size = %d\n", data_chunk.chunk_size);
data_chunk.data = (unsigned char *) malloc(data_chunk.chunk_size);
if (fread(data_chunk.data, 1, data_chunk.chunk_size, fp) != data_chunk.chunk_size) {
fprintf(stderr, "Corrupted data!!!\n");
break;
}
unsigned int max_samples = data_chunk.chunk_size / (fmt_header.bits_per_sample / 8);
if (fmt_header.bits_per_sample == 8) {
for (unsigned int i = 0; i < max_samples; ++i) {
// printf("%f;%f\n", i / freq, (double) data_chunk.data[i] / pow(2, fmt_header.bits_per_sample - 1));
char value = (char) (data_chunk.data[i]);
printf("%.0f\n", (double) value * 500 / 128);
}
} else if (fmt_header.bits_per_sample == 16) {
for (unsigned int i = 0; i < max_samples * 2; i += 2) {
// printf("%f;%f\n", i / freq, (double) data_chunk.data[i] / pow(2, fmt_header.bits_per_sample - 1));
short value = (short) (data_chunk.data[i] << 8 | data_chunk.data[i + 1]);
printf("%d\n", value * 300 / 32767);
}
}
free(data_chunk.data);
break;
}
fclose(fp);
}