音频采样率 wav->aac

71 篇文章 4 订阅

音频采样率

1.采样率sample_rate 44100(CD)
2.通道channels (左右声道)
3.样本大小(格式) sample_size

-AV_SAMPLE_FMT_S16
-AV_SAMPLE_FMT_FLTP

wav_to_aac.cpp

extern "C"
{
	#include <libavformat/avformat.h>
	#include <libswscale/swscale.h>
	#include <libswresample/swresample.h>
}
#include <iostream>
using namespace std;
int main()
{
	char infile[] = "16.wav";
	char outfile[] = "out.aac";

	//muxer,demuters
	av_register_all();
	avcodec_register_all();

	//1.打开音频编码格式
	AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
	if(!codec)
	{
		cout<<"avcodec_find_encoder error"<<endl;
		return -1;
	}
	AVCodecContext *c = avcodec_alloc_context3(codec);
	if(!c)
	{
		cout<<"avcodec_alloc_context3 error"<<endl;
		return -1;
	}
	c->bit_rate = 64000;
	c->sample_rate = 44100;
	c->sample_fmt = AV_SAMPLE_FMT_FLTP;
	c->channel_layout = AV_CH_LAYOUT_STEREO;
	c->channels = 2;

	c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

	int ret = avcodec_open2(c,codec,NULL);
	if(ret < 0)
	{
		cout<<"avcodec_open2 error"<<endl;
		return -1;
	}
	cout<<"avcodec_open2 success"<<endl;

	//2. 打开输出封装的上下文
	AVFormatContext *oc = NULL;
	avformat_alloc_output_context2(&oc,NULL,NULL,outfile);
	if(!oc)
	{
		cout<<"avformat_alloc_output_context2 error "<<endl;
		return -1;
	}

	AVStream *st = avformat_new_stream(oc,NULL);
	st->codecpar->codec_tag = 0;
	avcodec_parameters_from_context(st->codecpar,c);

	av_dump_format(oc,0,outfile,1);

	//3. open io, write head
	ret = avio_open(&oc->pb,outfile,AVIO_FLAG_WRITE);
	if(ret < 0)
	{
		cout<<"avio_open error"<<endl;
		return -1;
	} 
	ret = avformat_write_header(oc,NULL);

	//44100 16 2
	//5 创建音频重采样上下文
	SwrContext *actx = NULL;
	actx = swr_alloc_set_opts(actx,
	c->channel_layout,c->sample_fmt,c->sample_rate,//输出格式
	 AV_CH_LAYOUT_STEREO,AV_SAMPLE_FMT_S16,44100, //输入格式
					   0,0);
	if(!actx)
	{
		cout<<"swr_alloc_set_opts error "<<endl;
		return -1;
	}

	std::cout<<"swr_alloc_set_opts success"<<endl;
	ret = swr_init(actx);
	if(ret < 0)
	{
		cout<<"swr init error"<<endl;
	}
	cout<<"swr_init success "<<endl;
	
	//5 打开输入音频文件,进行重采样
	AVFrame *frame = av_frame_alloc();
	frame->format = AV_SAMPLE_FMT_FLTP;
	frame->channels = 2;
	frame->channel_layout = AV_CH_LAYOUT_STEREO;
	frame->nb_samples = 1024;//一帧音频的样本数量
	ret = av_frame_get_buffer(frame,0);
	
	if(ret < 0)
	{
		cout<<"av_frame_get_buffer error"<<endl;
		return -1;
	}
	std::cout<<"av_frame_get_buffer success"<<endl;
	int readSize = frame->nb_samples * 2 * 2;

	char  *pcm = new char[readSize];

	FILE *fp = fopen(infile,"rb");
	std::cout<<fp;
	for(;;)
	{
		int len = fread(pcm,1,readSize,fp);
		if(len < 0) break;
		const uint8_t *data[1];
		data[0] = (uint8_t *)pcm;

		swr_convert(actx,frame->data,frame->nb_samples,
					data,frame->nb_samples);
		if(len <= 0)
			break;

		AVPacket pkt;
		av_init_packet(&pkt);

		//6 音频编码
		ret = avcodec_send_frame(c,frame);
		if(ret != 0) continue;
		ret = avcodec_receive_packet(c,&pkt);
		if(ret != 0) continue;

		//7.音频封装如acc文件
		pkt.stream_index = 0;
		pkt.pts = 0;
		pkt.dts = 0;
		ret = av_interleaved_write_frame(oc,&pkt);

		cout<<"["<<len<<"]";
	}
	delete pcm;

	//写入视频索引
	av_write_trailer(oc);

	//关闭视频输出
	avio_close(oc->pb);

	//清理封装输出上下文
	avformat_free_context(oc);

	//关闭编码器
	avcodec_close(c);

	//清理编码器上下文
	avcodec_free_context(&c);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VS1005的数模转换器数据通路包含一个全数字,无PLL且无抖动的24位采样率转换器,可重新采样音频至6.144 MHz。在这个高频率时,音频信号被转换到模拟域,并通过VS1005的集成模拟立体声耳机放大器输出。VS1005高分辨率播放器是音频播放器的一个例子,它可以播放许多高分辨率音频格式,采用小巧精致的封装和较少的元件数量。该播放器既可以作为独立单元使用,也可以作为微控制器的协处理器,通过易于使用的UART Shell界面进行控制。它旨在作为客户自身高分辨率音频产品性能评估和开发的基础。 支持的高分辨率无损音频解码器的亮点包括ALAC,FLAC,AIFF,RIFF WAV,DXD和DSD(详情请参阅上面的功能列表)。除了这些格式之外,还支持流行的压缩音频格式,如MP3,Ogg Vorbis,WMA和AAC。解码后,音频既可以转发到VS1005的集成DAC,也可以数字转发到外部解码器。 包括DSD在内的所有音频都可以通过VS1005的高质量,无抖动的24位重采样硬件转换为96 kHz,以实现最高音频保真度和兼容性,并发送到光纤S / PDIF和I2S总线进行外部解码带放大器。 实物截图: 特性: 基于混合信号VS1005 SoC,可实现较少的元件数量 来自SD卡的高质量音频播放 支持多种高分辨率音频格式:ALAC(Apple Lossless)高达96 kHz 24位 FLAC(免费无损音频编解码器)高达96 kHz 24位 AIFF(Apple PCM)最高可达352 kHz 32位浮点 RIFF WAV(微软PCM)高达352 kHz 32位浮点 RIFF WAV容器中的DXD格式,最高可达352 kHz 32位浮点 DSD高达DSD256,11.3 MHz 1位与其他一些DSD实现不同,我们的包含正常的音量控制 还支持许多流行的压缩音频格式: Ogg Vorbis高达96 kHz,1000 kbit / s MP3高达48 kHz,320 kbit / s AAC高达48 kHz,280 kbit / s WMA高达48 kHz 集成DAC和100 dB动态范围的耳机放大器 用于外部DAC和放大器的S / PDIF输出,使用VS1005的高品质硬件采样率转换器将音频数据自动重新采样至96 kHz,以获得最佳音频质量 使用3.7 V 2500 mAh电池的播放时间长达120小时 底部连接器为模拟和数字输入和输出提供信号,用于外部ADC / DAC,扩展坞或微控制器 播放器和操作系统VSOS的完整源代码免费提供 VSIDE环境可以免费修改操作系统或播放器代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值