怎样用c语言读取WAV文件振幅信息

怎样用c语言读取WAV文件振幅信息,第1张

C也能像matlab一样读取wav格式文件

用外置库libsndfile,可以读取数据,频率,通道等等

以下是一段读取sample.wav,将数据存入double格式的array:

#include "sndfile.h"

#pragma comment(lib, "libsndfile-1.lib")

void readwav(double output[length]){

SNDFILE *sf

SF_INFO info

int num_channels

int num, num_items

double *buf

int f,sr,c

int i,j

info.format = 0

sf = sf_open("sample.wav",SFM_READ,&info)

if (sf == NULL){

printf("Failed to open the file.\n")

exit(-1)

}

f = info.frames

sr = info.samplerate

c = info.channels

printf("frames=%d\n",f)

printf("samplerate=%d\n",sr)

printf("channels=%d\n",c)

num_items = f*c

printf("num_items=%d\n",num_items)

buf = (double *)malloc(num_items*sizeof(double))

num = sf_read_double(sf,buf,num_items)

sf_close(sf)

printf("Read %d items\n",num)

for (i = 0i <numi += c){

for (j = 0j <c++j){

if ((i+j)<length) {

output[i+j] = buf[i+j]

}

}

}

}

用AudioServicesPlaySystemSound进行播放,代码如下:(wav文件名为end.wav)

SystemSoundID ph

id sndpath = [[NSBundle mainBundle]pathForResource:@"end" ofType:@"wav" inDirectory:@"/"]

CFURLRef baseUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:sndpath]

AudioServicesCreateSystemSoundID(baseUrl, &ph)

AudioServicesPlaySystemSound(ph)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11831523.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存