The instant play chart is displayed during the recording.

Source: Internet
Author: User
Real-time playback graphs are displayed during recording. For more information, see Delphi/Windows SDK/API.
Http://www.delphi2007.net/DelphiMultimedia/html/delphi_20061106094422173.html
I want to display the corresponding waveform data at the same time during the recording. I use the waveinstart series functions, but there is no sound in the final generated wave file, it's all noisy, and the waveform displayed by the quiet listener is strange. I used the following method to generate a wave file header.
Procedure TRecorder. WriteHead;
Var
FmtSize: Integer;
FileSize: DWORD;

Wf: file of TWavHeader;
Wh: TWavHeader;
Begin
Wh. rId: =$ 46464952;
Wh. rLen: = 36 + m_SumData;
Wh. wId: = $45564157;
Wh. fId: = $20746d66;
Wh. fLen: = 16;
Wh. wFormatTag: = 1;
Wh. nChannels: = m_WaveFormat.nChannels;
Wh. nSamplesPerSec: = m_WaveFormat.nSamplesPerSec;
Wh. nAvgBytesPerSec: = m_WaveFormat.nAvgBytesPerSec;
Wh. nBlockAlign: = m_WaveFormat.nBlockAlign;
Wh. wBitsPerSample: = m_WaveFormat.wBitsPerSample;
Wh. dId: = $61746164;
Wh. wSampleLength: = m_SumData;

FileStream. Write (wh, SizeOf (wh ));
End;

Other code is as follows:




Constructor TRecorder. Create (wBitsPerSample, wChannels: Word;
DwSampleRate: Cardinal; nBufferLength: Integer );
Var
I: Integer;
Begin
M_bRecording: = FALSE;
M_bDeviceOpen: = FALSE;
M_PcmFormat.wBitsPerSample: = wBitsPerSample;
M_PcmFormat.wChannels: = wChannels;
M_PcmFormat.dwSampleRate: = dwSampleRate;
M_dwBufferSize: = (nBufferLength * (m_PcmFormat.wChannels) * (m_PcmFormat.wBitsPerSample) div 8 );
FnProcessBuffer: = nil;
M_lpWaveHdr: = nil;
M_SumData: = 0;
M_hEvent: = 0;
M_hThread: = 0;
For I: = 0 to MAXNUMOFBUFFER-1 do
Begin
M_hWaveInHdr [I]: = 0;
M_hInBuffer [I]: = 0;
End;
End;

Constructor TRecorder. Create (nBufferLength: Integer );
Var
I: Integer;
Begin
M_bRecording: = False;
M_bDeviceOpen: = False;
M_PcmFormat.wBitsPerSample: = 16;
M_PcmFormat.wChannels: = 2;
M_PcmFormat.dwSampleRate := 11025;
M_dwBufferSize: = (nBufferLength * m_PcmFormat.wChannels * m_PcmFormat.wBitsPerSample div 8 );
FnProcessBuffer: = nil;
M_lpWaveHdr: = nil;

M_hEvent: = 0;
M_hThread: = 0;
For I: = 0 to MAXNUMOFBUFFER-1 do
Begin
M_hWaveInHdr [I]: = 0;
M_hInBuffer [I]: = 0;
End;
End;

Constructor TRecorder. Create (pcm: TPCMFormat; nBufferLength: Integer );
Var
I: Integer;
Begin
M_bRecording: = FALSE;
M_bDeviceOpen: = FALSE;
M_PcmFormat.wBitsPerSample: = pcm. wBitsPerSample;
M_PcmFormat.wChannels: = pcm. wChannels;
M_PcmFormat.dwSampleRate: = pcm. dwSampleRate;
M_dwBufferSize: = (nBufferLength * m_PcmFormat.wChannels * m_PcmFormat.wBitsPerSample div 8 );
FnProcessBuffer: = nil;
M_lpWaveHdr: = nil;

M_hEvent: = 0;
M_hThread: = 0;
For I: = 0 to MAXNUMOFBUFFER-1 do
Begin
M_hWaveInHdr [I]: = 0;
M_hInBuffer [I]: = 0;
End;

End;


Function TRecorder. GetPosition: Int64;
Var
Mmtime: TMMTime;
Begin
If m_hWaveIn <> 0 then
Begin
Mmtime. wType: = TIME_SAMPLES;
If waveInGetPosition (m_hWaveIn, @ mmTime, SizeOf (mmTime) <> MMSYSERR_NOERROR then
Result: =-1
Else
Result: = mmTime. sample;
End;
Result: =-1;
End;

Function TRecorder. IsDeviceOpen: Boolean;
Begin
Result: = m_bDeviceOpen;
End;

Function TRecorder. IsFormatSupported (wfEX: tWAVEFORMATEX;
NDev: Cardinal): Boolean;
Var
Mm: MMRESULT;
Begin
Mm: = waveInOpen (nil, nDev, @ wfEx, 0, 0, WAVE_FORMAT_QUERY );
If mm = MMSYSERR_NOERROR then
Result: = True
Else
Result: = False;
End;

Function TRecorder. IsRecording: Boolean;
Begin
Result: = m_bRecording;
End;

Procedure TRecorder. Open (dwCallBack, dwCallbackType: Cardinal;
WMCIDeviceID: MCIDEVICEID );
Var
I: Integer;
Begin
If not m_bDeviceOpen then
Begin
If dwCallBack = 0 then
DwCallBack: = DWord (@ waveinProc );

For I: = 0 to MAXNUMOFBUFFER-1 do
Begin
M_hWaveInHdr [I]: = GlobalAlloc (GHND or GMEM_SHARE, SizeOf (TWaveHdr ));
M_lpWaveInHdr [I]: = pWaveHdr (GlobalLock (m_hWaveInHdr [I]);
M_hInBuffer [I]: = GlobalAlloc (GHND or GMEM_SHARE, m_dwBufferSize );
M_lpInBuffer [I]: = PBYTE (GlobalLock (m_hInBuffer [I]);
M_lpWaveInHdr [I] ^. lpData: = PChar (m_lpInBuffer [I]);
M_lpWaveInHdr [I] ^. dwBufferLength: = m_dwBufferSize;
M_lpWaveInHdr [I] ^. dwBytesRecorded: = 0;
M_lpWaveInHdr [I] ^. dwUser: = Cardinal (Pointer (Self ));
M_lpWaveInHdr [I] ^. dwFlags: = 0;
M_lpWaveInHdr [I] ^. dwLoops: = 1;
M_lpWaveInHdr [I] ^. lpNext: = nil;
M_lpWaveInHdr [I] ^. reserved: = 0;
End;

M_WaveFormat.wFormatTag: = WAVE_FORMAT_PCM;
M_WaveFormat.nChannels: = m_PcmFormat.wChannels;
M_WaveFormat.wBitsPerSample: = m_PcmFormat.wBitsPerSample;
M_WaveFormat.nSamplesPerSec: = m_PcmFormat.dwSampleRate;
M_WaveFormat.nBlockAlign: = m_WaveFormat.nChannels * m_WaveFormat.wBitsPerSample div 8;
M_WaveFormat.nAvgBytesPerSec: = m_WaveFormat.nBlockAlign * m_WaveFormat.nSamplesPerSec;
M_waveClass.lpData: = Self;

If not (waveInOpen (PHWAVEIN (@ m_waveClass), wMCIDeviceID, PWaveFormatEx (@ m_WaveFormat ),
DwCallBack, 0, dwCallbackType) <> 0) or
(M_waveClass.hWave = 0)
) Then
Begin
M_waveClass.lpData: = Self;
M_hWaveIn: = HWAVEIN (m_waveClass.hWave );
M_hEvent: = CreateEvent (nil, False, False, nil );
M_bDeviceOpen: = True;
End;
End;
End;

Function TRecorder. Pause: Boolean;
Begin
Result: = False;
If m_hWaveIn <> 0 then
Begin
If waveInStop (m_hWaveIn) = MMSYSERR_NOERROR then
Begin
M_bRecording: = False;
Result: = True;
End;
End;
End;

Procedure TRecorder. ProcessNextBuffer (pwh: PWaveHdr );
Begin
If @ fnProcessBuffer <> nil then
FnProcessBuffer (m_lpData, pwh );
M_SumData: = m_samsung Data + 2048-1;
WaveInUnprepareHeader (m_hWaveIn, pwh, sizeof (WAVEHDR ));
WaveInPrepareHeader (m_hWaveIn, pwh, sizeof (WAVEHDR ));
WaveInAddBuffer (m_hWaveIn, pwh, sizeof (WAVEHDR ));
End;

Procedure TRecorder. SetBufferFunction (lpData: Pointer;
FnProcess: ProcessBuffer );
Begin
M_lpData: = lpData;
FnProcessBuffer: = fnProcess;
End;

Procedure TRecorder. SetFormat (lpPcmFormat: pPCMFormat );
Begin
If m_bDeviceOpen = False then
Begin
M_PcmFormat.wBitsPerSample: = lpPcmFormat ^. wBitsPerSample;
M_PcmFormat.wChannels: = lpPcmFormat ^. wChannels;
M_PcmFormat.dwSampleRate: = lpPcmFormat ^. dwSampleRate;
End;
End;

Procedure TRecorder. SetFormat (wBitsPerSample, wChannels: Word;
DwSampleRate: Cardinal );
Begin
If m_bDeviceOpen = False then
Begin
M_PcmFormat.wBitsPerSample: = wBitsPerSample;
M_PcmFormat.wChannels: = wChannels;
M_PcmFormat.dwSampleRate: = dwSampleRate;
End;
End;

Procedure TRecorder. Start;
Var
I: Integer;
ThreadId: DWORD;
Begin
If not m_bDeviceOpen then
Exit
Else
Begin
For I: = 0 to MAXNUMOFBUFFER-1 do
Begin
If waveInPrepareHeader (m_hWaveIn, m_lpWaveInHdr [I], SizeOf (TWaveHdr) <> MMSYSERR_NOERROR then
Exit;
If waveInAddBuffer (m_hWaveIn, m_lpWaveInHdr [I], SizeOf (TWaveHdr) <> MMSYSERR_NOERROR then
Exit;
End;
FileStream: = TFileStream. Create ('C: \ wave.wav ', fmCreate or fmOpenReadWrite );
WriteHead;
// Begin sampling
M_bRecording: = True;
M_hThread: = CreateThread (nil, 0, @ RecorderThreadFunc, Self, 0, ThreadId );
WaveInStart (m_hWaveIn );
If m_hThread <> 0 then
Begin
SetPriorityClass (m_hThread, REALTIME_PRIORITY_CLASS );
SetThreadPriority (m_hThread, THREAD_PRIORITY_HIGHEST );
End;
End;
End;

Procedure TRecorder. Stop;
Begin
If (m_bDeviceOpen = False) or (m_bRecording = False) then
Exit;
If (waveInStop (m_hWaveIn) <> MMSYSERR_NOERROR then
Exit
Else
M_bRecording: = False;
End;




Please give me some advice. If anyone has code, please help me. Online, very urgent.

The remaining code:
Procedure TRecorder. Close;
Var
I: Integer;
Begin
FileStream. Position: = 0; // relocate
WriteHead;
If m_bRecording then
Stop;
If m_hThread <> 0 then
CloseHandle (m_hThread );
If m_bDeviceOpen then
WaveInClose (m_hWaveIn );

For I: = 0 to MAXNUMOFBUFFER-1 do
Begin
If (m_hWaveInHdr [I] <> 0) then
Begin
If GlobalUnlock (m_hWaveInHdr [I]) then
GlobalFree (m_hWaveInHdr [I]);
If GlobalUnlock (m_hInBuffer [I]) then
GlobalFree (m_hInBuffer [I]);
M_hWaveInHdr [I]: = 0;
M_hInBuffer [I]: = 0;
End;
End;

M_bDeviceOpen: = False;
M_bRecording: = False;
M_hThread: = 0;
End;

Function TRecorder. Continue: Boolean;
Begin
Result: = False;
If m_hWaveIn <> 0 then
Begin
If waveInStart (m_hWaveIn) = MMSYSERR_NOERROR then
Begin
M_bRecording: = False;
Result: = True;
End;
End;
End;

It seems that few people have had so much time to check so much code. Debug it by yourself. Find information about the problem.

TAudioRecorder using the Wave Audio component (open-source)
You don't need to write your own code ~

Can this component be implemented ????

The data has been implemented. Which of the following functions can be used to display the waveform chart. Urgent

You can directly use DirectX to match the rectangle of the data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.