Skip to content

Commit

Permalink
Implemented WAVE header structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Soreing committed Aug 24, 2021
1 parent 8ce04d9 commit bf3f9dc
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Visual Studio
.vscode

# Build
bin/
build/


# Prerequisites
*.d

Expand Down
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.0.0)
project(new_project VERSION 0.1.0)

include(CTest)
enable_testing()

include_directories(./include)
file(GLOB TARGET_SRC "./src/*.cpp" )

add_executable(main ${TARGET_SRC})


set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
28 changes: 28 additions & 0 deletions include/adpcm-lib/wave.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef WAVE_H
#define WAVE_H

// Information taken from online at:
// http://soundfile.sapp.org/doc/WaveFormat/

struct WaveHeader
{
char chunkID[4] = {'R','I','F','F'}; // Contains the letters "RIFF" in ASCII form
long chunkSize; // 4 + (8 + SubChunk1Size) + (8 + SubChunk2Size)
char format[4] = {'W','A','V','E'}; // Contains the letters "WAVE" in ASCII form

char subchunk1ID[4] = {'f','m','t',' '}; // Contains the letters "fmt " in ASCII form
long subchunk1Size = 16; // 16 for PCM
short audioFormat; // PCM = 1 Values other than 1 indicate some form of compression
short numChannels; // Mono = 1, Stereo = 2, etc
long sampleRate; // 8000, 44100, etc.
long byteRate; // SampleRate * NumChannels * BitsPerSample/8
short blockAlign; // NumChannels * BitsPerSample/8
short bitsPerSample; // 8 bits = 8, 16 bits = 16, etc

char subchunk2ID[4] = {'d','a','t','a'}; // Contains the letters "data" in ASCII form
long subchunk2Size; // This is the number of bytes in the data
};

bool isCorrectHeader(struct WaveHeader *hdr);

#endif
13 changes: 13 additions & 0 deletions src/Source.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <adpcm-lib/wave.h>
#include <iostream>
#include <fstream>

int main()
{
struct WaveHeader hdr;
std::ifstream in("test.wav", std::ios::binary);

in.read((char*)&hdr, sizeof(struct WaveHeader));
std::cout<< isCorrectHeader(&hdr) << "\n";
system("PAUSE");
}
30 changes: 30 additions & 0 deletions src/wave.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <adpcm-lib/wave.h>
#include <string.h>

bool isCorrectHeader(struct WaveHeader *hdr)
{
if(memcmp(hdr->chunkID, "RIFF", 4) == 0)
{
if(memcmp(hdr->format, "WAVE", 4) == 0)
{
if(memcmp(hdr->subchunk1ID, "fmt ", 4) == 0)
{
if(memcmp(hdr->subchunk2ID, "data", 4) == 0)
{
int expectedSize = 4 + (8 + hdr->subchunk1Size) + (8 + hdr->subchunk2Size);
if(hdr->subchunk1Size == 16 && hdr->chunkSize == expectedSize)
{
int expectedAlign = hdr->numChannels * hdr->bitsPerSample/8;
int expectedByteRate = expectedAlign * hdr->sampleRate;
if(hdr->blockAlign == expectedAlign && hdr->byteRate == expectedByteRate)
{
return true;
}
}
}
}
}
}

return false;
}

0 comments on commit bf3f9dc

Please sign in to comment.