1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include "mpg123.h"
#include <windows.h>
static CRITICAL_SECTION cs;
static HWAVEOUT dev = NULL;
static int nBlocks = 0;
static int MAX_BLOCKS = 6;
static _inline void wait(void)
{
while(nBlocks)
Sleep(77);
}
static void CALLBACK wave_callback(HWAVE hWave, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
WAVEHDR *wh;
HGLOBAL hg;
if(uMsg == WOM_DONE)
{
EnterCriticalSection( &cs );
wh = (WAVEHDR *)dwParam1;
waveOutUnprepareHeader(dev, wh, sizeof (WAVEHDR));
//Deallocate the buffer memory
hg = GlobalHandle(wh->lpData);
GlobalUnlock(hg);
GlobalFree(hg);
//Deallocate the header memory
hg = GlobalHandle(wh);
GlobalUnlock(hg);
GlobalFree(hg);
// decrease the number of USED blocks
nBlocks--;
LeaveCriticalSection( &cs );
}
}
int audio_open(struct audio_info_struct *ai)
{
MMRESULT res;
WAVEFORMATEX outFormatex;
if(ai->rate == -1)
return(0);
if(!waveOutGetNumDevs())
{
MessageBox(NULL, "No audio devices present!", "Error...", MB_OK);
return -1;
}
outFormatex.wFormatTag = WAVE_FORMAT_PCM;
outFormatex.wBitsPerSample = 16;
outFormatex.nChannels = 2;
outFormatex.nSamplesPerSec = ai->rate;
outFormatex.nAvgBytesPerSec = outFormatex.nSamplesPerSec * outFormatex.nChannels * outFormatex.wBitsPerSample/8;
outFormatex.nBlockAlign = outFormatex.nChannels * outFormatex.wBitsPerSample/8;
res = waveOutOpen(&dev, (UINT)ai->device, &outFormatex, (DWORD)wave_callback, 0, CALLBACK_FUNCTION);
if(res != MMSYSERR_NOERROR)
{
switch(res)
{
case MMSYSERR_ALLOCATED:
MessageBox(NULL, "Device Is Already Open", "Error...", MB_OK);
break;
case MMSYSERR_BADDEVICEID:
MessageBox(NULL, "The Specified Device Is out of range", "Error...", MB_OK);
break;
case MMSYSERR_NODRIVER:
MessageBox(NULL, "There is no audio driver in this system.", "Error...", MB_OK);
break;
case MMSYSERR_NOMEM:
MessageBox(NULL, "Unable to allocate sound memory.", "Error...", MB_OK);
break;
case WAVERR_BADFORMAT:
MessageBox(NULL, "This audio format is not supported.", "Error...", MB_OK);
break;
case WAVERR_SYNC:
MessageBox(NULL, "The device is synchronous.", "Error...", MB_OK);
break;
default:
MessageBox(NULL, "Unknown Media Error", "Error...", MB_OK);
break;
}
return -1;
}
waveOutReset(dev);
InitializeCriticalSection(&cs);
return 0;
}
int audio_reset_parameters(struct audio_info_struct *ai)
{
return 0;
}
int audio_rate_best_match(struct audio_info_struct *ai)
{
return 0;
}
int audio_set_rate(struct audio_info_struct *ai)
{
return 0;
}
int audio_set_channels(struct audio_info_struct *ai)
{
return 0;
}
int audio_set_format(struct audio_info_struct *ai)
{
return 0;
}
int audio_get_formats(struct audio_info_struct *ai)
{
return AUDIO_FORMAT_SIGNED_16;
}
int audio_play_samples(struct audio_info_struct *ai,unsigned char *buf,int len)
{
HGLOBAL hg, hg2;
LPWAVEHDR wh;
MMRESULT res;
void *b;
///////////////////////////////////////////////////////
// Wait for a few FREE blocks...
///////////////////////////////////////////////////////
while(nBlocks > MAX_BLOCKS)
Sleep(77);
////////////////////////////////////////////////////////
// FIRST allocate some memory for a copy of the buffer!
////////////////////////////////////////////////////////
hg2 = GlobalAlloc(GMEM_MOVEABLE, len);
if(!hg2)
{
MessageBox(NULL, "GlobalAlloc failed!", "Error...", MB_OK);
return(-1);
}
b = GlobalLock(hg2);
//////////////////////////////////////////////////////////
// Here we can call any modification output functions we want....
///////////////////////////////////////////////////////////
CopyMemory(b, buf, len);
///////////////////////////////////////////////////////////
// now make a header and WRITE IT!
///////////////////////////////////////////////////////////
hg = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof (WAVEHDR));
if(!hg)
{
return -1;
}
wh = GlobalLock(hg);
wh->dwBufferLength = len;
wh->lpData = b;
EnterCriticalSection( &cs );
res = waveOutPrepareHeader(dev, wh, sizeof (WAVEHDR));
if(res)
{
GlobalUnlock(hg);
GlobalFree(hg);
LeaveCriticalSection( &cs );
return -1;
}
res = waveOutWrite(dev, wh, sizeof (WAVEHDR));
if(res)
{
GlobalUnlock(hg);
GlobalFree(hg);
LeaveCriticalSection( &cs );
return (-1);
}
nBlocks++;
LeaveCriticalSection( &cs );
return(len);
}
int audio_close(struct audio_info_struct *ai)
{
if(dev)
{
wait();
waveOutReset(dev); //reset the device
waveOutClose(dev); //close the device
dev=NULL;
}
DeleteCriticalSection(&cs);
nBlocks = 0;
return(0);
}
|