使用LibMad解码MP3,Windows上播放MP3,MP3转WAV实例代码

关于libmad开源mp3音频解码库的使用 MAD(libmad)是一个开源的高精度MPEG音频解码库。libmad提供24-bit的PCM输出,完全定点计算。使用libmad提供的一系列API可以实现MP3文件的解码。 这里仅介绍这个库的相关使用方法。 1,首先下载libmad的源码包 libmad-0.15.0b.tar.gz并解压 2.在解压出来的目录下编译规则文件Makefile, ./comfigure -prefix /usr 阅读详情

概述:

闲着也是闲着,就学习了下LibMad解码MP3(解码成PCM数据流),顺便把Windows上播放PCM,以及PCM文件转换成WAV文件学习了下。以前做过PCM转WAV,原理很简单,就是在PCM流前面加上一个WAV数据头。借鉴了网上的一些文章,感谢大家。

LIbMad解码部分:

在初始化LibMad前,我们需要定义回调函数

//读取mp3资源的函数
mad_flow input_fun(void *data, struct mad_stream *stream)
{
	MP3File* pMp3 = (MP3File*)data;
	if ( stream->buffer == NULL )
	{
		mad_stream_buffer(stream, pMp3->szBuffer, pMp3->nBufferUse);
		return MAD_FLOW_CONTINUE;
	}

	pMp3->nBufferPos = stream->next_frame - pMp3->szBuffer;
	int nRemain = pMp3->nBufferUse - pMp3->nBufferPos;
	memcpy(pMp3->szBuffer, pMp3->szBuffer+pMp3->nBufferPos, nRemain);
	size_t nRead = fread(pMp3->szBuffer+nRemain, 1, pMp3->nBufferPos, pMp3->fp);
	if ( nRead <= 0 )
	{
		return MAD_FLOW_STOP;
	}
	pMp3->nBufferPos = 0;
	pMp3->nBufferUse = nRemain + nRead;
	mad_stream_buffer(stream, pMp3->szBuffer, pMp3->nBufferUse);
	return MAD_FLOW_CONTINUE;
}

//处理mp3头部信息的函数
mad_flow header_fun(void *data, struct mad_header const *header)
{
	return MAD_FLOW_CONTINUE;
}

