音频demo:使用开源项目libmad来将MP3数据解码出PCM数据

1、README

前言

本demo是使用开源项目libmad来将MP3数据解码成PCM(16位有符号小字节序)数据。(环境:x86_64 Ubuntu16.04 64位)

a. 编译使用

libmad的编译:

  • 源码下载地址1:https://sourceforge.net/projects/mad/files/libmad/

  • 源码下载地址2:https://www.linuxfromscratch.org/blfs/view/svn/multimedia/libmad.html

tar xzf libmad-0.15.1b.tar.gz
cd libmad-0.15.1b/
sed -i '/-fforce-mem/d' configure   # 如果不执行这句命令,一些编译器可能会报"gcc: error: unrecognized command line option '-fforce-mem'"错误
./configure --prefix=$PWD/_install --enable-static --disable-shared
make
make install

demo的编译与使用:

$ make clean && make
$ 
$ ./mp32pcm
Usage:
    ./mp32pcm <in MP3 file> <out PCM file>
Examples:
    ./mp32pcm audio/test1_44100_stereo.mp3 out1_44100_16bit_stereo.pcm
    ./mp32pcm audio/test2_22050_stereo.mp3 out2_22050_16bit_stereo.pcm
    ./mp32pcm audio/test3_22050_mono.mp3   out3_22050_16bit_mono.pcm
    ./mp32pcm audio/test4_8000_mono.mp3    out4_8000_16bit_mono.pcm
b. 参考文章
  • libmad linux交叉编译移植_SongYuLong的博客的博客-CSDN博客_libmad 交叉编译l
  • 基于Libmad的流媒体解码播放Demo - 简书
  • libmad-0.15.1b/minimad.c(以放到本demo中的docs/reference_code/目录。)
c. demo目录架构
$ tree
.
├── audio
│   ├── out1_44100_16bit_stereo.pcm
│   ├── out2_22050_16bit_stereo.pcm
│   ├── out3_22050_16bit_mono.pcm
│   ├── out4_8000_16bit_mono.pcm
│   ├── test1_44100_stereo.mp3
│   ├── test2_22050_stereo.mp3
│   ├── test3_22050_mono.mp3
│   └── test4_8000_mono.mp3
├── docs
│   ├── libmad linux交叉编译移植_SongYuLong的博客的博客-CSDN博客_libmad 交叉编译.mhtml
│   ├── reference_code
│   │   └── minimad.c
│   └── 基于Libmad的流媒体解码播放Demo - 简书.mhtml
├── include
│   └── mad.h
├── lib
│   └── libmad.a
├── main.c
├── Makefile
└── README.md

2、主要代码片段

main.c
/*
 * libmad - MPEG audio decoder library
 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: minimad.c,v 1.4 2004/01/23 09:41:32 rob Exp $
 */

# include <stdio.h>
# include <unistd.h>
# include <sys/stat.h>
# include <sys/mman.h>

# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <stdlib.h>

# include "mad.h"


#ifdef ENABLE_DEBUG
    #define DEBUG(fmt, args...)     printf(fmt, ##args)
#else
    #define DEBUG(fmt, args...)
#endif

/*
 * This is a private message structure. A generic pointer to this structure
 * is passed to each of the callback functions. Put here any data you need
 * to access from within the callbacks.
 */

struct buffer {
  unsigned char const *inMp3Data;
  unsigned long inMp3DataLen;
  unsigned char *outPcmData;
  unsigned long outPcmDataLen;
};

/*
 * This is perhaps the simplest example use of the MAD high-level API.
 * Standard input is mapped into memory via mmap(), then the high-level API
 * is invoked with three callbacks: input, output, and error. The output
 * callback converts MAD's high-resolution PCM samples to 16 bits, then
 * writes them to standard output in little-endian, stereo-interleaved
 * format.
 */

static int mp3_decode_body(struct buffer *);

