2014-02-26 22:04:54 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms of the GFX License. If a copy of
|
|
|
|
* the license was not distributed with this file, you can obtain one at:
|
|
|
|
*
|
|
|
|
* http://ugfx.org/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gfx.h"
|
|
|
|
|
2014-03-11 07:13:31 +00:00
|
|
|
#if GFX_USE_GAUDIO && GAUDIO_NEED_RECORD
|
2014-02-26 22:04:54 +00:00
|
|
|
|
|
|
|
/* Include the driver defines */
|
2015-01-21 07:26:24 +00:00
|
|
|
#include "src/gaudio/gaudio_driver_record.h"
|
2014-02-26 22:04:54 +00:00
|
|
|
|
|
|
|
#undef Red
|
|
|
|
#undef Green
|
|
|
|
#undef Blue
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <mmsystem.h>
|
|
|
|
|
|
|
|
#define MAX_WAVE_HEADERS 2 // Larger numbers enable more buffering which is good for ensuring
|
|
|
|
// there are no skips due to data not being available, however larger
|
2014-03-11 07:13:31 +00:00
|
|
|
// numbers of buffers chews buffers on the free-list.
|
2014-02-26 22:04:54 +00:00
|
|
|
|
2014-03-11 07:13:31 +00:00
|
|
|
static HWAVEIN ah = 0;
|
2014-02-26 22:04:54 +00:00
|
|
|
static volatile int nQueuedBuffers;
|
|
|
|
static bool_t isRunning;
|
|
|
|
static WAVEHDR WaveHdrs[MAX_WAVE_HEADERS];
|
|
|
|
static HANDLE waveThread;
|
|
|
|
static DWORD threadID;
|
|
|
|
|
|
|
|
/**************************** waveProc() *******************************
|
|
|
|
* We don't use CALLBACK_FUNCTION because it is restricted to calling only
|
|
|
|
* a few particular Windows functions, namely some of the time functions,
|
|
|
|
* and a few of the Low Level MIDI API. If you violate this rule, your app can
|
|
|
|
* hang inside of the callback). One of the Windows API that a callback can't
|
2014-03-11 07:13:31 +00:00
|
|
|
* call is waveInAddBuffer() which is what we need to use whenever we receive a
|
|
|
|
* MM_WIM_DATA. My callback would need to defer that job to another thread
|
2014-02-26 22:04:54 +00:00
|
|
|
* anyway, so instead just use CALLBACK_THREAD here instead.
|
|
|
|
*************************************************************************/
|
|
|
|
|
2014-03-11 07:13:31 +00:00
|
|
|
static bool_t getbuffer(WAVEHDR *pwh) {
|
2014-03-20 13:33:32 +00:00
|
|
|
GDataBuffer *paud;
|
2014-02-26 22:04:54 +00:00
|
|
|
|
|
|
|
// Get the next data block to send
|
2014-03-02 21:39:46 +00:00
|
|
|
gfxSystemLock();
|
2014-03-11 07:13:31 +00:00
|
|
|
paud = gaudioRecordGetFreeBlockI();
|
|
|
|
if (!paud && !nQueuedBuffers)
|
|
|
|
gaudioRecordDoneI();
|
2014-03-02 21:39:46 +00:00
|
|
|
gfxSystemUnlock();
|
|
|
|
if (!paud)
|
2014-02-26 22:04:54 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
// Prepare the wave header for Windows
|
|
|
|
pwh->dwUser = (DWORD_PTR)paud;
|
|
|
|
pwh->lpData = (LPSTR)(paud+1); // The data is on the end of the structure
|
2014-03-11 07:13:31 +00:00
|
|
|
pwh->dwBufferLength = paud->size;
|
2014-02-26 22:04:54 +00:00
|
|
|
pwh->dwFlags = 0;
|
2014-03-11 07:13:31 +00:00
|
|
|
if (waveInPrepareHeader(ah, pwh, sizeof(WAVEHDR))) {
|
|
|
|
fprintf(stderr, "GAUDIO: Failed to prepare a record buffer");
|
2014-03-05 03:45:37 +00:00
|
|
|
exit(-1);
|
2014-02-26 22:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send it to windows
|
2014-03-11 07:13:31 +00:00
|
|
|
if (waveInAddBuffer(ah, pwh, sizeof(WAVEHDR))) {
|
|
|
|
fprintf(stderr, "GAUDIO: Failed to add the record buffer");
|
2014-03-05 03:45:37 +00:00
|
|
|
exit(-1);
|
2014-02-26 22:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nQueuedBuffers++;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI waveProc(LPVOID arg) {
|
|
|
|
MSG msg;
|
|
|
|
WAVEHDR *pwh;
|
2014-03-20 13:33:32 +00:00
|
|
|
GDataBuffer *paud;
|
2014-02-26 22:04:54 +00:00
|
|
|
(void) arg;
|
|
|
|
|
|
|
|
while (GetMessage(&msg, 0, 0, 0)) {
|
|
|
|
switch (msg.message) {
|
2014-03-11 07:13:31 +00:00
|
|
|
case MM_WIM_DATA:
|
2014-02-26 22:04:54 +00:00
|
|
|
pwh = (WAVEHDR *)msg.lParam;
|
|
|
|
|
|
|
|
// Windows - Let go!
|
2014-03-11 07:13:31 +00:00
|
|
|
waveInUnprepareHeader(ah, pwh, sizeof(WAVEHDR));
|
2014-02-26 22:04:54 +00:00
|
|
|
|
2014-03-11 07:13:31 +00:00
|
|
|
// Save the buffer in the audio record list
|
2014-03-20 13:33:32 +00:00
|
|
|
paud = (GDataBuffer *)pwh->dwUser;
|
2014-03-11 07:13:31 +00:00
|
|
|
paud->len = pwh->dwBytesRecorded;
|
2014-03-02 21:39:46 +00:00
|
|
|
gfxSystemLock();
|
2014-03-11 07:13:31 +00:00
|
|
|
gaudioRecordSaveDataBlockI(paud);
|
2014-03-02 21:39:46 +00:00
|
|
|
gfxSystemUnlock();
|
2014-02-26 22:04:54 +00:00
|
|
|
pwh->lpData = 0;
|
|
|
|
nQueuedBuffers--;
|
|
|
|
|
2014-03-11 07:13:31 +00:00
|
|
|
// Are we stopping?
|
|
|
|
if (!isRunning) {
|
|
|
|
// Have we finished yet?
|
|
|
|
if (!nQueuedBuffers) {
|
|
|
|
gfxSystemLock();
|
|
|
|
gaudioRecordDoneI();
|
|
|
|
gfxSystemUnlock();
|
|
|
|
}
|
|
|
|
break;
|
2014-03-05 03:45:37 +00:00
|
|
|
}
|
2014-03-11 07:13:31 +00:00
|
|
|
|
|
|
|
// Try and get a new block
|
|
|
|
getbuffer(pwh);
|
2014-02-26 22:04:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* External declarations. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2014-03-11 07:13:31 +00:00
|
|
|
bool_t gaudio_record_lld_init(uint16_t channel, uint32_t frequency, ArrayDataFormat format) {
|
2014-02-26 22:04:54 +00:00
|
|
|
WAVEFORMATEX wfx;
|
|
|
|
|
2014-03-02 21:39:46 +00:00
|
|
|
if (format != ARRAY_DATA_8BITUNSIGNED && format != ARRAY_DATA_16BITSIGNED)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-02-26 22:04:54 +00:00
|
|
|
if (!waveThread) {
|
|
|
|
if (!(waveThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)waveProc, 0, 0, &threadID))) {
|
2014-03-11 07:13:31 +00:00
|
|
|
fprintf(stderr, "GAUDIO: Can't create WAVE recording thread\n");
|
2014-03-05 03:45:37 +00:00
|
|
|
exit(-1);
|
2014-02-26 22:04:54 +00:00
|
|
|
}
|
|
|
|
CloseHandle(waveThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
wfx.wFormatTag = WAVE_FORMAT_PCM;
|
2014-03-11 07:13:31 +00:00
|
|
|
wfx.nChannels = channel == GAUDIO_RECORD_STEREO ? 2 : 1;
|
2014-02-26 22:04:54 +00:00
|
|
|
wfx.nSamplesPerSec = frequency;
|
2014-03-02 21:39:46 +00:00
|
|
|
wfx.nBlockAlign = wfx.nChannels * (format == ARRAY_DATA_8BITUNSIGNED ? 1 : 2);
|
2014-02-26 22:04:54 +00:00
|
|
|
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
|
2014-03-02 21:39:46 +00:00
|
|
|
wfx.wBitsPerSample = (format == ARRAY_DATA_8BITUNSIGNED ? 8 : 16);
|
2014-02-26 22:04:54 +00:00
|
|
|
wfx.cbSize = 0;
|
|
|
|
|
2014-03-11 07:13:31 +00:00
|
|
|
if (ah) {
|
|
|
|
waveInClose(ah);
|
|
|
|
ah = 0;
|
|
|
|
}
|
|
|
|
if (waveInOpen(&ah, WAVE_MAPPER, &wfx, (DWORD_PTR)threadID, 0, CALLBACK_THREAD)) {
|
|
|
|
fprintf(stderr, "GAUDIN: Can't open WAVE recording device\n");
|
2014-03-05 03:45:37 +00:00
|
|
|
exit(-1);
|
2014-02-26 22:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-03-11 07:13:31 +00:00
|
|
|
void gaudio_record_lld_start(void) {
|
2014-02-26 22:04:54 +00:00
|
|
|
WAVEHDR *pwh;
|
|
|
|
|
|
|
|
if (!ah)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (nQueuedBuffers < MAX_WAVE_HEADERS) {
|
|
|
|
// Find the empty one - there will always be at least one.
|
|
|
|
for(pwh = WaveHdrs; pwh->lpData; pwh++);
|
|
|
|
|
2014-03-11 07:13:31 +00:00
|
|
|
// Grab the next audio block from the free-list
|
|
|
|
if (!getbuffer(pwh))
|
2014-02-26 22:04:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-03-11 07:13:31 +00:00
|
|
|
if (!isRunning) {
|
|
|
|
isRunning = TRUE;
|
|
|
|
waveInStart(ah);
|
|
|
|
}
|
2014-02-26 22:04:54 +00:00
|
|
|
}
|
|
|
|
|
2014-03-11 07:13:31 +00:00
|
|
|
void gaudio_record_lld_stop(void) {
|
2014-02-26 22:04:54 +00:00
|
|
|
isRunning = FALSE;
|
2014-03-11 07:13:31 +00:00
|
|
|
waveInReset(ah);
|
|
|
|
while(nQueuedBuffers) Sleep(1);
|
2014-02-26 22:04:54 +00:00
|
|
|
}
|
|
|
|
|
2014-03-11 07:13:31 +00:00
|
|
|
#endif /* GFX_USE_GAUDIO && GAUDIO_NEED_RECORD */
|