////输出函数
mad_flow output_fun(void *data, struct mad_header const *header, struct mad_pcm *pcm)
{
	int *left_ch = pcm->samples[0], *right_ch = pcm->samples[1];
	int nLength = pcm->length;
	int nMalloc = nLength*2;
	if (pcm->channels == 2)//双声道,存储空间扩大4倍
		nMalloc = nLength * 4;
	if ( g_pBuffer == NULL )
		g_pBuffer = (unsigned char*)malloc(nMalloc);
	else
	{
		if ( g_nSize<nMalloc )
		{
			free(g_pBuffer);
			g_pBuffer = (unsigned char*)malloc(nMalloc);
		}
	}
	g_nSize = nMalloc;
	unsigned char* pBase = g_pBuffer;
	for (int i = 0; i < nLength; ++i)
	{
		signed int sample = scale(*left_ch++);
		*(pBase++) = sample >> 0;
		*(pBase++) = sample >> 8;
		if (pcm->channels == 2)
		{
			signed int sample = scale(*right_ch++);
			*(pBase++) = sample >> 0;
			*(pBase++) = sample >> 8;
		}
	}
	//播放
	g_pcmPlayer.Play(g_pBuffer, nMalloc);

// 	FILE* fp = fopen("mp3.dat", "ab+");
// 	if( fp )
// 	{
// 		fwrite(pBase, 1, nMalloc, fp);
// 		fclose(fp);
// 	}
	g_nFrames++;
	//g_nTime += header->duration;
	return MAD_FLOW_CONT
ESP32+ESP-IDF实战:从SD卡读取MP3并I2S解码播放 嵌入式音频播放是物联网与智能硬件开发中的常见需求,其核心链路涉及音频解码、数据传输与硬件接口协同。MP3作为一种有损压缩格式,通过心理声学模型剔除人耳不敏感信息,解码时需将压缩比特流还原为PCM数据,再经I2S接口送往外置功放驱动扬声器。在ESP32平台上,利用ESP-IDF框架可精细控制SD卡读取、环形缓冲、DMA传输及FreeRTOS多任务调度,实现稳定流畅的音乐播放。本文从音频数据通路与缓冲机制出发,解析MP3软解码原理,并给出基于libhelix的工程实现方法,适用于智能音箱、便携播放器及语音交互设 阅读详情

相关推荐

RT-Thread Radio开发套件深度解析:从硬件选型到内核调试

实时操作系统(RTOS)是嵌入式系统的核心软件平台,它通过任务调度、内存管理和进程间通信等机制,为资源受限的微控制器提供多任务并发执行能力。其技术价值在于能够高效管理硬件资源,确保关键任务的实时响应,从而广泛应用于物联网设备、工业控制和消费电子等领域。本文以RT-Thread Radio网络播放器开发套件为具体案例,深入剖析了其基于STM32F103的硬件架构设计,包括网络、音频、存储等关键外设的协同原理。同时,重点探讨了如何利用RT-Thread内置的Finsh Shell工具进行动态系统状态诊断和实时调

weixin_30947043的博客 403

易语言WAV转换到MP3源码-易语言

易语言WAV转换到MP3源码

易语言WAV特定音频生成器源码-易语言

易语言WAV特定音频生成器源码

音频转换大师1.3源码-易语言

支持:mp3 \ flac \ ape \ m4a 格式压制 输入文件支持:p3 \ flac \ ape \ m4a \ wav 支持:mp3 \ flac \ ape \ m4a 格式解码成wav 支持 vista \ win7 \win10 aero效果

【低空经济】多源异构数据融合的低空智能感知系统设计与实现

本文探讨了低空智能感知系统在多源异构数据融合中的设计与实现,重点解决无人机等低空飞行器的安全监测问题。通过雷达、光学和红外传感器的数据融合,系统识别准确率提升至92%,并实现120ms的低延迟处理。文章详细介绍了时空对齐、特征级融合和动态权重自适应等关键技术,为低空经济发展提供可靠技术支持。

weixin_29065659的博客 342

SigmaStudio进阶技巧:如何用ADAU1701实现60段EQ与防啸叫算法

本文深入探讨了如何在ADAU1701 DSP芯片上实现专业级音频处理系统。通过精密的资源规划与架构设计,详细讲解了构建高达60段参数均衡器(EQ)的有效方法,并集成了移频与自适应陷波双策略的智能防啸叫算法。内容涵盖从SigmaStudio工程优化、算法模块组合到系统联调实战的全流程,旨在帮助中高级开发者充分利用ADAU1701性能,打造稳定、高效的音频处理解决方案。

weixin_28256233的博客 385

skill使用教程,从小白到大神进阶

(2)突然无法使用(NodeJS安装参考):如果正常使用中,突然发现claude命令无法启动claude-code了,可能是claude-code正在后台自动更新,等待几分钟后再尝试使用,如果还是不行,请先运行下列代码移除claude-code,注意,如果你的node安装目录不同,要替换成自己的安装目录:rm -rf /opt/homebrew/lib/node_modules/@anthropic-ai/claude-code。安装完成后,让我们启动 Claude Code,看看它长什么样。

weixin_45036829的博客 2010

msf使用木马控制android手机

使用msfvenom生成木马控制手机

my_name_is_sy的博客 3522

Ubuntu系统下MRIcroN、FSL6.0.5、Freesurfer7.1.1、ANTs、MRtrix3、AFNI的安装详解——核磁共振数据处理必备

Ubuntu系统下MRIcroN、FSL6.05、Freesurfer7.1.1、MRtrix3、AFNI、ANTs的安装详解一、安装MRIcroN二、 FSL6.0.5完整安装与FSLeyes等Not Found修复Anaconda与虚拟环境(所有操作均在root下)获取最新版Anaconda3安装Anaconda3配置所有用户可用去掉恼人的base创建虚拟环境FSL与FSLeyes安装(所有操作均在root下)下载FSL安装FSL添加环境变量到/etc/bash.bashrc末尾 (ubuntu)或者

从事脑科学核磁共振方法学研究,在Nature communications等权威期刊发表研究论文,熟练掌握磁共振处理方法和统计学方法,欢迎大家和我交流。 6085

嵌入式开发环境Vscode开发STM32单片机程序

STM32单片机非常强大,大多数教程都是使用 keil 编译器,keil 是收费的而 gcc 是开源免费的。这里介绍一些使用 gcc +开发单片机程序的经验。(这里不解释 gcc 是什么)。

嵌入式技术开发 1843

Vue3 - Element Plus上传组件el-upload上传多个文件时,让on-change监听事件仅执行一次并获取最终文件列表方法(开启多选文件并手动上传,拿到文件列表统一循环调用接口上传)

vue3,elementplus,Upload上传,el-upload,文件上传组件,上传组件on-change里执行多次,取消自动上传后change重复执行问题,upload上传多个文件后导致on-change执行多次拿不到最终文件列表,Element-plus的upload组件on-change不断执行怎么办,上传多个文件多次执行调用,上传多个文件时如何只执行一次on-change方法拿到最终的文件列表,vue3elementplus上传组件开启多个上传后导致change方法执行好几次,仅调用一

🏆 前端领域优质创作者 1万+

Vue3+Openlayers教程导航页【目录】

欢迎来到 Vue3+Openlayers 系列教程,这里是我在 优快云 编写的所有 Vue3+Openlayers 系列相关文章的索引页,适合初学者到进阶用户系统阅读。👀 更多教程正在持续更新中,欢迎关注我,一起探索 Openlayers 的强大魅力!

小彭的博客 1263

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

上一篇: 安卓开发实例二:文件浏览器
下一篇: 开源小工具一:获取客户端内嵌IE控件(Internet Explorer_Server)的URL
疯狂-的-蜗牛
博客等级 码龄16年 427粉丝 104原创
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值