int main(int argc, char *argv[])
{
  struct stat stat;
  int fdMp3 = -1;
  void *vfdMp3 = NULL; /* mmap */
  FILE *fpPcm = NULL;
  char *inMp3FileName = NULL;
  char *outPcmFileName = NULL;
  struct buffer bufferInfo= {};

  if (argc != 3)
  {
	printf("Usage: \n"
           "    %s <in MP3 file> <out PCM file>\n"
           "Examples: \n"
           "    %s audio/test1_44100_stereo.mp3 out1_44100_16bit_stereo.pcm\n"
           "    %s audio/test2_22050_stereo.mp3 out2_22050_16bit_stereo.pcm\n"
           "    %s audio/test3_22050_mono.mp3   out3_22050_16bit_mono.pcm\n"
           "    %s audio/test4_8000_mono.mp3    out4_8000_16bit_mono.pcm\n",
		   argv[0], argv[0], argv[0], argv[0], argv[0]);

    return -1;
  }
  else
  {
    inMp3FileName = argv[1];
    outPcmFileName = argv[2];
  }

  /* open MP3 file and map to memory */
  fdMp3 = open(inMp3FileName, O_RDONLY);
  if (fdMp3 < 0)
  {
    perror("open input MP3 file failed");
    goto exit;
  }

  if (fstat(fdMp3/*STDIN_FILENO*/, &stat) == -1 ||
      stat.st_size == 0)
    goto exit;

  printf("decode input MP3 size: %lu\n", stat.st_size);

  vfdMp3 = mmap(0, stat.st_size, PROT_READ, MAP_SHARED, fdMp3/*STDIN_FILENO*/, 0);
  if (vfdMp3 == MAP_FAILED)
  {
    printf("map MP3 file to memory failed!\n");
    goto exit;
  }

  /* fix up our struct, and we will use to decode mp3 data! */
  bufferInfo.inMp3Data = vfdMp3;
  bufferInfo.inMp3DataLen = stat.st_size;
  bufferInfo.outPcmData = malloc(stat.st_size * 15); /* decode out buf size */
  if (!bufferInfo.outPcmData)
  {
    printf("alloc memory to decode output PCM failed!\n");
    goto exit;
  }
  bufferInfo.outPcmDataLen = 0; /* init to 0 */

  /* decode MP3 data with our struct !!!!! */
  mp3_decode_body(&bufferInfo);

  /* save the decoded out PCM data */
  fpPcm = fopen(outPcmFileName, "wb");
  if (!fpPcm)
  {
    printf("open output PCM file failed!\n");
    goto exit;
  }
  else
  {
    printf("decode output total PCM size: %lu\n", bufferInfo.outPcmDataLen);
    fwrite(bufferInfo.outPcmData, 1, bufferInfo.outPcmDataLen, fpPcm);
    fflush(fpPcm);
    fclose(fpPcm);
  }

exit:

  if (munmap(vfdMp3, stat.st_size) == -1)
    return -1;

  if (fdMp3)
    close(fdMp3);

  if (bufferInfo.outPcmData)
    free(bufferInfo.outPcmData);

  return 0;
}

/*
 * This is the input callback. The purpose of this callback is to (re)fill
 * the stream buffer which is to be decoded. In this example, an entire file
 * has been mapped into memory, so we just call mad_stream_buffer() with the
 * address and length of the mapping. When this callback is called a second
 * time, we are finished decoding.
 */

static
enum mad_flow mp3_decode_input(void *data,
		    struct mad_stream *stream)
{
  struct buffer *buffer = data;

  if (!buffer->inMp3DataLen)
    return MAD_FLOW_STOP;

  DEBUG("[%s: %d] decode input size: %lu\n", __FUNCTION__, __LINE__, buffer->inMp3DataLen);

  mad_stream_buffer(stream, buffer->inMp3Data, buffer->inMp3DataLen);

  buffer->inMp3DataLen = 0;

  return MAD_FLOW_CONTINUE;
}

/*
 * The following utility routine performs simple rounding, clipping, and
 * scaling of MAD's high-resolution samples down to 16 bits. It does not
 * perform any dithering or noise shaping, which would be recommended to
 * obtain any exceptional audio quality. It is therefore not recommended to
 * use this routine if high-quality output is desired.
 */

