-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f56b018
commit d5a83d1
Showing
5 changed files
with
233 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "wavLoader.h" | ||
|
||
#include <ncurses.h> | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
struct wavFile * myCoolWav; | ||
if(argc > 1){ | ||
myCoolWav = loadWav(argv[1]); | ||
}else{ | ||
return -1; | ||
} | ||
|
||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "wavLoader.h" | ||
|
||
unsigned long pullFromBuffer(unsigned char * buffer, int length, int startAddress, int dataType){ | ||
unsigned long byteSet; | ||
if (dataType){ //little endian | ||
byteSet = buffer[startAddress+(length-1)]; | ||
for (int i = length-2; i >= 0; i--){ // -2 offset as need -1 cos array start at 0 and -1 as we already done one above | ||
//std::cout << std::hex << byteSet << " " << buffer[startAddress+i] << std::endl; | ||
byteSet = byteSet << 8 | buffer[startAddress+i]; | ||
} | ||
}else{ | ||
byteSet = buffer[startAddress]; | ||
for (int i = 1; i < length; i++){ | ||
byteSet = byteSet << 8 | buffer[startAddress+i]; | ||
} | ||
} | ||
return byteSet; | ||
} | ||
|
||
struct wavFile * loadWav(char * filePath){ | ||
//open file and get size | ||
FILE *fptr; | ||
unsigned long fSize; | ||
fptr = fopen(filePath,"rb"); | ||
if (fptr == NULL){ | ||
fprintf(stderr, "%s", "File could not be opened!\n"); | ||
return NULL; | ||
} | ||
fseek ( fptr , 0 , SEEK_END ); | ||
fSize = ftell(fptr); | ||
rewind(fptr); | ||
|
||
//create file buffer | ||
unsigned char * buffer = (unsigned char*) malloc (sizeof(char)*fSize); | ||
if (buffer == NULL){ | ||
fprintf(stderr, "%s", "Memory allocation error will attempt to read from disk!\n"); | ||
//TODO: disk streaming | ||
} | ||
|
||
//populate buffer in 1 byte increments | ||
if (fread(buffer,1,fSize,fptr) != fSize){ | ||
fprintf(stderr, "%s", "File could not be read!\n"); | ||
return NULL; | ||
}else{ | ||
fclose(fptr); | ||
} | ||
|
||
if (!( (pullFromBuffer(buffer, 4, 8, BIG_ENDIAN_TAG) == 0x57415645) && (pullFromBuffer(buffer, 2, 20, LITTLE_ENDIAN_TAG) == 1) )){ //byte 8 is format, byte 20 is audioFormat | ||
fprintf(stderr, "%s", "File is not standard wav!\n"); | ||
return NULL; | ||
} | ||
|
||
struct wavFile *wf; | ||
//file info | ||
wf->Channels = pullFromBuffer(buffer, 2, 22, LITTLE_ENDIAN_TAG);//2 bytes at 20 | ||
wf->SampleRate = pullFromBuffer(buffer, 2, 24, LITTLE_ENDIAN_TAG);//4 bytes at 24 | ||
wf->BlockAlign = pullFromBuffer(buffer, 2, 32, LITTLE_ENDIAN_TAG);//2 bytes at 32 | ||
wf->BitsPerSample = pullFromBuffer(buffer, 2, 34, LITTLE_ENDIAN_TAG);//2 bytes at 34 | ||
|
||
//file data | ||
unsigned long audioDataSize = pullFromBuffer(buffer, 4, 40, LITTLE_ENDIAN_TAG); | ||
wf->AudioData = (unsigned char*) malloc (sizeof(char)*audioDataSize); //4 bytes at 40 | ||
|
||
//move the data array | ||
memmove(wf->AudioData, buffer+44, audioDataSize); | ||
free(buffer); | ||
|
||
printf ("Channels: %d\n", wf->Channels); | ||
printf ("Sample Rate: %d\n", wf->SampleRate); | ||
printf ("Block Align: %d\n", wf->BlockAlign); | ||
printf ("Bits Per Sample: %d\n", wf->BitsPerSample); | ||
printf ("Audio Data Size: %lu\n\n", audioDataSize); | ||
|
||
printf ("Audio Data: %x %x %x %x\n", wf->AudioData[0], wf->AudioData[1], wf->AudioData[2], wf->AudioData[3]); | ||
|
||
|
||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#ifndef WAVLOADER_H | ||
#define WAVLOADER_H | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#define BIG_ENDIAN_TAG 0 | ||
#define LITTLE_ENDIAN_TAG 1 | ||
|
||
/* | ||
wav format | ||
wav is actually a wrapper for another format which is the riff format | ||
riff files store everything in juicy lil chunks | ||
every riff chunk has this layout: | ||
4 byte ChunkID | ||
4 byte ChunkSize (this excludes the size of chunk id and chunk size) | ||
the rest of the chunks info | ||
How are Wave files stored: | ||
Chunk 1 ("Riff Chunk Descriptor"): | ||
Endian |Pos| Size |Data Stored | Descriptions | ||
big | 0 |4 bytes |Chunk ID | RIFF (0x52494646) | ||
little | 4 |4 bytes |Chunk Size | 4 + (8 + Chunk2Size) + (8 + Chunk3Size) (size of full file excluding the first 8 bytes) | ||
big | 8 |4 bytes |Format | WAVE (0x57415645) | ||
Chunk 2 ("Format chunk"): | ||
big | 12 |4 bytes |Chunk ID | fmt (0x666D7420) | ||
little | 16 |4 bytes |Chunk Size | 16 (0x10000000) | ||
little | 20 |2 bytes |AudioFormat | If this is equal to 1 its PCM audio otherwise its got some compression or something | ||
little | 22 |2 bytes |NumChannels | number of channels | ||
little | 24 |4 bytes |Sample Rate | sample rate like 8000 or 44100 | ||
little | 28 |4 bytes |Byte Rate | SampleRate * NumChannels * BitsPerSample/8 | ||
little | 32 |2 bytes |Block Align | NumChannels * BitsPerSample/8 (num of bytes per one audio sample) | ||
little | 34 |2 bytes |Bits Per Sample | number of bits per sample | ||
Chunk 2 ("Data Chunk"): | ||
big | 36 |4 bytes |Chunk ID | data (0x64617461) | ||
little | 40 |4 bytes |Chunk Size | wav data size | ||
little | 44 |X bytes |WAV Data | The actual sound data | ||
how is the wav data stored: | ||
each sample has (Block Align)Bytes, the left channel is the first half of the sample and the right channel is the second half. | ||
*/ | ||
|
||
//struct sample?? | ||
|
||
struct wavFile{ | ||
int Channels; | ||
int SampleRate; | ||
int BlockAlign; | ||
int BitsPerSample; | ||
unsigned char * AudioData; | ||
}; | ||
|
||
struct wavFile * loadWav(char * filePath); | ||
|
||
unsigned long pullFromBuffer(unsigned char * buffer, int length, int startAddress, int dataType); | ||
|
||
#endif /* WAVLOADER_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters