299 lines
No EOL
17 KiB
HTML
299 lines
No EOL
17 KiB
HTML
<?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">// <![CDATA[
|
|
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">
|
|
|
|
|
|
<p><span class="sc"><abbr title="Too long; don't read">tl;dr</abbr>: </span> Playing to process a <code>wav</code> file. <code>C</code> was easier and cleaner than Ruby.</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<p>I had to calculate the sum of the absolute value of datas of a <code>.wav</code> file.
|
|
For efficiency (and fun) reasons, I had chosen <code>C</code> language.</p>
|
|
|
|
<p>It was a long time I didn’t used <code>C</code>.
|
|
From my memory it was a pain to read and write to files.
|
|
But I was really impressed by how clean the code is.
|
|
And it is even more impressive knowing I used mostly low level functions.</p>
|
|
|
|
<p>A <code>wav</code> file has an header containing many meta-datas.
|
|
This header was optimized to take the less space possible.
|
|
Therefore, header is thinked with Bytes.</p>
|
|
|
|
<ul>
|
|
<li>The 4th first Bytes must contains <code>RIFF</code> in ASCII,</li>
|
|
<li>the following 4th Bytes is an 32 bits integer giving the size of the file minus 8, etc…</li>
|
|
</ul>
|
|
|
|
<p>Surprisingly, I believe read this kind of file with a higher level language would have more difficult than in <code>C</code>.
|
|
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
|
|
{
|
|
<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;
|
|
};
|
|
</pre>
|
|
|
|
<p>If I had to read it in Ruby (for example), I believe I’d had to write a read entry for each bloc value.
|
|
But in <code>C</code> I simply written:</p>
|
|
|
|
<pre class="twilight">
|
|
<span class="SupportFunction">fread</span>(&header,<span class="Keyword">sizeof</span>(header),<span class="Constant">1</span>,wav);
|
|
</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">
|
|
<span class="Support">int16_t</span> value=<span class="Constant">0</span>;
|
|
<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>
|
|
<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="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;
|
|
};
|
|
|
|
<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>
|
|
<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 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>;
|
|
<span class="Support">int16_t</span> value=<span class="Constant">0</span>;
|
|
<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>
|
|
|
|
<p>I am curious to know if somebody know a nice way to do this with Ruby or Python.</p>
|
|
|
|
<p><em>edit: for compatibility reasons (64bit machines) used <code>int16_t</code> instead of <code>short</code> and <code>int32_t</code> instead of <code>int</code>.</em></p>
|
|
|
|
</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
|
|
Modified: 10/14/2010
|
|
</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> |