static inline
signed int scale(mad_fixed_t sample)
{
  /* round */
  sample += (1L << (MAD_F_FRACBITS - 16));

  /* clip */
  if (sample >= MAD_F_ONE)
    sample = MAD_F_ONE - 1;
  else if (sample < -MAD_F_ONE)
    sample = -MAD_F_ONE;

  /* quantize */
  return sample >> (MAD_F_FRACBITS + 1 - 16);
}

/*
 * This is the output callback function. It is called after each frame of
 * MPEG audio data has been completely decoded. The purpose of this callback
 * is to output (or play) the decoded PCM audio.
 */

static
enum mad_flow mp3_decode_output(void *data,
		     struct mad_header const *header,
		     struct mad_pcm *pcm)
{
  struct buffer *buffer = data;
  unsigned int nchannels, nsamples;
  mad_fixed_t const *left_ch, *right_ch;

  /* pcm->samplerate contains the sampling frequency */

  nchannels = pcm->channels;
  nsamples  = pcm->length;
  left_ch   = pcm->samples[0];
  right_ch  = pcm->samples[1];

  DEBUG("[%s: %d] decode ouput size: %d\n", __FUNCTION__, __LINE__, nsamples*2*nchannels/* print as 16bit */);

  while (nsamples--) {
    signed int sample;

    /* output sample(s) in 16-bit signed little-endian PCM */

    sample = scale(*left_ch++);
    #if 0
    putchar((sample >> 0) & 0xff);
    putchar((sample >> 8) & 0xff);
    #else
    buffer->outPcmData[buffer->outPcmDataLen++] = (sample >> 0) & 0xff;
    buffer->outPcmData[buffer->outPcmDataLen++] = (sample >> 8) & 0xff;
    #endif

    if (nchannels == 2) {
      sample = scale(*right_ch++);
      #if 0
      putchar((sample >> 0) & 0xff);
      putchar((sample >> 8) & 0xff);
      #else
      buffer->outPcmData[buffer->outPcmDataLen++] = (sample >> 0) & 0xff;
      buffer->outPcmData[buffer->outPcmDataLen++] = (sample >> 8) & 0xff;
      #endif
    }
  }

  return MAD_FLOW_CONTINUE;
}

/*
 * This is the error callback function. It is called whenever a decoding
 * error occurs. The error is indicated by stream->error; the list of
 * possible MAD_ERROR_* errors can be found in the mad.h (or stream.h)
 * header file.
 */

static
enum mad_flow mp3_decode_error(void *data,
		    struct mad_stream *stream,
		    struct mad_frame *frame)
{
  struct buffer *buffer = data;

  fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %lu\n",
	  stream->error, mad_stream_errorstr(stream),
	  stream->this_frame - buffer->inMp3Data);

  /* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */

  return MAD_FLOW_CONTINUE;
}

/*
 * This is the function called by main() above to perform all the decoding.
 * It instantiates a decoder object and configures it with the input,
 * output, and error callback functions above. A single call to
 * mad_decoder_run() continues until a callback function returns
 * MAD_FLOW_STOP (to stop decoding) or MAD_FLOW_BREAK (to stop decoding and
 * signal an error).
 */

static
int mp3_decode_body(struct buffer *bufferInfo)
{
  //struct buffer buffer;
  struct mad_decoder decoder;
  int result;

  #if 0
  /* initialize our private message structure */

  buffer.start  = start;
  buffer.length = length;
  #endif

  /* configure input, output, and error functions */

  mad_decoder_init(&decoder, bufferInfo,
		   mp3_decode_input, 0 /* header */, 0 /* filter */, mp3_decode_output,
		   mp3_decode_error, 0 /* message */);

  /* start decoding */

  result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);

  /* release the decoder */

  mad_decoder_finish(&decoder);

  return result;
}

3、demo下载地址(任选一个)

  • https://download.csdn.net/download/weixin_44498318/89525488

  • https://gitee.com/linriming/audio_mp32pcm_with_libmad.git

  • https://github.com/linriming20/audio_mp32pcm_with_libmad.git

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/784727.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

