This repository was archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwavdecoder.cpp
67 lines (62 loc) · 2.04 KB
/
wavdecoder.cpp
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
#define _CRT_SECURE_NO_WARNINGS 1
#include "wavdecoder.h"
#include <malloc.h>
unsigned char* load_wav_file(const char *fname, int16_t * chans, int32_t *rate, int32_t *length, int16_t *bits)
{
FILE *fp;
fp = fopen(fname,"rb");
if (fp)
{
char id2[5];
char id[4]; //four bytes to hold 'RIFF'
unsigned char *sound_buffer;
int32_t size; //32 bit value to hold file size
int16_t format_tag, channels, block_align, bits_per_sample; //our 16 values
int32_t format_length, sample_rate, avg_bytes_sec, data_size; //our 32 bit values
fread(id, sizeof(char), 4, fp); //read in first four bytes
memcpy(id2, id, 4);
id2[4]='\0';
if (!strcmp(id2, "RIFF"))
{ //we had 'RIFF' let's continue
fread(&size, sizeof(int32_t), 1, fp); //read in 32bit size value
fread(id, sizeof(char), 4, fp); //read in 4 byte string now
memcpy(id2, id, 4);
id2[4]='\0';
if (!strcmp(id2,"WAVE"))
{ //this is probably a wave file since it contained "WAVE"
fread(id, sizeof(char), 4, fp); //read in 4 bytes "fmt ";
fread(&format_length, sizeof(int32_t),1,fp);
fread(&format_tag, sizeof(int16_t), 1, fp); //check mmreg.h (i think?) for other
// possible format tags like ADPCM
if(format_tag == 1) {
fread(&channels, sizeof(int16_t),1,fp); //1 mono, 2 stereo
*chans = channels;
fread(&sample_rate, sizeof(int32_t), 1, fp); //like 44100, 22050, etc...
*rate = sample_rate;
fread(&avg_bytes_sec, sizeof(int32_t), 1, fp); //probably won't need this
fread(&block_align, sizeof(int16_t), 1, fp); //probably won't need this
fread(&bits_per_sample, sizeof(int16_t), 1, fp); //8 bit or 16 bit file?
*bits = bits_per_sample;
fread(id, sizeof(char), 4, fp); //read in 'data'
fread(&data_size, sizeof(int32_t), 1, fp); //how many bytes of sound data we have
*length = data_size;
sound_buffer = (unsigned char *) malloc (sizeof(unsigned char) * data_size); //set aside sound buffer space
fread(sound_buffer, sizeof(unsigned char), data_size, fp); //read in our whole sound data chunk
return sound_buffer;
}
else {
return NULL;
}
}
else {
return NULL;
}
}
else {
return NULL;
}
}
else {
return NULL;
}
}