Android tinysndfile

Android源码中system/media/audio_utils中提供了sndfile的实现,但是却是一个tiny版本的。
在Android中仅仅支持wav中的RIFF格式,基于此它只有两个文件。由于有需求,所以把它从Android源码中提取出来。

简单介绍目录树:

.
├── Android.mk 此tinysndfile的Makefile
├── Application.mk Makefile的全局配置文件
├── libs
│   └── armeabi-v7a
│       └── libjni_shared.so 生成的动态库
├── primitives.c 提供tinysndfile不同类型的memcpy的操作
├── primitives.h
├── private.h 仅包含一个含有3个byte的数组的结构体uint8x3_t
├── sndfile.h sndfile对外提供的函数都在此了
└── tinysndfile.c tinysndfile的核心文件

tinysndfile对外提供的接口非常少,用法几乎和open、read、write一样了。
这里用到了__BEGIN_DECLS,它就是extern "C"的宏,bionic/libc/include/sys/cdefs.h中定义的。

#if defined(__cplusplus)
#define __BEGIN_DECLS       extern "C" {
#define __END_DECLS     }
#define __static_cast(x,y)  static_cast<x>(y)
#else
#define __BEGIN_DECLS
#define __END_DECLS
#define __static_cast(x,y)  (x)y
#endif

tinysndfile对外提供的结构体和函数,虽说是tiny版本,但是和标准的sndfile还是存在差异的,结构体请勿错用。

__BEGIN_DECLS

// visible to clients
typedef int sf_count_t;

typedef struct {
    sf_count_t frames;
    int samplerate;
    int channels;
    int format;
} SF_INFO;

// opaque to clients
typedef struct SNDFILE_ SNDFILE;

// Access modes
#define SFM_READ    1
#define SFM_WRITE   2

// Format
#define SF_FORMAT_TYPEMASK  1
#define SF_FORMAT_WAV       1
#define SF_FORMAT_SUBMASK   14
#define SF_FORMAT_PCM_16    2
#define SF_FORMAT_PCM_U8    4
#define SF_FORMAT_FLOAT     6
#define SF_FORMAT_PCM_32    8

// Open stream
SNDFILE *sf_open(const char *path, int mode, SF_INFO *info);

// Close stream
void sf_close(SNDFILE *handle);

// Read interleaved frames and return actual number of frames read
sf_count_t sf_readf_short(SNDFILE *handle, short *ptr, sf_count_t desired);
sf_count_t sf_readf_float(SNDFILE *handle, float *ptr, sf_count_t desired);
sf_count_t sf_readf_int(SNDFILE *handle, int *ptr, sf_count_t desired);

// Write interleaved frames and return actual number of frames written
sf_count_t sf_writef_short(SNDFILE *handle, const short *ptr, sf_count_t desired);
sf_count_t sf_writef_float(SNDFILE *handle, const float *ptr, sf_count_t desired);

__END_DECLS

tinysndfile github地址

下载&&编译:

$ git clone https://github.com/momo0853/tinysndfile.git
$ cd tinysndfile/jni
$ ndk-build -j4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值