掌握计算机网络基础:从零开始的指南

计算机网络是现代信息社会的重要基石。本文将以简洁明了的方式为基础小白介绍计算机网络的基本概念、分类、以及其在信息时代中的重要作用。 计算机网络在信息时代中的作用 21世纪是以数字化、网络化、信息化为重要特征的信息时代。 计算机网络作为信息的最大载体和传输媒介&…

微信自动加好友工具

批量导入数据到后台&#xff0c;可设置添加速度、间隔时间、验证信息和自动备注等&#xff0c;任务执行时间&#xff0c;后台会自动执行操作。

ubuntu 分区情况

ubuntu系统安装与分区指南 - Philbert - 博客园 (cnblogs.com)https://www.cnblogs.com/liangxuran/p/14872811.html 详解安装Ubuntu Linux系统时硬盘分区最合理的方法-腾讯云开发者社区-腾讯云 (tencent.com)https://cloud.tencent.com/developer/article/1711884

基于flask的猫狗图像预测案例

&#x1f4da;博客主页&#xff1a;knighthood2001 ✨公众号&#xff1a;认知up吧 &#xff08;目前正在带领大家一起提升认知&#xff0c;感兴趣可以来围观一下&#xff09; &#x1f383;知识星球&#xff1a;【认知up吧|成长|副业】介绍 ❤️如遇文章付费&#xff0c;可先看…

uni-app 封装http请求

1.引言 前面一篇文章写了使用Pinia进行全局状态管理。 这篇文章主要介绍一下封装http请求&#xff0c;发送数据请求到服务端进行数据的获取。 感谢&#xff1a; 1.yudao-mall-uniapp: 芋道商城&#xff0c;基于 Vue Uniapp 实现&#xff0c;支持分销、拼团、砍价、秒杀、优…

2024年6月总结 | 软件开发技术月度回顾(第一期)

最新技术资源&#xff08;建议收藏&#xff09; https://www.grapecity.com.cn/resources/ Hello&#xff0c;大家好啊&#xff01;随着欧洲杯和奥运会的临近&#xff0c;2024 年下半年的序幕也随之拉开。回顾 2024 年上半年的技术圈&#xff0c;我们看到了一系列令人振奋的进展…

ELfK logstash filter模块常用的插件 和ELFK部署

ELK之filter模块常用插件 logstash filter模块常用的插件&#xff1a; filter&#xff1a;表示数据处理层&#xff0c;包括对数据进行格式化处理、数据类型转换、数据过滤等&#xff0c;支持正则表达式 grok 对若干个大文本字段进行再分割成一些小字段 (?<字段名…

51单片机嵌入式开发:5、按键、矩阵按键操作及protues仿真

按键、矩阵按键操作及protues仿真 1 按键介绍1.1 按键种类1.2 按键应用场景 2 按键电路3 按键软件设计3.1 按键实现3.2 按键滤波方法3.3 矩阵按键软件设计3.4 按键Protues 仿真 4 按键操作总结 提示 1 按键介绍 1.1 按键种类 按键是一种用于控制电子设备或电路连接和断开的按…

LLM之RAG实战(四十一)| 使用LLamaIndex和Gemini构建高级搜索引擎

Retriever 是 RAG&#xff08;Retrieval Augmented Generation&#xff09;管道中最重要的部分。在本文中&#xff0c;我们将使用 LlamaIndex 实现一个结合关键字和向量搜索检索器的自定义检索器&#xff0c;并且使用 Gemini大模型来进行多个文档聊天。 通过本文&#xff0c;我…

Face_recognition实现人脸识别

这里写自定义目录标题 欢迎使用Markdown编辑器一、安装人脸识别库face_recognition1.1 安装cmake1.2 安装dlib库1.3 安装face_recognition 二、3个常用的人脸识别案例2.1 识别并绘制人脸框2.2 提取并绘制人脸关键点2.3 人脸匹配及标注 欢迎使用Markdown编辑器 本文基于face_re…

Python 安装Numpy 出现异常信息

