Last active
July 2, 2021 11:58
-
-
Save ghedo/963387 to your computer and use it in GitHub Desktop.
WAVE file info reader (http://blog.ghedini.me/post/1611609904/c-fun-with-wave-files)
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
/* | |
* WAVE file info reader. | |
* | |
* Compile: | |
* $ cc -o wav_info wav_info.c | |
* | |
* Usage: | |
* $ ./wav_info <file> | |
* | |
* Examples: | |
* $ ./wav_info /path/to/file.wav | |
* | |
* Copyright (C) 2010 Alessandro Ghedini <alessandro@ghedini.me> | |
* -------------------------------------------------------------- | |
* "THE BEER-WARE LICENSE" (Revision 42): | |
* Alessandro Ghedini wrote this file. As long as you retain this | |
* notice you can do whatever you want with this stuff. If we | |
* meet some day, and you think this stuff is worth it, you can | |
* buy me a beer in return. | |
* -------------------------------------------------------------- | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
struct wav_header{ | |
char id[4]; /* must be "RIFF" */ | |
int size; /* file size - 8 */ | |
char fmt[8]; /* must be "WAVEfmt " */ | |
int format; | |
short pcm; /* 1 if PCM */ | |
short channels; | |
int frequency; | |
int bytes_per_second; | |
short bytes_by_capture; | |
short bits_per_sample; | |
char data[4]; | |
int bytes_in_data; | |
}; | |
int main(int argc, char **argv) { | |
FILE *fd; | |
struct wav_header header; | |
if (argc < 2) { | |
printf("Usage: %s <file>\n", argv[0]); | |
return -1; | |
} | |
fd = fopen(argv[1], "rb"); | |
fread(&header, sizeof(header), 1, fd); | |
if (strncmp(header.id, "RIFF", 4)) { | |
printf("File \"%s\" not valid WAV.\n", argv[1]); | |
return -1; | |
} | |
printf("format: %i bit\n", header.format); | |
printf("channels: %i ", header.channels); | |
if (header.channels == 1) | |
printf("(mono)\n"); | |
else if (header.channels == 2) | |
printf("(stereo)\n"); | |
printf("size: %i bytes\n", header.size); | |
printf("lenght: %i secs\n", header.bytes_in_data / header.bytes_per_second); | |
printf("frequency: %i Hz\n", header.frequency); | |
printf("bytes/second: %i\n", header.bytes_per_second); | |
printf("bytes by capture: %i\n", header.bytes_by_capture); | |
printf("bits/sample: %i\n", header.bits_per_sample); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment