scratch/multi/blog/2010-10-14-Fun-with-wav.md

322 lines
11 KiB
Markdown
Raw Normal View History

2010-10-14 13:07:25 +00:00
-----
isHidden: false
menupriority: 1
kind: article
created_at: 2010-10-14T11:04:58+02:00
fr: title: S'amuser avec un .wav
en: title: Fun with wav
author_name: Yann Esposito
author_uri: yannesposito.com
# tags:
-----
begindiv(intro)
2010-10-15 13:08:51 +00:00
en: <%= tldr %> Played to process a `wav` file. `C` was easier and cleaner than Ruby.
2010-10-14 13:07:25 +00:00
fr: <%= tlal %> Je me suis amusé à lire un fichier `wav`. Le `C` fut le langage le mieux adapté à ce traitement. Bien meilleur que Ruby par exemple.
2010-10-15 13:08:51 +00:00
en: 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 _hack_.
fr: edit: Je voulais que ce programme fonctionne sur une machine spécifique. En aucun cas je ne pensais publier ce code pour une utilisation autre que celle-ci.
2010-10-14 13:07:25 +00:00
enddiv
2010-10-15 13:08:51 +00:00
en: I had to compute the sum of the absolute values of data of a `.wav` file.
2010-10-14 13:07:25 +00:00
en: For efficiency (and fun) reasons, I had chosen `C` language.
fr: J'ai eu besoin de calculer la somme des valeurs absolue des données d'un fichier `wav`.
fr: Pour des raison d'efficacité (et aussi de fun), j'ai fait le programme en `C`.
2010-10-15 13:08:51 +00:00
en: I didn't programmed in `C` for a long time.
2010-10-14 13:07:25 +00:00
en: From my memory it was a pain to read and write to files.
2010-10-15 13:08:51 +00:00
en: But in the end I was really impressed by the code I get.
en: It was really clean.
en: This is even more impressive knowing I used mostly low level functions.
2010-10-14 13:07:25 +00:00
fr: Celà faisait longtemps que je n'avais pas programmé en `C`.
fr: De mémoire il était peu aisé de manipuler des fichiers.
fr: Mais je dois concéder que j'ai été étonné de la clarté du code que j'ai obtenu.
2010-10-15 13:08:51 +00:00
en: A `wav` file has an header containing many metadata.
en: This header was optimized to take as few space as possible.
en: The header is then a block of packed bytes.
2010-10-14 13:07:25 +00:00
fr: Tout d'abord, un fichier `wav` se compose d'un entête qui contient pas mal de meta données.
fr: Cet entête a été optimisé pour prendre peu de place.
fr: Donc on discute de l'entête avec des nombres d'octets :
2010-10-15 13:08:51 +00:00
en: - The 4th first bytes must contains `RIFF` in ASCII,
2010-10-14 13:07:25 +00:00
en: - the following 4th Bytes is an 32 bits integer giving the size of the file minus 8, etc...
fr: - Les 4 premiers octets doivent contenir `RIFF` en ASCII ;
fr: - les 4 octects suivant correspondent à un entier codé sur 32 bits qui donne la taille du fichier moins 8 octets. etc..
2010-10-15 13:08:51 +00:00
en: Surprisingly, I believe that reading this kind of file is easier in `C` than in most higher level language.
2010-10-14 13:07:25 +00:00
en: Proof: I only have to search on the web the complete header format and write it in a struct.
fr: Etonnamment je pense que lire ce type de fichier avec un langage de haut niveau aurait été plus pénible qu'en C.
fr: La preuve, il m'a suffit de chercher sur le net le format complet de l'entête et de l'écrire dans un struct.
<code class="c">
struct wavfile
{
2010-10-14 22:31:08 +00:00
char id[4]; // should always contain "RIFF"
2010-10-15 13:08:51 +00:00
int totallength; // total file length minus 8
2010-10-14 22:31:08 +00:00
char wavefmt[8]; // should be "WAVEfmt "
2010-10-15 13:08:51 +00:00
int format; // 16 for PCM format
short pcm; // 1 for PCM format
short channels; // channels
int frequency; // sampling frequency
int bytes_per_second;
short bytes_by_capture;
short bits_per_sample;
2010-10-14 22:31:08 +00:00
char data[4]; // should always contain "data"
2010-10-15 13:08:51 +00:00
int bytes_in_data;
2010-10-14 13:07:25 +00:00
};
</code>
2010-10-15 13:08:51 +00:00
en: 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:07:25 +00:00
en: But in `C` I simply written:
fr: Si j'avais eu à faire ça en Ruby, je pense qu'il m'aurait fallu pour chaque bloc de l'entête écrire un bout de code de lecture du bon nombre d'octets.
fr: Alors qu'en `C` il m'a suffit d'écrire:
<code class="c">
2010-10-15 13:08:51 +00:00
fread(&header,sizeof(header),1,wav)
2010-10-14 13:07:25 +00:00
</code>
en: Only one step to fill my data structure. Magic!
fr: Et en une seule étape ma structure de donnée a été remplie avec les valeurs souhaitées. Magique !
en: Then, get an int value coded on two Bytes is also not a natural operation for high level language.
en: In `C`, to read a sequence of 2 Bytes numbers I only had to write:
fr: Ensuite, récupérer un entier à partir de deux octets n'est pas non plus une opération naturelle dans les nouveaux langages de programmation.
fr: Alors qu'en `C`. Pour récupérer un entier codé sur 16 bits il suffit d'écrire :
<code class="c">
2010-10-15 13:08:51 +00:00
short value=0;
2010-10-14 13:07:25 +00:00
while( fread(&value,sizeof(value),1,wav) ) {
// do something with value
}
</code>
en: Finally I ended with the following code. Remark I know the wav format (16 bit / 48000Hz):
fr: Finallement je suis arrivé au code suivant, sachant que le format de wav était connu, avec notamment échantillonage sur 16 bits en 48000Hz :
<code class="c" file="wavsum.c">
#include <stdio.h>
#include <stdlib.h>
2010-10-14 14:25:30 +00:00
#include <stdint.h>
2010-10-14 13:07:25 +00:00
struct wavfile
{
2010-10-14 22:31:08 +00:00
char id[4]; // should always contain "RIFF"
2010-10-15 13:08:51 +00:00
int totallength; // total file length minus 8
2010-10-14 22:31:08 +00:00
char wavefmt[8]; // should be "WAVEfmt "
2010-10-15 13:08:51 +00:00
int format; // 16 for PCM format
short pcm; // 1 for PCM format
short channels; // channels
int frequency; // sampling frequency
int bytes_per_second;
short bytes_by_capture;
short bits_per_sample;
2010-10-14 22:31:08 +00:00
char data[4]; // should always contain "data"
2010-10-15 13:08:51 +00:00
int bytes_in_data;
2010-10-14 13:07:25 +00:00
};
int main(int argc, char *argv[]) {
char *filename=argv[1];
FILE *wav = fopen(filename,"rb");
struct wavfile header;
if ( wav == NULL ) {
fprintf(stderr,"Can't open input file %s", filename);
exit(1);
}
// read header
2010-10-14 22:26:39 +00:00
if ( fread(&header,sizeof(header),1,wav) < 1 )
2010-10-14 13:07:25 +00:00
{
fprintf(stderr,"Can't read file header\n");
exit(1);
}
if ( header.id[0] != 'R'
|| header.id[1] != 'I'
|| header.id[2] != 'F'
|| header.id[3] != 'F' ) {
fprintf(stderr,"ERROR: Not wav format\n");
exit(1);
}
fprintf(stderr,"wav format\n");
// read data
long sum=0;
2010-10-15 13:08:51 +00:00
short value=0;
2010-10-14 13:07:25 +00:00
while( fread(&value,sizeof(value),1,wav) ) {
// fprintf(stderr,"%d\n", value);
if (value<0) { value=-value; }
sum += value;
}
printf("%ld\n",sum);
exit(0);
}
</code>
en: Of course it is only a hack.
en: But we can see how easy and clean it should be to improve.
en: As I say often: the right tool for your need instead of the same tool for all your needs.
en: Because here `C` is clearly far superior than Ruby to handle this simple tasks.
fr: Bien entendu ce code n'est qu'un _hack_.
fr: Mais on voit bien comment on peut facilement améliorer ce code, ajouter des cas possibles par exemple.
fr: Comme je dis souvent : le bon outil pour la bonne tâche.
fr: On voit en effet que pour cette tâche `C` est bien supérieur à Ruby par exemple.
2010-10-14 13:46:48 +00:00
en: I am curious to know if somebody know a nice way to do this with Ruby or Python.
2010-10-14 13:07:25 +00:00
2010-10-15 13:08:51 +00:00
en: _edit: for compatibility reasons (64bit machines) used `int16_t` instead of `short` and `int` instead of `int`._
fr: _màj : pour des raisons de compatibilité (machines 64 bits) j'ai utilisé `int16_t` au lieu de `short` et `int` au lieu de `int`.
2010-10-14 14:21:42 +00:00
2010-10-14 13:07:25 +00:00
fr: Je serai curieux de savoir s'il existe un manière plus propre en Ruby que je ne connais pas.
fr: Certainement qu'en Python ça doit être la cas.
2010-10-15 13:08:51 +00:00
begindiv(intro)
en: edit (2): after most consideration about portability I made an _hopefully_ more portable version.
en: But I must confess this task was a bit tedious.
en: The code remain as readable as before.
en: But I had to use some compiler specific declaration to force the structure to be packed:
fr: màj (2) : après toutes les remarques concernant la portabilité.
fr: J'ai fait une nouvelle version qui devrait être plus portable.
fr: Elle fait aussi plus de test pour vérifier le fichier.
fr: Cependant j'utilise une assertion spécifique à `gcc` pour être certain que la structure de donnée n'ai pas de "trou" :
<code class="c">
__attribute__((__packed__))
</code>
2010-10-15 13:24:31 +00:00
en: Therefore this implementation should for big and little endian architecture.
en: However, it must be compiled with `gcc`.
2010-10-15 13:08:51 +00:00
en: The new code make more tests but still don't use `mmap`.
en: Here it is:
fr: Le nouveau code n'utilise pas mmap et devrait être plus compatible.
fr: Voici le dernier résultat :
enddiv
<code class="c" file="wavsum.c">
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // for memcmp
#include <stdint.h> // for int16_t and int32_t
struct wavfile
{
char id[4]; // should always contain "RIFF"
int32_t totallength; // total file length minus 8
char wavefmt[8]; // should be "WAVEfmt "
int32_t format; // 16 for PCM format
int16_t pcm; // 1 for PCM format
int16_t channels; // channels
int32_t frequency; // sampling frequency
int32_t bytes_per_second;
int16_t bytes_by_capture;
int16_t bits_per_sample;
char data[4]; // should always contain "data"
int32_t bytes_in_data;
} __attribute__((__packed__));
int is_big_endian(void) {
union {
uint32_t i;
char c[4];
} bint = {0x01000000};
return bint.c[0]==1;
}
int main(int argc, char *argv[]) {
char *filename=argv[1];
FILE *wav = fopen(filename,"rb");
struct wavfile header;
if ( wav == NULL ) {
fprintf(stderr,"Can't open input file %s\n", filename);
exit(1);
}
// read header
if ( fread(&header,sizeof(header),1,wav) < 1 ) {
fprintf(stderr,"Can't read input file header %s\n", filename);
exit(1);
}
// if wav file isn't the same endianness than the current environment
// we quit
if ( is_big_endian() ) {
if ( memcmp( header.id,"RIFX", 4) != 0 ) {
fprintf(stderr,"ERROR: %s is not a big endian wav file\n", filename);
exit(1);
}
} else {
if ( memcmp( header.id,"RIFF", 4) != 0 ) {
fprintf(stderr,"ERROR: %s is not a little endian wav file\n", filename);
exit(1);
}
}
if ( memcmp( header.wavefmt, "WAVEfmt ", 8) != 0
|| memcmp( header.data, "data", 4) != 0
) {
fprintf(stderr,"ERROR: Not wav format\n");
exit(1);
}
if (header.format != 16) {
fprintf(stderr,"\nERROR: not 16 bit wav format.");
exit(1);
}
fprintf(stderr,"format: %d bits", header.format);
if (header.format == 16) {
fprintf(stderr,", PCM");
} else {
fprintf(stderr,", not PCM (%d)", header.format);
}
if (header.pcm == 1) {
fprintf(stderr, " uncompressed" );
} else {
fprintf(stderr, " compressed" );
}
fprintf(stderr,", channel %d", header.pcm);
fprintf(stderr,", freq %d", header.frequency );
fprintf(stderr,", %d bytes per sec", header.bytes_per_second );
fprintf(stderr,", %d bytes by capture", header.bytes_by_capture );
fprintf(stderr,", %d bits per sample", header.bytes_by_capture );
fprintf(stderr,"\n" );
if ( memcmp( header.data, "data", 4) != 0 ) {
fprintf(stderr,"ERROR: Prrroblem?\n");
exit(1);
}
fprintf(stderr,"wav format\n");
// read data
long long sum=0;
int16_t value;
int i=0;
fprintf(stderr,"---\n", value);
while( fread(&value,sizeof(value),1,wav) ) {
if (value<0) { value=-value; }
sum += value;
}
printf("%lld\n",sum);
exit(0);
}
</code>