FFMPEG学习----使用SDL播放PCM数据
参考雷神的代码:
/**
* 最简单的SDL2播放音频的例子(SDL2播放PCM)
* Simplest Audio Play SDL2 (SDL2 play PCM)
*
* 本程序使用SDL2播放PCM音频采样数据。SDL实际上是对底层绘图
* API(Direct3D,OpenGL)的封装,使用起来明显简单于直接调用底层
* API。
*
* 函数调用步骤如下:
*
* [初始化]
* SDL_Init(): 初始化SDL。
* SDL_OpenAudio(): 根据参数(存储于SDL_AudioSpec)打开音频设备。
* SDL_PauseAudio(): 播放音频数据。
*
* [循环播放数据]
* SDL_Delay(): 延时等待播放完成。
*
* This software plays PCM raw audio data using SDL2.
* SDL is a wrapper of low-level API (DirectSound).
* Use SDL is much easier than directly call these low-level API.
*
* The process is shown as follows:
*
* [Init]
* 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_Delay(): Wait for completetion of playback.
*/ #include <stdio.h>
#include <tchar.h> extern "C"
{
#include "SDL.h"
}; Uint32 audio_len;//音频数据大小
Uint8 *audio_pos;//指向音频数据的指针 /**回调函数(由系统调用)
* 函数声明:typedef void (SDLCALL * SDL_AudioCallback)
* (void *userdata, Uint8 * stream, int len);
* This function is called when the audio device needs more data.
*
* userdata: An application-specific parameter saved in the SDL_AudioSpec structure(SDL_AudioSpec结构中的用户自定义数据,一般情况下可以不用)
* stream: A pointer to the audio data buffer.(该指针指向需要填充的音频缓冲区)
* len: The length of that buffer in bytes.(音频缓冲区的大小,以字节为单位)
*
* Once the callback returns, the buffer will no longer be valid.
* Stereo samples are stored in a LRLRLR ordering.
*
* You can choose to avoid callbacks and use SDL_QueueAudio() instead, if
* you like. Just open your audio device with a NULL callback.
*/ void fill_audio(void *userdata, Uint8 *stream, int len)
{
//SDL2中必须首先使用SDL_memset()将stream中的数据设置为0
SDL_memset(stream, 0, len);
if (audio_len == 0) /* Only play if we have data left */
{
return;
}
len = (len > audio_len ? audio_len : len); /* Mix as much data as possible */ /**
* 函数声明:extern DECLSPEC void SDLCALL
* SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume);
* This takes two audio buffers of the playing audio format and mixes
* them, performing addition, volume adjustment, and overflow clipping.
* The volume ranges from 0 - 128, and should be set to ::SDL_MIX_MAXVOLUME
* for full audio volume. Note this does not change hardware volume.
* This is provided for convenience -- you can mix your own audio data.
*
* #define SDL_MIX_MAXVOLUME 128
*/ SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
audio_pos += len;
audio_len -= len;
} int main(int argc, char* argv[])
{
//初始化SDL
if (SDL_Init(SDL_INIT_AUDIO))
{
printf("Could not initialize SDL - %s\n", SDL_GetError());
return -1;
} //SDL_AudioSpec初始化
SDL_AudioSpec wanted_spec;
wanted_spec.freq = 44100; //音频数据的采样率(常用的有48000,44100等)
wanted_spec.format = AUDIO_S16SYS;//音频数据的格式
wanted_spec.channels = 2; //声道数(例如单声道取值为1,立体声取值为2)
wanted_spec.silence = 0; //设置静音的值
wanted_spec.samples = 1024; //音频缓冲区中的采样个数(要求必须是2的n次方)
wanted_spec.callback = fill_audio;//填充音频缓冲区的回调函数 //打开音频
if (SDL_OpenAudio(&wanted_spec, NULL) < 0)
{
printf("can't open audio.\n");
return -1;
} FILE *fp_pcm = fopen("..\\FFmpeg_PCM\\output.pcm", "rb");
if (fp_pcm == NULL)
{
printf("cannot open this file\n");
return -1;
} int pcm_buffer_size = 4096;//每次读取4096字节的数据,同时也是音频帧大小
char *pcm_buffer = (char *)malloc(pcm_buffer_size);
int data_count = 0; //播放音频数据
SDL_PauseAudio(0); while (true)
{
int ret = fread(pcm_buffer, 1, pcm_buffer_size, fp_pcm);
if (ret != pcm_buffer_size)
{
//这里有可能是会有剩余音频数据的,不知道这样改对不对?
audio_pos = (Uint8 *)pcm_buffer;
audio_len = ret;
while (audio_len > 0)
{
SDL_Delay(1);
} //退出
break; //循环播放
fseek(fp_pcm, 0, SEEK_SET);
fread(pcm_buffer, 1, pcm_buffer_size, fp_pcm);
data_count = 0;
}
printf("Now Playing %10d Bytes data.\n", data_count);
data_count += pcm_buffer_size; audio_pos = (Uint8 *)pcm_buffer;
//Audio buffer length
audio_len = pcm_buffer_size; while (audio_len > 0)//Wait until finish
{
SDL_Delay(1);
}
}
free(pcm_buffer);
fclose(fp_pcm);
SDL_Quit();
return 0;
}
雷神代码中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字节的数据,同时也是音频帧大小
FFMPEG学习----使用SDL播放PCM数据的更多相关文章
- FFMPEG学习----使用SDL播放YUV数据
命令行下配置: G:\Coding\Video\SDL\proj>tree /F 文件夹 PATH 列表 卷序列号为 0FD5-0CC8 G:. │ sdl.cpp │ SDL2.dll │ S ...
- FFmpeg学习3:播放音频
参考dranger tutorial,本文将介绍如何使用FFmpeg解码音频数据,并使用SDL将解码后的数据输出. 本文主要包含以下几方面的内容: 关于播放音频的需要的一些基础知识介绍 使用SDL2播 ...
- ffplay代码播放pcm数据
摘抄雷兄 http://blog.csdn.net/leixiaohua1020/article/details/46890259 /** * 最简单的SDL2播放音频的例子(SDL2播放PCM) * ...
- ffmpeg学习笔记-音频播放
前文讲到音频解码,将音频解码,并且输入到PCM文件,这里将音频通过AudioTrack直接输出 音频播放说明 在Android中自带的MediaPlayer也可以对音频播放,但其支持格式太少 使用ff ...
- FFMPEG学习----使用SDL构建音频播放器
ffmpeg版本:ffmpeg-20160413-git-0efafc5 #include <stdio.h> #include <stdlib.h> #include < ...
- 使用 audioqueue 播放PCM数据
// // MainViewController.h // RawAudioDataPlayer // // Created by SamYou on 12-8-18. // Copyright (c ...
- FFMPEG学习----使用SDL构建视频播放器
#include <stdio.h> #include <string.h> extern "C" { #include "libavcodec/ ...
- Android OpenSL ES 开发:使用 OpenSL 播放 PCM 数据
OpenSL ES 是基于NDK也就是c语言的底层开发音频的公开API,通过使用它能够做到标准化, 高性能,低响应时间的音频功能实现方法. 这次是使用OpenSL ES来做一个音乐播放器,它能够播放m ...
- SDL 开发实战(七): 使用 SDL 实现 PCM播放器
在上文,我们做了YUV播放器,这样我们就入门了SDL播放视频.下面我们来做一个PCM播放,即使用SDL播放PCM数据. 下面说明一下使用SDL播放PCM音频的基本流程,主要分为两大部分:初始化SDL. ...
随机推荐
- Linux开发环境及应用—《第一、二周单元测验》
一单元 使用more命令逐屏显示文本文件时,使得显示内容上滚一行而不是滚动一屏,应按下哪个键? 回车 Linux中用来实现计数功能,比如:统计系统有多少个登录用户,实现计数功能的命令是: wc -l ...
- 1025 反转链表 (25 分)C语言
题目描述 给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转.例如:给定L为1→2→3→4→5→6,K为3,则输出应该为 3→2→1→6→5→4:如果K为4,则输出应该为4→3→2→1→5 ...
- linux条件变量使用和与信号量的区别
近来在项目中用到条件变量和信号量做同步时,这一块一直都有了解,但也一直没有总结,这次总结一下,给大家提供点参考,也给自己留点纪念. 首先,关于信号量和条件变量的概念可以自行查看APUE,我这直接把AP ...
- 道格拉斯-普克算法(JavaScript实现)
需求: 有时候当移动速度很慢,GPS定位的轨迹点就非常的多,这时候为了缩减数据量,需要将不突出的点去掉. 思路: (1) 在曲线首尾两点间虚连一条直线,求出其余各点到该直线的距离. (2)选其最大者与 ...
- 【转】常见Java面试题 – 第四部分:迭代(iteration)和递归(recursion)
ImportNew注: 本文是ImportNew编译整理的Java面试题系列文章之一.你可以从这里查看全部的Java面试系列. Q.请写一段代码来计算给定文本内字符“A”的个数.分别用迭代和递归两种方 ...
- Deferred shading rendering path翻译
Overview 概述 When using deferred shading, there is no limit on the number of lights that can affect a ...
- 我怎么感觉 ConcurrentDictionary<,> 不是线程安全的喃?
直接上代码 class Program { static readonly ConcurrentDictionary<string, Person> Dic = new Concurren ...
- target 和 currentTarget的区别
target是当前点击的组件,currentTarget是扑捉到事件的组件
- Oracle GoldenGate 12.3微服务架构指北
Microservices Architecture introduction Microservices Architecture is a method or approach to develo ...
- php代码没解析成功
在Apache中加载PHP模块 1.打开Apache的配置文件httpd.conf(位于Apache2\conf 目录下). 2.查找 “#LoadModule ssl_module modules/ ...