SDL音频播放
来源:互联网 发布:mac如何无线投射电视 编辑:程序博客网 时间:2024/05/21 10:43
`sdl-config --cflags --libs`
AUDIO名词解释:
samples:采样,通过PCM来采样,通常样本16bit,PCM的采样精度从14-bit发展到16-bit、18-bit、20-bit直到24-bit
Samplesrate:采样率,22.05KHz and 44.1KHz,每秒从连续信号中提取并组成离散信号的采样个数
位速:采样率*样本bit*通道数,CD上未经压缩的音频位速是1411.2 kbit/s(16 位/采样点 × 44100 采样点/秒× 2 通道)
pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO, 在for循环找找到audiostream的索引i
AVCodecContext*aCodecCtx=pFormatCtx->streams[audioStream]->codec;得到音频编码的信息
SDL_AudioSpec
wanted_spec.freq =aCodecCtx->sample_rate;
wanted_spec.format = AUDIO_S16SYS;
//告诉SDL使用什么格式,S指代signed,16为样本16bit,SYS指代大小端由系统决定
wanted_spec.channels = aCodecCtx->channels;//有多少个通道
wanted_spec.silence = 0; //silence值,由于为signed,故为0
wanted_spec.samples =1024; //缓存大小
wanted_spec.callback = audio_callback; //音频的回调函数
wanted_spec.userdata = aCodecCtx; //给回调函数处理的数据
SDL_OpenAudio(&wanted_spec,&spec)
返回-1则打开失败,spec为NULL则以wanted_spec指定的方式播放,若spec不为NULL,则使用根据硬件改变的spec指定的方式播放,而wanted_spec可以删除
VCodec
avcodec_open(aCodecCtx, aCodec);
找到解码器,并进行解码
typedef struct PacketQueue {
AVPacketList *first_pkt, *last_pkt;
intnb_packets;
int size;
SDL_mutex *mutex; //互斥锁
SDL_cond *cond; //条件变量
} PacketQueue;
我们自己创建的用于构建Packet队列的数据结构
AVPacketList
A simple linked list for packets.
AVPacket pkt
AVPacketList * next
void packet_queue_init(PacketQueue *q)
{
}
对PacketQueue数据结构进行初始化
用于给PacketQueue数据结构中填入包的函数
int
{
}
接收数据
static int packet_queue_get(PacketQueue *q, AVPacket *pkt, intblock)
{