Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 25
  1. #1

    [C++ Win32] waveInStart

    Salve,

    ho scritto quello che dovrebbe essere il codice base per catturare un primo chunk di 4096 bytes di audio dal microfono del pc:

    codice:
    #include <windows.h>
    #pragma comment (lib, "winmm.lib")
    #include <mmsystem.h>
    #include <iostream>
    #include <stdlib.h> // Define "system" function
    #define system_buf_len 4096
    
    LRESULT CALLBACK mycallback(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    int main()
    {
    	// Definisco la struttura WAVEFORMATEX
    	WAVEFORMATEX waveFormat;
    	waveFormat.wFormatTag      = WAVE_FORMAT_PCM;
    	waveFormat.wBitsPerSample  = 16;
    	waveFormat.nChannels       = 1;
    	waveFormat.nSamplesPerSec  = 44100;
    	waveFormat.nBlockAlign     = waveFormat.nChannels * waveFormat.wBitsPerSample / 8;
    	waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
    	waveFormat.cbSize          = 0;
    
    	MMRESULT mmres;   // ...
    	HWAVEIN phvi;     // Handle for the input device
    	UINT uDevice = 0; // Device id "Gruppo Microfoni"
    
    	// waveInOpen
    	mmres = waveInOpen(&phvi,
    					uDevice,
    					(LPWAVEFORMATEX)&waveFormat,
    					(DWORD)mycallback,
    					0,
    					CALLBACK_EVENT
    					);
    
    	// Prepare Buffer
    
    	char *buf = (char *)malloc(system_buf_len);
    
    	WAVEHDR buffer;
    	buffer.lpData          = buf;
    	buffer.dwBufferLength  = system_buf_len;
    	buffer.dwBytesRecorded = 0;
    	buffer.dwUser          = 0;
    	buffer.dwFlags         = 0;
    	buffer.dwLoops         = 0;
    
    	// waveInPrepareHeader
    	waveInPrepareHeader(phvi, &buffer, sizeof(WAVEHDR));
    
    	// waveInAddBuffer
    	waveInAddBuffer(phvi, &buffer, sizeof(WAVEHDR));
    
    	//waveInStart;
    	waveInStart(phvi);
    	system("pause");
    	return 0;
    }
    
    LRESULT CALLBACK mycallback(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	MessageBox(NULL, LPCTSTR(message), "Caption", MB_OK);
    	return 0;
    }
    Dopo la chiamata waveInStart() credevo che dovessi aspettare una chiamata da parte del sistema alla callback (mycallback) quando l'intero buffer era pieno, per questo ho settato in maniera molto semplice la callback per vedere se veniva chiamata dal sistema (a limite stampando il valore di "message" che in teoria dovrebbe ritornare la costante MM_WIM_DATA). Tuttavia il codice viene eseguito senza errori da parte del compilatore ma la callback non viene richiamata!

    A questo punto mi domando se la callback è scritta nella forma corretta
    Alla batteria dai retta ballA

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Un "Evento" è un oggetto di Windows non una funzione

    http://msdn.microsoft.com/en-us/library/ms535863(VS.85).aspx

    Non è così che si usa quella API ...

    Dai un'occhiata a

    http://www.relisoft.com/freeware/recorder.html
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Ok, grazie per il link, anche se mi sarebbe piaciuto approfondire dove dice:

    Once the buffer is full, Windows notifies the client, who is supposed to process the bufferful of data. There are several notification options.The second option is to get notifications in the form of a Windows messages.
    Alla batteria dai retta ballA

  4. #4
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Se vuoi utilizzare una "funzione di callback" (non un evento) allora devi indicare la costante

    CALLBACK_FUNCTION

    nella open e utilizzare una funzione del tipo


    void CALLBACK waveInProc(
    HWAVEIN hwi,
    UINT uMsg,
    DWORD dwInstance,
    DWORD dwParam1,
    DWORD dwParam2
    )

    Tramite il campo uMsg gestirai i messaggi in arrivo.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  5. #5
    ok, ma c'è una cosa che non capisco la struttura della callback è:

    codice:
    void CALLBACK waveInProc(
    HWAVEIN hwi,
    UINT uMsg,
    DWORD dwInstance,
    DWORD dwParam1,
    DWORD dwParam2
    )
    ma quando aspetto MM_WIM_DATA non mi sembra che i parametti corrispondano alla callback:

    MM_WIM_DATA
    wParam = (WPARAM) hInputDev
    lParam = (LONG) lpwvhdr
    come ottego wParam e lParam??
    Alla batteria dai retta ballA

  6. #6
    cmq ho provato ad eseguire questo codice:

    codice:
    #include <windows.h>
    #pragma comment (lib, "winmm.lib")
    #include <mmsystem.h>
    #include <iostream>
    #include <stdlib.h> // Define "system" function
    #define system_buf_len 4096
    
    void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2);
    
    int main()
    {
    	// Definisco la struttura WAVEFORMATEX
    	WAVEFORMATEX waveFormat;
    	waveFormat.wFormatTag      = WAVE_FORMAT_PCM;
    	waveFormat.wBitsPerSample  = 16;
    	waveFormat.nChannels       = 1;
    	waveFormat.nSamplesPerSec  = 44100;
    	waveFormat.nBlockAlign     = waveFormat.nChannels * waveFormat.wBitsPerSample / 8;
    	waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
    	waveFormat.cbSize          = 0;
    
    	MMRESULT mmres;   // ...
    	HWAVEIN phvi;     // Handle for the input device
    	UINT uDevice = 0; // Device id "Gruppo Microfoni"
    
    	// waveInOpen
    	mmres = waveInOpen(&phvi,
    					uDevice,
    					(LPWAVEFORMATEX)&waveFormat,
    					(DWORD)waveInProc,
    					0,
    					CALLBACK_FUNCTION
    					);
    
    	// Prepare Buffer
    
    	char *buf = (char *)malloc(system_buf_len);
    
    	WAVEHDR buffer;
    	buffer.lpData          = buf;
    	buffer.dwBufferLength  = system_buf_len;
    	buffer.dwBytesRecorded = 0;
    	buffer.dwUser          = 0;
    	buffer.dwFlags         = 0;
    	buffer.dwLoops         = 0;
    
    	// waveInPrepareHeader
    	waveInPrepareHeader(phvi, &buffer, sizeof(WAVEHDR));
    
    	// waveInAddBuffer
    	waveInAddBuffer(phvi, &buffer, sizeof(WAVEHDR));
    
    	//waveInStart;
    	waveInStart(phvi);
    	
    	//
    	system("pause");
    	return 0;
    }
    
    void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2)
    {
    	MessageBox(NULL, LPCTSTR(uMsg), "Caption", MB_OK);
    }
    ed ottengo un errore /unhandle exception/ il seguente:

    waveinxxx.exe!waveInProc(HWAVEIN__ * hwi=0x004448c8, unsigned int uMsg=958, unsigned long dwInstance=0, unsigned long dwParam1=0, unsigned long dwParam2=0) Line 63 + 0x15 bytes C++
    Alla batteria dai retta ballA

  7. #7
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Originariamente inviato da gianvituzzi

    ... ed ottengo un errore ...
    Beh ... per forza ... se scrivi

    MessageBox(NULL, LPCTSTR(uMsg), "Caption", MB_OK);

    ... cosa intendevi fare?

    uMsg è un numero intero ... non puoi passarlo come se fosse un indirizzo di una stringa!

    Ma tu hai chiaro il concetto di "messaggio" per Windows ?
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  8. #8
    assolutamente, è solo che volevo fare una cosa tipo:

    codice:
    std::cout << uMsg << std::endl;
    ovvero stampare un numero...
    Alla batteria dai retta ballA

  9. #9
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Dato che la messagebox accetta un dato di tipo stringa (quelle del C), dovrai prima trasformare il valore numerico in stringa ...

    Dovrai quindi allocare lo spazio per una stringa e utilizzarlo per memorizzarci il valore di uMsg ... puoi usare la funzione sprintf ...
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  10. #10
    Utente di HTML.it L'avatar di XWolverineX
    Registrato dal
    Aug 2005
    residenza
    Prague
    Messaggi
    2,565
    Quanti wave quest'oggi.
    "Se proprio devono piratare, almeno piratino il nostro." (Bill Gates)

    "Non è possibile che 2 istituzioni statali mi mettano esami nello stesso giorno." (XWolverineX)

    http://xvincentx.netsons.org/programBlog

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.