IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Dotnet Discussion :

Decoder fichier wave et FFT


Sujet :

Dotnet

  1. #1
    Membre régulier
    Homme Profil pro
    Inscrit en
    Juillet 2008
    Messages
    121
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations forums :
    Inscription : Juillet 2008
    Messages : 121
    Points : 84
    Points
    84
    Par défaut Decoder fichier wave et FFT
    Bonjour,
    Je suis un peu perdu sur une données que les professeur nous as donnée.

    Il nous demandes
    Vous devez analyser le fichier WAV à votre nom afin d’en caractériser le contenu (fréquences représentées et leurs amplitudes).
    Vous utiliserez une FFT en C#. Vous en trouverez facilement des versions sur Internet.
    Vous devrez aussi décoder le contenu de votre fichier WAV. Là aussi, vous trouverez la structure d’un fichier WAV sur Internet.
    J'ai donc trouvé sur Wikipedia, la structure d'entête du fichier WAV.
    http://fr.wikipedia.org/wiki/WAVEform_audio_format.

    J'arrive a lire l'entête jusqu'au moment ou j'arrive au DATAS[] :, voici mon code

    Class InformationWav
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    using System;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
    using System.Resources;
    using System.Globalization;
     
     
    namespace TP4MBB
    {
        class InformationWAV
        {
            wavFile wav;
            GestionWAV gestion;
            bool estValide = false;
     
            public InformationWAV(string gsChemin)
            {
             try
                {
                    gestion = new GestionWAV(gsChemin);
                    StreamReader laReader = new StreamReader(gsChemin, Encoding.Default);
                    wav = new wavFile();
     
                    char[] EnteteChar = new char[44];
                    laReader.Read(EnteteChar, 0, 44);
                    String entete = new String(EnteteChar);            
     
                    /*
                     * On lit nos valeur présente dans EnteteChar
                     */
                    wav.FileType = entete.Substring(0, 4).ToCharArray();
                    entete = entete.Remove(0, 4);
     
                    /* 
                     * Lecture de la taille et conversion en valeur numérique
                     */
                    string Size = entete.Substring(0, 4);
                    entete = entete.Remove(0, 4);
                    Size = ConvertStringToHex(Size);
                    Size = gestion.ConvertOffsetToStr(Size);
                    wav.FileSize = uint.Parse(Size, System.Globalization.NumberStyles.HexNumber);
     
                    /* Suite lecture */
                    wav.FileFormat = entete.Substring(0, 4).ToCharArray();
                    entete = entete.Remove(0, 4);
                    if (new String(wav.FileFormat).EndsWith("WAVE"))
                    {
                        estValide = true;
                    }
     
                    wav.FormatBloc = entete.Substring(0, 4).ToCharArray();
                    entete = entete.Remove(0, 4);
     
                    /* Conversion du BlocSize */
                    Size = entete.Substring(0, 4);
                    entete = entete.Remove(0, 4);
                    Size = ConvertStringToHex(Size);
                    Size = gestion.ConvertOffsetToStr(Size);
                    wav.BlocSize = uint.Parse(Size, System.Globalization.NumberStyles.HexNumber);
     
                    /* Conversion du Format */
                    Size = entete.Substring(0, 2);
                    entete = entete.Remove(0, 2);
                    Size = ConvertStringToHex(Size);
                    Size = gestion.ConvertOffsetToStr(Size);
                    wav.Format = ushort.Parse(Size, System.Globalization.NumberStyles.HexNumber);
     
                    /* Conversion du NbrCanaux */
                    Size = entete.Substring(0, 2);
                    entete = entete.Remove(0, 2);
                    Size = ConvertStringToHex(Size);
                    Size = gestion.ConvertOffsetToStr(Size);
                    wav.NbrCanaux = ushort.Parse(Size, System.Globalization.NumberStyles.HexNumber);
     
                    /* Conversion de la Frequence */
                    Size = entete.Substring(0, 4);
                    entete = entete.Remove(0, 4);
                    Size = ConvertStringToHex(Size);
                    Size = gestion.ConvertOffsetToStr(Size);
                    wav.Frequence = uint.Parse(Size, System.Globalization.NumberStyles.HexNumber);
     
                    /* Conversion du Nombre de byte par sec */
                    Size = entete.Substring(0, 4);
                    entete = entete.Remove(0, 4);
                    Size = ConvertStringToHex(Size);
                    Size = gestion.ConvertOffsetToStr(Size);
                    wav.BitBySec = uint.Parse(Size, System.Globalization.NumberStyles.HexNumber);
     
                    /* Conversion du Nombre de byte par bloc */
                    Size = entete.Substring(0, 2);
                    entete = entete.Remove(0, 2);
                    Size = ConvertStringToHex(Size);
                    Size = gestion.ConvertOffsetToStr(Size);
                    wav.BitByBloc = ushort.Parse(Size, System.Globalization.NumberStyles.HexNumber);
     
                    /* Conversion du Nombre de byte par Sample (échantillon ??) */
                    Size = entete.Substring(0, 2);
                    entete = entete.Remove(0, 2);
                    Size = ConvertStringToHex(Size);
                    Size = gestion.ConvertOffsetToStr(Size);
                    wav.BitBySample = uint.Parse(Size, System.Globalization.NumberStyles.HexNumber);
     
     
                    /*
                     * Chargement des info data
                     */
                    wav.DataBlocID = entete.Substring(0, 4).ToCharArray();
                    entete = entete.Remove(0, 4);
     
                    /* Conversion du dataSize */
                    Size = entete.Substring(0, 4);
                    entete = entete.Remove(0, 4);
                    Size = ConvertStringToHex(Size);
                    Size = gestion.ConvertOffsetToStr(Size);
                    wav.DataSize = int.Parse(Size, System.Globalization.NumberStyles.HexNumber);
     
                    /*
                     * Lecture des DATA (donnée du wav)
                     */
                    // Lire les data Sample : NbrCanaux * 4
     
                    /* Conversion du data */ 
                    // ICI CA BOGUE ET JE SAIS PLUS
                    char[] chrValue;
                    int SizeData = Convert.ToInt32(wav.BitBySample); // Convert.ToInt32(wav.BitByBloc) * Convert.ToInt32(wav.NbrCanaux);
                    EnteteChar = new char[SizeData];
                    laReader.Read(EnteteChar, 0, SizeData);
                    entete = new String(EnteteChar);
     
                    while (entete.Length > 0)
                    {
                        String StrValue = entete.Substring(0, 4);
                        entete = entete.Remove(0, 4);
                        StrValue = ConvertStringToHex(StrValue);
                        StrValue = gestion.ConvertOffsetToStr(StrValue);
                        int intValue = int.Parse(StrValue, System.Globalization.NumberStyles.HexNumber);
                        wav.addData(intValue);
                    }
     
     
                    // Fermture du fichier
                    laReader.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "Erreur !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
     
            /*
             * Source : http://snipplr.com/view/36461/
             */
            public string ConvertStringToHex(string asciiString)
            {
                string hex = "";
                foreach (char c in asciiString)
                {
                    int tmp = c;
                    hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
                }
                return hex;
            }
     
            /*
             * Source : http://snipplr.com/view/36461/
             */
            public string ConvertHexToString(string HexValue)
            {
                string StrValue = "";
                while (HexValue.Length > 0)
                {
                    StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
                    HexValue = HexValue.Substring(2, HexValue.Length - 2);
                }
                return StrValue;
            }
     
            public wavFile WAV
            {
                get
                {
                    return this.wav;
                }
            }
        }
    }
    Cependant, ensuite j'ai aucune idée de comment lire ou/et sauvegardé le data (ou valeur du son je sais quoi) dans ma classe.

    Est-ce un tableau d'entier, de float, de double, de bytes ou autres?
    Qu'elle quantité (nombre octect) suis-je sensé lire après ?

    Disons que je suis loin d'être un amateur de musique, donc quand on me parle de fréquence, de sample, d'amplitude ou je sais j'ai presque aucune idée de ce que ça signifie.

    Et au niveau du FFT, je ne suis pas parvenu a trouvé d'algorithme qui s'occuperait de me donner la réponse à la question de l'énonce, tous ce que je trouve, c'est des explications qui me donne la formule mathématique, mais disons que ça ne dis pas grand choses


    Class GestionWav
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;
     
    namespace TP4MBB
    {
        class GestionWAV
        {
            string gFileName;
     
            /*
             * Constructeur de la classe
             */
            public GestionWAV(string localFileRoot)
            {
                gFileName = localFileRoot;
            }
     
            ~GestionWAV()
            {
                gFileName = "";
            }
     
            /*
             * Méthode de traitement
             */
     
            public string readHEX(long offset, int quantiteRead)
            {
                byte[] octets;
                using (FileStream fs = File.OpenRead(gFileName))
                {
                    fs.Seek(offset, SeekOrigin.Begin);
     
                    using (BinaryReader reader = new BinaryReader(fs))
                    {
                        octets = reader.ReadBytes(quantiteRead);
                    }
                }
                return BitConverter.ToString(octets);
            }
     
            private string EliminerCaractere(string chaine, char caractere)
            {
                for (int i = chaine.Length-1; i > 0; --i)
                {
                    if (chaine[i].Equals('-'))
                    {
                        chaine = chaine.Remove(i, 1);
                    }
                }
                return chaine;
            }
     
            public long ConvertOffset(string offset)
            {
                string OffsetConvert = "";
                OffsetConvert = offset.Substring(4, 2);
                OffsetConvert += offset.Substring(2, 2);  
                OffsetConvert += offset.Substring(0, 2);  
     
                return long.Parse(OffsetConvert, System.Globalization.NumberStyles.HexNumber);
            }
     
            public string ConvertOffsetToStr(string offset)
            {
                string OffsetConvert = "";
                if ((offset.Length > 4) && (!offset.Substring(4, 2).Equals("00")))
                {
                    OffsetConvert += offset.Substring(4, 2);
                }
                if (!offset.Substring(2, 2).Equals("00"))
                {
                    OffsetConvert += offset.Substring(2, 2);
                }
                if (!offset.Substring(0, 2).Equals("00"))
                {
                    OffsetConvert += offset.Substring(0, 2);
                }
     
                return OffsetConvert;
            }
     
     
            public long searchHex(string value, int nbOctets)
            {
                byte[] octets;
                long offset = 0;
                using (FileStream fs = File.OpenRead(gFileName))
                {
                    int Pos = 288; 
                    bool Etat = false;
                    BinaryReader reader = new BinaryReader(fs);
                    while ((!Etat) && (Pos < fs.Length))
                    {
                        fs.Seek(Pos,SeekOrigin.Begin);
                        octets = reader.ReadBytes(nbOctets);
                        if (!BitConverter.ToString(octets).Equals(value))
                        {
                            Pos++;
                        }
                        else
                        {
                            offset = reader.BaseStream.Position;
                            Etat = true;
                        }
                    }
                }
                return offset-nbOctets;
            }
        }
    }
    Class WavFile
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace TP4MBB
    {
        class wavFile
        {
            /*
             * Entête format WAVE
             */
            char[] fileType = new char[4];
            uint fileSize;
            char[] fileFormat = new char[4];
     
            /*
             * Format de l'audio
             */
            char[] formatBloc = new char[4];
            uint blocSize;
            ushort format;
            ushort nbrCanaux;
            uint frequence;
            uint bitBySec;
            ushort bitByBloc;
            uint bitBySample;
     
            /*
             * Bloc Data
             */
            char[] dataBlocID = new char[4];
            int dataSize;
            List<int> DataSample = new List<int>();
     
            public wavFile()
            {
            }
     
            /*
             * Entête format WAVE
             */
     
            public char[] FileType
            {
                set {
                    this.fileType = value;
                }
                get{
                    return this.fileType;
                }
            }
     
            public uint FileSize
            {
                set
                {
                    this.fileSize = value;
                }
                get
                {
                    return this.fileSize;
                }
            }
     
            public char[] FileFormat
            {
                set
                {
                    this.fileFormat = value;
                }
                get
                {
                    return this.fileFormat;
                }
            }
     
            /*
             * Format de l'audio
             */
            public char[] FormatBloc
            {
                set
                {
                    this.formatBloc = value;
                }
                get
                {
                    return this.formatBloc;
                }
            }
     
            public uint BlocSize
            {
                set
                {
                    this.blocSize = value;
                }
                get
                {
                    return this.blocSize;
                }
            }
     
            public ushort Format
            {
                set
                {
                    this.format = value;
                }
                get
                {
                    return this.format;
                }
            }
     
            public ushort NbrCanaux
            {
                set
                {
                    this.nbrCanaux = value;
                }
                get
                {
                    return this.nbrCanaux;
                }
            }
     
            public uint Frequence
            {
                set
                {
                    this.frequence = value;
                }
                get
                {
                    return this.frequence;
                }
            }
     
            public uint BitBySec
            {
                set
                {
                    this.bitBySec = value;
                }
                get
                {
                    return this.bitBySec;
                }
            }
     
            public ushort BitByBloc
            {
                set
                {
                    this.bitByBloc = value;
                }
                get
                {
                    return this.bitByBloc;
                }
            }
     
            public uint BitBySample
            {
                set
                {
                    this.bitBySample = value;
                }
                get
                {
                    return this.bitBySample;
                }
            }
     
            /*
             * Bloc Data
             */
            public char[] DataBlocID
            {
                set
                {
                    this.dataBlocID = value;
                }
                get
                {
                    return this.dataBlocID;
                }
            }
            public int DataSize
            {
                set
                {
                    this.dataSize = value;
                }
                get
                {
                    return this.dataSize;
                }
            }
            public List<int> Data
            {
                set
                {
                    this.DataSample = value;
                }
                get
                {
                    return this.DataSample;
                }
            }
     
            public void addData(int value)
            {
                DataSample.Add(value);
            }
     
     
            public override string ToString()
            {
                return "File Type : " + new String(FileType) + "\n" +
                       "File Size : " + FileSize + "\n" +
                       "File Format : " + new String(FileFormat) + "\n\n" +
                       "Format Bloc: " + new String(FormatBloc) + "\n" +
                       "Bloc Size: " + BlocSize + "\n" +
                       "Format : " + new String(FormatBloc) + "\n" +
                       "Nombre de canaux : " + NbrCanaux + "\n" +
                       "Frequence : " + Frequence + "\n" +
                       "Bit par sec : " + BitBySec + "\n" +
                       "Bit par bloc : " + BitByBloc + "\n" +
                       "Bit par sample : " + BitBySample + "\n\n" +
                       "DataBloc ID : " + new String(DataBlocID) + "\n" +
                       "Data Size : " + DataSize + "\n";
            }
     
        }
     
    }
    Donc, voilà, si quelqu'un à une idée, une explication ou quelques choses pour me débloquer :/ Je fais des essais depuis 2-3 jours, mais la je suis un peu perdu et j'ai jusqu'à lundi 23h59 xD

  2. #2
    Membre régulier
    Homme Profil pro
    Inscrit en
    Juillet 2008
    Messages
    121
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations forums :
    Inscription : Juillet 2008
    Messages : 121
    Points : 84
    Points
    84
    Par défaut
    Personne n'as de solution ? En partant, j'ai aucune idée ou sont les informations de la trame sonore dans le fichier WAV.

    J'ai fais des tests et trouver certaine choses dans différent langage, mais ce que j'ai trouvé ne donne pas beaucoup d'explication sur le format WAV

    EDIT :
    J'ai nettoyé un peu mon code en ajoutant un class pour chaque section du fichier wav.

    waveHeader.cs
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace TP4MBB.Wav
    {
        class waveHeader
        {
            public char[] FileTypeBlocID = new char[4];     // Constante RIFF
            public uint FileSize;                           // Taille fichier -8 oct
            public char[] FileFormatID = new char[4];       // Constante DATA
     
            public override string ToString()
            {
                return "FileTypeBlocID : " + new String(FileTypeBlocID) + "\n"
                     + "     File Size : " + FileSize + "\n"
                     + "  FileFormatID : " + new String(FileFormatID);
            }
        }
    }
    waveFormatAudio.cs
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace TP4MBB.Wav
    {
        class waveFormatAudio
        {
            public char[] formatBlocID = new char[4];  // Identifiant FMT
            public uint BlocSize;                      // Nombre d'octets du bloc - 8
            public ushort format;                      // Format du stockage dans le fichier (1: PCM,  ...)
            public ushort nbrCanaux;                   // Nombre de canaux (de 1 à 6, cf. ci-dessous)
            public uint frequence;                     // Frequence d'echantillonnage (en Hertz) 
            // [Valeurs standardisees : 11025, 22050, 44100 et eventuellement 48000 et 96000]
            public uint bitBySec;                      // Nombre d'octets à lire par seconde (i.e., Frequence * BytePerBloc).
            public ushort bitByBloc;                   // Nombre d'octets par bloc d'echantillonnage (i.e., tous canaux confondus : NbrCanaux * BitsPerSample/8).
            public ushort bitBySample;                   // Nombre de bits utilisees pour le codage de chaque echantillon (8, 16, 24)
     
            public override string ToString()
            {
                return "  FormatBlocID : " + new String(formatBlocID) + "\n"
                     + "      BlocSize : " + BlocSize + "\n"
                     + "        Format : " + format + "\n"
                     + "     NbrCanaux : " + nbrCanaux + "\n"
                     + "     Frequence : " + frequence + "\n"
                     + "      bitBySec : " + bitBySec + "\n"
                     + "     bitByBloc : " + bitByBloc + "\n"
                     + "   bitBySample : " + bitBySample;
            }
        }
    }
    waveDataBloc.cs
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace TP4MBB.Wav
    {
        class waveDataBloc
        {
            public char[] DataBlocID = new char[4];    // Constante « data »
            public uint DataSize;                      // Nombre d'octets des donnees 
     
     
            public override string ToString()
            {
                return "    DataBlocID : " + new String(DataBlocID) + "\n"
                     + "     Data Size : " + DataSize;
            }
        }
    }
    Puis, dans mon fichier de lecture, au lieu de toujours lire la valeur, la convertir en string, en HEX puis en UINT (ou uSHORT), j'ai créer 2 méthode qui faisais la même chose.

    InformationWAV.Cs
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    using System;
    using System.IO;
    using System.Text;
    using System.Windows.Forms;
    using System.Resources;
    using System.Globalization;
    using TP4MBB.Wav;
     
    namespace TP4MBB
    {
        class InformationWAV
        {
            /*
             * WavFile
             */
            waveHeader header;
            waveFormatAudio audioFormat;
            waveDataBloc audioData;
     
            // Orignale
            GestionWAV gestion;
            bool estValide = false;
     
            public InformationWAV(string gsChemin)
            {
                header = new waveHeader();
                audioFormat = new waveFormatAudio();
                audioData = new waveDataBloc();
     
             try
                {
                    gestion = new GestionWAV(gsChemin);
                    StreamReader laReader = new StreamReader(gsChemin, Encoding.Default);
     
                    char[] EnteteChar = new char[44];
                    laReader.Read(EnteteChar, 0, 44);
                    String entete = new String(EnteteChar);            
     
                    /*
                     * On lit nos valeur présente dans EnteteChar
                     */
                    header.FileTypeBlocID = entete.Substring(0, 4).ToCharArray();
                    entete = entete.Remove(0, 4);
     
                    /* 
                     * Lecture de la taille et conversion en valeur numérique
                     */
                    header.FileSize = convertToUint(entete.Substring(0, 4));
                    entete = entete.Remove(0, 4);
     
                    /* Suite lecture */
                    header.FileFormatID = entete.Substring(0, 4).ToCharArray();
                    entete = entete.Remove(0, 4);
                    if (new String(header.FileFormatID).EndsWith("WAVE"))
                    {
                        estValide = true;
                    }
     
                    //wav.FormatBloc = entete.Substring(0, 4).ToCharArray();
                    audioFormat.formatBlocID = entete.Substring(0, 4).ToCharArray();
                    entete = entete.Remove(0, 4);
     
                    /* Conversion du BlocSize */
                    audioFormat.BlocSize = convertToUint(entete.Substring(0, 4));
                    entete = entete.Remove(0, 4);
     
                    /* Conversion du Format */
                    audioFormat.format = convertToUShort(entete.Substring(0, 2));
                    entete = entete.Remove(0, 2);
     
                    /* Conversion du NbrCanaux */
                    audioFormat.nbrCanaux = convertToUShort(entete.Substring(0, 2));
                    entete = entete.Remove(0, 2);
     
                    /* Conversion de la Frequence */
                    audioFormat.frequence = convertToUint(entete.Substring(0, 4));
                    entete = entete.Remove(0, 4);
     
                    /* Conversion du Nombre de byte par sec */
                    audioFormat.bitBySec = convertToUint(entete.Substring(0, 4));
                    entete = entete.Remove(0, 4);
     
                    /* Conversion du Nombre de byte par bloc */
                    audioFormat.bitByBloc = convertToUShort(entete.Substring(0, 2));
                    entete = entete.Remove(0, 2);
     
                    /* Conversion du Nombre de byte par Sample (échantillon ??) */
                    audioFormat.bitBySample = convertToUShort(entete.Substring(0, 2));
                    entete = entete.Remove(0, 2);
     
     
                    /*
                     * Chargement des info data
                     */
                    audioData.DataBlocID = entete.Substring(0, 4).ToCharArray();
                    entete = entete.Remove(0, 4);
     
                    /* Conversion du dataSize */
                    audioData.DataSize = convertToUint(entete.Substring(0, 4));
                    entete = entete.Remove(0, 4);
     
                    // Fermture du fichier
                    laReader.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "Erreur !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
     
            public ushort convertToUShort(string value)
            {
                value = ConvertStringToHex(value);
                value = gestion.ConvertOffsetToStr(value);
                return ushort.Parse(value, System.Globalization.NumberStyles.HexNumber);
            }
     
            public uint convertToUint(string value)
            {
                value = ConvertStringToHex(value);
                value = gestion.ConvertOffsetToStr(value);
                return uint.Parse(value, System.Globalization.NumberStyles.HexNumber);
            }
     
            /*
             * Source : http://snipplr.com/view/36461/
             */
            public string ConvertStringToHex(string asciiString)
            {
                string hex = "";
                foreach (char c in asciiString)
                {
                    int tmp = c;
                    hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
                }
                return hex;
            }
     
            /*
             * Source : http://snipplr.com/view/36461/
             */
            public string ConvertHexToString(string HexValue)
            {
                string StrValue = "";
                while (HexValue.Length > 0)
                {
                    StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
                    HexValue = HexValue.Substring(2, HexValue.Length - 2);
                }
                return StrValue;
            }
     
            //public wavFile WAV
            //{
            //    get
            //    {
            //        return this.wav;
            //    }
            //}
     
            public bool fileValid
            {
                get
                {
                    return this.estValide;
                }
            }
     
            public waveDataBloc WAVDataBloc
            {
                get
                {
                    return this.audioData;
                }
            }
            public waveHeader WAVHeader
            {
                get
                {
                    return this.header;
                }
            }
     
            public waveFormatAudio WavFormat
            {
                get
                {
                    return this.audioFormat;
                }
            }
        }
    }

Discussions similaires

  1. Utilisation FFT - Power Spectrum sur un fichier WAVE
    Par lekev62 dans le forum LabVIEW
    Réponses: 5
    Dernier message: 07/04/2014, 22h11
  2. FFT avec le signal d'un fichier WAVE
    Par rominous41 dans le forum Débuter
    Réponses: 8
    Dernier message: 24/05/2010, 15h57
  3. fichier wave
    Par miminou dans le forum Autres Logiciels
    Réponses: 2
    Dernier message: 04/10/2005, 14h29
  4. fichier wave
    Par miminou dans le forum Windows
    Réponses: 1
    Dernier message: 30/09/2005, 22h42
  5. Commandes MCI : durée fichier wave
    Par eag35 dans le forum MFC
    Réponses: 1
    Dernier message: 11/04/2005, 12h25

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo