Skip to content

Commit

Permalink
fix noise problem
Browse files Browse the repository at this point in the history
  • Loading branch information
leixiaohua1020 committed Mar 12, 2016
1 parent 647dabe commit 8d2a888
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
12 changes: 7 additions & 5 deletions simplest_audio_play_sdl2/simplest_audio_play_sdl2.cpp
Expand Up @@ -17,9 +17,9 @@
* [初始化]
* SDL_Init(): 初始化SDL。
* SDL_OpenAudio(): 根据参数(存储于SDL_AudioSpec)打开音频设备。
* SDL_PauseAudio(): 播放音频数据。
*
* [循环播放数据]
* SDL_PauseAudio(): 播放音频数据。
* SDL_Delay(): 延时等待播放完成。
*
* This software plays PCM raw audio data using SDL2.
Expand All @@ -32,9 +32,9 @@
* SDL_Init(): Init SDL.
* SDL_OpenAudio(): Opens the audio device with the desired
* parameters (In SDL_AudioSpec).
* SDL_PauseAudio(): Play Audio.
*
* [Loop to play data]
* SDL_PauseAudio(): Play Audio.
* SDL_Delay(): Wait for completetion of playback.
*/

Expand Down Expand Up @@ -97,11 +97,14 @@ int main(int argc, char* argv[])
printf("cannot open this file\n");
return -1;
}
//For YUV420P

int pcm_buffer_size=4096;
char *pcm_buffer=(char *)malloc(pcm_buffer_size);
int data_count=0;

//Play
SDL_PauseAudio(0);

while(1){
if (fread(pcm_buffer, 1, pcm_buffer_size, fp) != pcm_buffer_size){
// Loop
Expand All @@ -116,8 +119,7 @@ int main(int argc, char* argv[])
//Audio buffer length
audio_len =pcm_buffer_size;
audio_pos = audio_chunk;
//Play
SDL_PauseAudio(0);

while(audio_len>0)//Wait until finish
SDL_Delay(1);
}
Expand Down
16 changes: 8 additions & 8 deletions simplest_ffmpeg_audio_player/simplest_ffmpeg_audio_player.cpp
Expand Up @@ -81,8 +81,9 @@ static Uint8 *audio_pos;
void fill_audio(void *udata,Uint8 *stream,int len){
//SDL 2.0
SDL_memset(stream, 0, len);
if(audio_len==0) /* Only play if we have data left */
return;
if(audio_len==0)
return;

len=(len>audio_len?audio_len:len); /* Mix as much data as possible */

SDL_MixAudio(stream,audio_pos,len,SDL_MIX_MAXVOLUME);
Expand Down Expand Up @@ -110,7 +111,7 @@ int main(int argc, char* argv[])
struct SwrContext *au_convert_ctx;

FILE *pFile=NULL;
char url[]="WavinFlag.aac";
char url[]="xiaoqingge.mp3";

av_register_all();
avformat_network_init();
Expand Down Expand Up @@ -208,6 +209,9 @@ int main(int argc, char* argv[])
in_channel_layout,pCodecCtx->sample_fmt , pCodecCtx->sample_rate,0, NULL);
swr_init(au_convert_ctx);

//Play
SDL_PauseAudio(0);

while(av_read_frame(pFormatCtx, packet)>=0){
if(packet->stream_index==audioStream){
ret = avcodec_decode_audio4( pCodecCtx, pFrame,&got_picture, packet);
Expand Down Expand Up @@ -239,8 +243,6 @@ int main(int argc, char* argv[])
audio_len =out_buffer_size;
audio_pos = audio_chunk;

//Play
SDL_PauseAudio(0);
#endif
}
av_free_packet(packet);
Expand All @@ -252,14 +254,12 @@ int main(int argc, char* argv[])
SDL_CloseAudio();//Close SDL
SDL_Quit();
#endif
// Close file

#if OUTPUT_PCM
fclose(pFile);
#endif
av_free(out_buffer);
// Close the codec
avcodec_close(pCodecCtx);
// Close the video file
avformat_close_input(&pFormatCtx);

return 0;
Expand Down
Binary file added simplest_ffmpeg_audio_player/xiaoqingge.mp3
Binary file not shown.
Binary file modified simplest_ffmpeg_audio_player2.suo
Binary file not shown.

0 comments on commit 8d2a888

Please sign in to comment.