SDL播放PCM音頻數(shù)據(jù)
1.PCM簡(jiǎn)介
	  PCM(Pulse CodeModulation,脈沖編碼調(diào)制)音頻數(shù)據(jù)是未經(jīng)壓縮的音頻采樣數(shù)據(jù)裸流,它是由模擬信號(hào)經(jīng)過采樣、量化、編碼轉(zhuǎn)換成的標(biāo)準(zhǔn)數(shù)字音頻數(shù)據(jù)。
	描述PCM數(shù)據(jù)的6個(gè)參數(shù):
	  1.Sample Rate : 采樣頻率。8kHz(電話)、44.1kHz(CD)、48kHz(DVD)。
	  2.Sample Size : 量化位數(shù)。通常該值為16-bit。
	  3.Number of Channels : 通道個(gè)數(shù)。常見的音頻有立體聲(stereo)和單聲道(mono)兩種類型,立體聲包含左聲道和右聲道。另外還有環(huán)繞立體聲等其它不太常用的類型。
	  4.Sign : 表示樣本數(shù)據(jù)是否是有符號(hào)位,比如用一字節(jié)表示的樣本數(shù)據(jù),有符號(hào)的話表示范圍為-128 ~ 127,無符號(hào)是0 ~ 255。
	  5.Byte Ordering : 字節(jié)序。字節(jié)序是little-endian還是big-endian。通常均為little-endian。
	  6.Integer Or Floating Point : 整形或浮點(diǎn)型。大多數(shù)格式的PCM樣本數(shù)據(jù)使用整形表示,而在一些對(duì)精度要求高的應(yīng)用方面,使用浮點(diǎn)類型表示PCM樣本數(shù)據(jù)。
2. ffmpeg將mp3轉(zhuǎn)pcm
ffmpeg -i audio1.mp3 -f s16le audio1.pcm
3. SDL播放示例
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
static unsigned int audio_len=0;
static unsigned char *audio_pos;
void  AudioCallback(void *userdata, Uint8 * stream,int len)
{
	SDL_memset(stream, 0,len);
	if(audio_len==0)return ;
	len=(len>audio_len?audio_len:len);
	SDL_MixAudio(stream,audio_pos,len,SDL_MIX_MAXVOLUME);
	audio_pos+=len;
	audio_len-=len;
	//printf("len=%dn",len);
}
int main(int argc,char *argv[])
{
	SDL_Init(SDL_INIT_AUDIO|SDL_INIT_TIMER);/*初始化SDL*/
	SDL_AudioSpec desired;
	desired.freq=44100;/*采樣率*/
	desired.format=AUDIO_S16SYS;/*無符號(hào)16位*/
	desired.channels=2;/*雙聲道*/
	desired.samples=1024;/*樣本數(shù)1024*/
	desired.silence=0;/*靜音值*/
	desired.callback=AudioCallback;
	SDL_OpenAudio(&desired,NULL);
	int fd=open("audio.pcm",O_RDWR);
	if(fd<0)
	{
		printf("%s open failedn","audio.pcm");
		return 0;
	}
	struct stat statbuf;
	fstat(fd,&statbuf);
	if(statbuf.st_size<=0)
	{
		printf("audio.pcm size is 0n");
		return 0;
	}
	unsigned char *src_p=mmap(NULL,statbuf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
	close(fd);
	if(src_p==NULL)
	{
		printf("mmap failedn");
		return 0;
	}
	unsigned char *p= src_p;
	
	int pcm_buff_size=1024*2*2;
	unsigned int count=statbuf.st_size;
	char *pcm_buffer=malloc(pcm_buff_size);
	SDL_PauseAudio(0);/*開始播放音頻,1為播放靜音值*/
	while(1)
	{
		if(pcm_buff_size>count)pcm_buff_size=count;
		memcpy(pcm_buffer,p,pcm_buff_size);
		p+=pcm_buff_size;
		count-=pcm_buff_size;
		if(count==0)break;
		audio_len=pcm_buff_size;
		audio_pos=pcm_buffer;
		while(audio_len>0)
		{
			
		}
	}
	SDL_CloseAudio();
	free(pcm_buffer);
	SDL_Quit();
}
4.Makefile文件
CFLAGS =-I/home/wbyq/src_pack/SDL2-2.0.14/_install/include -I/home/wbyq/src_pack/SDL2-2.0.14/_install/include/SDL2 -L/home/wbyq/src_pack/SDL2-2.0.14/_install/lib
CFLAGS +=-L/home/wbyq/src_pack/SDL2_image-2.0.5/_install/lib -I/home/wbyq/src_pack/SDL2_image-2.0.5/_install/include -I/home/wbyq/src_pack/SDL2_image-2.0.5/_install/include/SDL2
CFLAGS+=-lSDL2 -lpthread -lm -ldl -lSDL2_image
app:
	gcc sdl_test.c  $(CFLAGS)
審核編輯:湯梓紅
- 
                                PCM
                                +關(guān)注
關(guān)注
1文章
206瀏覽量
55244 - 
                                SDL
                                +關(guān)注
關(guān)注
0文章
18瀏覽量
7870 - 
                                音頻數(shù)據(jù)
                                +關(guān)注
關(guān)注
0文章
13瀏覽量
10112 
發(fā)布評(píng)論請(qǐng)先 登錄
播放avi視頻時(shí),音頻數(shù)據(jù)該如何處理啊 ?
PCM1865能否通過配置,同時(shí)采集4個(gè)立體聲(即8個(gè)聲道)的音頻數(shù)據(jù)?
PCM1860音頻數(shù)據(jù)傳輸格式不可控怎么處理?
使用PCM4204進(jìn)行音頻數(shù)據(jù)采集的過程中,如何把采集到的音頻數(shù)據(jù)通過USB傳輸?shù)絇C上呢?
請(qǐng)問TLV320AIC3254采樣后的音頻數(shù)據(jù)如何在電腦上播放?
TLV320AIC3106播放有雜音是為什么?如何解決?
dm8168 dvr_rdk4.0 MIC采集到音頻數(shù)據(jù)編碼成aac之后下載到手機(jī)端播放,擴(kuò)音器輸出聲音不正常怎么解決這個(gè)問題?
請(qǐng)問CC3200 I2S總線如何正確播放單聲道音頻數(shù)據(jù)?
BT音頻數(shù)據(jù)可以通過UART傳輸而不是I2S/PCM嗎
關(guān)于音頻數(shù)據(jù)的獲取
Windows Mobile下播放PCM音頻的雙緩沖用法
立體聲音頻數(shù)位類比轉(zhuǎn)換器
    
AD1851/AD1861:16位/18位,16-3-F-ub/sub/sub-PCM音頻數(shù)據(jù)Sheet
    
          
        
        
SDL播放PCM音頻數(shù)據(jù)
                
 
           
            
            
                
            
評(píng)論