FFMPEG学习

参考雷神的代码:

1/** 2* 最简单的SDL2播放音频的例子(SDL2播放PCM) 3* Simplest Audio Play SDL2 (SDL2 play PCM) 4* 5* 本程序使用SDL2播放PCM音频采样数据。SDL实际上是对底层绘图 6* API(Direct3D,OpenGL)的封装,使用起来明显简单于直接调用底层 7* API。 8* 9* 函数调用步骤如下: 10* 11* [初始化] 12* SDL_Init(): 初始化SDL。 13* SDL_OpenAudio(): 根据参数(存储于SDL_AudioSpec)打开音频设备。 14* SDL_PauseAudio(): 播放音频数据。 15* 16* [循环播放数据] 17* SDL_Delay(): 延时等待播放完成。 18* 19* This software plays PCM raw audio data using SDL2. 20* SDL is a wrapper of low-level API (DirectSound). 21* Use SDL is much easier than directly call these low-level API. 22* 23* The process is shown as follows: 24* 25* [Init] 26* SDL_Init(): Init SDL. 27* SDL_OpenAudio(): Opens the audio device with the desired 28* parameters (In SDL_AudioSpec). 29* SDL_PauseAudio(): Play Audio. 30* 31* [Loop to play data] 32* SDL_Delay(): Wait for completetion of playback. 33*/ 34 35#include <stdio.h> 36#include <tchar.h> 37 38extern "C" 39{ 40#include "SDL.h" 41}; 42 43Uint32 audio_len;//音频数据大小 44Uint8 *audio_pos;//指向音频数据的指针 45 46 47/**回调函数(由系统调用) 48* 函数声明:typedef void (SDLCALL * SDL_AudioCallback) 49* (void *userdata, Uint8 * stream, int len); 50* This function is called when the audio device needs more data. 51* 52* userdata: An application-specific parameter saved in the SDL_AudioSpec structure(SDL_AudioSpec结构中的用户自定义数据,一般情况下可以不用) 53* stream: A pointer to the audio data buffer.(该指针指向需要填充的音频缓冲区) 54* len: The length of that buffer in bytes.(音频缓冲区的大小,以字节为单位) 55* 56* Once the callback returns, the buffer will no longer be valid. 57* Stereo samples are stored in a LRLRLR ordering. 58* 59* You can choose to avoid callbacks and use SDL_QueueAudio() instead, if 60* you like. Just open your audio device with a NULL callback. 61*/ 62 63void fill_audio(void *userdata, Uint8 *stream, int len) 64{ 65 //SDL2中必须首先使用SDL_memset()将stream中的数据设置为0 66 SDL_memset(stream, 0, len); 67 if (audio_len == 0) /* Only play if we have data left */ 68 { 69 return; 70 } 71 len = (len > audio_len ? audio_len : len); /* Mix as much data as possible */ 72 73 /** 74 * 函数声明:extern DECLSPEC void SDLCALL 75 * SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume); 76 * This takes two audio buffers of the playing audio format and mixes 77 * them, performing addition, volume adjustment, and overflow clipping. 78 * The volume ranges from 0 - 128, and should be set to ::SDL_MIX_MAXVOLUME 79 * for full audio volume. Note this does not change hardware volume. 80 * This is provided for convenience -- you can mix your own audio data. 81 * 82 * #define SDL_MIX_MAXVOLUME 128 83 */ 84 85 SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME); 86 audio_pos += len; 87 audio_len -= len; 88} 89 90int main(int argc, char* argv[]) 91{ 92 //初始化SDL 93 if (SDL_Init(SDL_INIT_AUDIO)) 94 { 95 printf("Could not initialize SDL - %s\n", SDL_GetError()); 96 return -1; 97 } 98 99 //SDL_AudioSpec初始化 100 SDL_AudioSpec wanted_spec; 101 wanted_spec.freq = 44100; //音频数据的采样率(常用的有48000,44100等) 102 wanted_spec.format = AUDIO_S16SYS;//音频数据的格式 103 wanted_spec.channels = 2; //声道数(例如单声道取值为1,立体声取值为2) 104 wanted_spec.silence = 0; //设置静音的值 105 wanted_spec.samples = 1024; //音频缓冲区中的采样个数(要求必须是2的n次方) 106 wanted_spec.callback = fill_audio;//填充音频缓冲区的回调函数 107 108 //打开音频 109 if (SDL_OpenAudio(&wanted_spec, NULL) < 0) 110 { 111 printf("can't open audio.\n"); 112 return -1; 113 } 114 115 FILE *fp_pcm = fopen("..\\FFmpeg_PCM\\output.pcm", "rb"); 116 if (fp_pcm == NULL) 117 { 118 printf("cannot open this file\n"); 119 return -1; 120 } 121 122 int pcm_buffer_size = 4096;//每次读取4096字节的数据,同时也是音频帧大小 123 char *pcm_buffer = (char *)malloc(pcm_buffer_size); 124 int data_count = 0; 125 126 //播放音频数据 127 SDL_PauseAudio(0); 128 129 while (true) 130 { 131 int ret = fread(pcm_buffer, 1, pcm_buffer_size, fp_pcm); 132 if (ret != pcm_buffer_size) 133 { 134 //这里有可能是会有剩余音频数据的,不知道这样改对不对? 135 audio_pos = (Uint8 *)pcm_buffer; 136 audio_len = ret; 137 while (audio_len > 0) 138 { 139 SDL_Delay(1); 140 } 141 142 //退出 143 break; 144 145 //循环播放 146 fseek(fp_pcm, 0, SEEK_SET); 147 fread(pcm_buffer, 1, pcm_buffer_size, fp_pcm); 148 data_count = 0; 149 } 150 printf("Now Playing %10d Bytes data.\n", data_count); 151 data_count += pcm_buffer_size; 152 153 154 audio_pos = (Uint8 *)pcm_buffer; 155 //Audio buffer length 156 audio_len = pcm_buffer_size; 157 158 159 while (audio_len > 0)//Wait until finish 160 { 161 SDL_Delay(1); 162 } 163 } 164 free(pcm_buffer); 165 fclose(fp_pcm); 166 SDL_Quit(); 167 return 0; 168}

雷神代码中if (fread(pcm_buffer, 1, pcm_buffer_size, fp) != pcm_buffer_size){}
如果获取不到4096个字节的音频数据,就从头接着播放了,当然这意味着读到末尾了,但是剩下的音频数据没有做处理?

疑问:

wanted_spec.silence = 0; //设置静音的值
wanted_spec.samples = 1024; //音频缓冲区中的采样个数(要求必须是2的n次方)

int pcm_buffer_size = 4096;//每次读取4096字节的数据,同时也是音频帧大小

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )