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

Lazarus Pascal Discussion :

waveIn : enregistre n'importe quoi [Lazarus]


Sujet :

Lazarus Pascal

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Homme Profil pro
    Paramétreur de progiciels
    Inscrit en
    Octobre 2006
    Messages
    970
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Paramétreur de progiciels

    Informations forums :
    Inscription : Octobre 2006
    Messages : 970
    Par défaut waveIn : enregistre n'importe quoi
    Bonjour,

    J'essaie de faire de l'enregistrement de ce qui passe sur la carte son avec les API waveIn*.

    Voici mon code :

    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
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
     
    unit umain;
     
    {$mode objfpc}{$H+}
     
    interface
     
    uses
      Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, mmsystem, windows;
     
    type
     
      { TMain }
     
      TMain = class(TForm)
          BStart: TButton;
          BStop: TButton;
          CBListDevices: TComboBox;
          MLog: TMemo;
          procedure BStartClick(Sender: TObject);
          procedure BStopClick(Sender: TObject);
          procedure FormCreate(Sender: TObject);
      private
        { private declarations }
        gBuffer1, gBuffer2 : array of Byte;
        gRecording : Boolean;
        gWaveFormatEx : WAVEFORMATEX;
        gWaveHdr1, gWaveHdr2 : WAVEHDR;
        gWaveIn : HWAVEIN;
        function waveInError(Nom: String; Resultat: MMRESULT): String;
        procedure Log(Ligne: String);
        procedure MMWimOpen;
        procedure MMWimData(var wParam: WPARAM; var lParam: LPARAM);
        procedure MMWimClose;
      public
        { public declarations }
      end;
     
    var
      Main: TMain;
      PrevWndProc: WNDPROC;
     
    implementation
     
    {$R *.lfm}
     
    { TMain }
     
    function WndCallBack(Ahwnd: HWND; uMsg: UINT; wParam: WParam; lParam: LParam):LRESULT; stdcall;
    begin
        case uMsg of
            MM_WIM_OPEN: Main.MMWimOpen;
            MM_WIM_DATA: Main.MMWimData(wParam, lParam);
            MM_WIM_CLOSE: Main.MMWimClose;
        end;
        Result := CallWindowProc(PrevWndProc, Ahwnd, uMsg, WParam, LParam);
    end;
     
    function TMain.waveInError(Nom: String; Resultat: MMRESULT): String;
    begin
        Nom := Nom + ' : ';
        case Resultat of
            MMSYSERR_ALLOCATED: Result := Nom + 'Specified resource is already allocated.';
            MMSYSERR_BADDEVICEID: Result := Nom + 'Specified device identifier is out of range.';
            MMSYSERR_INVALHANDLE: Result := Nom + 'Specified device handle is invalid.';
            MMSYSERR_NODRIVER: Result := Nom + 'No device driver is present.';
            MMSYSERR_NOMEM: Result := Nom + 'Unable to allocate or lock memory.';
            WAVERR_BADFORMAT: Result := Nom + 'Attempted to open with an unsupported waveform-audio format.';
            WAVERR_UNPREPARED: Result := Nom + 'The buffer pointed to by the pwh parameter hasn''t been prepared.';
            else Result := Nom + 'Unknow error.';
        end;
    end;
     
    procedure TMain.Log(Ligne: String);
    begin
        MLog.Lines.Add(FormatDateTime('dd/mm/yyyy hh:nn:ss', Now) + ' -> ' + Ligne);
    end;
     
    procedure TMain.FormCreate(Sender: TObject);
    var
        i : Integer;
        wic : WAVEINCAPS;
        res : MMRESULT;
    begin
        PrevWndProc := Windows.WNDPROC(SetWindowLong(Self.Handle, GWL_WNDPROC, PtrInt(@WndCallBack)));
     
        BStart.Enabled := False;
        BStop.Enabled := False;
        gRecording := False;
     
        for i:=0 to waveInGetNumDevs-1 do
        begin
            res := waveInGetDevCaps(i, @wic, SizeOf(WAVEINCAPS));
            if res <> MMSYSERR_NOERROR then
            begin
                 Log(waveInError('waveInGetDevCaps', res));
                 Exit;
            end;
            CBListDevices.Items.Add(wic.szPname);
        end;
     
        if CBListDevices.Items.Count > 0 then
        begin
            CBListDevices.ItemIndex := 0;
            BStart.Enabled := True;
        end;
    end;
     
    procedure TMain.BStartClick(Sender: TObject);
    var
        res : MMRESULT;
    begin
        BStart.Enabled := False;
        BStop.Enabled := True;
        CBListDevices.Enabled := False;
     
        // Paramètres d'un en-tête de fichier WAV au format PCM : 19500Hz, 8 bits, mono
        with gWaveFormatEx do
        begin
            wFormatTag := WAVE_FORMAT_PCM;
            nChannels := 1;
            nSamplesPerSec := 19500;
            wBitsPerSample := 8;
            nBlockAlign := (nChannels * wBitsPerSample) div 8;
            nAvgBytesPerSec := nSamplesPerSec * nBlockAlign;
            cbSize := 0;
        end;
     
        res := waveInOpen(@gWaveIn, CBListDevices.ItemIndex, @gWaveFormatEx, Main.Handle, 0, CALLBACK_WINDOW);
        if res <> MMSYSERR_NOERROR then
        begin
            Log(waveInError('waveInOpen', res));
            Exit;
        end;
     
        gRecording := True;
    end;
     
    procedure TMain.BStopClick(Sender: TObject);
    var
        res : MMRESULT;
    begin
        BStart.Enabled := True;
        BStop.Enabled := False;
        CBListDevices.Enabled := True;
     
        gRecording := False;
     
        res := waveInStop(gWaveIn);
        if res <> MMSYSERR_NOERROR then
        begin
            Log(waveInError('waveInStop', res));
            Exit;
        end;
     
        res := waveInReset(gWaveIn);
        if res <> MMSYSERR_NOERROR then
        begin
            Log(waveInError('waveInReset', res));
            Exit;
        end;
     
        res := waveInUnprepareHeader(gWaveIn, @gWaveHdr1, SizeOf(WAVEHDR));
        if res <> MMSYSERR_NOERROR then
        begin
            Log(waveInError('waveInUnprepareHeader', res));
            Exit;
        end;
     
        res := waveInUnprepareHeader(gWaveIn, @gWaveHdr2, SizeOf(WAVEHDR));
        if res <> MMSYSERR_NOERROR then
        begin
            Log(waveInError('waveInUnprepareHeader', res));
            Exit;
        end;
     
        res := waveInClose(gWaveIn);
        if res <> MMSYSERR_NOERROR then
        begin
            Log(waveInError('waveInClose', res));
            Exit;
        end;
    end;
     
    procedure TMain.MMWimOpen;
    var
        res : MMRESULT;
    begin
        Log('MM_WIM_OPEN');
     
        SetLength(gBuffer1, gWaveFormatEx.nAvgBytesPerSec * 3);
     
        with gWaveHdr1 do
        begin
            lpData := PChar(gBuffer1);
            dwBufferLength := Length(gBuffer1);
            dwFlags := 0;
        end;
     
        res := waveInPrepareHeader(gWaveIn, @gWaveHdr1, SizeOf(WAVEHDR));
        if res <> MMSYSERR_NOERROR then
        begin
            Log(waveInError('waveInPrepareHeader', res));
            Exit;
        end;
     
        res := waveInAddBuffer(gWaveIn, @gWaveHdr1, SizeOf(WAVEHDR));
        if res <> MMSYSERR_NOERROR then
        begin
            Log(waveInError('waveInAddBuffer', res));
            Exit;
        end;
     
        SetLength(gBuffer2, gWaveFormatEx.nAvgBytesPerSec * 3);
     
        with gWaveHdr2 do
        begin
            lpData := PChar(gBuffer2);
            dwBufferLength := Length(gBuffer2);
            dwFlags := 0;
        end;
     
        res := waveInPrepareHeader(gWaveIn, @gWaveHdr2, SizeOf(WAVEHDR));
        if res <> MMSYSERR_NOERROR then
        begin
            Log(waveInError('waveInPrepareHeader', res));
            Exit;
        end;
     
        res := waveInAddBuffer(gWaveIn, @gWaveHdr2, SizeOf(WAVEHDR));
        if res <> MMSYSERR_NOERROR then
        begin
            Log(waveInError('waveInAddBuffer', res));
            Exit;
        end;
     
        res := waveInStart(gWaveIn);
        if res <> MMSYSERR_NOERROR then
        begin
            Log(waveInError('waveInStart', res));
            Exit;
        end;
    end;
     
    procedure TMain.MMWimData(var wParam: WPARAM; var lParam: LPARAM);
    var
        res : MMRESULT;
        //hBuffer : HGLOBAL;
        //lBuffer : LPVOID;
        Stream : TMemoryStream;
    begin
        Log('MM_WIM_DATA');
     
        //hBuffer := GlobalAlloc(GHND or GMEM_SHARE, gWaveFormatEx.nAvgBytesPerSec * 3);
        //lBuffer := GlobalLock(hBuffer);
        //CopyMemory(lBuffer, LPWAVEHDR(lParam)^.lpData, gWaveFormatEx.nAvgBytesPerSec * 3);
        if Length(LPWAVEHDR(lParam)^.lpData) > 0 then
        begin
            Stream := TMemoryStream.Create;
            Stream.WriteBuffer(LPWAVEHDR(lParam)^.lpData, Length(LPWAVEHDR(lParam)^.lpData));
            Stream.SaveToFile(FormatDateTime('yyyy-mm-dd hh-nn-ss', Now) + '.raw');
            Stream.Free;
        end;
        //GlobalUnlock(hBuffer);
        //GlobalFree(hBuffer);
     
        if gRecording then
        begin
            res := waveInAddBuffer(HWAVEIN(wParam), LPWAVEHDR(lParam), SizeOf(WAVEHDR));
            if res <> MMSYSERR_NOERROR then
            begin
                Log(waveInError('waveInAddBuffer', res));
                Exit;
            end;
        end;
    end;
     
    procedure TMain.MMWimClose;
    var
        res : MMRESULT;
    begin
        Log('MM_WIM_CLOSE');
    end;
     
    end.
    Vous trouverez ci-joint mon projet Lazarus complet.

    Le problème, c'est que les données enregistrées (données brutes qui peuvent être normalement écoutées avec Audacity en important des données brutes au format *.raw) ne semblent pas correctes...

    Pouvez-vous m'aider à corriger ce problème ?

    Merci,
    ZiP
    Fichiers attachés Fichiers attachés

  2. #2
    Membre éprouvé
    Homme Profil pro
    Paramétreur de progiciels
    Inscrit en
    Octobre 2006
    Messages
    970
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Paramétreur de progiciels

    Informations forums :
    Inscription : Octobre 2006
    Messages : 970
    Par défaut
    Bonjour,

    J'ai corrigé :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Stream.WriteBuffer(LPWAVEHDR(lParam)^.lpData, Length(LPWAVEHDR(lParam)^.lpData));
    Par :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Stream.WriteBuffer(LPWAVEHDR(lParam)^.lpData, LPWAVEHDR(lParam)^.dwBytesRecorded);
    Ce qui est plus logique, cependant ça ne fonctionne toujours pas...

    Merci,
    ZiP
    Fichiers attachés Fichiers attachés

  3. #3
    Membre éprouvé
    Homme Profil pro
    Paramétreur de progiciels
    Inscrit en
    Octobre 2006
    Messages
    970
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Paramétreur de progiciels

    Informations forums :
    Inscription : Octobre 2006
    Messages : 970
    Par défaut
    Bonjour,

    J'ai corrigé :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if Length(LPWAVEHDR(lParam)^.lpData) > 0 then
    Par :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if LPWAVEHDR(lParam)^.dwBytesRecorded > 0 then
    Et aussi :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Stream.WriteBuffer(LPWAVEHDR(lParam)^.lpData, LPWAVEHDR(lParam)^.dwBytesRecorded);
    Par :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Stream.WriteBuffer(LPWAVEHDR(lParam)^.lpData^, LPWAVEHDR(lParam)^.dwBytesRecorded);
    Ça ne semble toujours pas fonctionner...

    Merci,
    ZiP
    Fichiers attachés Fichiers attachés

  4. #4
    Membre éprouvé
    Homme Profil pro
    Paramétreur de progiciels
    Inscrit en
    Octobre 2006
    Messages
    970
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Paramétreur de progiciels

    Informations forums :
    Inscription : Octobre 2006
    Messages : 970
    Par défaut
    Bonjour,

    Correction, ce dernier code fonctionne !

    Il y a très peu d'exemple de ce type sur Internet en Pascal ou Delphi sur l'utilisation des API waveIn, j'espère qu'il servira à d’autres !

    Cordialement,
    ZiP

  5. #5
    Expert éminent
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Par défaut
    Citation Envoyé par [ZiP] Voir le message
    Bonjour,

    Correction, ce dernier code fonctionne !

    Il y a très peu d'exemple de ce type sur Internet en Pascal ou Delphi sur l'utilisation des API waveIn, j'espère qu'il servira à d’autres !

    Cordialement,
    ZiP
    SIPInside, développé sous Delphi 6 l'utilise.

    EDIT: la version WinCE compile aussi bien sous Delphi(win32) que sous FreePascal pour WinCE, elle devrait donc compiler sous Lazarus
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

  6. #6
    Membre éprouvé
    Homme Profil pro
    Paramétreur de progiciels
    Inscrit en
    Octobre 2006
    Messages
    970
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Paramétreur de progiciels

    Informations forums :
    Inscription : Octobre 2006
    Messages : 970
    Par défaut
    Merci pour l'information Paul !

    Cordialement,
    ZiP

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Programme qui m'affiche n'importe quoi
    Par Gryzzly dans le forum C
    Réponses: 8
    Dernier message: 02/01/2006, 19h25
  2. Prendre que les 5 premiers enregistrements (les + important)
    Par __fabrice dans le forum MS SQL Server
    Réponses: 3
    Dernier message: 02/11/2005, 09h24

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