welcome,
i must doing an aplication which record sound from microphone with win mobile 6.5 pro and save it to the file. I wrote some code, but it doesn`t work. I have a problem with copymemory. What im doing wrong ? I`m using mmsystem unit . Maybe you know better idea to use wavein and wave out under win mobile (or CE). This is a project to school. I must have it tommorow. Please help me.
Code:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, mmsystem, variants, windows, comctrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure WndProc(var m: TMessage);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
var
whIn1, whIn2, whOut: mmsystem.WAVEHDR;
hWaveIn, hWaveOut: HWAVE;
fmt: mmsystem.WAVEINCAPS;
buf1, buf2, SaveBuf: Array of Byte;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption:= 'start';
Button2.Caption:= 'Stop';
Button3.Caption:= 'Play';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
fmt.dwFormats := WAVE_FORMAT_1M16 ;
fmt.wChannels := 2;
SaveBuf:= nil; //(clear the recorded content)
if waveInOpen (@ hWaveIn, WAVE_MAPPER, @ fmt, Handle, 0, CALLBACK_WINDOW) = 0 then
begin
SetLength(buf1, 1024);
SetLength (buf2, 1024 *
;
whIn1.lpData:= PAnsiChar (buf1);
whIn1.dwBufferLength:= Length(buf1);
whIn1.dwBytesRecorded:= 0;
whIn1.dwUser:= 0;
whIn1.dwFlags:= 0;
whIn1.dwLoops:= 0;
whIn1. lpNext:= nil;
whIn1.reserved:= 0;
whIn2.lpData:= PAnsiChar (buf2);
whIn2.dwBufferLength:= Length (buf2);
whIn2.dwBytesRecorded:= 0;
whIn2.dwUser:= 0;
whIn2.dwFlags:= 0;
whIn2.dwLoops:= 0;
whIn2.lpNext:= nil;
whIn2.reserved:= 0;
waveInPrepareHeader (hWaveIn, @ whIn1, SizeOf (mmsystem.WAVEHDR));
waveInPrepareHeader (hWaveIn, @ whIn2, SizeOf (mmsystem.WAVEHDR) );
waveInAddBuffer (hWaveIn, @ whIn1, SizeOf (mmsystem.WAVEHDR));
waveInAddBuffer (hWaveIn, @ whIn2, SizeOf (mmsystem.WAVEHDR));
waveInStart (hWaveIn);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
waveInStop (hWaveIn) ;
waveInUnprepareHeader (hWaveIn, @ whIn1, SizeOf (WaveHdr));
waveInUnprepareHeader (hWaveIn, @ whIn2, SizeOf (WaveHdr));
waveInClose (hWaveIn);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
whOut.lpData:= PAnsiChar(SaveBuf);
whOut.dwBufferLength:= Length(SaveBuf);
whOut.dwBytesRecorded:= 0;
whOut.dwUser:= 0;
whOut.dwFlags:= 0;
whOut.dwLoops:= 1;
whOut.lpNext:= nil;
whOut.reserved:= 0;
waveOutOpen (@ hWaveOut, WAVE_MAPPER, @ fmt, Handle, 0, CALLBACK_WINDOW);
waveOutPrepareHeader (hWaveOut, @ whOut, SizeOf (WaveHdr));
waveOutWrite (hWaveOut, @ whOut, SizeOf (WaveHdr));
end;
procedure TForm1.WndProc(var m: TMessage);
var
ordLen: Integer;
begin
inherited;
case m.Msg of
MM_WIM_OPEN:;
MM_WIM_CLOSE:;
MM_WIM_DATA:
begin
ordLen:= Length (SaveBuf);
SetLength(SaveBuf, ordLen + PWaveHdr(m.LParam).dwBytesRecorded);
CopyMemory(Ptr(DWORD(SaveBuf) + ordLen), PWaveHdr(m.LParam).lpData, PWaveHdr(m.LParam).dwBytesRecorded);
waveInAddBuffer (hWaveIn, PWaveHdr(m.LParam), SizeOf(TWaveHdr));
end;
(handle broadcast news)
MM_WOM_OPEN:; (This message is only carrying a device handle)
MM_WOM_CLOSE:; (This message is only carrying a device handle)
MM_WOM_DONE: begin (The message carrying the device handle and WaveHdr pointer (LParam))
waveOutUnprepareHeader (hWaveOut, PWaveHdr (m. LParam), SizeOf (TWaveHdr));
waveOutClose (hWaveOut);
end;
end;
end;
initialization
{$I unit1.lrs}
end.