[xiph-cvs] cvs commit: ao/src/plugins/mmsound mmsound.c

Chris Wolf cwolf at xiph.org
Mon Sep 10 17:43:07 PDT 2001



cwolf       01/09/10 17:43:06

  Modified:    src/plugins/mmsound mmsound.c
  Log:
  Final fixes from Matt.

Revision  Changes    Path
1.5       +41 -63    ao/src/plugins/mmsound/mmsound.c

Index: mmsound.c
===================================================================
RCS file: /usr/local/cvsroot/ao/src/plugins/mmsound/mmsound.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- mmsound.c	2001/09/09 15:20:47	1.4
+++ mmsound.c	2001/09/11 00:43:06	1.5
@@ -2,7 +2,7 @@
  *
  *  mmsound.c
  *
- *      Copyright (C) Matthew Brown - Sept. July 2001
+ *      Copyright (C) Matthew Brown - Sept. 2001
  *
  *  This file is part of libao, a cross-platform library.  See
  *  README for a history of this source code.
@@ -32,20 +32,17 @@
 typedef struct ao_mmsound_internal {
         HWAVEOUT m_hWaveOut;
         WAVEHDR	m_waveHeader[2];
+	uint_32 buf_size;
         uint_32 num_bytes[2];
         void *buffer[2];
-	void *tempbuf;
-	uint_32 tb_num_bytes;
         int currentb;
-	uint_32 buf_size;
-	int running;
-	HANDLE notdone;
-	HANDLE datanotdone;
-	CRITICAL_SECTION  lock;
 } ao_mmsound_internal;
 
 static char *ao_mmsound_options[] = {"buf_size"};
 
+HANDLE devicenum;
+CRITICAL_SECTION  mylock;
+
 static ao_info ao_mmsound_info =
 {
         AO_TYPE_LIVE,
@@ -59,49 +56,14 @@
         1
 };
 
-void fillnextbuffer(DWORD dwInstance)
-{
-	ao_mmsound_internal *internal = (ao_mmsound_internal *)dwInstance;
-
-	EnterCriticalSection(&internal->lock);
-
-	if (internal->m_waveHeader[internal->currentb].dwFlags&WHDR_PREPARED)
-		waveOutUnprepareHeader(internal->m_hWaveOut,&internal->m_waveHeader[internal->currentb],sizeof(WAVEHDR));
-
-	ReleaseSemaphore(internal->notdone, 1, NULL);
-	WaitForSingleObject(internal->datanotdone,INFINITE);
-	memcpy(internal->buffer[internal->currentb], internal->tempbuf, internal->tb_num_bytes);
-	internal->num_bytes[internal->currentb] = internal->tb_num_bytes;
-	
-	internal->m_waveHeader[internal->currentb].lpData = (char*)internal->buffer[internal->currentb];
-	internal->m_waveHeader[internal->currentb].dwBufferLength = (unsigned long)internal->num_bytes[internal->currentb];
-	waveOutPrepareHeader(internal->m_hWaveOut,&internal->m_waveHeader[internal->currentb],sizeof(WAVEHDR));
-
-	waveOutWrite(internal->m_hWaveOut,&internal->m_waveHeader[internal->currentb],sizeof(WAVEHDR));
-	internal->currentb++;
-	if(internal->currentb == 2)
-		internal->currentb = 0;
-	LeaveCriticalSection(&internal->lock);
-
-}
-
-void startbuffer(DWORD dwInstance)
-{
-	ao_mmsound_internal *internal = (ao_mmsound_internal *)dwInstance;
-
-	fillnextbuffer(internal);
-	fillnextbuffer(internal);
-	_endthread();
-}
 
 static	void CALLBACK waveOutProc(HWAVEOUT hwo,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2)
 {
-	ao_mmsound_internal *internal = (ao_mmsound_internal *)dwInstance;
-
         if (uMsg == WOM_DONE)
         {
-		if(internal->running)
-			fillnextbuffer(dwInstance);
+		EnterCriticalSection(&mylock);
+		ReleaseSemaphore(devicenum, 1, NULL);
+		LeaveCriticalSection(&mylock);
         }
 }
 
@@ -162,7 +124,7 @@
                                                         WAVE_MAPPER,
                                                         &wfx,
                                                         (DWORD)waveOutProc,
-							(DWORD)device->internal,
+							NULL,
                                                         (DWORD)CALLBACK_FUNCTION);
 
         if (errCode != MMSYSERR_NOERROR) return 0;
@@ -176,19 +138,16 @@
         memset(&internal->m_waveHeader[1],0,sizeof(WAVEHDR));
         internal->buffer[0] = malloc(internal->buf_size);
         internal->buffer[1] = malloc(internal->buf_size);
-	internal->tempbuf = malloc(internal->buf_size);
+	internal->num_bytes[0] = 0;
+	internal->num_bytes[1] = 0;
         internal->currentb = 0;
-	internal->running = 1;
         
-	internal->notdone = CreateSemaphore(NULL, 0, 1, NULL);
-	internal->datanotdone = CreateSemaphore(NULL, 0, 1, NULL);
+	devicenum = CreateSemaphore(NULL, 0, 2, NULL);
 
         device->driver_byte_format = AO_FMT_NATIVE;
 
-	InitializeCriticalSection(&internal->lock);
+	InitializeCriticalSection(&mylock);
 
-	_beginthread(startbuffer, 0, internal);
-	
         return 1;
 }
 
@@ -202,14 +161,26 @@
         
         if(num_bytes > internal->buf_size)
                 return 0;
+
+	if(internal->num_bytes[internal->currentb] != 0)
+		WaitForSingleObject(devicenum, INFINITE);
 
-	WaitForSingleObject(internal->notdone, INFINITE);
+	if (internal->m_waveHeader[internal->currentb].dwFlags&WHDR_PREPARED)
+		waveOutUnprepareHeader(internal->m_hWaveOut,&internal->m_waveHeader[internal->currentb],sizeof(WAVEHDR));
         
-	memcpy(internal->tempbuf, output_samples, num_bytes);
-	internal->tb_num_bytes = num_bytes;
+	memcpy(internal->buffer[internal->currentb], output_samples, num_bytes);
+	internal->num_bytes[internal->currentb] = num_bytes;
         
-	ReleaseSemaphore(internal->datanotdone, 1, NULL);
+	internal->m_waveHeader[internal->currentb].lpData = (char*)internal->buffer[internal->currentb];
+	internal->m_waveHeader[internal->currentb].dwBufferLength = (unsigned long)internal->num_bytes[internal->currentb];
+	waveOutPrepareHeader(internal->m_hWaveOut,&internal->m_waveHeader[internal->currentb],sizeof(WAVEHDR));
 
+	waveOutWrite(internal->m_hWaveOut,&internal->m_waveHeader[internal->currentb],sizeof(WAVEHDR));
+
+	internal->currentb++;
+	if(internal->currentb == 2)
+		internal->currentb = 0;
+
         return 1;
 }
 
@@ -217,13 +188,20 @@
 {
         ao_mmsound_internal *internal = (ao_mmsound_internal *) device->internal;
 
-	internal->running = 0;
-	while((!internal->m_waveHeader[0].dwFlags&WHDR_DONE) || (!internal->m_waveHeader[1].dwFlags&WHDR_DONE))
-	{}
-	
+	if (internal->m_waveHeader[0].dwFlags&WHDR_PREPARED)
+		waveOutUnprepareHeader(internal->m_hWaveOut,&internal->m_waveHeader[0],sizeof(WAVEHDR));
+
+	if (internal->m_waveHeader[1].dwFlags&WHDR_PREPARED)
+		waveOutUnprepareHeader(internal->m_hWaveOut,&internal->m_waveHeader[1],sizeof(WAVEHDR));
+
+	WaitForSingleObject(devicenum, 2000);
+	if(internal->num_bytes[1] != 0)
+		WaitForSingleObject(devicenum, 4000);
+
         waveOutReset(internal->m_hWaveOut);
         waveOutClose(internal->m_hWaveOut);
-	CloseHandle(internal->notdone);
+	CloseHandle(devicenum);
+	DeleteCriticalSection(&mylock);
         free(internal->buffer[0]);
         free(internal->buffer[1]);
 

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list