2010-10-14 13:30:15 +00:00
<?xml version="1.0" encoding="utf-8"?>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" lang = "fr" xml:lang = "fr" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" / >
< link rel = "shortcut icon" type = "image/x-icon" href = "/Scratch/img/favicon.ico" / >
< link rel = "stylesheet" type = "text/css" href = "/Scratch/assets/css/main.css" / >
< link rel = "stylesheet" type = "text/css" href = "/Scratch/css/twilight.css" / >
< link rel = "stylesheet" type = "text/css" href = "/Scratch/css/idc.css" / >
< link rel = "alternate" type = "application/rss+xml" title = "RSS" href = "http://feeds.feedburner.com/yannespositocomen" / >
< link rel = "alternate" lang = "fr" xml:lang = "fr" title = "S'amuser avec un .wav" type = "text/html" hreflang = "fr" href = "/Scratch/fr/blog/2010-10-14-Fun-with-wav/" / >
< link rel = "alternate" lang = "en" xml:lang = "en" title = "Fun with wav" type = "text/html" hreflang = "en" href = "/Scratch/en/blog/2010-10-14-Fun-with-wav/" / >
< script type = "text/javascript" src = "/Scratch/js/jquery-1.3.1.min.js" > < / script >
< script type = "text/javascript" src = "/Scratch/js/jquery.cookie.js" > < / script >
< script type = "text/javascript" src = "/Scratch/js/index.js" > < / script >
< title > Fun with wav< / title >
< / head >
< body lang = "en" >
< script type = "text/javascript" > / / < ! [ C D A T A [
document.write('< div id = "blackpage" > < img src = "/Scratch/img/loading.gif" alt = "loading..." / > < / div > ');
// ]]>
< / script >
< div id = "content" >
< div id = "choix" >
< div class = "return" > < a href = "#entete" > ↓ Menu ↓ < / a > < / div >
< div id = "choixlang" >
< a href = "/Scratch/fr/blog/2010-10-14-Fun-with-wav/" onclick = "setLanguage('fr')" > en Français< / a >
< / div >
< / div >
< img src = "/Scratch/img/presentation.png" alt = "Presentation drawing" / >
< div id = "titre" >
< h1 >
Fun with wav
< / h1 >
< / div >
< div class = "flush" > < / div >
< div class = "flush" > < / div >
< div id = "afterheader" >
< div class = "corps" >
< div class = "intro" >
2010-10-15 13:11:51 +00:00
< p > < span class = "sc" > < abbr title = "Too long; don't read" > tl;dr< / abbr > : < / span > Played to process a < code > wav< / code > file. < code > C< / code > was easier and cleaner than Ruby.< / p >
< p > edit: I wanted this program to work only on one specific machine (a x86 on a 32 bit Ubuntu). Therefore I didn’ t had any portability consideration. This is only a < em > hack< / em > .< / p >
2010-10-14 13:30:15 +00:00
< / div >
2010-10-15 13:11:51 +00:00
< p > I had to compute the sum of the absolute values of data of a < code > .wav< / code > file.
2010-10-14 13:30:15 +00:00
For efficiency (and fun) reasons, I had chosen < code > C< / code > language.< / p >
2010-10-15 13:11:51 +00:00
< p > I didn’ t programmed in < code > C< / code > for a long time.
2010-10-14 13:30:15 +00:00
From my memory it was a pain to read and write to files.
2010-10-15 13:11:51 +00:00
But in the end I was really impressed by the code I get.
It was really clean.
This is even more impressive knowing I used mostly low level functions.< / p >
2010-10-14 13:30:15 +00:00
2010-10-15 13:11:51 +00:00
< p > A < code > wav< / code > file has an header containing many metadata.
This header was optimized to take as few space as possible.
The header is then a block of packed bytes.< / p >
2010-10-14 13:30:15 +00:00
< ul >
2010-10-15 13:11:51 +00:00
< li > The 4th first bytes must contains < code > RIFF< / code > in ASCII,< / li >
2010-10-14 13:30:15 +00:00
< li > the following 4th Bytes is an 32 bits integer giving the size of the file minus 8, etc… < / li >
< / ul >
2010-10-15 13:11:51 +00:00
< p > Surprisingly, I believe that reading this kind of file is easier in < code > C< / code > than in most higher level language.
2010-10-14 13:30:15 +00:00
Proof: I only have to search on the web the complete header format and write it in a struct.< / p >
< pre class = "twilight" >
< span class = "Storage" > struct< / span > wavfile
{
2010-10-14 22:33:07 +00:00
< span class = "Storage" > char< / span > id[< span class = "Constant" > 4< / span > ]; < span class = "Comment" > < span class = "Comment" > //< / span > should always contain " RIFF" < / span >
2010-10-15 13:11:51 +00:00
< span class = "Storage" > int< / span > totallength; < span class = "Comment" > < span class = "Comment" > //< / span > total file length minus 8< / span >
2010-10-14 22:33:07 +00:00
< span class = "Storage" > char< / span > wavefmt[< span class = "Constant" > 8< / span > ]; < span class = "Comment" > < span class = "Comment" > //< / span > should be " WAVEfmt " < / span >
2010-10-15 13:11:51 +00:00
< span class = "Storage" > int< / span > format; < span class = "Comment" > < span class = "Comment" > //< / span > 16 for PCM format< / span >
< span class = "Storage" > short< / span > pcm; < span class = "Comment" > < span class = "Comment" > //< / span > 1 for PCM format< / span >
< span class = "Storage" > short< / span > channels; < span class = "Comment" > < span class = "Comment" > //< / span > channels< / span >
< span class = "Storage" > int< / span > frequency; < span class = "Comment" > < span class = "Comment" > //< / span > sampling frequency< / span >
< span class = "Storage" > int< / span > bytes_per_second;
< span class = "Storage" > short< / span > bytes_by_capture;
< span class = "Storage" > short< / span > bits_per_sample;
2010-10-14 22:33:07 +00:00
< span class = "Storage" > char< / span > data[< span class = "Constant" > 4< / span > ]; < span class = "Comment" > < span class = "Comment" > //< / span > should always contain " data" < / span >
2010-10-15 13:11:51 +00:00
< span class = "Storage" > int< / span > bytes_in_data;
2010-10-14 13:30:15 +00:00
};
< / pre >
2010-10-15 13:11:51 +00:00
< p > To read this kind of data in Ruby, I certainly had to write a block of code for each element in the struct.
2010-10-14 13:30:15 +00:00
But in < code > C< / code > I simply written:< / p >
< pre class = "twilight" >
2010-10-15 13:11:51 +00:00
< span class = "SupportFunction" > fread< / span > (& header,< span class = "Keyword" > sizeof< / span > (header),< span class = "Constant" > 1< / span > ,wav)
2010-10-14 13:30:15 +00:00
< / pre >
< p > Only one step to fill my data structure. Magic!< / p >
< p > Then, get an int value coded on two Bytes is also not a natural operation for high level language.
In < code > C< / code > , to read a sequence of 2 Bytes numbers I only had to write:< / p >
< pre class = "twilight" >
2010-10-15 13:11:51 +00:00
< span class = "Storage" > short< / span > value=< span class = "Constant" > 0< / span > ;
2010-10-14 13:30:15 +00:00
< span class = "Keyword" > while< / span > ( < span class = "SupportFunction" > fread< / span > (& value,< span class = "Keyword" > sizeof< / span > (value),< span class = "Constant" > 1< / span > ,wav) ) {
< span class = "Comment" > < span class = "Comment" > //< / span > do something with value< / span >
}
< / pre >
< p > Finally I ended with the following code. Remark I know the wav format (16 bit / 48000Hz):< / p >
< div class = "code" > < div class = "file" > < a href = "/Scratch/en/blog/2010-10-14-Fun-with-wav/code/wavsum.c" > ➥ wavsum.c < / a > < / div > < div class = "withfile" >
< pre class = "twilight" >
< span class = "CCCPreprocessorLine" > #< span class = "CCCPreprocessorDirective" > include< / span > < span class = "String" > < span class = "String" > < < / span > stdio.h< span class = "String" > > < / span > < / span > < / span >
< span class = "CCCPreprocessorLine" > #< span class = "CCCPreprocessorDirective" > include< / span > < span class = "String" > < span class = "String" > < < / span > stdlib.h< span class = "String" > > < / span > < / span > < / span >
2010-10-14 14:30:48 +00:00
< span class = "CCCPreprocessorLine" > #< span class = "CCCPreprocessorDirective" > include< / span > < span class = "String" > < span class = "String" > < < / span > stdint.h< span class = "String" > > < / span > < / span > < / span >
2010-10-14 13:30:15 +00:00
< span class = "Storage" > struct< / span > wavfile
{
2010-10-14 22:33:07 +00:00
< span class = "Storage" > char< / span > id[< span class = "Constant" > 4< / span > ]; < span class = "Comment" > < span class = "Comment" > //< / span > should always contain " RIFF" < / span >
2010-10-15 13:11:51 +00:00
< span class = "Storage" > int< / span > totallength; < span class = "Comment" > < span class = "Comment" > //< / span > total file length minus 8< / span >
2010-10-14 22:33:07 +00:00
< span class = "Storage" > char< / span > wavefmt[< span class = "Constant" > 8< / span > ]; < span class = "Comment" > < span class = "Comment" > //< / span > should be " WAVEfmt " < / span >
2010-10-15 13:11:51 +00:00
< span class = "Storage" > int< / span > format; < span class = "Comment" > < span class = "Comment" > //< / span > 16 for PCM format< / span >
< span class = "Storage" > short< / span > pcm; < span class = "Comment" > < span class = "Comment" > //< / span > 1 for PCM format< / span >
< span class = "Storage" > short< / span > channels; < span class = "Comment" > < span class = "Comment" > //< / span > channels< / span >
< span class = "Storage" > int< / span > frequency; < span class = "Comment" > < span class = "Comment" > //< / span > sampling frequency< / span >
< span class = "Storage" > int< / span > bytes_per_second;
< span class = "Storage" > short< / span > bytes_by_capture;
< span class = "Storage" > short< / span > bits_per_sample;
2010-10-14 22:33:07 +00:00
< span class = "Storage" > char< / span > data[< span class = "Constant" > 4< / span > ]; < span class = "Comment" > < span class = "Comment" > //< / span > should always contain " data" < / span >
2010-10-15 13:11:51 +00:00
< span class = "Storage" > int< / span > bytes_in_data;
2010-10-14 13:30:15 +00:00
};
< span class = "Storage" > int< / span > < span class = "Entity" > ma< span class = "Entity" > in< / span > < / span > (< span class = "Storage" > int< / span > argc, < span class = "Storage" > char< / span > *argv[]) {
< span class = "Storage" > char< / span > *filename=argv[< span class = "Constant" > 1< / span > ];
FILE *wav = < span class = "SupportFunction" > fopen< / span > (filename,< span class = "String" > < span class = "String" > " < / span > rb< span class = "String" > " < / span > < / span > );
< span class = "Storage" > struct< / span > wavfile header;
< span class = "Keyword" > if< / span > ( wav == < span class = "Constant" > NULL< / span > ) {
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > Can't open input file < span class = "StringConstant" > %s< / span > < span class = "String" > " < / span > < / span > , filename);
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 1< / span > );
}
< span class = "Comment" > < span class = "Comment" > //< / span > read header< / span >
2010-10-14 22:26:39 +00:00
< span class = "Keyword" > if< / span > ( < span class = "SupportFunction" > fread< / span > (& header,< span class = "Keyword" > sizeof< / span > (header),< span class = "Constant" > 1< / span > ,wav) < < span class = "Constant" > 1< / span > )
2010-10-14 13:30:15 +00:00
{
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > Can't read file header< span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > );
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 1< / span > );
}
< span class = "Keyword" > if< / span > ( header.id[< span class = "Constant" > 0< / span > ] != < span class = "String" > < span class = "String" > '< / span > R< span class = "String" > '< / span > < / span >
|| header.id[< span class = "Constant" > 1< / span > ] != < span class = "String" > < span class = "String" > '< / span > I< span class = "String" > '< / span > < / span >
|| header.id[< span class = "Constant" > 2< / span > ] != < span class = "String" > < span class = "String" > '< / span > F< span class = "String" > '< / span > < / span >
|| header.id[< span class = "Constant" > 3< / span > ] != < span class = "String" > < span class = "String" > '< / span > F< span class = "String" > '< / span > < / span > ) {
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > ERROR: Not wav format< span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > );
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 1< / span > );
}
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > wav format< span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > );
< span class = "Comment" > < span class = "Comment" > //< / span > read data< / span >
< span class = "Storage" > long< / span > sum=< span class = "Constant" > 0< / span > ;
2010-10-15 13:11:51 +00:00
< span class = "Storage" > short< / span > value=< span class = "Constant" > 0< / span > ;
2010-10-14 13:30:15 +00:00
< span class = "Keyword" > while< / span > ( < span class = "SupportFunction" > fread< / span > (& value,< span class = "Keyword" > sizeof< / span > (value),< span class = "Constant" > 1< / span > ,wav) ) {
< span class = "Comment" > < span class = "Comment" > //< / span > fprintf(stderr," %d\n" , value);< / span >
< span class = "Keyword" > if< / span > (value< < span class = "Constant" > 0< / span > ) { value=-value; }
sum += value;
}
< span class = "SupportFunction" > printf< / span > (< span class = "String" > < span class = "String" > " < / span > < span class = "StringConstant" > %ld< / span > < span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > ,sum);
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 0< / span > );
}
< / pre >
< / div > < / div >
< p > Of course it is only a hack.
But we can see how easy and clean it should be to improve.
As I say often: the right tool for your need instead of the same tool for all your needs.
Because here < code > C< / code > is clearly far superior than Ruby to handle this simple tasks.< / p >
2010-10-14 13:46:48 +00:00
< p > I am curious to know if somebody know a nice way to do this with Ruby or Python.< / p >
2010-10-14 13:30:15 +00:00
2010-10-15 13:11:51 +00:00
< p > < em > edit: for compatibility reasons (64bit machines) used < code > int16_t< / code > instead of < code > short< / code > and < code > int< / code > instead of < code > int< / code > .< / em > < / p >
< div class = "intro" >
< p > edit (2): after most consideration about portability I made an < em > hopefully< / em > more portable version.
But I must confess this task was a bit tedious.
The code remain as readable as before.
But I had to use some compiler specific declaration to force the structure to be packed:< / p >
< pre class = "twilight" >
__attribute__((__packed__))
< / pre >
2010-10-15 13:25:46 +00:00
< p > Therefore this implementation should for big and little endian architecture.
However, it must be compiled with < code > gcc< / code > .
2010-10-15 13:11:51 +00:00
The new code make more tests but still don’ t use < code > mmap< / code > .
Here it is:< / p >
< / div >
< div class = "code" > < div class = "file" > < a href = "/Scratch/en/blog/2010-10-14-Fun-with-wav/code/wavsum.c" > ➥ wavsum.c < / a > < / div > < div class = "withfile" >
< pre class = "twilight" >
< span class = "CCCPreprocessorLine" > #< span class = "CCCPreprocessorDirective" > include< / span > < span class = "String" > < span class = "String" > < < / span > stdio.h< span class = "String" > > < / span > < / span > < / span >
< span class = "CCCPreprocessorLine" > #< span class = "CCCPreprocessorDirective" > include< / span > < span class = "String" > < span class = "String" > < < / span > stdlib.h< span class = "String" > > < / span > < / span > < / span >
< span class = "CCCPreprocessorLine" > #< span class = "CCCPreprocessorDirective" > include< / span > < span class = "String" > < span class = "String" > < < / span > string.h< span class = "String" > > < / span > < / span > < / span > < span class = "Comment" > < span class = "Comment" > //< / span > for memcmp< / span >
< span class = "CCCPreprocessorLine" > #< span class = "CCCPreprocessorDirective" > include< / span > < span class = "String" > < span class = "String" > < < / span > stdint.h< span class = "String" > > < / span > < / span > < / span > < span class = "Comment" > < span class = "Comment" > //< / span > for int16_t and int32_t< / span >
< span class = "Storage" > struct< / span > wavfile
{
< span class = "Storage" > char< / span > id[< span class = "Constant" > 4< / span > ]; < span class = "Comment" > < span class = "Comment" > //< / span > should always contain " RIFF" < / span >
< span class = "Support" > int32_t< / span > totallength; < span class = "Comment" > < span class = "Comment" > //< / span > total file length minus 8< / span >
< span class = "Storage" > char< / span > wavefmt[< span class = "Constant" > 8< / span > ]; < span class = "Comment" > < span class = "Comment" > //< / span > should be " WAVEfmt " < / span >
< span class = "Support" > int32_t< / span > format; < span class = "Comment" > < span class = "Comment" > //< / span > 16 for PCM format< / span >
< span class = "Support" > int16_t< / span > pcm; < span class = "Comment" > < span class = "Comment" > //< / span > 1 for PCM format< / span >
< span class = "Support" > int16_t< / span > channels; < span class = "Comment" > < span class = "Comment" > //< / span > channels< / span >
< span class = "Support" > int32_t< / span > frequency; < span class = "Comment" > < span class = "Comment" > //< / span > sampling frequency< / span >
< span class = "Support" > int32_t< / span > bytes_per_second;
< span class = "Support" > int16_t< / span > bytes_by_capture;
< span class = "Support" > int16_t< / span > bits_per_sample;
< span class = "Storage" > char< / span > data[< span class = "Constant" > 4< / span > ]; < span class = "Comment" > < span class = "Comment" > //< / span > should always contain " data" < / span >
< span class = "Support" > int32_t< / span > bytes_in_data;
} __attribute__((__packed__));
< span class = "Storage" > int< / span > is_big_endian(< span class = "Storage" > void< / span > ) {
< span class = "Storage" > union< / span > {
< span class = "Support" > uint32_t< / span > i;
< span class = "Storage" > char< / span > c[< span class = "Constant" > 4< / span > ];
} bint = {< span class = "Constant" > 0x01000000< / span > };
< span class = "Keyword" > return< / span > bint.c[< span class = "Constant" > 0< / span > ]==< span class = "Constant" > 1< / span > ;
}
< span class = "Storage" > int< / span > < span class = "Entity" > ma< span class = "Entity" > in< / span > < / span > (< span class = "Storage" > int< / span > argc, < span class = "Storage" > char< / span > *argv[]) {
< span class = "Storage" > char< / span > *filename=argv[< span class = "Constant" > 1< / span > ];
FILE *wav = < span class = "SupportFunction" > fopen< / span > (filename,< span class = "String" > < span class = "String" > " < / span > rb< span class = "String" > " < / span > < / span > );
< span class = "Storage" > struct< / span > wavfile header;
< span class = "Keyword" > if< / span > ( wav == < span class = "Constant" > NULL< / span > ) {
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > Can't open input file < span class = "StringConstant" > %s< / span > < span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > , filename);
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 1< / span > );
}
< span class = "Comment" > < span class = "Comment" > //< / span > read header< / span >
< span class = "Keyword" > if< / span > ( < span class = "SupportFunction" > fread< / span > (& header,< span class = "Keyword" > sizeof< / span > (header),< span class = "Constant" > 1< / span > ,wav) < < span class = "Constant" > 1< / span > ) {
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > Can't read input file header < span class = "StringConstant" > %s< / span > < span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > , filename);
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 1< / span > );
}
< span class = "Comment" > < span class = "Comment" > //< / span > if wav file isn't the same endianness than the current environment< / span >
< span class = "Comment" > < span class = "Comment" > //< / span > we quit< / span >
< span class = "Keyword" > if< / span > ( is_big_endian() ) {
< span class = "Keyword" > if< / span > ( < span class = "SupportFunction" > memcmp< / span > ( header.id,< span class = "String" > < span class = "String" > " < / span > RIFX< span class = "String" > " < / span > < / span > , < span class = "Constant" > 4< / span > ) != < span class = "Constant" > 0< / span > ) {
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > ERROR: < span class = "StringConstant" > %s< / span > is not a big endian wav file< span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > , filename);
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 1< / span > );
}
} < span class = "Keyword" > else< / span > {
< span class = "Keyword" > if< / span > ( < span class = "SupportFunction" > memcmp< / span > ( header.id,< span class = "String" > < span class = "String" > " < / span > RIFF< span class = "String" > " < / span > < / span > , < span class = "Constant" > 4< / span > ) != < span class = "Constant" > 0< / span > ) {
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > ERROR: < span class = "StringConstant" > %s< / span > is not a little endian wav file< span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > , filename);
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 1< / span > );
}
}
< span class = "Keyword" > if< / span > ( < span class = "SupportFunction" > memcmp< / span > ( header.wavefmt, < span class = "String" > < span class = "String" > " < / span > WAVEfmt < span class = "String" > " < / span > < / span > , < span class = "Constant" > 8< / span > ) != < span class = "Constant" > 0< / span >
|| < span class = "SupportFunction" > memcmp< / span > ( header.data, < span class = "String" > < span class = "String" > " < / span > data< span class = "String" > " < / span > < / span > , < span class = "Constant" > 4< / span > ) != < span class = "Constant" > 0< / span >
) {
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > ERROR: Not wav format< span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > );
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 1< / span > );
}
< span class = "Keyword" > if< / span > (header.format != < span class = "Constant" > 16< / span > ) {
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > < span class = "StringConstant" > \n< / span > ERROR: not 16 bit wav format.< span class = "String" > " < / span > < / span > );
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 1< / span > );
}
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > format: < span class = "StringConstant" > %d< / span > bits< span class = "String" > " < / span > < / span > , header.format);
< span class = "Keyword" > if< / span > (header.format == < span class = "Constant" > 16< / span > ) {
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > , PCM< span class = "String" > " < / span > < / span > );
} < span class = "Keyword" > else< / span > {
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > , not PCM (< span class = "StringConstant" > %d< / span > )< span class = "String" > " < / span > < / span > , header.format);
}
< span class = "Keyword" > if< / span > (header.pcm == < span class = "Constant" > 1< / span > ) {
< span class = "SupportFunction" > fprintf< / span > (stderr, < span class = "String" > < span class = "String" > " < / span > uncompressed< span class = "String" > " < / span > < / span > );
} < span class = "Keyword" > else< / span > {
< span class = "SupportFunction" > fprintf< / span > (stderr, < span class = "String" > < span class = "String" > " < / span > compressed< span class = "String" > " < / span > < / span > );
}
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > , channel < span class = "StringConstant" > %d< / span > < span class = "String" > " < / span > < / span > , header.pcm);
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > , freq < span class = "StringConstant" > %d< / span > < span class = "String" > " < / span > < / span > , header.frequency );
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > , < span class = "StringConstant" > %d< / span > bytes per sec< span class = "String" > " < / span > < / span > , header.bytes_per_second );
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > , < span class = "StringConstant" > %d< / span > bytes by capture< span class = "String" > " < / span > < / span > , header.bytes_by_capture );
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > , < span class = "StringConstant" > %d< / span > bits per sample< span class = "String" > " < / span > < / span > , header.bytes_by_capture );
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > < span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > );
< span class = "Keyword" > if< / span > ( < span class = "SupportFunction" > memcmp< / span > ( header.data, < span class = "String" > < span class = "String" > " < / span > data< span class = "String" > " < / span > < / span > , < span class = "Constant" > 4< / span > ) != < span class = "Constant" > 0< / span > ) {
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > ERROR: Prrroblem?< span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > );
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 1< / span > );
}
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > wav format< span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > );
< span class = "Comment" > < span class = "Comment" > //< / span > read data< / span >
< span class = "Storage" > long< / span > < span class = "Storage" > long< / span > sum=< span class = "Constant" > 0< / span > ;
< span class = "Support" > int16_t< / span > value;
< span class = "Storage" > int< / span > i=< span class = "Constant" > 0< / span > ;
< span class = "SupportFunction" > fprintf< / span > (stderr,< span class = "String" > < span class = "String" > " < / span > ---< span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > , value);
< span class = "Keyword" > while< / span > ( < span class = "SupportFunction" > fread< / span > (& value,< span class = "Keyword" > sizeof< / span > (value),< span class = "Constant" > 1< / span > ,wav) ) {
< span class = "Keyword" > if< / span > (value< < span class = "Constant" > 0< / span > ) { value=-value; }
sum += value;
}
< span class = "SupportFunction" > printf< / span > (< span class = "String" > < span class = "String" > " < / span > < span class = "StringConstant" > %lld< / span > < span class = "StringConstant" > \n< / span > < span class = "String" > " < / span > < / span > ,sum);
< span class = "SupportFunction" > exit< / span > (< span class = "Constant" > 0< / span > );
}
< / pre >
< / div > < / div >
2010-10-14 14:23:07 +00:00
2010-10-14 13:30:15 +00:00
< / div >
< div id = "choixrss" >
< a id = "rss" href = "http://feeds.feedburner.com/yannespositocomen" >
Subscribe
< / a >
< / div >
< script type = "text/javascript" >
$(document).ready(function(){
$('#comment').hide();
$('#clickcomment').click(showComments);
});
function showComments() {
$('#comment').show();
$('#clickcomment').fadeOut();
}
document.write('< div id = "clickcomment" > Comments< / div > ');
< / script >
< div class = "flush" > < / div >
< div class = "corps" id = "comment" >
< h2 class = "first" > comments< / h2 >
< noscript >
Vous devez activer javascript pour commenter.
< / noscript >
< script type = "text/javascript" >
var idcomments_acct = 'a307f0044511ff1b5cfca573fc0a52e7';
var idcomments_post_id = '/Scratch/en/blog/2010-10-14-Fun-with-wav/';
var idcomments_post_url = 'http://yannesposito.com/Scratch/en/blog/2010-10-14-Fun-with-wav/';
< / script >
< span id = "IDCommentsPostTitle" style = "display:none" > < / span >
< script type = 'text/javascript' src = '/Scratch/js/genericCommentWrapperV2.js' > < / script >
< / div >
< div id = "entete" class = "corps_spaced" >
< div id = "liens" >
< ul > < li > < a href = "/Scratch/en/" > Homepage< / a > < / li >
< li > < a href = "/Scratch/en/blog/" > Blog< / a > < / li >
< li > < a href = "/Scratch/en/softwares/" > Softwares< / a > < / li >
< li > < a href = "/Scratch/en/about/" > About< / a > < / li > < / ul >
< / div >
< div class = "flush" > < / div >
< hr / >
< div id = "next_before_articles" >
< div id = "previous_articles" >
previous entries
< div class = "previous_article" >
< a href = "/Scratch/en/blog/2010-10-10-Secure-eMail-on-Mac-in-few-steps/" > < span class = "nicer" > «< / span > Secure eMail on Mac in few steps< / a >
< / div >
< div class = "previous_article" >
< a href = "/Scratch/en/blog/2010-10-06-New-Blog-Design-Constraints/" > < span class = "nicer" > «< / span > New Blog Design Constraints< / a >
< / div >
< div class = "previous_article" >
< a href = "/Scratch/en/blog/2010-09-02-Use-git-to-calculate-trusted-mtimes/" > < span class = "nicer" > «< / span > Use git to calculate trusted mtimes< / a >
< / div >
< / div >
< div id = "next_articles" >
next entries
< / div >
< div class = "flush" > < / div >
< / div >
< / div >
< div id = "bottom" >
< div >
< a rel = "license" href = "http://creativecommons.org/licenses/by-sa/3.0/" > Copyright ©, Yann Esposito< / a >
< / div >
< div id = "lastmod" >
Created: 10/14/2010
2010-10-14 22:33:07 +00:00
Modified: 10/15/2010
2010-10-14 13:30:15 +00:00
< / div >
< div >
Entirely done with
< a href = "http://www.vim.org" > Vim< / a >
and
< a href = "http://nanoc.stoneship.org" > nanoc< / a >
< / div >
< div >
< a href = "/Scratch/en/validation/" > Validation< / a >
< a href = "http://validator.w3.org/check?uri=referer" > [xhtml] < / a >
.
< a href = "http://jigsaw.w3.org/css-validator/check/referer?profile=css3" > [css] < / a >
.
< a href = "http://validator.w3.org/feed/check.cgi?url=http%3A//yannesposito.com/Scratch/en/blog/feed/feed.xml" > [rss]< / a >
< / div >
< / div >
< div class = "clear" > < / div >
< / div >
< / body >
< / html >