Code cleanup.
This commit is contained in:
6104
source/pc/lodepng.cpp
Normal file
6104
source/pc/lodepng.cpp
Normal file
File diff suppressed because it is too large
Load Diff
1702
source/pc/lodepng.h
Normal file
1702
source/pc/lodepng.h
Normal file
File diff suppressed because it is too large
Load Diff
69
source/pc/wav.cpp
Normal file
69
source/pc/wav.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "wav.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
bool wav_find_chunk(FILE* fd, const char* magic) {
|
||||
char curr[5] = {0};
|
||||
while(strcmp(curr, magic) != 0) {
|
||||
u32 read = (u32) fread(curr, 1, 4, fd);
|
||||
if(read == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
fseek(fd, -4, SEEK_CUR);
|
||||
return true;
|
||||
}
|
||||
|
||||
WAV* wav_read(const char* file) {
|
||||
FILE* fd = fopen(file, "r");
|
||||
if(!fd) {
|
||||
printf("ERROR: Could not open WAV file: %s\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(!wav_find_chunk(fd, "RIFF")) {
|
||||
printf("ERROR: Could not find WAV RIFF chunk.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Riff riff;
|
||||
fread(&riff, sizeof(Riff), 1, fd);
|
||||
|
||||
if(!wav_find_chunk(fd, "fmt ")) {
|
||||
printf("ERROR: Could not find WAV format chunk.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Format format;
|
||||
fread(&format, sizeof(Format), 1, fd);
|
||||
|
||||
if(!wav_find_chunk(fd, "data")) {
|
||||
printf("ERROR: Could not find WAV data chunk.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Data data;
|
||||
fread(&(data.chunkId), sizeof(data.chunkId), 1, fd);
|
||||
fread(&(data.chunkSize), sizeof(data.chunkSize), 1, fd);
|
||||
data.data = (u8*) malloc(data.chunkSize);
|
||||
fread(data.data, 1, data.chunkSize, fd);
|
||||
|
||||
fclose(fd);
|
||||
|
||||
WAV* wav = (WAV*) malloc(sizeof(WAV));
|
||||
wav->riff = riff;
|
||||
wav->format = format;
|
||||
wav->data = data;
|
||||
return wav;
|
||||
}
|
||||
|
||||
void wav_free(WAV* wav) {
|
||||
if(wav != NULL) {
|
||||
free(wav->data.data);
|
||||
free(wav);
|
||||
}
|
||||
}
|
||||
38
source/pc/wav.h
Normal file
38
source/pc/wav.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef __WAV_H__
|
||||
#define __WAV_H__
|
||||
|
||||
#include "../types.h"
|
||||
|
||||
typedef struct {
|
||||
char chunkId[4];
|
||||
u32 chunkSize;
|
||||
char format[4];
|
||||
} Riff;
|
||||
|
||||
typedef struct {
|
||||
char chunkId[4];
|
||||
u32 chunkSize;
|
||||
u16 format;
|
||||
u16 numChannels;
|
||||
u32 sampleRate;
|
||||
u32 byteRate;
|
||||
u16 align;
|
||||
u16 bitsPerSample;
|
||||
} Format;
|
||||
|
||||
typedef struct {
|
||||
char chunkId[4];
|
||||
u32 chunkSize;
|
||||
u8* data;
|
||||
} Data;
|
||||
|
||||
typedef struct {
|
||||
Riff riff;
|
||||
Format format;
|
||||
Data data;
|
||||
} WAV;
|
||||
|
||||
WAV* wav_read(const char* file);
|
||||
void wav_free(WAV* wav);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user