gabuzomeuh2 | Voila j'ai tout réecrit. On dirait que ca marche.
Tu peux me dire ce que tu en penses KarLKox ?
Code :
- #include <windows.h>
- #include "stdafx.h"
- #include "resource.h"
- #include <mmsystem.h>
- #include <malloc.h>
- #define TAILLE_BLOC 4096
- #define NB_BUFFER 3
- #define CANAUX 2
- #define OCTETS 2
- #define BUFFER_SIZE CANAUX * OCTETS * TAILLE_BLOC
- #define SAMPLERATE 44100
- BOOL OpenedIn = FALSE;
- HANDLE recordDone;
- HWAVEIN hWaveIn;
- DWORD threadidIn;
- HANDLE RecordHandle;
- PWAVEHDR pWaveHeaderIn[NB_BUFFER];
- PBYTE pBufferIn[NB_BUFFER];
- BOOL OpenedOut = FALSE;
- HANDLE playDone;
- HWAVEOUT hWaveOut;
- DWORD threadidOut;
- HANDLE PlayHandle;
- PWAVEHDR pWaveHeaderOut[NB_BUFFER];
- PBYTE pBufferOut[NB_BUFFER];
- char Buffer[BUFFER_SIZE];
- HWND DlgMain;
- CRITICAL_SECTION CriticalSection;
- //****************************************************************************
-
- void copier_buffer(char dest[], char src[], long taille)
- {
- __asm
- {
- mov ecx, taille
- cmp ecx, 0
- je fin
- mov edi, dest
- mov esi, src
- debut: mov al, [esi]
- mov [edi], al
- inc esi
- inc edi
- loop debut
- fin:
- }
- }
-
- //****************************************************************************
-
- void CALLBACK waveInProc(HWAVEIN hwi, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
- {
- if (uMsg == WIM_OPEN)
- {
- MessageBox(DlgMain,"WIM_OPEN","RTA",MB_OK);
- OpenedIn = TRUE;
- }
-
- if (uMsg == WIM_CLOSE)
- {
- MessageBox(DlgMain,"WIM_CLOSE","RTA",MB_OK);
- }
-
- if (uMsg == WIM_DATA)
- {
- SetEvent(recordDone);
- }
- }
-
- //****************************************************************************
- // 01 WHDR_DONE indique que c'est fini avec ce buffer.
- // 02 WHDR_PREPARED indique que le buffer a ete prepare (waveInPrepareHeader ou waveOutPrepareHeader)
- // 04 WHDR_BEGINLOOP indique que ce buffer est le premier dans une boucle, drapeau utilisé seulement en sortie.
- // 08 WHDR_ENDLOOP indique que ce buffer est le dernier dans une boucle. Ce drapeau est utilisé seulement en sortie.
- // 10 WHDR_INQUEUE indique que le buffer est mis en queue pour la sortie.
- //****************************************************************************
-
- DWORD WINAPI InThreadProc(LPVOID lpParam)
- {
- int i=0;
-
- for(;;)
- {
- WaitForSingleObject(recordDone, INFINITE);
- if (!(pWaveHeaderIn[i]->dwFlags & WHDR_DONE)) continue;
-
- EnterCriticalSection(&CriticalSection );
-
- copier_buffer(Buffer, pWaveHeaderIn[i]->lpData, BUFFER_SIZE); // sauvegarder bloc recu
-
- LeaveCriticalSection(&CriticalSection );
-
- waveInUnprepareHeader(hWaveIn, pWaveHeaderIn[i], sizeof(WAVEHDR));
- pWaveHeaderIn[i]->dwFlags = 0;
- waveInPrepareHeader(hWaveIn, pWaveHeaderIn[i], sizeof(WAVEHDR));
- waveInAddBuffer(hWaveIn, pWaveHeaderIn[i], sizeof(WAVEHDR));
-
- __asm
- {
- mov eax, i
- mov ebx, NB_BUFFER
- inc eax // i++
- cmp eax, ebx // compare i et NB_BUFFER
- jb fin1 // si i < NB_BUFFER goto fin1
- xor eax, eax // si i = NB_BUFFER i=0
- fin1: mov i, eax
- }
-
- }
- waveInReset(hWaveIn);
- waveInClose(hWaveIn);
- MessageBox(DlgMain,"FIN","RTA",MB_OK);
- OpenedIn = FALSE;
- return 0;
- }
-
- //****************************************************************************
-
- void CALLBACK waveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
- {
- if (uMsg == WOM_OPEN)
- {
- MessageBox(DlgMain,"WOM_OPEN","RTA",MB_OK);
- OpenedOut = TRUE;
- }
-
- if (uMsg == WOM_CLOSE)
- {
- MessageBox(DlgMain,"WOM_CLOSE","RTA",MB_OK);
- }
-
- if (uMsg == WOM_DONE)
- {
- SetEvent(playDone);
- }
- }
-
- //****************************************************************************
-
- DWORD WINAPI OutThreadProc(LPVOID lpParam)
- {
- int i=0;
-
- for(;;)
- {
- WaitForSingleObject(playDone, INFINITE);
-
- if (!(pWaveHeaderOut[i]->dwFlags & WHDR_DONE)) continue;
-
- copier_buffer(pWaveHeaderOut[i]->lpData, Buffer, BUFFER_SIZE); // reprendre bloc recu
- waveOutUnprepareHeader(hWaveOut, pWaveHeaderOut[i], sizeof(WAVEHDR));
- pWaveHeaderOut[i]->dwFlags = 0;
- waveOutPrepareHeader(hWaveOut, pWaveHeaderOut[i], sizeof(WAVEHDR));
- waveOutWrite(hWaveOut, pWaveHeaderOut[i], sizeof(WAVEHDR));
-
- __asm
- {
- mov eax, i
- mov ebx, NB_BUFFER
- inc eax // i++
- cmp eax, ebx // compare i et NB_BUFFER
- jb fin1 // si i < NB_BUFFER goto fin1
- xor eax, eax // si i = NB_BUFFER i=0
- fin1: mov i, eax
- }
-
-
- }
- return 0;
- }
-
- //****************************************************************************
-
- int openWaveDev()
- {
- int i;
- WAVEFORMATEX WaveFormat;
-
- WaveFormat.nChannels = CANAUX; // 1 pour mono 2 pour stereo
- WaveFormat.wBitsPerSample = 8 * OCTETS; // 8 ou 16 bit
- WaveFormat.nAvgBytesPerSec = SAMPLERATE * WaveFormat.nChannels * WaveFormat.wBitsPerSample/8; // nombre d'octets par seconde
- WaveFormat.wFormatTag = 1; // 1 pour PCM
- WaveFormat.nSamplesPerSec = SAMPLERATE; // frequence d'echantillonnage
- WaveFormat.nBlockAlign = 1;
- WaveFormat.cbSize = 0;
-
- for(i=0; i<NB_BUFFER; i++)
- {
- pWaveHeaderIn[i] = (PWAVEHDR) malloc(sizeof(WAVEHDR));
- pBufferIn[i] = (PBYTE)malloc(BUFFER_SIZE);
- }
-
- InitializeCriticalSection(&CriticalSection);
-
- if (waveInOpen(&hWaveIn, WAVE_MAPPER, &WaveFormat, (DWORD)waveInProc, (DWORD)&CriticalSection, CALLBACK_FUNCTION))
- {
- MessageBox(DlgMain,"Ouverture périphérique d'entrée impossible","RTA",MB_OK);
- return 0;
- }
-
- for(i=0; i<NB_BUFFER; i++)
- {
- pWaveHeaderIn[i]->lpData = (LPSTR)pBufferIn[i];
- pWaveHeaderIn[i]->dwBufferLength = BUFFER_SIZE;
- pWaveHeaderIn[i]->dwBytesRecorded = 0;
- pWaveHeaderIn[i]->dwUser = 0;
- pWaveHeaderIn[i]->dwFlags = 0;
- pWaveHeaderIn[i]->dwLoops = 0;
- pWaveHeaderIn[i]->lpNext = NULL;
- pWaveHeaderIn[i]->reserved = 0;
- }
-
- for(i=0; i<NB_BUFFER; i++)
- {
- if (waveInPrepareHeader(hWaveIn, pWaveHeaderIn[i], sizeof(WAVEHDR)))
- {
- MessageBox(DlgMain,"Erreur PrepareHeader","RTA",MB_OK);
- return 0;
- }
- }
-
- for(i=0; i<NB_BUFFER; i++)
- {
- if (waveInAddBuffer(hWaveIn, pWaveHeaderIn[i], sizeof(WAVEHDR)))
- {
- MessageBox(DlgMain,"Erreur AddBuffer","RTA",MB_OK);
- return 0;
- }
- }
-
- ResetEvent(recordDone);
-
- if (waveInStart(hWaveIn))
- {
- waveInClose(hWaveIn);
- }
-
-
-
- //****
- for(i=0; i<NB_BUFFER; i++)
- {
- pWaveHeaderOut[i] = (PWAVEHDR) malloc(sizeof(WAVEHDR));
- pBufferOut[i] = (PBYTE)malloc(BUFFER_SIZE);
- }
-
- if (waveOutOpen(&hWaveOut, WAVE_MAPPER, &WaveFormat, (DWORD)waveOutProc, 0L, CALLBACK_FUNCTION))
- {
- MessageBox(DlgMain,"Ouverture périphérique de sortie impossible","RTA",MB_OK);
- return 0;
- }
-
- for(i=0; i<NB_BUFFER; i++)
- {
- pWaveHeaderOut[i]->lpData = (LPSTR)pBufferOut[i];
- pWaveHeaderOut[i]->dwBufferLength = BUFFER_SIZE;
- pWaveHeaderOut[i]->dwBytesRecorded = 0;
- pWaveHeaderOut[i]->dwUser = 0;
- pWaveHeaderOut[i]->dwFlags = 0;
- pWaveHeaderOut[i]->dwLoops = 0;
- pWaveHeaderOut[i]->lpNext = NULL;
- pWaveHeaderOut[i]->reserved = 0;
- }
-
- for(i=0; i<NB_BUFFER; i++)
- {
- if (waveOutPrepareHeader(hWaveOut, pWaveHeaderOut[i], sizeof(WAVEHDR)))
- {
- MessageBox(DlgMain,"Erreur PrepareHeader","RTA",MB_OK);
- return 0;
- }
- }
-
- for(i=0; i<NB_BUFFER; i++)
- {
- if (waveOutWrite(hWaveOut, pWaveHeaderOut[i], sizeof(WAVEHDR)))
- {
- MessageBox(DlgMain,"Erreur OutWrite","RTA",MB_OK);
- return 0;
- }
- }
- ResetEvent(playDone);
- //*******
-
-
- return 0;
- }
-
- //****************************************************************************
-
- BOOL CALLBACK DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- if (msg == WM_INITDIALOG)
- {
- DlgMain = hWnd;
- return 0;
- }
-
-
-
-
- if (msg == WM_COMMAND)
- {
- if (wParam == IDC_START)
- {
- if(OpenedIn == TRUE)
- {
- MessageBox(DlgMain,"Déjà ouvert","RTA",MB_OK);
- return 0;
- }
- SetDlgItemText(DlgMain, IDC_STATIC1, "" );
-
- recordDone = CreateEvent(0, FALSE, FALSE, 0);
- ResetEvent(recordDone);
- RecordHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) InThreadProc, NULL, 0, &threadidIn);
-
- playDone = CreateEvent(0, FALSE, FALSE, 0);
- ResetEvent(playDone);
- PlayHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) OutThreadProc, NULL, 0, &threadidOut);
-
- openWaveDev();
- return 0;
- }
-
- if (wParam == IDCANCEL)
- {
- EndDialog(hWnd, 0);
- return 0;
- }
- }
-
-
-
-
- return 0;
- }
-
- //****************************************************************************
-
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- HANDLE hMutex;
- hMutex = CreateMutex (NULL,FALSE, "RTA" );
- if (GetLastError() == ERROR_ALREADY_EXISTS)
- {
- MessageBox(NULL,"Ce programme est déjà en service !","WARNING",MB_OK|MB_ICONEXCLAMATION);
- return 0;
- }
-
- DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1),HWND_DESKTOP,DlgProc);
- return 0;
- }
-
- //****************************************************************************
|
|