changeset 12093:f54d02f6ddbf

let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
author faust3
date Thu, 01 Apr 2004 19:33:58 +0000
parents 16701d1754a7
children b36254dfecfd
files libao2/ao_sdl.c libao2/ao_win32.c
diffstat 2 files changed, 27 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/libao2/ao_sdl.c	Thu Apr 01 14:17:58 2004 +0000
+++ b/libao2/ao_sdl.c	Thu Apr 01 19:33:58 2004 +0000
@@ -12,6 +12,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "../config.h"
 #include "../mp_msg.h"
@@ -20,6 +21,7 @@
 #include "audio_out_internal.h"
 #include "afmt.h"
 #include <SDL.h>
+#include "osdep/timer.h"
 
 #include "../libvo/fastmemcpy.h"
 
@@ -34,16 +36,12 @@
 LIBAO_EXTERN(sdl)
 
 // Samplesize used by the SDLlib AudioSpec struct
-#ifdef WIN32
 #define SAMPLESIZE 2048
-#else
-#define SAMPLESIZE 1024
-#endif
 
 // General purpose Ring-buffering routines
 
 #define BUFFSIZE 4096
-#define NUM_BUFS 16 
+#define NUM_BUFS 8
 
 static unsigned char *buffer[NUM_BUFS];
 
@@ -51,7 +49,7 @@
 static unsigned int buf_write=0;
 static unsigned int buf_read_pos=0;
 static unsigned int buf_write_pos=0;
-static unsigned int volume=127;
+static unsigned char volume=SDL_MIX_MAXVOLUME;
 static int full_buffers=0;
 static int buffered_bytes=0;
 
@@ -80,11 +78,11 @@
   int len2=0;
   int x;
   while(len>0){
-    if(full_buffers==0) break; // no more data buffered!
+    if(buffered_bytes==0) break; // no more data buffered!
     x=BUFFSIZE-buf_read_pos;
     if(x>len) x=len;
-    memcpy(data+len2,buffer[buf_read]+buf_read_pos,x);
-    SDL_MixAudio(data+len2, data+len2, x, volume);
+    if (x>buffered_bytes) x=buffered_bytes;
+    SDL_MixAudio(data+len2,buffer[buf_read]+buf_read_pos,x,volume);
     len2+=x; len-=x;
     buffered_bytes-=x; buf_read_pos+=x;
     if(buf_read_pos>=BUFFSIZE){
@@ -122,15 +120,15 @@
 		case AOCONTROL_GET_VOLUME:
 		{
 			ao_control_vol_t* vol = (ao_control_vol_t*)arg;
-			vol->left = vol->right = (float)((volume + 127)/2.55);
+			vol->left = vol->right = volume * 100 / SDL_MIX_MAXVOLUME;
 			return CONTROL_OK;
 		}
 		case AOCONTROL_SET_VOLUME:
 		{
-			float diff;
+			int diff;
 			ao_control_vol_t* vol = (ao_control_vol_t*)arg;
 			diff = (vol->left+vol->right) / 2;
-			volume = (int)(diff * 2.55) - 127;
+			volume = diff * SDL_MIX_MAXVOLUME / 100;
 			return CONTROL_OK;
 		}
 	}
@@ -265,6 +263,8 @@
 // close audio device
 static void uninit(){
 	mp_msg(MSGT_AO,MSGL_V,"SDL: Audio Subsystem shutting down!\n");
+	while(buffered_bytes > 0)
+		usec_sleep(50000);
 	SDL_CloseAudio();
 	SDL_QuitSubSystem(SDL_INIT_AUDIO);
 }
@@ -304,7 +304,7 @@
 
 // return: how many bytes can be played without blocking
 static int get_space(){
-    return (NUM_BUFS-full_buffers)*BUFFSIZE - buf_write_pos;
+    return NUM_BUFS*BUFFSIZE - buffered_bytes;
 }
 
 // plays 'len' bytes of 'data'
--- a/libao2/ao_win32.c	Thu Apr 01 14:17:58 2004 +0000
+++ b/libao2/ao_win32.c	Thu Apr 01 19:33:58 2004 +0000
@@ -28,15 +28,15 @@
 #include "audio_out_internal.h"
 #include "../mp_msg.h"
 #include "../libvo/fastmemcpy.h"
+#include "osdep/timer.h"
 
-#define SAMPLESIZE   1024
+#define SAMPLESIZE   2048
 #define BUFFER_SIZE  4096
-#define BUFFER_COUNT 16 
+#define BUFFER_COUNT 8
 
 
 static WAVEHDR*     waveBlocks;         //pointer to our ringbuffer memory
 static HWAVEOUT     hWaveOut;           //handle to the waveout device
-static DWORD        restoredvolume;     //saves the volume to restore after playing
 static unsigned int buf_write=0;
 static unsigned int buf_write_pos=0;
 static int          full_buffers=0;
@@ -58,9 +58,12 @@
 {
 	if(uMsg != WOM_DONE)
         return;
-    if(full_buffers==0) return;    //no more data buffered!
-    buffered_bytes-=BUFFER_SIZE;
-    --full_buffers;
+	if (full_buffers) {
+		buffered_bytes-=BUFFER_SIZE;
+		--full_buffers;
+	} else {
+		buffered_bytes=0;
+	}
 }
 
 // to set/get/query special features/parameters
@@ -149,7 +152,6 @@
 		return 0;
     }
     //save volume
-	waveOutGetVolume(hWaveOut,&restoredvolume); 
 	//allocate buffer memory as one big block
 	buffer = malloc(totalBufferSize);
 	memset(buffer,0x0,totalBufferSize);
@@ -157,7 +159,6 @@
     waveBlocks = (WAVEHDR*)buffer;
     buffer += sizeof(WAVEHDR) * BUFFER_COUNT;
     for(i = 0; i < BUFFER_COUNT; i++) {
-        waveBlocks[i].dwBufferLength = BUFFER_SIZE;
         waveBlocks[i].lpData = buffer;
         buffer += BUFFER_SIZE;
     }
@@ -168,11 +169,11 @@
 // close audio device
 static void uninit()
 {
-    waveOutSetVolume(hWaveOut,restoredvolume);  //restore volume
+    while (buffered_bytes > 0)
+	usec_sleep(50000);
 	waveOutReset(hWaveOut);
 	waveOutClose(hWaveOut);
 	mp_msg(MSGT_AO, MSGL_V,"waveOut device closed\n");
-	full_buffers=0;
     free(waveBlocks);
 	mp_msg(MSGT_AO, MSGL_V,"buffer memory freed\n");
 }
@@ -202,7 +203,7 @@
 // return: how many bytes can be played without blocking
 static int get_space()
 {
-    return (BUFFER_COUNT-full_buffers)*BUFFER_SIZE - buf_write_pos;
+    return BUFFER_COUNT*BUFFER_SIZE - buffered_bytes;
 }
 
 //writes data into buffer, based on ringbuffer code in ao_sdl.c
@@ -212,7 +213,7 @@
   int x;
   while(len>0){                       
     current = &waveBlocks[buf_write];
-	if(full_buffers==BUFFER_COUNT) break;  
+    if(buffered_bytes==BUFFER_COUNT*BUFFER_SIZE) break;
     //unprepare the header if it is prepared
 	if(current->dwFlags & WHDR_PREPARED) 
            waveOutUnprepareHeader(hWaveOut, current, sizeof(WAVEHDR));
@@ -222,6 +223,7 @@
     len2+=x; len-=x;                 
 	buffered_bytes+=x; buf_write_pos+=x; 
 	//prepare header and write data to device
+	current->dwBufferLength = buf_write_pos;
 	waveOutPrepareHeader(hWaveOut, current, sizeof(WAVEHDR));
 	waveOutWrite(hWaveOut, current, sizeof(WAVEHDR));