文章目录 前言一、包源二、安装完成异常 前言 安装Python Numpy包出现异常问题 Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. 一、包源 使用默认的包源出现超时异常&#xff0c;改用清华包源 pip …

娱乐圈幕后揭秘孙俪天选打工人

【娱乐圈幕后揭秘&#xff1a;孙俪“天选打工人”背后的热议风暴】在聚光灯下光鲜亮丽的娱乐圈&#xff0c;每一位明星的日常备受瞩目。近日&#xff0c;实力派演员孙俪在社交媒体上分享了一段片场棚拍的趣事&#xff0c;本是无心之举&#xff0c;意外引爆了网络热议的导火索。…

这几类人,千万不要买纯电车

文 | AUTO芯球 作者 | 响铃 纯电车的冤大头真是太多了&#xff0c; 我之前劝过&#xff0c;有些人不适合买纯电车&#xff0c; 你们看&#xff0c;果然吧&#xff0c;麦卡锡最近的一份报告就披露了 去年啊&#xff0c;22%的人在买了电车后后悔了&#xff0c; 这些人说了&a…

面试常考题---128陷阱(详细)

1.问题引入 分别引入了int和Integer变量&#xff0c;并进行比较 int b 128; int b1 128;Integer d 127; Integer d1 127;Integer e 128; Integer e1 128;System.out.println(bb1); System.out.println(dd1); System.out.println(ee1); System.out.println(e.equals(e1)…

kafka系列之offset超强总结及消费后不提交offset情况的分析总结

概述 每当我们调用Kafka的poll()方法或者使用Spring的KafkaListener(其实底层也是poll()方法)注解消费Kafka消息时&#xff0c;它都会返回之前被写入Kafka的记录&#xff0c;即我们组中的消费者还没有读过的记录。 这意味着我们有一种方法可以跟踪该组消费者读取过的记录。 如前…

【力扣高频题】014.最长公共前缀

经常刷算法题的小伙伴对于 “最长”&#xff0c;“公共” 两个词一定不陌生。与此相关的算法题目实在是太多了 &#xff01;&#xff01;&#xff01; 之前的 「动态规划」 专题系列文章中就曾讲解过两道相关的题目&#xff1a;最长公共子序列 和 最长回文子序列 。 关注公众…

跨境电商代购系统与电商平台API结合的化学反应

随着全球化的不断推进和互联网技术的飞速发展&#xff0c;跨境电商已成为国际贸易的重要组成部分。跨境电商代购系统作为连接国内外消费者与商品的桥梁&#xff0c;不仅为消费者提供了更多元化的购物选择&#xff0c;也为商家开辟了更广阔的市场空间。在这一过程中&#xff0c;…

如何将heic转jpg格式?四种图片格式转换方法【附教程】

如何把heic转jpg格式&#xff1f;heic是用于存储静态图像和图形的压缩格式&#xff0c;旨在以更小的文件大小保持高质量的图像。HEIC格式自iOS 11和macOS High Sierra&#xff08;10.13&#xff09;内测开始&#xff0c;被苹果设置为图片存储的默认格式&#xff0c;广泛应用于i…

【VUE基础】VUE3第四节—核心语法之computed、watch、watcheffect

computed 接受一个 getter 函数&#xff0c;返回一个只读的响应式 ref 对象。该 ref 通过 .value 暴露 getter 函数的返回值。它也可以接受一个带有 get 和 set 函数的对象来创建一个可写的 ref 对象。 创建一个只读的计算属性 ref&#xff1a; <template><div cl…

【一次成功】清华大学和智谱AI公司的ChatGLM-4-9B-Chat-1M大模型本地化部署教程

【一次成功】清华大学和智谱AI公司的ChatGLM-4-9B-Chat-1M大模型本地化部署教程 一、环境准备二、ChatGLM-4-9B-Chat-1M简介三、模型下载2.1 安装Git LFS2.2 初始化仓库2.3 同步Git文件2.4 拉取文件2.5 下载完毕 四、python和源码安装与下载4.1 安装python4.2 下载源码4.3 安装…