Skip to content

Instantly share code, notes, and snippets.

@2garryn
Created February 10, 2021 18:15
Show Gist options
  • Save 2garryn/7309bff86ab74769cd1fd43cc94cf587 to your computer and use it in GitHub Desktop.
Save 2garryn/7309bff86ab74769cd1fd43cc94cf587 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct {
char chunkId[4];
uint32_t chunkSize;
char format[4]; // "WAVE"
char subchunk1Id[4]; // "fmt"
uint32_t subchunk1Size;
uint16_t audioFormat;
uint16_t numChannels;
uint32_t sampleRate;
uint32_t byteRate;
uint16_t blockAlign;
uint16_t bitsPerSample;
char subchunk2Id[4]; // "data"
uint32_t subchunk2Size;
} WavFile;
int main(int argc, char *argv[]){
if(argc < 2){
printf("File path not mentioned\n");
exit(0);
}
const char *filepath = argv[1];
int fd = open(filepath, O_RDONLY);
if(fd < 0){
printf("\n\"%s \" could not open\n",
filepath);
exit(1);
}
struct stat statbuf;
int err = fstat(fd, &statbuf);
if(err < 0){
printf("\n\"%s \" could not open\n",
filepath);
exit(2);
}
WavFile *wav = (WavFile *) malloc(sizeof(WavFile));
uint16_t* data;
char *ptr = mmap(wav, sizeof(WavFile), PROT_READ, MAP_PRIVATE, fd, 0);
//char *ptr = mmap(wav, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if(ptr == MAP_FAILED){
printf("Mapping Failed\n");
return 1;
}
close(fd);
printf("format: %s\n", wav->chunkId);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment