ringbuffer剖析

#include <stdio.h>

#define WAVEOUT_BUF_GUARD	  1
#define MAX_QUEUE             1
#define WAVEOUT_BUF_BLK_NUM   16
#define WAVEOUT_BUF_BLK_SIZE  1024


int audio_dma_has_worked = 0;
typedef struct {
	int read;
	int write;
	int lastsize;
	unsigned char *buf;
}_wave_buf;

_wave_buf WaveBuffer;


void Audio_MAL_Play(unsigned int Addr, unsigned int Size)
{   

	int read     = WaveBuffer.read;
	int write     = WaveBuffer.write; 
	int lastsize = WaveBuffer.lastsize; 
	int queued;
	
	if(write >= read) 
		queued = write - read;
	else             
		queued = WAVEOUT_BUF_BLK_NUM - (read - write);
	
	
	if(queued >0)
	{
	
		/*after send data ,we need to update buffer*/
		if(WaveBuffer.read != WaveBuffer.write) 
		{
			WaveBuffer.read ++;
			if(WaveBuffer.read >= WAVEOUT_BUF_BLK_NUM) 
			{
				WaveBuffer.read = 0;	
			}
       	}
	}
}

void WaveOut_Init(void)    
{
	WaveBuffer.read = 0;
	WaveBuffer.write = 0;
	WaveBuffer.lastsize = 0;
	WaveBuffer.buf = (unsigned char *)malloc(WAVEOUT_BUF_BLK_SIZE*WAVEOUT_BUF_BLK_NUM); 
	if( 0 != WaveBuffer.buf )
	{
		printf(" WaveOut_Init malloc successl\n");
		memset(WaveBuffer.buf, 0, WAVEOUT_BUF_BLK_SIZE*WAVEOUT_BUF_BLK_NUM);
	}
	else
	{
		printf(" WaveOut_Init malloc fail\r\n");
		WaveBuffer.buf = NULL;
	}
}

void A2dp_WaveOut_Close()
{
	printf("A2dp_WaveOut_Close\r\n");

	WaveBuffer.read = 0;
	WaveBuffer.write = 0;
	WaveBuffer.lastsize = 0;
	if(0 != WaveBuffer.buf)
	{
		printf("free wave buffer\n");
		free(WaveBuffer.buf);
		WaveBuffer.buf = NULL;
	}

}

void A2dp_WaveOut_GetBuffer(unsigned char **outBufPtr, int *outBufSize)
{

	int read     = WaveBuffer.read;
	int write     = WaveBuffer.write; 
	int lastsize = WaveBuffer.lastsize; 
	int queued;
	
	if((write + 1) % WAVEOUT_BUF_BLK_NUM == read)
	{
		printf("WARNING : ring buffer is FUll\n");

		*outBufPtr  = 0;
		*outBufSize = 0;
	}

	printf("A2dp_WaveOut_GetBuffer read is %d,write is %d,lastsize is %d\n",read,write,lastsize);
	if(write >= read) 
	{
		queued = write - read;
	}
	else             
		queued = WAVEOUT_BUF_BLK_NUM - (read - write);  
	
	*outBufPtr  = WaveBuffer.buf + write*WAVEOUT_BUF_BLK_SIZE + lastsize;
	*outBufSize = WAVEOUT_BUF_BLK_SIZE;
	
}

void A2dp_WaveOut_Write(int waveOutSize)
{
	int queued;
	int read     = WaveBuffer.read;
	int write     = WaveBuffer.write; 	
	int lastsize = WaveBuffer.lastsize;	
	
	printf("A2dp_WaveOut_Write read is %d,write is %d,lastsize is %d\n",read,write,lastsize);
	lastsize += waveOutSize;
	printf("A2dp_WaveOut_Write read is %d,write is %d,lastsize is %d\n",read,write,lastsize);
	
	if(lastsize >= WAVEOUT_BUF_BLK_SIZE) 
	{     
		lastsize -= WAVEOUT_BUF_BLK_SIZE;
		write++;
		if(write >= WAVEOUT_BUF_BLK_NUM)  
		{
			
			//return to the frist buffer 
			if(lastsize) 
			{
				memcpy(WaveBuffer.buf,  WaveBuffer.buf + (WAVEOUT_BUF_BLK_NUM)*WAVEOUT_BUF_BLK_SIZE,  lastsize);
			}
			write = 0;
		}
	}	
	
	WaveBuffer.write     = write;
	WaveBuffer.lastsize = lastsize;

		
	if(write >= read) 
		queued = write - read;
	else             
		queued = WAVEOUT_BUF_BLK_NUM - (read - write);
		
	if(queued >= 1)	
		Audio_MAL_Play((unsigned int)(WaveBuffer.buf + WaveBuffer.read*WAVEOUT_BUF_BLK_SIZE), WAVEOUT_BUF_BLK_SIZE/*/2*/  );
		
	

}

int main()
{
	unsigned char *waveOutBufPtr;
	int waveOutBufSize;
	unsigned char music_data1[222] = {0};
	unsigned char music_data2[888] = {0};
	unsigned char music_data3[1024] = {0};
	unsigned char music_data4[1222] = {0};
	unsigned char music_data5[2500] = {0};
	memset(&music_data1,1,sizeof(music_data1));
	memset(&music_data2,2,sizeof(music_data2));
	memset(&music_data3,3,sizeof(music_data3));
	memset(&music_data4,4,sizeof(music_data4));
	memset(&music_data5,5,sizeof(music_data5));

	WaveOut_Init();
	

	//模拟五次来MUSIC STREAM
	A2dp_WaveOut_GetBuffer(&waveOutBufPtr, &waveOutBufSize);
	if(waveOutBufPtr != NULL)
	{
		memcpy(waveOutBufPtr,music_data1,sizeof(music_data1));
		A2dp_WaveOut_Write(sizeof(music_data1));
	}
	

	A2dp_WaveOut_GetBuffer(&waveOutBufPtr, &waveOutBufSize);
	if(waveOutBufPtr != NULL)
	{
		memcpy(waveOutBufPtr,music_data2,sizeof(music_data2));
		A2dp_WaveOut_Write(sizeof(music_data2));
	}


	A2dp_WaveOut_GetBuffer(&waveOutBufPtr, &waveOutBufSize);
	if(waveOutBufPtr != NULL)
	{
		memcpy(waveOutBufPtr,music_data3,sizeof(music_data3));
		A2dp_WaveOut_Write(sizeof(music_data3));
	}
	
	A2dp_WaveOut_GetBuffer(&waveOutBufPtr, &waveOutBufSize);
	if(waveOutBufPtr != NULL)
	{
		memcpy(waveOutBufPtr,music_data4,sizeof(music_data4));
		A2dp_WaveOut_Write(sizeof(music_data4));
	}

	A2dp_WaveOut_GetBuffer(&waveOutBufPtr, &waveOutBufSize);
	if(waveOutBufPtr != NULL)
	{
		memcpy(waveOutBufPtr,music_data5,sizeof(music_data5));
		A2dp_WaveOut_Write(sizeof(music_data5));
	}

	A2dp_WaveOut_Close();
	return 0;
}

你可能感兴趣的:(C语言,Ringbuffer)